cancel
Showing results for 
Search instead for 
Did you mean: 

WebService API: Renaming Folders

robobot
Champ in-the-making
Champ in-the-making
Urgent Problem. I have the following code to rename a folder.

/**
     * folderPath e.g. /app:company_home/app:user_homes/cm:Main/cm:Users/cm:Bewerber/cm:Hancock_Paul
     * newFoldername e.g. Hancock_Herbie
     */
    public static void updateFolderName (String folderPath, String newFoldername){

     UpdateResult [] result = null; 
     //get the reference to the data which should be updated
     final Reference reference = new Reference(STORE, null, folderPath);    
     final Predicate p = new Predicate(new Reference[]{reference},STORE,null);

     //create CMLUpdate instance referring to the current data
     final CMLUpdate updateFolder = new CMLUpdate( new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, newFoldername)},p,null);
    
     CML cml = new CML();
     cml.setUpdate(new CMLUpdate[]{updateFolder});
     try {
       result = WebServiceFactory.getRepositoryService().update(cml);      
       // still the old path!
       System.out.println("new path: "+result[0].getDestination().getPath());      
     } catch ( RepositoryFault rf ) {
       rf.printStackTrace();
     } catch ( RemoteException re ) {
       re.printStackTrace();
     }    
    }

May anybody help me, why does sysout not print what i am expecting? when i look in alfresco, the folder has been successfully renamed!
Thank you very much for any advice!
7 REPLIES 7

robobot
Champ in-the-making
Champ in-the-making
may somebody please give me a short hint ?
still don't know how to manage this.  Smiley Indifferent

mrogers
Star Contributor
Star Contributor
Some more details of what you were getting from your System.out seeing may have helped.

There's two "name" fields and you are changing the "display name" of the node which is a multilingual property.   The display name will not affect the "path" of the node which is based on the association name.

robobot
Champ in-the-making
Champ in-the-making
The sysout still prints the old Path after the update

/app:company_home/app:user_homes/cm:Main/cm:Users/cm:Bewerber/cm:Hancock_Paul
i excepted the new path

/app:company_home/app:user_homes/cm:Main/cm:Users/cm:Bewerber/cm:Hancock_Herbie

i am sure your hint is useful. but which is the right property to be set here ?


new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, newFoldername)}

mrogers
Star Contributor
Star Contributor
I'm not that familiar with the WebService API but a casual inspection suggests you probably want to use CMLMove rather than CMLUpdate.   Since it is the association name between your document's parent and your document that you want to change not a property of your document.

robobot
Champ in-the-making
Champ in-the-making
Thank you for your message. It seems, that using CMLMove does not solve my problem. here is a complete example taken from

http://wiki.alfresco.com/wiki/IngresTutorial_Alfresco_Web_Service_API_for_Java

package org.alfresco.sample;

import org.alfresco.webservice.repository.QueryResult;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLMove;
import org.alfresco.webservice.types.CMLUpdate;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.ResultSet;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;

public class RenameSpaceTest {
   
   public static final Store STORE = new Store(Constants.WORKSPACE_STORE,"SpacesStore");
   
   //the folder which should be renamed
   static String OLD_PATH = "/app:company_home/app:user_homes/cm:Users/cm:Candidate/cm:Old";   

   public static void main(String[] args) throws Exception {

      AuthenticationUtils.startSession("admin", "admin");

      try {
          
        renameSpace(new Reference(STORE, null, OLD_PATH), "NEW");
       
      } catch (Throwable e) {
         e.printStackTrace();
      } finally {
         // End the session
         AuthenticationUtils.endSession();
         System.exit(0);
      }
   }

   protected static Reference getParent(Reference space) throws Exception {
      QueryResult result = WebServiceFactory.getRepositoryService().queryParents(space);
      ResultSet resultset = result.getResultSet();
      String first_parent_id = resultset.getRows(0).getNode().getId();
      System.out.println("The queried parent id is: " + first_parent_id);
      Reference parent = new Reference(STORE, first_parent_id, null);
      return parent;
   }

   protected static void moveSpace(Reference space, Reference dest,String newname) throws Exception {
      ParentReference parentDest = ReferenceToParent(dest);
      parentDest.setChildName(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, normilizeNodeName(newname)));

      CMLMove move = new CMLMove();
      move.setTo(parentDest);
      move.setWhere(new Predicate(new Reference[] { space }, STORE, null));

      NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(
            Constants.PROP_NAME, newname) };
      CMLUpdate update = new CMLUpdate();
      update.setProperty(properties);
      update.setWhere(new Predicate(new Reference[] { space }, STORE, null));

      CML cml = new CML();
      cml.setMove(new CMLMove[] { move });
      cml.setUpdate(new CMLUpdate[] { update });

      // Execute the CML move and Update statement
      try {
         System.out.println("Moving the space with path " + space.getPath()
               + " or id " + space.getUuid() + "\n"
               + "to destination space with path " + dest.getPath()
               + " or id " + dest.getUuid() + "\n" + "by using the name "
               + newname + ".");
         UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
         
                         //check if the path has changed after the update->
                        // there is still the old path !
                       //-> "/app:company_home/app:user_homes/cm:Users/cm:Candidate/cm:Old

         System.out.println("destination: " + result[0].getDestination().getPath());
         System.out.println("source: " + result[0].getSource().getPath());

      } catch (Exception e2) {
         System.err.println("Can not move the space.");
         throw e2;
      }
   }

   protected static void renameSpace(Reference space, String newName)throws Exception {
      Reference parent = getParent(space);
      moveSpace(space, parent, newName);
   }

   protected static ParentReference ReferenceToParent(Reference spaceref) {
      ParentReference parent = new ParentReference();

      parent.setStore(STORE);
      parent.setPath(spaceref.getPath());
      parent.setUuid(spaceref.getUuid());
      parent.setAssociationType(Constants.ASSOC_CONTAINS);

      return parent;
   }

   // Blanks are allowed in space names but not in paths. Because the path
   // should depend on the name we need a version of the name which contains no
   // blanks

   protected static String normilizeNodeName(String name) {
      return name.replace(" ", "_");
   }

   
}

The root problem is, that the documents can not be found any more, after a space name was changed, because i am using path queries to find documents in a specific folder. a path query does not work after the folder update

+PATH:/app:company_home/app:user_homes/cm:Users/cm:Candidate/cm:NEW


Failed to resolve to a single NodeRef with parameters (store=workspace:SpacesStore uuid=null path=/app:company_home/app:user_homes/cm:Users/cm:Candidate/cm:NEW), found 0 nodes.</ns1:message>

clock1988
Champ in-the-making
Champ in-the-making
I got the same problem. Is there any help?

Thanks you man.

chantren
Champ in-the-making
Champ in-the-making
I have the same problem, anybody has an answer ?