Search in sources :

Example 21 with AspectDefinition

use of org.alfresco.service.cmr.dictionary.AspectDefinition in project records-management by Alfresco.

the class RmRestApiTest method postCustomReferenceDefinitions.

/**
 * This method creates a child and a non-child reference and returns their generated ids.
 *
 * @return String[] with element 0 = refId of p/c ref, 1 = refId pf bidi.
 */
private String[] postCustomReferenceDefinitions() throws JSONException, IOException, UnsupportedEncodingException {
    String[] result = new String[2];
    // 1. Child association.
    String jsonString = new JSONStringer().object().key("referenceType").value(RelationshipType.PARENTCHILD).key("source").value(CHILD_SRC).key("target").value(CHILD_TGT).endObject().toString();
    // System.out.println(jsonString);
    // Submit the JSON request.
    final int expectedStatus = 200;
    Response rsp = sendRequest(new PostRequest(RMA_CUSTOM_REFS_DEFINITIONS_URL, jsonString, APPLICATION_JSON), expectedStatus);
    String rspContent = rsp.getContentAsString();
    assertTrue(rspContent.contains("success"));
    // System.out.println(rspContent);
    JSONObject jsonRsp = new JSONObject(new JSONTokener(rspContent));
    String generatedChildRefId = jsonRsp.getJSONObject("data").getString("refId");
    result[0] = generatedChildRefId;
    // 2. Non-child or standard association.
    jsonString = new JSONStringer().object().key("referenceType").value(RelationshipType.BIDIRECTIONAL).key("label").value(BI_DI).endObject().toString();
    // System.out.println(jsonString);
    // Submit the JSON request.
    rsp = sendRequest(new PostRequest(RMA_CUSTOM_REFS_DEFINITIONS_URL, jsonString, APPLICATION_JSON), expectedStatus);
    rspContent = rsp.getContentAsString();
    assertTrue(rspContent.contains("success"));
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    String generatedBidiRefId = jsonRsp.getJSONObject("data").getString("refId");
    result[1] = generatedBidiRefId;
    // Now assert that both have appeared in the data dictionary.
    AspectDefinition customAssocsAspect = dictionaryService.getAspect(ASPECT_CUSTOM_ASSOCIATIONS);
    assertNotNull("Missing customAssocs aspect", customAssocsAspect);
    QName newRefQname = adminService.getQNameForClientId(generatedChildRefId);
    Map<QName, AssociationDefinition> associations = customAssocsAspect.getAssociations();
    assertTrue("Custom child assoc not returned by dataDictionary.", associations.containsKey(newRefQname));
    newRefQname = adminService.getQNameForClientId(generatedBidiRefId);
    assertTrue("Custom std assoc not returned by dataDictionary.", customAssocsAspect.getAssociations().containsKey(newRefQname));
    return result;
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) QName(org.alfresco.service.namespace.QName) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) JSONStringer(org.json.JSONStringer)

Example 22 with AspectDefinition

use of org.alfresco.service.cmr.dictionary.AspectDefinition in project records-management by Alfresco.

the class RecordsManagementAdminServiceImplTest method createAndUseCustomReference.

private void createAndUseCustomReference(final RelationshipType refType, final String label, final String source, final String target) throws Exception {
    final NodeRef testRecord1 = retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Throwable {
            NodeRef result = utils.createRecord(rmFolder, "testRecordA" + System.currentTimeMillis());
            return result;
        }
    });
    final NodeRef testRecord2 = retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Throwable {
            NodeRef result = utils.createRecord(rmFolder, "testRecordB" + System.currentTimeMillis());
            return result;
        }
    });
    final QName generatedQName = retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<QName>() {

        public QName execute() throws Throwable {
            utils.completeRecord(testRecord1);
            utils.completeRecord(testRecord2);
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put("referenceType", refType.toString());
            if (label != null)
                params.put("label", label);
            if (source != null)
                params.put("source", source);
            if (target != null)
                params.put("target", target);
            // Create the relationship display name
            RelationshipDisplayName displayName;
            if (label != null) {
                // A bidirectional reference
                displayName = new RelationshipDisplayName(label, label);
            } else {
                // A parent/child reference
                displayName = new RelationshipDisplayName(source, target);
            }
            // Create the relationship definition
            RelationshipDefinition relationshipDefinition = relationshipService.createRelationshipDefinition(displayName);
            // Get the qualified name
            QName qNameResult = QName.createQName(RM_CUSTOM_PREFIX, relationshipDefinition.getUniqueName(), namespaceService);
            ;
            System.out.println("Creating new " + refType + " reference definition: " + qNameResult);
            System.out.println("  params- label: '" + label + "' source: '" + source + "' target: '" + target + "'");
            return qNameResult;
        }
    });
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            RelationshipDefinition relationshipDefinition = relationshipService.getRelationshipDefinition(generatedQName.getLocalName());
            assertNotNull("Relationship definition from relationshipService was null.", relationshipDefinition);
            assertEquals(generatedQName.getLocalName(), relationshipDefinition.getUniqueName());
            assertTrue(refType.equals(relationshipDefinition.getType()));
            // Now we need to use the custom reference.
            // So we apply the aspect containing it to our test records.
            nodeService.addAspect(testRecord1, ASPECT_CUSTOM_ASSOCIATIONS, null);
            if (RelationshipType.PARENTCHILD.equals(refType)) {
                nodeService.addChild(testRecord1, testRecord2, generatedQName, generatedQName);
            } else {
                nodeService.createAssociation(testRecord1, testRecord2, generatedQName);
            }
            return null;
        }
    });
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // Read back the reference value to make sure it was correctly applied.
            List<ChildAssociationRef> childAssocs = nodeService.getChildAssocs(testRecord1);
            List<AssociationRef> retrievedAssocs = nodeService.getTargetAssocs(testRecord1, RegexQNamePattern.MATCH_ALL);
            Object newlyAddedRef = null;
            if (RelationshipType.PARENTCHILD.equals(refType)) {
                for (ChildAssociationRef caRef : childAssocs) {
                    QName refInstanceQName = caRef.getQName();
                    if (generatedQName.equals(refInstanceQName))
                        newlyAddedRef = caRef;
                }
            } else {
                for (AssociationRef aRef : retrievedAssocs) {
                    QName refQName = aRef.getTypeQName();
                    if (generatedQName.equals(refQName))
                        newlyAddedRef = aRef;
                }
            }
            assertNotNull("newlyAddedRef was null.", newlyAddedRef);
            // Check that the reference has appeared in the data dictionary
            AspectDefinition customAssocsAspect = dictionaryService.getAspect(ASPECT_CUSTOM_ASSOCIATIONS);
            assertNotNull(customAssocsAspect);
            if (RelationshipType.PARENTCHILD.equals(refType)) {
                assertNotNull("The customReference is not returned from the dictionaryService.", customAssocsAspect.getChildAssociations().get(generatedQName));
            } else {
                assertNotNull("The customReference is not returned from the dictionaryService.", customAssocsAspect.getAssociations().get(generatedQName));
            }
            return null;
        }
    });
}
Also used : RelationshipDisplayName(org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDisplayName) Serializable(java.io.Serializable) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) QName(org.alfresco.service.namespace.QName) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RelationshipDefinition(org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDefinition) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)22 QName (org.alfresco.service.namespace.QName)21 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)7 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 Map (java.util.Map)4 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)4 Serializable (java.io.Serializable)3 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)3 HashSet (java.util.HashSet)2 ParameterDefinitionImpl (org.alfresco.repo.action.ParameterDefinitionImpl)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 AssociationDefinition (org.alfresco.service.cmr.dictionary.AssociationDefinition)2 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)2 DictionaryService (org.alfresco.service.cmr.dictionary.DictionaryService)2 CustomProperty (org.alfresco.web.config.AdvancedSearchConfigElement.CustomProperty)2 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 SelectItem (javax.faces.model.SelectItem)1