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;
}
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;
}
});
}
Aggregations