cancel
Showing results for 
Search instead for 
Did you mean: 

CML Move problem

mangar
Star Contributor
Star Contributor
I have 2 directories like this:

/app:company_home/cm:divisionA/cmSmiley TongueroductDetails
/app:company_home/cm:divisionB

I am trying to move the directory and contents from divistionA to divisionB.  I have done this successfully with the following code:


String newParentDir = "/app:company_home/cm:divisionB";

ParentReference folderParentRef = new ParentReference();
folderParentRef.setStore(storeRef);
folderParentRef.setPath(newParentDir);
folderParentRef.setAssociationType(Constants.ASSOC_CONTAINS);

// create ref to current dir
String currentDir = "/app:company_home/cm:divisionA/cm:productDetails";
Reference curr = new Reference();
curr.setStore(storeRef);
curr.setPath(currentDir);

CMLMove move = new CMLMove();
move.setAssociationType(Constants.ASSOC_CONTAINS);
move.setChildName("productDetails");
move.setTo(folderParentRef);
move.setWhere(new Predicate(new Reference[]{curr}, null, null));

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

Like I said the code works fine. 

The problem comes when I try to switch it back.  This should be as simple as switching the values of currentDir and newParentDir, but when I do, I get that ever popular "Node does not exist" error.

Well of course it exists, I just moved it there!  Why can't I move it back?
It's all there in the web front end, content and all.

Anyone have any ideas?
3 REPLIES 3

jcustovic
Champ in-the-making
Champ in-the-making
Are you trying to say that after moving /app:company_home/cm:divisionA/cmSmiley TongueroductDetails to /app:company_home/cm:divisionB you cannot move /app:company_home/cm:divisionB/cmSmiley TongueroductDetails back to /app:company_home/cm:divisionA?

mangar
Star Contributor
Star Contributor
Yes, that is exactly what is happening.  I get the node not found error.

jcustovic
Champ in-the-making
Champ in-the-making
Here try this code. I tested this code and it works.

private static Store WORKSPACE_STORE = new Store(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");

public static void main(String[] args) throws RepositoryFault, RemoteException {
   …
   alfSoap.startSession(); // Start session
   Reference refA = new Reference(WORKSPACE_STORE, null, "/app:company_home/cm:divisionB"); // Where to move
   Reference refB = new Reference(WORKSPACE_STORE, null, "/app:company_home/cm:divisionA/cm:productDetails"); // What to move
   try {
      // Move
      UpdateResult[] results = alfSoap.moveNode(refA, refB, "productDetails"); // important to send wanted nodeName because the new node will have path refA.getPath + "cm:/" + nodeName

      // Return to original location
      refA = new Reference(WORKSPACE_STORE, null, "/app:company_home/cm:divisionA"); // Where to move
      refB = new Reference(WORKSPACE_STORE, null, "/app:company_home/cm:divisionB/cm:productDetails"); // What to move
      results = moveNode(refA, refB, "productDetails"); // important to send wanted nodeName because the new node will have path refA.getPath + "cm:/" + nodeName
   } finally {
      // End session
      alfSoap.endSession();
   }
}
public UpdateResult[] moveNode(Reference toReference, Reference whatReference, String nodeName) throws RepositoryFault, RemoteException {
   ParentReference toParentReference = referenceToParent(toReference);
   toParentReference.setChildName(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, nodeName)); // Should be the same as whatReference nodeName
   CMLMove move = new CMLMove();
   move.setTo(toParentReference);
   move.setWhere(new Predicate(new Reference[] { whatReference }, WORKSPACE_STORE, null));
   CML cml = new CML();
   cml.setMove(new CMLMove[] { move });
   return getRepositoryService().update(cml);        
}

private ParentReference referenceToParent(Reference spaceReference) {
   ParentReference parent = new ParentReference();
   parent.setStore(WORKSPACE_STORE);
   parent.setPath(spaceReference.getPath());
   parent.setUuid(spaceReference.getUuid());
   parent.setAssociationType(Constants.ASSOC_CONTAINS);
   return parent;
}
toParentReference.setChildName(…) is the key.
For example if you set it to "testTest" that the path to node "productDetails" would be (after you move it for the first time) "/app:company_home/cm:divisionB/cm:testTest" and not "/app:company_home/cm:divisionB/cmSmiley TongueroductDetails" as you would expect it to be.