cancel
Showing results for 
Search instead for 
Did you mean: 

Move Category

molti
Champ in-the-making
Champ in-the-making
Hello!
Can anyone help?
I have a categoryA and categoryAA, I need in Javascript to make a categoryA become a parent of a categoryAA. Is it possible in principle?
Like this
A
|_AA
Thank you.
3 REPLIES 3

jpotts
World-Class Innovator
World-Class Innovator
Have you tried calling move() on categoryAA and passing in the noderef to categoryA as the destination?

http://wiki.alfresco.com/wiki/3.4_JavaScript_API

Jeff

molti
Champ in-the-making
Champ in-the-making
yes. I tried, but was a error

15:19:34,663 User:admin ERROR [node.integrity.IntegrityChecker] Found 1 integrity violations:
The association source type is incorrect:
   Source Node: workspace://SpacesStore/485e127a-5496-479c-bea5-a56cfd801781
   Association: Association[ class=ClassDef[name={http://www.alfresco.org/model/content/1.0}folder], name={http://www.alfresco.org/model/content/1.0}contains, target class={http://www.alfresco.org/model/system/1.0}base, source role=null, target role=null]
   Required Source Type: {http://www.alfresco.org/model/content/1.0}folder
   Actual Source Type: {http://www.alfresco.org/model/content/1.0}category

jpotts
World-Class Innovator
World-Class Innovator
Ah, yes, I see the problem. ScriptNode.move() assumes that the QName of the association it is creating between the parent and child is cm:contains. For categories, the association is called cm:subcategories. So you'll have to use Java to do this. Here's a Java-backed web script (just the executeImpl method) that will do it. It assumes you pass in the source node reference and the target node reference, like this:
http://localhost:8080/alfresco/s/someco/example/moveCategory?sourceNodeRef=workspace://SpacesStore/4...
And here is the code:
    protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {

      final String sourceNodeRefString = req.getParameter("sourceNodeRef");
      final String targetNodeRefString = req.getParameter("targetNodeRef");

      if (sourceNodeRefString == null || targetNodeRefString == null) {
         logger.debug("Source or target node ref not set");
         status.setCode(400);         
         status.setMessage("Required data has not been provided");
         status.setRedirect(true);
      }
      
      NodeRef sourceNodeRef = new NodeRef(sourceNodeRefString);
      NodeRef targetNodeRef = new NodeRef(targetNodeRefString);

      String sourceName = (String) nodeService.getProperty(sourceNodeRef, ContentModel.PROP_NAME);
      
      nodeService.moveNode(sourceNodeRef, targetNodeRef, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, sourceName));
      
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("sourceNodeRef", sourceNodeRefString);
      model.put("sourceName", sourceName);
      model.put("targetNodeRef", targetNodeRefString);
      
      return model;
   }
If you want me to email you an Eclipse project with this code ready to deploy, just PM me.

Jeff

UPDATED March 7, 2012: The original snippet had a bug–it was using the wrong namespace URI to create the name of the association.