Search in sources :

Example 6 with ContentService

use of org.alfresco.service.cmr.repository.ContentService 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 7 with ContentService

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

the class EditUserDetailsDialog method getPersonDescription.

public String getPersonDescription() {
    if (personDescription == null) {
        ContentService cs = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getContentService();
        ContentReader reader = cs.getReader(this.person.getNodeRef(), ContentModel.PROP_PERSONDESC);
        if (reader != null && reader.exists()) {
            personDescription = reader.getContentString();
        }
    }
    return personDescription;
}
Also used : ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentService(org.alfresco.service.cmr.repository.ContentService)

Example 8 with ContentService

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

the class PickerBean method getFileFolderNodes.

/**
 * Return the JSON objects representing a list of cm:folder and cm:content nodes.
 *
 * IN: "parent" - noderef (can be null) of the parent to retrieve the child nodes for. Null is valid
 *        and specifies the Company Home root as the parent.
 * IN: "child" - non-null value of the child noderef to retrieve the siblings for - the parent value returned
 *        in the JSON response will be the parent of the specified child.
 * IN: "mimetypes" (optional) - if set, a comma separated list of mimetypes to restrict the file list.
 *
 * It is assumed that only files should be selectable, all cm:folder nodes will be marked with the
 * 'selectable:false' property. Therefore the parent (which is a folder) is not selectable.
 *
 * The 16x16 pixel node icon path is output as the 'icon' property for each child, in addition each
 * cm:content node has an property of 'url' for content download.
 */
@InvokeCommand.ResponseMimetype(value = MimetypeMap.MIMETYPE_HTML)
public void getFileFolderNodes() throws Exception {
    FacesContext fc = FacesContext.getCurrentInstance();
    UserTransaction tx = null;
    try {
        tx = Repository.getUserTransaction(FacesContext.getCurrentInstance(), true);
        tx.begin();
        DictionaryService dd = Repository.getServiceRegistry(fc).getDictionaryService();
        ContentService cs = Repository.getServiceRegistry(fc).getContentService();
        List<ChildAssociationRef> childRefs;
        NodeRef companyHomeRef = new NodeRef(Repository.getStoreRef(), Application.getCompanyRootId(fc));
        NodeRef parentRef = null;
        Map params = fc.getExternalContext().getRequestParameterMap();
        String strChildRef = Utils.encode((String) params.get(PARAM_CHILD));
        if (strChildRef != null && strChildRef.length() != 0) {
            // TODO: check permission on the parent
            NodeRef childRef = new NodeRef(strChildRef);
            parentRef = this.getNodeService().getPrimaryParent(childRef).getParentRef();
        } else {
            // TODO: check permission on the parent
            String strParentRef = Utils.encode((String) params.get(PARAM_PARENT));
            if (strParentRef == null || strParentRef.length() == 0) {
                parentRef = companyHomeRef;
                strParentRef = parentRef.toString();
            } else {
                parentRef = new NodeRef(strParentRef);
            }
        }
        // look for mimetype restriction parameter
        Set<String> mimetypes = null;
        String mimetypeParam = (String) params.get(PARAM_MIMETYPES);
        if (mimetypeParam != null && mimetypeParam.length() != 0) {
            // convert to a set of mimetypes to test each file against
            mimetypes = new HashSet<String>();
            for (StringTokenizer t = new StringTokenizer(mimetypeParam, ","); t.hasMoreTokens(); ) /**/
            {
                mimetypes.add(t.nextToken());
            }
        }
        List<FileInfo> items = this.getFileFolderService().list(parentRef);
        JSONWriter out = new JSONWriter(fc.getResponseWriter());
        out.startObject();
        out.startValue(ID_PARENT);
        out.startObject();
        out.writeValue(ID_ID, parentRef.toString());
        out.writeValue(ID_NAME, Repository.getNameForNode(this.getInternalNodeService(), parentRef));
        if (parentRef.equals(companyHomeRef)) {
            out.writeValue(ID_ISROOT, true);
        }
        out.writeValue(ID_SELECTABLE, false);
        out.endObject();
        out.endValue();
        out.startValue(ID_CHILDREN);
        out.startArray();
        for (FileInfo item : items) {
            if (dd.isSubClass(this.getInternalNodeService().getType(item.getNodeRef()), ContentModel.TYPE_FOLDER)) {
                // found a folder
                out.startObject();
                out.writeValue(ID_ID, item.getNodeRef().toString());
                String name = (String) item.getProperties().get(ContentModel.PROP_NAME);
                out.writeValue(ID_NAME, name);
                String icon = (String) item.getProperties().get(ApplicationModel.PROP_ICON);
                out.writeValue(ID_ICON, FOLDER_IMAGE_PREFIX + (icon != null ? icon + "-16.gif" : BrowseBean.SPACE_SMALL_DEFAULT + ".gif"));
                out.writeValue(ID_SELECTABLE, false);
                out.endObject();
            } else {
                // must be a file
                boolean validFile = true;
                if (mimetypes != null) {
                    validFile = false;
                    ContentReader reader = cs.getReader(item.getNodeRef(), ContentModel.PROP_CONTENT);
                    if (reader != null) {
                        String mimetype = reader.getMimetype();
                        validFile = (mimetype != null && mimetypes.contains(mimetype));
                    }
                }
                if (validFile) {
                    out.startObject();
                    out.writeValue(ID_ID, item.getNodeRef().toString());
                    String name = (String) item.getProperties().get(ContentModel.PROP_NAME);
                    out.writeValue(ID_NAME, name);
                    String icon = FileTypeImageUtils.getFileTypeImage(fc, name, FileTypeImageSize.Small);
                    out.writeValue(ID_ICON, icon);
                    out.writeValue(ID_URL, DownloadContentServlet.generateBrowserURL(item.getNodeRef(), name));
                    out.endObject();
                }
            }
        }
        out.endArray();
        out.endValue();
        out.endObject();
        tx.commit();
    } catch (Throwable err) {
        Utils.addErrorMessage("PickerBean exception in getFileFolderNodes()", err);
        fc.getResponseWriter().write("ERROR: " + err.getMessage());
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) FacesContext(javax.faces.context.FacesContext) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentService(org.alfresco.service.cmr.repository.ContentService) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) StringTokenizer(java.util.StringTokenizer) FileInfo(org.alfresco.service.cmr.model.FileInfo) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap)

Example 9 with ContentService

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

the class BaseActionWizard method insertTemplate.

/**
 * Action handler called to insert a template as the email body
 */
public void insertTemplate(ActionEvent event) {
    String template = (String) this.currentActionProperties.get(MailHandler.PROP_TEMPLATE);
    if (template != null && template.equals(TemplateSupportBean.NO_SELECTION) == false) {
        // get the content of the template so the user can get a basic preview of it
        try {
            NodeRef templateRef = new NodeRef(Repository.getStoreRef(), template);
            ContentService cs = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getContentService();
            ContentReader reader = cs.getReader(templateRef, ContentModel.PROP_CONTENT);
            if (reader != null && reader.exists()) {
                this.currentActionProperties.put(MailHandler.PROP_MESSAGE, reader.getContentString());
                usingTemplate = template;
            }
        } catch (Throwable err) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentService(org.alfresco.service.cmr.repository.ContentService)

Example 10 with ContentService

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

the class TemplateMailHelperBean method insertTemplate.

/**
 * Action handler called to insert a template as the email body
 */
public void insertTemplate(ActionEvent event) {
    if (this.template != null && this.template.equals(TemplateSupportBean.NO_SELECTION) == false) {
        // get the content of the template so the user can get a basic preview of it
        try {
            NodeRef templateRef = new NodeRef(Repository.getStoreRef(), this.template);
            ContentService cs = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getContentService();
            ContentReader reader = cs.getReader(templateRef, ContentModel.PROP_CONTENT);
            if (reader != null && reader.exists()) {
                this.body = reader.getContentString();
                this.usingTemplate = this.template;
            }
        } catch (Throwable err) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentService(org.alfresco.service.cmr.repository.ContentService)

Aggregations

ContentService (org.alfresco.service.cmr.repository.ContentService)18 NodeRef (org.alfresco.service.cmr.repository.NodeRef)12 ContentReader (org.alfresco.service.cmr.repository.ContentReader)9 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)6 QName (org.alfresco.service.namespace.QName)6 ServiceRegistry (org.alfresco.service.ServiceRegistry)5 NodeService (org.alfresco.service.cmr.repository.NodeService)5 Serializable (java.io.Serializable)4 StringTokenizer (java.util.StringTokenizer)3 FacesContext (javax.faces.context.FacesContext)3 FileInfo (org.alfresco.service.cmr.model.FileInfo)3 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 InputStream (java.io.InputStream)2 SocketException (java.net.SocketException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Locale (java.util.Locale)2 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)2 DictionaryService (org.alfresco.service.cmr.dictionary.DictionaryService)2 FileFolderService (org.alfresco.service.cmr.model.FileFolderService)2