Working with Relationships Between Folders/Files - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

Setting up relationships, referred to as associations, between different types of nodes is useful when modelling a specific domain.

There are quite a few API calls involved in managing associations between nodes in the repository:

API Call Description API Explorer
GET nodes/{parentId}/children List the parent-to-primary-child associations (i.e. cm:contains) for a parent node. http://localhost:8080/api-explorer/#!/nodes/listNodeChildren
POST nodes/{parentId}/children Create a new parent-to-primary-child association (i.e. cm:contains) for a parent node. http://localhost:8080/api-explorer/#!/nodes/createNode
GET nodes/{parentId}/secondary-children List the parent-to-secondary-child associations for a parent node. http://localhost:8080/api-explorer/#!/nodes/listSecondaryChildren
GET nodes/{childId}/parents Get a list of parent nodes that are associated with a child node. http://localhost:8080/api-explorer/#!/nodes/listParents
POST nodes/{parentId}/secondary-children Create a new parent-to-secondary-child association for a parent node. http://localhost:8080/api-explorer/#!/nodes/createSecondaryChildAssociation
DELETE /nodes/{parentId}/secondary-children/{childId} Delete a parent-to-secondary-child association. http://localhost:8080/api-explorer/#!/nodes/deleteSecondaryChildAssociation
GET nodes/{sourceId}/targets List the peer-to-peer associations for a source node. http://localhost:8080/api-explorer/#!/nodes/listTargetAssociations
GET nodes/{targetId}/sources Get a list of source nodes that are associated with a target node. http://localhost:8080/api-explorer/#!/nodes/listSourceAssociations
POST nodes/{sourceId}/targets Create a new peer-to-peer association for a source node. http://localhost:8080/api-explorer/#!/nodes/createAssociation
DELETE /nodes/{sourceId}/targets/{targetId} Delete a peer-to-peer association. http://localhost:8080/api-explorer/#!/nodes/deleteSecondaryChildAssociation

When working with content, it’s important to be able to set up relationships such as folder to files and file to file. These relationships are referred to as associations in the Alfresco content model. Out of the box you have already worked with a folder to folders and files association, which is used to build the folder hierarchy that you see in the UI clients. This association is called cm:contains and is part of the cm:folder type:

<typename="cm:folder">
  <title>Folder</title>
  <parent>cm:cmobject</parent>
  <archive>true</archive>
  <associations>
    <child-association name="cm:contains">
      <source>
        <mandatory>false</mandatory>
        <many>true</many>
      </source>
      <target>
        <class>sys:base</class>
        <mandatory>false</mandatory>
        <many>true</many>
      </target>
      <duplicate>false</duplicate>
      <propagateTimestamps>true</propagateTimestamps>
    </child-association>
  </associations>
</type>

This association is of the type child-association, which means that if you delete the folder the contained folders and files will also be automatically deleted. There is also a peer-to-peer association type that you can see an example of in the out-of-the-box cm:person type definition:

<typename="cm:person">
  <title>Person</title>
  <parent>cm:authority</parent>
  <properties>
    ...
  </properties>
  <associations>
    <association name="cm:avatar">
      <source>
        <role>cm:avatarOf</role>
        <mandatory>false</mandatory>
        <many>false</many>
      </source>
      <target>
        <class>cm:content</class>
        <role>cm:hasAvatar</role>
        <mandatory>false</mandatory>
        <many>false</many>
      </target>
    </association>
  </associations>
</type>
      

The peer-to-peer association type is called just association. If you delete a person node the associated avatar will not be automatically deleted.

An association is built up of a source and a target definition, and the source is basically the class (i.e. type or aspect) that defines the association. You can use the mandatory and many properties to define the cardinality for the association.

When you work with the cm:contains association type you set up what is referred to as primary parent-child associations. This is what happens when you upload a file or create a folder. There is also the possibility to work with other types of child associations, they are then referred to as secondary child associations. We will have a look at them now.