user1134181
user1134181

Reputation:

How to get associations from the data list by using WebScript?

In the Alfresco One 5.x Developer's Guide there is an example of the data list.

This functionality I would like to use in my project. For example, there are some business process for which performers are predefined. Each Department has its own set of business processes. It's possible to read the metadata of the incoming contract (from email or scanner - is not important) and automatically run a business process, depending on the Department. The concept of data lists seems to me appropriate...

The problem is that I can't obtain associations. In my case, this is the type cm:person.

For example, data list model definition described as follows:

<?xml version="1.0" encoding="UTF-8"?>
<model name="mspdl:MSpredefinedAssigneesDataListModel" xmlns="http://www.alfresco.org/model/dictionary/1.0">

    <description>...</description>
    <author>...</author>
    <version>...</version>

    <imports>
        <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
        <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/>
        <import uri="http://www.alfresco.org/model/datalist/1.0" prefix="dl" />
    </imports>

    <namespaces>
        <namespace uri="http://www.....com/model/datalist/3.0" prefix="mspdl"/>
    </namespaces>

    <types>
        <type name="mspdl:assigneesListItem">
            <title>...</title>
            <parent>dl:dataListItem</parent>
            <properties>
                <property name="mspdl:serviceName">
                    <type>d:text</type>
                    <mandatory>true</mandatory>
                </property>
            </properties>
            <associations>

                <association name="mspdl:projectMember1">
                    <source>
                        <mandatory>true</mandatory>
                        <many>false</many>
                    </source>
                    <target>
                        <class>cm:person</class>
                        <mandatory>true</mandatory>
                        <many>false</many>
                    </target>
                </association>

                <association name="mspdl:projectMember2">
                    <source>
                        <mandatory>true</mandatory>
                        <many>false</many>
                    </source>
                    <target>
                        <class>cm:person</class>
                        <mandatory>true</mandatory>
                        <many>false</many>
                    </target>
                </association>
                ...
                <association name="mspdl:projectMemberN">
                    <source>
                        <mandatory>true</mandatory>
                        <many>false</many>
                    </source>
                    <target>
                        <class>cm:person</class>
                        <mandatory>true</mandatory>
                        <many>false</many>
                    </target>
                </association>

            </associations>
        </type>
    </types>
</model>

The web script where I'm trying to retrieve association:

public class DataListAssignmentsRetriever extends DeclarativeWebScript {

    private final String DATA_LIST_SITE_CONTAINER = "dataLists";
    private final String NAMESPACE_URI = "http://www.......com/model/datalist/3.0";

    @Override
    protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {        
        QName ASSOC_NAME_PROJECT_MEMBER_1 = QName.createQName(NAMESPACE_URI, "projectMember1");
        List<AssociationRef> temp00List = serviceRegistry.getNodeService().getSourceAssocs(dataListNodeRef, ASSOC_NAME_PROJECT_MEMBER_1);
        // temp00List.size() == 0 ???

        List<AssociationRef> temp01List = serviceRegistry.getNodeService().getTargetAssocs(dataListNodeRef, ASSOC_NAME_PROJECT_MEMBER_1);
        // temp01List.size() == 0 ???

        List<ChildAssociationRef> temp02List = serviceRegistry.getNodeService().getChildAssocs(dataListNodeRef);
        // temp02List == 1 < -- Allows to find just only the property 'serviceName'.

        List<ChildAssociationRef> temp03List = 
            serviceRegistry.getNodeService().getChildAssocs(dataListNodeRef, RegexQNamePattern.MATCH_ALL, ASSOC_NAME_PROJECT_MEMBER_1);
        // temp03List.size() == 0 ???

        List<AssociationRef> temp04List.size() = 
            serviceRegistry.getNodeService().getSourceAssocs(dataListNodeRef, RegexQNamePattern.MATCH_ALL);
        // temp04List.size() == 0 ???

        List<AssociationRef> temp05List = serviceRegistry.getNodeService().getTargetAssocs(nodeRef, RegexQNamePattern.MATCH_ALL);
        // temp05List.size() == 0 ???

        ...
    }
}

Service serviceRegistry is injected correctly.

What I'm doing wrong?. How to get associations from the data list?.

I would be very grateful for the information. Thanks to all.


Updated.

NodeRef of my data list is workspace://SpacesStore/b136bebc-fe2c-40fb-aec6-93d9fd22533d. When I searching it in the Node Browser, I get the following:

enter image description here

Hence, I go through the link and see the following: associations is missing.

enter image description here

Then I go through the reference to the child element (showing at the top of the screenshot) and see the following:

enter image description here

And when I go via this link, I see my associations:

enter image description here


Updated.

Thanks for the consulting, Gagravarr and ratik.singhal _. By using the following code I can get the reference to the data list item:

List<ChildAssociationRef> childAssociationRefs = serviceRegistry.getNodeService().getChildAssocs(
        dataListNodeRef, 
        ContentModel.ASSOC_CONTAINS, 
        RegexQNamePattern.MATCH_ALL
);

NodeRef dataListItemNodeRef = childAssociationRefs.get(0).getChildRef();

Here I can see the properties and associations:

Properties:

enter image description here

Associations:

enter image description here

I can access the properties using the following code:

Map<QName, Serializable> properties = serviceRegistry.getNodeService().getProperties(dataListItemNodeRef);
Iterator iterator = properties.entrySet().iterator();
while(iterator.hasNext()) {
    Map.Entry keyValuePairs = (Map.Entry)iterator.next();

    Object key = keyValuePairs.getKey();
    Object value = keyValuePairs.getValue();

    ...

}

But how to obtain associations?...

Upvotes: 0

Views: 771

Answers (1)

user1134181
user1134181

Reputation:

Angel Borroy gave an excellent example that helped me:

And of course, Gagravarr gave a great example:

The solution may look like the following:

...
NodeRef dataListContainer = 
        siteService.getContainer("contracts-site", "dataLists");
List<ChildAssociationRef> dataListsNodes = 
        nodeService.getChildAssocs(dataListContainer);
for(ChildAssociationRef dataList : dataListsNodes) {
    if (dataList.getTypeQName().isMatch(ContentModel.ASSOC_CONTAINS)) {     
        if(nodeService.getProperty(
                dataList.getChildRef(), 
                ContentModel.PROP_TITLE).toString().equals("Data list title here")) { 
            List<ChildAssociationRef> childAssocsRef = 
                    nodeService.getChildAssocs(dataList.getChildRef());
            for(ChildAssociationRef childAssocRef : childAssocsRef) {
                List<AssociationRef> customAssocs = nodeService.getTargetAssocs(
                        childAssocRef.getChildRef(), 
                        QName.createQName(DATALIST_MODEL_URI, "projectMember1"));

                NodeRef nodeRef = customAssocs.get(0).getTargetRef();
                if(ContentModel.TYPE_PERSON.equals(nodeService.getType(nodeRef))) {
                    nodeService.getProperty(nodeRef, ContentModel.PROP_USERNAME);
                }
                ...
            }
        } else continue;
    }
}
...

This solved my issue.

Upvotes: 1

Related Questions