BlueStar
BlueStar

Reputation: 411

How to programatically update links in Alfresco?

I need to create links for all the documents within a folder programatically. I managed to create the links successfully and my file link node looks similar to following:

 linkNode = nodeService.createNode(
   linkFolderNode, 
   ContentModel.ASSOC_CONTAINS,
   QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, display_Name),
   ApplicationModel.TYPE_FILELINK,
   props
 ).getChildRef();

display_Name here refers to the file name displayed.

However if you update the name of the original file it generates another link to the updated file. Is it possible to avoid this issue and maintain just a single link to a given file?

Upvotes: 0

Views: 101

Answers (1)

kinjelom
kinjelom

Reputation: 6460

display_Name here refers to the file name displayed.

display_Name is just link's name and may be different that linked object name.

NodeService.createNode() returns ChildAssociationRef class, that represents a child relationship between two nodes (parent and child - link that was created). ChildAssociationRef.getChildRef() returns created link NodeRef, for example: workspace://SpacesStore/8dc27c51-cf23-4262-9431-f154edc913d0 (NodeRef.toString()). If you change name of the linked node, its NodeRef will not change.

Try this implementation:

public NodeRef createLink(NodeRef parentRef, NodeRef toLinkRef, 
                          boolean isDocument, String linkName){

    linkName = QName.createValidLocalName(linkName);
    QName linkQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, linkName);

    Map<QName, Serializable> props = new HashMap<QName, Serializable>();
    props.put(ContentModel.PROP_NAME, linkName);
    props.put(ContentModel.PROP_LINK_DESTINATION, toLinkRef);

    final NodeRef linkRef = nodeService.createNode(
        parentRef,
        ContentModel.ASSOC_CONTAINS,
        linkQName,
        isDocument ? ApplicationModel.TYPE_FILELINK : ApplicationModel.TYPE_FOLDERLINK,
        props
    ).getChildRef();
    return linkRef;
}

You can use documentLinkService.createDocumentLink(toLinkRef,parentRef) too, just inject it:

 <property name="documentLinkService" ref="DocumentLinkService" />

Upvotes: 1

Related Questions