Search in sources :

Example 31 with AssociationRef

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);
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) AccessDeniedException(net.sf.acegisecurity.AccessDeniedException) ArrayList(java.util.ArrayList) ACLEntryVoterException(org.alfresco.repo.security.permissions.impl.acegi.ACLEntryVoterException) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) PermissionCheckValue(org.alfresco.repo.security.permissions.PermissionCheckValue) PermissionCheckCollection(org.alfresco.repo.security.permissions.PermissionCheckCollection)

Example 32 with AssociationRef

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);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 33 with AssociationRef

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()));
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMessage(javax.mail.internet.MimeMessage) InputStream(java.io.InputStream) Part(javax.mail.Part) ContentReader(org.alfresco.service.cmr.repository.ContentReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 34 with AssociationRef

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;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ParameterCheck.mandatoryString(org.alfresco.util.ParameterCheck.mandatoryString) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) HashSet(java.util.HashSet)

Example 35 with AssociationRef

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

AssociationRef (org.alfresco.service.cmr.repository.AssociationRef)35 NodeRef (org.alfresco.service.cmr.repository.NodeRef)26 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)22 QName (org.alfresco.service.namespace.QName)13 ArrayList (java.util.ArrayList)9 Serializable (java.io.Serializable)8 HashMap (java.util.HashMap)7 List (java.util.List)6 Map (java.util.Map)6 Iterator (java.util.Iterator)4 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)4 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)3 QNamePattern (org.alfresco.service.namespace.QNamePattern)3 IOException (java.io.IOException)2 Date (java.util.Date)2 ListDataModel (javax.faces.model.ListDataModel)2 MimeMessage (javax.mail.internet.MimeMessage)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 DictionaryService (org.alfresco.service.cmr.dictionary.DictionaryService)2 ContentData (org.alfresco.service.cmr.repository.ContentData)2