cancel
Showing results for 
Search instead for 
Did you mean: 

Ayuda con JAVA para recorrer asociaciones de un NodeRef

ruben_arjonilla
Champ in-the-making
Champ in-the-making
Buenas,

Estoy realizando un pequeño scheduledJob, aunque supongo que tanto dará que sea un scheduledJob que un WebScript, que cualquier otra cosa.
Lo estoy haciendo con codigo JAVA, y no consigo recorrer las asociaciones de un Nodo.

por ejemplo, llego a tener este nodo

NodeRef node = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
node = nodeService.getChildByName(node, ContentModel.ASSOC_CONTAINS, "mi-fichero.pdf");

este nodo, es de un tipo mio, es del tipo "tipo1", y tiene una asociacion multiple a otros ficheros del tipo "tipo2"
   <type name="dt:tipo1">
      <title>Mi tipo</title>
      <parent>cm:content</parent>
      <properties>

         <property name="dt:propiedad1">
            <title>Propiedad 1</title>
            <description>Propiedad 1.</description>
            <type>d:text</type>
            <mandatory>false</mandatory>
            <default></default>
         </property>

      </properties>
      <associations>

         <association name="dt:associacion1">
            <title>Documentos asociados</title>
            <source>
               <mandatory>false</mandatory>
               <many>true</many>
            </source>
            <target>
               <class>dt:tipo2</class>
               <mandatory>false</mandatory>
               <many>true</many>
            </target>
         </association>

      </associations>
   </type>

   <type name="dt:tipo2">
      <title>Mi tipo 2</title>
      <parent>cm:content</parent>
      <properties>

         <property name="dt:propiedad1">
            <title>Propiedad 2</title>
            <description>Propiedad 2.</description>
            <type>d:text</type>
            <mandatory>false</mandatory>
            <default></default>
         </property>
            
      </properties>
   </type>

en javascript es muy sencillo, basta con esto:

var assocs = node.assocs["associacion1"];
for (var i = 0; i < assocs.length; i++)
{
   var assoc = assocs[i];
}

pero con JAVA no consigo obtener esos documentos.

Muchas gracias de antemano
2 REPLIES 2

cristinamr
World-Class Innovator
World-Class Innovator
Buenas.

He encontrado este ejemplo que creo que puede servirte (de aquí😞

   // Process the collections
        NodeRef srcCollection = nodeService.getChildByName(
                sourceSection, ContentModel.ASSOC_CONTAINS, "collections"
        );
        NodeRef dstCollection = nodeService.getChildByName(
                newSection, ContentModel.ASSOC_CONTAINS, "collections"
        );
        if(srcCollection != null && dstCollection != null)
        {
            // Copy each child of the collection in turn
            for(ChildAssociationRef ref :nodeService.getChildAssocs(srcCollection))
            {
                if(ref.isPrimary())
                {
                    String name = (String)nodeService.getProperty(ref.getChildRef(), ContentModel.PROP_NAME);
                    if(nodeService.getChildByName(dstCollection, ref.getTypeQName(), name) != null)
                    {
                        // There's already something in the destination collection
                        //  with this name. Assume it's deliberate, and don't copy
                    }
                    else
                    {
                        // Copy the resource over
                        NodeRef copy = copyService.copy(
                                ref.getChildRef(),
                                dstCollection,
                                ref.getTypeQName(),
                                ref.getQName(),
                                true
                        );
                        nodeService.setProperty(copy, ContentModel.PROP_NAME, name);
                    }
                }
                else
                {
                    // Don't copy non primary associations
                   
                    // Note: If it's a static asset collection, we may in future want to try to
                    //  identify the equivalent translated assets, ask Brian R for details…
                }
            }
        }
        else
        {
            // This shouldn't happen - aspect behaviour should have already triggered
            log.warn("Missing collections on WCM Section! Unable to migrate assets");
        }

Es decir, en este ejemplo se rescatan las asociaciones :

   NodeRef srcCollection = nodeService.getChildByName(
                sourceSection, ContentModel.ASSOC_CONTAINS, "collections"
        );

Y después lo recorre con este bucle:

    for(ChildAssociationRef ref :nodeService.getChildAssocs(srcCollection))
            {
                if(ref.isPrimary())
                {
                    String name = (String)nodeService.getProperty(ref.getChildRef(), ContentModel.PROP_NAME);
                    if(nodeService.getChildByName(dstCollection, ref.getTypeQName(), name) != null)
                    {
                        // There's already something in the destination collection
                        //  with this name. Assume it's deliberate, and don't copy
                    }
                    else
                    {

                    

Lo que quieres hacer debería ser similar  :wink:

Espero que te sirva de orientación 😃

Un saludo!!!
--
VenziaIT: helping companies since 2005! Our ECM products: AQuA & Seidoc

ruben_arjonilla
Champ in-the-making
Champ in-the-making
Al final he dado con la solución


String ASSOC_URL = "la-url-que-tengamos-en-el-modelo";

NodeRef node = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
node = nodeService.getChildByName(node, ContentModel.ASSOC_CONTAINS, "mi-fichero.pdf");

QName asociacionQName = QName.createQName(ASSOC_URL, "asociacion1")
List<AssociationRef> assocNodeRefs = nodeService.getTargetAssocs(node, asociacionQName);
for(int i=0; i< assocNodeRefs.size(); i++)
{
   NodeRef assocNodeRef = assocNodeRefs.get(i).getTargetRef();
}

al tratarse de una asociación, y no de una child, se ha de utilizar el getTargetAssocs y no el getChildByName, ya que este solo da los child.

Gracias de nuevo Cristina, tu mensaje me dio la idea de donde tenia que buscar la solución, en el nodeService, de ahi ya fui probando las distintas funciones que encontré hasta que vi esta Smiley Wink
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.