Search in sources :

Example 16 with AssociationRef

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

the class EditUserDetailsDialog method finishImpl.

@Override
protected String finishImpl(FacesContext context, String outcome) throws Exception {
    try {
        ServiceRegistry services = Repository.getServiceRegistry(FacesContext.getCurrentInstance());
        DictionaryService dd = services.getDictionaryService();
        Map<QName, Serializable> props = getNodeService().getProperties(getPerson().getNodeRef());
        for (String key : getPerson().getProperties().keySet()) {
            QName propQName = QName.createQName(key);
            if (dd.getProperty(propQName) == null || dd.getProperty(propQName).isProtected() == false) {
                props.put(propQName, (Serializable) getPerson().getProperties().get(key));
            }
        }
        // persist all property changes
        NodeRef personRef = getPerson().getNodeRef();
        this.getNodeService().setProperties(personRef, props);
        // save person description content field
        if (this.personDescription != null) {
            ContentService cs = services.getContentService();
            ContentWriter writer = cs.getWriter(personRef, ContentModel.PROP_PERSONDESC, true);
            writer.setMimetype(MimetypeMap.MIMETYPE_HTML);
            writer.putContent(this.personDescription);
        }
        // setup user avatar association
        if (this.photoRef != null) {
            List<AssociationRef> refs = this.getNodeService().getTargetAssocs(personRef, ContentModel.ASSOC_AVATAR);
            // remove old association if it exists
            if (refs.size() == 1) {
                NodeRef existingRef = refs.get(0).getTargetRef();
                this.getNodeService().removeAssociation(personRef, existingRef, ContentModel.ASSOC_AVATAR);
            }
            // setup new association
            this.getNodeService().createAssociation(personRef, this.photoRef, ContentModel.ASSOC_AVATAR);
        }
        // if the above calls were successful, then reset Person Node in the session
        Application.getCurrentUser(context).reset();
    } catch (Throwable err) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), err.getMessage()), err);
        outcome = null;
        ReportedException.throwIfNecessary(err);
    }
    return outcome;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) Serializable(java.io.Serializable) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) QName(org.alfresco.service.namespace.QName) ServiceRegistry(org.alfresco.service.ServiceRegistry) ContentService(org.alfresco.service.cmr.repository.ContentService) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef)

Example 17 with AssociationRef

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

the class WorkflowUtil method prepareTaskParams.

/**
 * Prepares the given node for persistence in the workflow engine.
 *
 * @param node The node to package up for persistence
 * @return The map of data representing the node
 */
@SuppressWarnings("unchecked")
public static Map<QName, Serializable> prepareTaskParams(Node node) {
    Map<QName, Serializable> params = new HashMap<QName, Serializable>();
    // marshal the properties and associations captured by the property sheet
    // back into a Map to pass to the workflow service
    // go through all the properties in the transient node and add them to params map
    Map<String, Object> props = node.getProperties();
    for (String propName : props.keySet()) {
        QName propQName = Repository.resolveToQName(propName);
        params.put(propQName, (Serializable) props.get(propName));
    }
    // go through any associations that have been added to the start task
    // and build a list of NodeRefs representing the targets
    Map<String, Map<String, AssociationRef>> assocs = node.getAddedAssociations();
    for (String assocName : assocs.keySet()) {
        QName assocQName = Repository.resolveToQName(assocName);
        // get the associations added and create list of targets
        Map<String, AssociationRef> addedAssocs = assocs.get(assocName);
        List<AssociationRef> originalAssocRefs = (List<AssociationRef>) node.getAssociations().get(assocName);
        List<NodeRef> targets = new ArrayList<NodeRef>(addedAssocs.size());
        if (originalAssocRefs != null) {
            for (AssociationRef assoc : originalAssocRefs) {
                targets.add(assoc.getTargetRef());
            }
        }
        for (AssociationRef assoc : addedAssocs.values()) {
            targets.add(assoc.getTargetRef());
        }
        params.put(assocQName, (Serializable) targets);
    }
    // go through the removed associations and either setup or adjust the
    // parameters map accordingly
    assocs = node.getRemovedAssociations();
    for (String assocName : assocs.keySet()) {
        QName assocQName = Repository.resolveToQName(assocName);
        // get the associations removed and create list of targets
        Map<String, AssociationRef> removedAssocs = assocs.get(assocName);
        List<NodeRef> targets = (List<NodeRef>) params.get(assocQName);
        if (targets == null) {
            // if there weren't any assocs of this type added get the current
            // set of assocs from the node
            List<AssociationRef> originalAssocRefs = (List<AssociationRef>) node.getAssociations().get(assocName);
            targets = new ArrayList<NodeRef>(originalAssocRefs.size());
            for (AssociationRef assoc : originalAssocRefs) {
                targets.add(assoc.getTargetRef());
            }
        }
        // remove the assocs the user deleted
        for (AssociationRef assoc : removedAssocs.values()) {
            targets.remove(assoc.getTargetRef());
        }
        params.put(assocQName, (Serializable) targets);
    }
    // TODO: Deal with child associations if and when we need to support
    // them for workflow tasks, for now warn that they are being used
    Map<?, ?> childAssocs = node.getAddedChildAssociations();
    if (childAssocs.size() > 0) {
        if (logger.isWarnEnabled())
            logger.warn("Child associations are present but are not supported for workflow tasks, ignoring...");
    }
    return params;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with AssociationRef

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

the class RelationshipServiceImpl method associationExists.

/**
 * Check if an instance of the association already exists from the given
 * source node reference to the given target node reference
 *
 * @param associationDefinition The association definition
 * @param source The source node reference
 * @param target The target node reference
 * @return <code>true</code> if an association already exists, <code>false</code> otherwise
 */
private boolean associationExists(AssociationDefinition associationDefinition, NodeRef source, NodeRef target) {
    boolean associationAlreadyExists = false;
    QName associationDefinitionName = associationDefinition.getName();
    if (associationDefinition.isChild()) {
        List<ChildAssociationRef> childAssocs = getNodeService().getChildAssocs(source, associationDefinitionName, associationDefinitionName);
        for (ChildAssociationRef chAssRef : childAssocs) {
            if (chAssRef.getChildRef().equals(target)) {
                associationAlreadyExists = true;
            }
        }
    } else {
        List<AssociationRef> assocs = getNodeService().getTargetAssocs(source, associationDefinitionName);
        for (AssociationRef assRef : assocs) {
            if (assRef.getTargetRef().equals(target)) {
                associationAlreadyExists = true;
            }
        }
    }
    return associationAlreadyExists;
}
Also used : QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 19 with AssociationRef

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

the class DocumentPropertiesDialog method save.

/**
 * Event handler used to save the edited properties back to the repository
 *
 * @return The outcome
 */
public String save() {
    String outcome = "cancel";
    UserTransaction tx = null;
    try {
        tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
        tx.begin();
        NodeRef nodeRef = this.browseBean.getDocument().getNodeRef();
        Map<String, Object> props = this.editableNode.getProperties();
        // get the name and move the node as necessary
        String name = (String) props.get(ContentModel.PROP_NAME);
        if (name != null) {
            getFileFolderService().rename(nodeRef, name);
        }
        Map<QName, Serializable> properties = getNodeService().getProperties(nodeRef);
        // we need to put all the properties from the editable bag back into
        // the format expected by the repository
        // Deal with the special mimetype property for ContentData
        String mimetype = (String) props.get(TEMP_PROP_MIMETYPE);
        if (mimetype != null) {
            // remove temporary prop from list so it isn't saved with the others
            props.remove(TEMP_PROP_MIMETYPE);
            ContentData contentData = (ContentData) props.get(ContentModel.PROP_CONTENT);
            if (contentData != null) {
                contentData = ContentData.setMimetype(contentData, mimetype);
                props.put(ContentModel.PROP_CONTENT.toString(), contentData);
            }
        }
        // Deal with the special encoding property for ContentData
        String encoding = (String) props.get(TEMP_PROP_ENCODING);
        if (encoding != null) {
            // remove temporary prop from list so it isn't saved with the others
            props.remove(TEMP_PROP_ENCODING);
            ContentData contentData = (ContentData) props.get(ContentModel.PROP_CONTENT);
            if (contentData != null) {
                contentData = ContentData.setEncoding(contentData, encoding);
                props.put(ContentModel.PROP_CONTENT.toString(), contentData);
            }
        }
        // extra and deal with the Author prop if the aspect has not been applied yet
        String author = (String) props.get(ContentModel.PROP_AUTHOR);
        if (author != null && author.length() != 0) {
            // add aspect if required
            if (getNodeService().hasAspect(nodeRef, ContentModel.ASPECT_AUTHOR) == false) {
                Map<QName, Serializable> authorProps = new HashMap<QName, Serializable>(1, 1.0f);
                authorProps.put(ContentModel.PROP_AUTHOR, author);
                getNodeService().addAspect(nodeRef, ContentModel.ASPECT_AUTHOR, authorProps);
            }
        // else it will get updated in the later setProperties() call
        }
        // deal with adding the "titled" aspect if required
        String title = (String) props.get(ContentModel.PROP_TITLE);
        String description = (String) props.get(ContentModel.PROP_DESCRIPTION);
        if (title != null || description != null) {
            // add the aspect to be sure it's present
            getNodeService().addAspect(nodeRef, ContentModel.ASPECT_TITLED, null);
        // props will get added later in setProperties()
        }
        // add the remaining properties
        Iterator<String> iterProps = props.keySet().iterator();
        while (iterProps.hasNext()) {
            String propName = iterProps.next();
            QName qname = QName.createQName(propName);
            // make sure the property is represented correctly
            Serializable propValue = (Serializable) props.get(propName);
            // check for empty strings when using number types, set to null in this case
            if ((propValue != null) && (propValue instanceof String) && (propValue.toString().length() == 0)) {
                PropertyDefinition propDef = getDictionaryService().getProperty(qname);
                if (propDef != null) {
                    if (propDef.getDataType().getName().equals(DataTypeDefinition.DOUBLE) || propDef.getDataType().getName().equals(DataTypeDefinition.FLOAT) || propDef.getDataType().getName().equals(DataTypeDefinition.INT) || propDef.getDataType().getName().equals(DataTypeDefinition.LONG)) {
                        propValue = null;
                    }
                }
            }
            properties.put(qname, propValue);
        }
        // send the properties back to the repository
        getNodeService().setProperties(this.browseBean.getDocument().getNodeRef(), properties);
        // we also need to persist any association changes that may have been made
        // add any associations added in the UI
        Map<String, Map<String, AssociationRef>> addedAssocs = this.editableNode.getAddedAssociations();
        for (Map<String, AssociationRef> typedAssoc : addedAssocs.values()) {
            for (AssociationRef assoc : typedAssoc.values()) {
                getNodeService().createAssociation(assoc.getSourceRef(), assoc.getTargetRef(), assoc.getTypeQName());
            }
        }
        // remove any association removed in the UI
        Map<String, Map<String, AssociationRef>> removedAssocs = this.editableNode.getRemovedAssociations();
        for (Map<String, AssociationRef> typedAssoc : removedAssocs.values()) {
            for (AssociationRef assoc : typedAssoc.values()) {
                getNodeService().removeAssociation(assoc.getSourceRef(), assoc.getTargetRef(), assoc.getTypeQName());
            }
        }
        // add any child associations added in the UI
        Map<String, Map<String, ChildAssociationRef>> addedChildAssocs = this.editableNode.getAddedChildAssociations();
        for (Map<String, ChildAssociationRef> typedAssoc : addedChildAssocs.values()) {
            for (ChildAssociationRef assoc : typedAssoc.values()) {
                getNodeService().addChild(assoc.getParentRef(), assoc.getChildRef(), assoc.getTypeQName(), assoc.getTypeQName());
            }
        }
        // remove any child association removed in the UI
        Map<String, Map<String, ChildAssociationRef>> removedChildAssocs = this.editableNode.getRemovedChildAssociations();
        for (Map<String, ChildAssociationRef> typedAssoc : removedChildAssocs.values()) {
            for (ChildAssociationRef assoc : typedAssoc.values()) {
                getNodeService().removeChild(assoc.getParentRef(), assoc.getChildRef());
            }
        }
        // commit the transaction
        tx.commit();
        // set the outcome to refresh
        outcome = "finish";
        // reset the document held by the browse bean as it's just been updated
        this.browseBean.getDocument().reset();
    } catch (FileExistsException e) {
        // rollback the transaction
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
        }
        // print status message
        String statusMsg = MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), "error_exists"), e.getName());
        Utils.addErrorMessage(statusMsg);
        // no outcome
        outcome = null;
    } catch (InvalidNodeRefException err) {
        // rollback the transaction
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
        }
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object[] { this.browseBean.getDocument().getId() }));
        // this failure means the node no longer exists - we cannot show the doc properties screen
        outcome = "browse";
    } catch (Throwable e) {
        // rollback the transaction
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
        }
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e);
    }
    return outcome;
}
Also used : UserTransaction(javax.transaction.UserTransaction) Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentData(org.alfresco.service.cmr.repository.ContentData) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) HashMap(java.util.HashMap) Map(java.util.Map) FileExistsException(org.alfresco.service.cmr.model.FileExistsException)

Example 20 with AssociationRef

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

the class AdminNodeBrowseBean method selectFromNode.

/**
 * Action to select association From node
 *
 * @return next action
 */
public String selectFromNode() {
    AssociationRef assocRef = (AssociationRef) getSourceAssocs().getRowData();
    NodeRef sourceRef = assocRef.getSourceRef();
    setNodeRef(sourceRef);
    return "success";
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

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