cancel
Showing results for 
Search instead for 
Did you mean: 

check for duplicate name in javascript

sharifu
Confirmed Champ
Confirmed Champ
I have the following javascript which i execute on a folder manually in /alfresco.


/**
*   validates document metadata and moves to correct folder
*   @author sharifu
*/

var thirdParties = document.properties["sp:thirdParties"];
var letter = "";
var location = document.properties["sp:location"];
var responsibleEmp = document.properties["sp:responsibleEmp"];
var contractRef = document.properties["sp:contractRef"];
var logNumber = document.properties["sp:logNumber"];
var effectiveDate = document.properties["sp:effectiveDate"];

var sourceFile = document;
var targetFolder = "";
var success = true;

//Check to see if values are null
if( thirdParties == null || location == "" || responsibleEmp == null || contractRef == null || logNumber == null || effectiveDate == null  || location.toString() != "UK" ){
   success = false;
   
}else{
   var effectiveDate = new Date ( document.properties["sp:effectiveDate"] );
   var vendor = thirdParties.replace(/[|;$%@"<>()+,\/.]/g,"").replace(".","").replace(/^\s+|\s+$/g,'');
   
   //Check if folder exist
   if(checkFolderExists(location, thirdParties )){
      letter = thirdParties.charAt(0);
      targetFolder = space.parent.childByNamePath(location.toString()).childByNamePath("Contracts").childByNamePath(letter.toLowerCase()).childByNamePath(vendor);
      
   }else{
      success = false; // do not move doc no folder exists
   }
}

if(success){
   var targetFile = targetFolder.childByNamePath(document.name);
   
   if(targetFile == null){
      sourceFile.move(targetFolder);
   }else{
      var targetFileWorkingCopy = targetFile.checkout();
      targetFileWorkingCopy.content = sourceFile.content;
      targetFileWorkingCopy.checkin("",false);
      sourceFile.remove();
   }
}

/**   Function checks if vendor folder exists if not then it creates it
*   @param String site
*   @param String vendor
*   @return Boolean true if creates or exists, false all others
*/
function checkFolderExists(site, thirdParties){
   var vendor = thirdParties.replace(/[|;$%@"<>()+,\/.]/g,"").replace(".","").replace(/^\s+|\s+$/g,'');
   var letter = vendor.charAt(0);
   //.childByNamePath(letter.toLowerCase())
   if( space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ) == null ){
      space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").createFolder( letter.toLowerCase() );
      space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).createFolder( vendor );
      return true;
      
   }else if( !space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).childByNamePath( vendor) ){
      space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).createFolder( vendor );
      return true;
      
   }else if ( space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).childByNamePath( vendor) ) {
      return true;
      
   }else{
      return false;
   }

}



The problem I am having is if the same name is capitalized share does not recognize it as duplicate, however when you try to replicate the contents replication moans about duplicate nodes. The function i need to check whether it is already created is in <i>checkFolderExists</i>, i need a check on <i>!space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).childByNamePath( vendor)</i>. I have to make sure vendor exists only once and not in different capitalization.
1 REPLY 1

kaynezhang
World-Class Innovator
World-Class Innovator
You can try ScriptNode.childrenByXPath or You can  list all children of a folder and  find duplicates one by one.