cancel
Showing results for 
Search instead for 
Did you mean: 

How to copy a list of associations???

motion11
Champ in-the-making
Champ in-the-making
Hi,

Is there any way in Alfresco to copy a list of associations from one document to another??
This is my situation:

I have a document that has an association in it's model definition with the following code:

<association name="zis:DistributionList">
               <title>Distribution List</title>
          <source>
                   <mandatory>false</mandatory>
                   <many>true</many>
               </source>
               <target>
                  <class>cm:person</class>
                  <mandatory>false</mandatory>
                  <many>true</many>
               </target>
</association>
This represents a distribution list form which I will later on extract user names and send emails to each one of them.
When you edit the properties of this document you can easily select users from the system and ad them to the list. The next thing I want to do is extract them from this list and this document and write them in the same kind of association BUT in another document.

This is what I tried to do:

var source = companyhome.childByNamePath("/Qd1");
var destination = companyhome.childByNamePath("/Destination");
var dlist = source.assocs["zis:DistributionList"];
destination.assocs["zis:DistributionList"] = dlist;

That didn't work! I also tried:
 
for (i in dlist)
   {
   var uid = dlist[i].properties["cm:userName"];
   destination.assocs["zis:DistributionList"][i] = people.getPerson(uid);
   }

That didn't work either!! I also tried:
destination.assocs["zis:DistributionList"] = destination.assocs["zis:DistributionList"].concat(dlist);

That didn't work either!!! I also tried:

for (i in dlist)
   {
   destination.assocs["zis:DistributionList"] = destination.assocs["zis:DistributionList"].concat(dlist[i]);
   }

That didn't work either!!!!
Then I found out that destination.assocs["zisSmiley Very HappyistributionList"] is a read-only.
So I said ok!! Lets try a different approach. I'll create an aspect that has this type of an association and add it to an ordinary ("content") type of document with the data extracted from my source file as in the previous attempts.

<aspects>
      <aspect name="zis:DistributionListAsp">
         <title>Distribution</title>
         <associations>
            <association name="zis:DistributionListAss">
               <title>Distribution List</title>
            <source>
                   <mandatory>false</mandatory>
                   <many>true</many>
               </source>
               <target>
                  <class>cm:person</class>
                  <mandatory>false</mandatory>
                  <many>true</many>
               </target>
            </association>
         </associations>
      </aspect>
</aspects>  

Added some code in the web-client-config-custom.xml as well:

<config evaluator="aspect-name" condition="zis:DistributionListAsp">
      <property-sheet>
         <show-association name="zis:DistributionListAss"/>
      </property-sheet>
</config>
  
<config evaluator="string-compare" condition="Action Wizards">
      <aspects>
         <aspect name="zis:DistributionListAsp" />
      </aspects>
</config>


I tried to do so with the following script:


var source = companyhome.childByNamePath("/Qd1");
var destination = companyhome.childByNamePath("/Regular");
var dlist = source.assocs["zis:DistributionList"];
var asocs = new Array(1);
asocs["zis:DistributionListAss"] = dlist;
destination.addAspect("zis:DistributionListAsp", asocs);
destination.save();

That didn't work either!!!!! The document "Regular" had the [zisSmiley Very HappyistributionListAsp] aspect BUT the aspect had no data within.
I also tried:

var source = companyhome.childByNamePath("/Qd1");
var destination = companyhome.childByNamePath("/Regular");
var dlist = source.assocs["zis:DistributionList"];
var asocs = new Array(1);
for(i in dlist)
{
asocs["zis:DistributionListAss"] = dlist[i].nodeRef;
destination.addAspect("zis:DistributionListAsp", asocs);
out.content += "\nAsocs: (" + i + ")" + asocs["zis:DistributionListAss"];
}
destination.save();

That didn't work either!!!!

Can anyone help me with this?? How can I fill in the Distribution list from another list???

Thanks,

Tom
3 REPLIES 3

dgenard
Champ on-the-rise
Champ on-the-rise
Try the node method
    createAssociation(ScriptNode target, string assocType)
This method is not supported by older Alfresco releases however (before 2.1 if I remember well).

Details on http://wiki.alfresco.com/wiki/JavaScript_API#Modifying_and_Creating_API

Denis

motion11
Champ in-the-making
Champ in-the-making
Thanks for the reply Denis, but that didn't solve the problem either. The method you suggested creates the association, but that is not what I'm trying do to. Because, when the association is created I still have to copy the data from another file.
I found out that this is actually not possible from JavaScript. Association field in the content has only read method in JavaScript. Instead, I will have to dig into Alfresco Java SDK and try to solve the problem by writing some Java code and deploying it onto the server.
I'm still in the process of figuring out how to achieve my goal through Java. If you have any helpful ideas or suggestions please let me know.

Thanks anyway!!

Tom

dgenard
Champ on-the-rise
Champ on-the-rise
Are you sure ?
node.assocs is indeed read-only.
node.createAssociation will create an association to the target node.
By iterating on the source node assocs collection, you should be able to copy the assocations collections.

Denis