Search in sources :

Example 81 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 82 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 83 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 84 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)

Example 85 with AssociationRef

use of org.alfresco.service.cmr.repository.AssociationRef in project alfresco-repository by Alfresco.

the class CopyServiceImpl method copyChildren.

/**
 * @param copyChildren              <tt>false</tt> if the client selected not to recurse
 */
private void copyChildren(CopyDetails copyDetails, QName classQName, NodeRef copyTarget, boolean copyTargetIsNew, boolean copyChildren, Map<NodeRef, NodeRef> copiesByOriginals, Set<NodeRef> copies, Map<QName, CopyBehaviourCallback> callbacks) {
    NodeRef sourceNodeRef = copyDetails.getSourceNodeRef();
    ClassDefinition classDef = dictionaryService.getClass(classQName);
    if (classDef == null) {
        // Ignore missing types
        return;
    }
    // Check the behaviour
    CopyBehaviourCallback callback = callbacks.get(classQName);
    if (callback == null) {
        throw new IllegalStateException("Source node class has no callback: " + classQName);
    }
    // Prepare storage for post-copy association handling
    List<Pair<AssociationRef, AssocCopyTargetAction>> postCopyAssocs = TransactionalResourceHelper.getList(KEY_POST_COPY_ASSOCS);
    // Handle peer associations.
    for (Map.Entry<QName, AssociationDefinition> entry : classDef.getAssociations().entrySet()) {
        QName assocTypeQName = entry.getKey();
        AssociationDefinition assocDef = entry.getValue();
        if (assocDef.isChild()) {
            // Ignore child assocs
            continue;
        }
        boolean haveRemovedFromCopyTarget = false;
        // Get the associations
        List<AssociationRef> assocRefs = nodeService.getTargetAssocs(sourceNodeRef, assocTypeQName);
        for (AssociationRef assocRef : assocRefs) {
            // Get the copy action for the association instance
            CopyAssociationDetails assocCopyDetails = new CopyAssociationDetails(assocRef, copyTarget, copyTargetIsNew);
            Pair<AssocCopySourceAction, AssocCopyTargetAction> assocCopyAction = callback.getAssociationCopyAction(classQName, copyDetails, assocCopyDetails);
            // Consider the source side first
            switch(assocCopyAction.getFirst()) {
                case IGNORE:
                    // Do nothing
                    continue;
                case COPY_REMOVE_EXISTING:
                    if (!copyTargetIsNew && !haveRemovedFromCopyTarget) {
                        // Only do this if we are copying over an existing node and we have NOT
                        // already cleaned up for this association type
                        haveRemovedFromCopyTarget = true;
                        for (AssociationRef assocToRemoveRef : internalNodeService.getTargetAssocs(copyTarget, assocTypeQName)) {
                            internalNodeService.removeAssociation(assocToRemoveRef.getSourceRef(), assocToRemoveRef.getTargetRef(), assocTypeQName);
                        }
                    }
                // Fall through to copy
                case COPY:
                    // Record the type of target behaviour that is expected
                    switch(assocCopyAction.getSecond()) {
                        case USE_ORIGINAL_TARGET:
                        case USE_COPIED_TARGET:
                        case USE_COPIED_OTHERWISE_ORIGINAL_TARGET:
                            // Have to save for later to see if the target node is copied, too
                            postCopyAssocs.add(new Pair<AssociationRef, AssocCopyTargetAction>(assocRef, assocCopyAction.getSecond()));
                            break;
                        default:
                            throw new IllegalStateException("Unknown association target copy action: " + assocCopyAction);
                    }
                    break;
                default:
                    throw new IllegalStateException("Unknown association source copy action: " + assocCopyAction);
            }
        }
    }
    // Handle child associations.  These need special attention due to their recursive nature.
    for (Map.Entry<QName, ChildAssociationDefinition> childEntry : classDef.getChildAssociations().entrySet()) {
        QName childAssocTypeQName = childEntry.getKey();
        ChildAssociationDefinition childAssocDef = childEntry.getValue();
        if (!childAssocDef.isChild()) {
            // Ignore non-child assocs
            continue;
        }
        // Get the child associations
        List<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(sourceNodeRef, childAssocTypeQName, RegexQNamePattern.MATCH_ALL);
        for (ChildAssociationRef childAssocRef : childAssocRefs) {
            NodeRef childNodeRef = childAssocRef.getChildRef();
            QName assocQName = childAssocRef.getQName();
            CopyChildAssociationDetails childAssocCopyDetails = new CopyChildAssociationDetails(childAssocRef, copyTarget, copyTargetIsNew, copyChildren);
            // Handle nested copies
            if (copies.contains(childNodeRef)) {
                // of the same type.  This is ignorable.
                continue;
            }
            // Get the copy action for the association instance
            ChildAssocCopyAction childAssocCopyAction = callback.getChildAssociationCopyAction(classQName, copyDetails, childAssocCopyDetails);
            switch(childAssocCopyAction) {
                case IGNORE:
                    break;
                case COPY_ASSOC:
                    nodeService.addChild(copyTarget, childNodeRef, childAssocTypeQName, assocQName);
                    break;
                case COPY_CHILD:
                    // Handle potentially cyclic relationships
                    if (copiesByOriginals.containsKey(childNodeRef)) {
                        // This is either a cyclic relationship or there are multiple different
                        // types of associations between the same parent and child.
                        // Just hook the child up with the association.
                        nodeService.addChild(copyTarget, childNodeRef, childAssocTypeQName, assocQName);
                    } else {
                        // Find out whether to force a recursion
                        ChildAssocRecurseAction childAssocRecurseAction = callback.getChildAssociationRecurseAction(classQName, copyDetails, childAssocCopyDetails);
                        switch(childAssocRecurseAction) {
                            case RESPECT_RECURSE_FLAG:
                                // Keep child copy flag the same
                                break;
                            case FORCE_RECURSE:
                                // Force recurse
                                copyChildren = true;
                                break;
                            default:
                                throw new IllegalStateException("Unrecognized enum");
                        }
                        // This copy may fail silently
                        copyImpl(childNodeRef, copyTarget, childAssocTypeQName, assocQName, // Keep child names for deep copies
                        copyChildren, // Keep child names for deep copies
                        false, copiesByOriginals, copies);
                    }
                    break;
                default:
                    throw new IllegalStateException("Unrecognized enum");
            }
        }
    }
}
Also used : CopyAssociationDetails(org.alfresco.repo.copy.CopyBehaviourCallback.CopyAssociationDetails) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) AssocCopySourceAction(org.alfresco.repo.copy.CopyBehaviourCallback.AssocCopySourceAction) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ChildAssocRecurseAction(org.alfresco.repo.copy.CopyBehaviourCallback.ChildAssocRecurseAction) Pair(org.alfresco.util.Pair) ChildAssocCopyAction(org.alfresco.repo.copy.CopyBehaviourCallback.ChildAssocCopyAction) QName(org.alfresco.service.namespace.QName) AssocCopyTargetAction(org.alfresco.repo.copy.CopyBehaviourCallback.AssocCopyTargetAction) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) CopyChildAssociationDetails(org.alfresco.repo.copy.CopyBehaviourCallback.CopyChildAssociationDetails) ChildAssociationDefinition(org.alfresco.service.cmr.dictionary.ChildAssociationDefinition) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) ChildAssociationDefinition(org.alfresco.service.cmr.dictionary.ChildAssociationDefinition) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AssociationRef (org.alfresco.service.cmr.repository.AssociationRef)138 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)112 NodeRef (org.alfresco.service.cmr.repository.NodeRef)102 QName (org.alfresco.service.namespace.QName)56 ArrayList (java.util.ArrayList)44 Serializable (java.io.Serializable)30 Test (org.junit.Test)26 HashMap (java.util.HashMap)24 List (java.util.List)15 Pair (org.alfresco.util.Pair)14 HashSet (java.util.HashSet)13 Map (java.util.Map)13 StoreRef (org.alfresco.service.cmr.repository.StoreRef)12 BaseSpringTest (org.alfresco.util.BaseSpringTest)10 Extend (org.alfresco.traitextender.Extend)8 Iterator (java.util.Iterator)7 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)7 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)7 Path (org.alfresco.service.cmr.repository.Path)6 Version (org.alfresco.service.cmr.version.Version)6