use of org.alfresco.service.cmr.repository.AssociationRef in project records-management by Alfresco.
the class RMAfterInvocationProvider method decide.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection decide(Authentication authentication, Object object, ConfigAttributeDefinition config, Collection returnedObject) {
if (returnedObject == null) {
return null;
}
List<ConfigAttributeDefintion> supportedDefinitions = extractSupportedDefinitions(config);
if (logger.isDebugEnabled()) {
logger.debug("Entries are " + supportedDefinitions);
}
if (supportedDefinitions.size() == 0) {
return returnedObject;
}
// Default to the system-wide values and we'll see if they need to be reduced
long targetResultCount = returnedObject.size();
int maxPermissionChecks = Integer.MAX_VALUE;
long maxPermissionCheckTimeMillis = this.maxPermissionCheckTimeMillis;
if (returnedObject instanceof PermissionCheckCollection<?>) {
PermissionCheckCollection permissionCheckCollection = (PermissionCheckCollection) returnedObject;
// Get values
targetResultCount = permissionCheckCollection.getTargetResultCount();
if (permissionCheckCollection.getCutOffAfterCount() > 0) {
maxPermissionChecks = permissionCheckCollection.getCutOffAfterCount();
}
if (permissionCheckCollection.getCutOffAfterTimeMs() > 0) {
maxPermissionCheckTimeMillis = permissionCheckCollection.getCutOffAfterTimeMs();
}
}
// Start timer and counter for cut-off
boolean cutoff = false;
long startTimeMillis = System.currentTimeMillis();
int count = 0;
// Keep values explicitly
List<Object> keepValues = new ArrayList<Object>(returnedObject.size());
for (Object nextObject : returnedObject) {
// if the maximum result size or time has been exceeded, then we have to remove only
long currentTimeMillis = System.currentTimeMillis();
// NOTE: for reference - the "maxPermissionChecks" has never been honoured by this loop (since previously the count was not being incremented)
if (count >= targetResultCount) {
// We have enough results. We stop without cutoff.
break;
} else if (count >= maxPermissionChecks) {
// We have been cut off by count
cutoff = true;
if (logger.isDebugEnabled()) {
logger.debug("decide (collection) cut-off: " + count + " checks exceeded " + maxPermissionChecks + " checks");
}
break;
} else if ((currentTimeMillis - startTimeMillis) > maxPermissionCheckTimeMillis) {
// We have been cut off by time
cutoff = true;
if (logger.isDebugEnabled()) {
logger.debug("decide (collection) cut-off: " + (currentTimeMillis - startTimeMillis) + "ms exceeded " + maxPermissionCheckTimeMillis + "ms");
}
break;
}
boolean allowed = true;
for (ConfigAttributeDefintion cad : supportedDefinitions) {
if (cad.mode.equalsIgnoreCase("FilterNode")) {
NodeRef testNodeRef = null;
if (cad.parent) {
if (StoreRef.class.isAssignableFrom(nextObject.getClass())) {
// Will be allowed
testNodeRef = null;
} else if (NodeRef.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = nodeService.getPrimaryParent((NodeRef) nextObject).getParentRef();
} else if (ChildAssociationRef.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = ((ChildAssociationRef) nextObject).getParentRef();
} else if (AssociationRef.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = ((AssociationRef) nextObject).getSourceRef();
} else if (PermissionCheckValue.class.isAssignableFrom(nextObject.getClass())) {
NodeRef nodeRef = ((PermissionCheckValue) nextObject).getNodeRef();
testNodeRef = nodeService.getPrimaryParent(nodeRef).getParentRef();
} else {
throw new ACLEntryVoterException("The specified parameter is recognized: " + nextObject.getClass());
}
} else {
if (StoreRef.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = nodeService.getRootNode((StoreRef) nextObject);
} else if (NodeRef.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = (NodeRef) nextObject;
} else if (ChildAssociationRef.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = ((ChildAssociationRef) nextObject).getChildRef();
} else if (AssociationRef.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = ((AssociationRef) nextObject).getTargetRef();
} else if (PermissionCheckValue.class.isAssignableFrom(nextObject.getClass())) {
testNodeRef = ((PermissionCheckValue) nextObject).getNodeRef();
} else {
throw new ACLEntryVoterException("The specified parameter is recognized: " + nextObject.getClass());
}
}
if (logger.isDebugEnabled()) {
logger.debug("\t" + cad.typeString + " test on " + testNodeRef + " from " + nextObject.getClass().getName());
}
// Null allows
if (isUnfiltered(testNodeRef)) {
// Continue to next ConfigAttributeDefintion
continue;
}
if (allowed && testNodeRef != null && checkRead(testNodeRef) != AccessDecisionVoter.ACCESS_GRANTED) {
allowed = false;
// No point evaluating more ConfigAttributeDefintions
break;
}
}
}
// Failure or success, increase the count
count++;
if (allowed) {
keepValues.add(nextObject);
}
}
// Work out how many were left unchecked (for whatever reason)
int sizeOriginal = returnedObject.size();
int checksRemaining = sizeOriginal - count;
// So make sure that the collection needs modification at all
if (keepValues.size() < sizeOriginal) {
// There are values that need to be removed. We have to modify the collection.
try {
returnedObject.clear();
returnedObject.addAll(keepValues);
} catch (UnsupportedOperationException e) {
throw new AccessDeniedException("Permission-checked list must be modifiable", e);
}
}
// Attach the extra permission-check data to the collection
return PermissionCheckedCollectionMixin.create(returnedObject, cutoff, checksRemaining, sizeOriginal);
}
use of org.alfresco.service.cmr.repository.AssociationRef in project records-management by Alfresco.
the class OnReferencedRecordActionedUpon method processRecord.
private void processRecord(NodeRef record) {
List<AssociationRef> fromAssocs = nodeService.getTargetAssocs(record, RegexQNamePattern.MATCH_ALL);
for (AssociationRef fromAssoc : fromAssocs) {
if (reference.equals(fromAssoc.getTypeQName())) {
NodeRef nodeRef = fromAssoc.getTargetRef();
doEventComplete(nodeRef);
}
}
List<AssociationRef> toAssocs = nodeService.getSourceAssocs(record, RegexQNamePattern.MATCH_ALL);
for (AssociationRef toAssoc : toAssocs) {
if (reference.equals(toAssoc.getTypeQName())) {
NodeRef nodeRef = toAssoc.getSourceRef();
doEventComplete(nodeRef);
}
}
}
use of org.alfresco.service.cmr.repository.AssociationRef in project records-management by Alfresco.
the class SplitEmailAction method executeImpl.
/**
* @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action,
* org.alfresco.service.cmr.repository.NodeRef)
*/
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
// get node type
getNodeService().getType(actionedUponNodeRef);
if (logger.isDebugEnabled()) {
logger.debug("split email:" + actionedUponNodeRef);
}
if (getRecordService().isRecord(actionedUponNodeRef)) {
if (!getRecordService().isDeclared(actionedUponNodeRef)) {
ChildAssociationRef parent = getNodeService().getPrimaryParent(actionedUponNodeRef);
/**
* Check whether the email message has already been split - do nothing if it has already been split
*/
List<AssociationRef> refs = getNodeService().getTargetAssocs(actionedUponNodeRef, ImapModel.ASSOC_IMAP_ATTACHMENT);
if (refs.size() > 0) {
if (logger.isDebugEnabled()) {
logger.debug("mail message has already been split - do nothing");
}
return;
}
/**
* Get the content and if its a mime message then create atachments for each part
*/
try {
ContentReader reader = getContentService().getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
InputStream is = reader.getContentInputStream();
MimeMessage mimeMessage = new MimeMessage(null, is);
Object content = mimeMessage.getContent();
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0, n = multipart.getCount(); i < n; i++) {
Part part = multipart.getBodyPart(i);
if ("attachment".equalsIgnoreCase(part.getDisposition())) {
createAttachment(actionedUponNodeRef, parent.getParentRef(), part);
}
}
}
} catch (Exception e) {
throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_NO_READ_MIME_MESSAGE, e.toString()), e);
}
} else {
throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_EMAIL_DECLARED, actionedUponNodeRef.toString()));
}
} else {
throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_EMAIL_NOT_RECORD, actionedUponNodeRef.toString()));
}
}
use of org.alfresco.service.cmr.repository.AssociationRef in project records-management by Alfresco.
the class RelationshipServiceImpl method generateRelationshipFromAssociationRef.
/**
* Generates relationships from the given association references
*
* @param associationRefs Association references
* @return Relationships generated from the given association references
*/
private Set<Relationship> generateRelationshipFromAssociationRef(List<AssociationRef> associationRefs, String nameFilter) {
Set<Relationship> relationships = new HashSet<Relationship>();
for (AssociationRef associationRef : associationRefs) {
String uniqueName = associationRef.getTypeQName().getLocalName();
if (existsRelationshipDefinition(uniqueName) && (nameFilter == null || uniqueName.equals(nameFilter))) {
NodeRef from = associationRef.getSourceRef();
NodeRef to = associationRef.getTargetRef();
relationships.add(new RelationshipImpl(uniqueName, from, to));
}
}
return relationships;
}
use of org.alfresco.service.cmr.repository.AssociationRef 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