Search in sources :

Example 1 with Relationship

use of org.alfresco.module.org_alfresco_module_rm.relationship.Relationship in project records-management by Alfresco.

the class CustomRefsGet method getRelationshipData.

/**
 * Creates relationship data for the ftl template
 *
 * @param relationships The relationships
 * @return The relationship data
 */
private List<Map<String, String>> getRelationshipData(Set<Relationship> relationships) {
    List<Map<String, String>> relationshipData = new ArrayList<Map<String, String>>();
    for (Relationship relationship : relationships) {
        String uniqueName = relationship.getUniqueName();
        RelationshipDefinition relationshipDefinition = getRelationshipService().getRelationshipDefinition(uniqueName);
        NodeRef source = relationship.getSource();
        NodeRef target = relationship.getTarget();
        if (relationshipDefinition != null && hasView(source) && hasView(target)) {
            Map<String, String> data = new HashMap<String, String>();
            RelationshipType type = relationshipDefinition.getType();
            RelationshipDisplayName displayName = relationshipDefinition.getDisplayName();
            if (RelationshipType.BIDIRECTIONAL.equals(type)) {
                data.put(LABEL, displayName.getSourceText());
                data.put(SOURCE_REF, source.toString());
                data.put(TARGET_REF, target.toString());
            } else if (RelationshipType.PARENTCHILD.equals(type)) {
                data.put(SOURCE, displayName.getSourceText());
                data.put(TARGET, displayName.getTargetText());
                data.put(PARENT_REF, source.toString());
                data.put(CHILD_REF, target.toString());
            } else {
                StringBuilder sb = new StringBuilder();
                sb.append("Unsupported relationship type '").append(type).append("'.");
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, sb.toString());
            }
            data.put(REFERENCE_TYPE, type.toString().toLowerCase());
            data.put(REF_ID, uniqueName);
            relationshipData.add(data);
        }
    }
    return relationshipData;
}
Also used : RelationshipDisplayName(org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDisplayName) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelationshipType(org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipType) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Relationship(org.alfresco.module.org_alfresco_module_rm.relationship.Relationship) RelationshipDefinition(org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Relationship

use of org.alfresco.module.org_alfresco_module_rm.relationship.Relationship in project records-management by Alfresco.

the class RelationshipsGet method buildRelationshipData.

/**
 * Creates the relationship data
 *
 * @param relationships The {@link Set} of relationships
 * @param relationshipEndPoint The end point of the relationship, which is either {@link RelationshipEndpoint#SOURCE} or {@link RelationshipEndpoint#TARGET}
 * @return The relationship data as {@link List}
 */
private List<String> buildRelationshipData(Set<Relationship> relationships, RelationshipEndPoint relationshipEndPoint) {
    List<String> result = new ArrayList<String>();
    for (Relationship relationship : relationships) {
        String uniqueName = relationship.getUniqueName();
        RelationshipDefinition relationshipDefinition = getRelationshipService().getRelationshipDefinition(uniqueName);
        if (relationshipDefinition != null) {
            NodeRef node;
            String label;
            if (RelationshipEndPoint.SOURCE.equals(relationshipEndPoint)) {
                node = relationship.getSource();
                label = relationshipDefinition.getDisplayName().getSourceText();
            } else if (RelationshipEndPoint.TARGET.equals(relationshipEndPoint)) {
                node = relationship.getTarget();
                label = relationshipDefinition.getDisplayName().getTargetText();
            } else {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unknown relationship end point type '" + relationshipEndPoint + "'.");
            }
            String nodeDetails = getJsonConversionComponent().toJSON(node, true);
            JSONObject jsonObject = WebScriptUtils.createJSONObject(nodeDetails);
            WebScriptUtils.putValueToJSONObject(jsonObject, RELATIONSHIP_LABEL, label);
            WebScriptUtils.putValueToJSONObject(jsonObject, RELATIONSHIP_UNIQUE_NAME, relationshipDefinition.getUniqueName());
            result.add(jsonObject.toString());
        }
    }
    return result;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Relationship(org.alfresco.module.org_alfresco_module_rm.relationship.Relationship) ArrayList(java.util.ArrayList) RelationshipDefinition(org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDefinition)

Example 3 with Relationship

use of org.alfresco.module.org_alfresco_module_rm.relationship.Relationship in project records-management by Alfresco.

the class AutoRecordableVersionsTest method testVersionRecordsRelated.

/**
 * Given that all revisions will be automatically recorded,
 * When I update a document 3 times,
 * Then all 3 created records will be related together using the "VersionedBy" relationship
 */
public void testVersionRecordsRelated() {
    doBehaviourDrivenTest(new BehaviourDrivenTest(dmCollaborator, false) {

        /**
         * given *
         */
        public void given() throws Exception {
            doTestInTransaction(new VoidTest() {

                @Override
                public void runImpl() throws Exception {
                    // set the recordable version policy
                    PropertyMap recordableVersionProperties = new PropertyMap(1);
                    recordableVersionProperties.put(PROP_RECORDABLE_VERSION_POLICY, RecordableVersionPolicy.ALL);
                    recordableVersionProperties.put(PROP_FILE_PLAN, filePlan);
                    nodeService.addAspect(dmDocument, RecordableVersionModel.ASPECT_VERSIONABLE, recordableVersionProperties);
                    // make the node versionable
                    PropertyMap versionableProperties = new PropertyMap(1);
                    versionableProperties.put(ContentModel.PROP_INITIAL_VERSION, false);
                    nodeService.addAspect(dmDocument, ContentModel.ASPECT_VERSIONABLE, versionableProperties);
                }
            });
        }

        /**
         * when *
         */
        public void when() {
            // update the content 3 times
            updateContent();
            updateContent();
            updateContent();
        }

        /**
         * then
         */
        public void then() {
            doTestInTransaction(new VoidTest() {

                @Override
                public void runImpl() throws Exception {
                    // check that the record has been recorded
                    checkRecordedVersion(dmDocument, null, "0.3");
                    Version version = versionService.getCurrentVersion(dmDocument);
                    NodeRef record = recordableVersionService.getVersionRecord(version);
                    boolean foundPrevious = false;
                    Set<Relationship> relationships = relationshipService.getRelationshipsFrom(record);
                    assertNotNull(relationships);
                    assertEquals(1, relationships.size());
                    for (Relationship relationship : relationships) {
                        if (relationship.getUniqueName().equals(CUSTOM_REF_VERSIONS.getLocalName())) {
                            NodeRef previousVersionRecord = relationship.getTarget();
                            assertNotNull(previousVersionRecord);
                            foundPrevious = true;
                        }
                    }
                    assertTrue(foundPrevious);
                }
            });
        }

        /**
         * Helper method to update content of dmDocument
         */
        private void updateContent() {
            doTestInTransaction(new VoidTest() {

                @Override
                public void runImpl() throws Exception {
                    ContentWriter writer = contentService.getWriter(dmDocument, ContentModel.PROP_CONTENT, true);
                    writer.putContent(MY_NEW_CONTENT);
                }
            });
        }
    });
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) Version(org.alfresco.service.cmr.version.Version) Relationship(org.alfresco.module.org_alfresco_module_rm.relationship.Relationship)

Aggregations

Relationship (org.alfresco.module.org_alfresco_module_rm.relationship.Relationship)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 ArrayList (java.util.ArrayList)2 RelationshipDefinition (org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDefinition)2 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RelationshipDisplayName (org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDisplayName)1 RelationshipType (org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipType)1 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)1 Version (org.alfresco.service.cmr.version.Version)1 PropertyMap (org.alfresco.util.PropertyMap)1 JSONObject (org.json.JSONObject)1