Search in sources :

Example 6 with JSONWriter

use of org.springframework.extensions.webscripts.json.JSONWriter in project alfresco-remote-api by Alfresco.

the class AbstractDocLink method addActivityEntry.

/**
 * Generates an activity entry for the link
 */
protected void addActivityEntry(String activityType, String title, String nodeRef, String site) {
    try {
        StringWriter activityJson = new StringWriter();
        JSONWriter activity = new JSONWriter(activityJson);
        activity.startObject();
        activity.writeValue("title", title);
        activity.writeValue("nodeRef", nodeRef);
        activity.writeValue("page", "document-details?nodeRef=" + nodeRef);
        activity.endObject();
        activityService.postActivity(activityType, site, ACTIVITY_TOOL, activityJson.toString());
    } catch (Exception e) {
        // Warn, but carry on
        logger.warn("Error adding link event to activities feed", e);
    }
}
Also used : JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) StringWriter(java.io.StringWriter) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 7 with JSONWriter

use of org.springframework.extensions.webscripts.json.JSONWriter in project acs-community-packaging by Alfresco.

the class PickerBean method getTagNodes.

/**
 * Return the JSON objects representing a list of cm:folder nodes.
 *
 * IN: "parent" - noderef (can be null) of the parent to retrieve the child folder 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.
 *
 * The 16x16 pixel folder icon path is output as the 'icon' property for each child folder.
 */
@InvokeCommand.ResponseMimetype(value = MimetypeMap.MIMETYPE_HTML)
public void getTagNodes() throws Exception {
    FacesContext fc = FacesContext.getCurrentInstance();
    UserTransaction tx = null;
    try {
        tx = Repository.getUserTransaction(FacesContext.getCurrentInstance(), true);
        tx.begin();
        Collection<ChildAssociationRef> childRefs;
        NodeRef parentRef = null;
        Map params = fc.getExternalContext().getRequestParameterMap();
        String strParentRef = Utils.encode((String) params.get(ID_PARENT));
        if (strParentRef == null || strParentRef.length() == 0) {
            childRefs = this.getCategoryService().getRootCategories(Repository.getStoreRef(), ContentModel.ASPECT_TAGGABLE);
        } else {
            parentRef = new NodeRef(strParentRef);
            childRefs = this.getCategoryService().getChildren(parentRef, CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE);
        }
        JSONWriter out = new JSONWriter(fc.getResponseWriter());
        out.startObject();
        out.startValue(ID_PARENT);
        out.startObject();
        if (parentRef == null) {
            out.writeNullValue(ID_ID);
            out.writeValue(ID_NAME, Application.getMessage(fc, MSG_TAGS));
            out.writeValue(ID_ISROOT, true);
            out.writeValue(ID_SELECTABLE, false);
        } else {
            out.writeValue(ID_ID, strParentRef);
            out.writeValue(ID_NAME, Repository.getNameForNode(this.getInternalNodeService(), parentRef));
        }
        out.endObject();
        out.endValue();
        out.startValue(ID_CHILDREN);
        out.startArray();
        for (ChildAssociationRef ref : childRefs) {
            NodeRef nodeRef = ref.getChildRef();
            out.startObject();
            out.writeValue(ID_ID, nodeRef.toString());
            out.writeValue(ID_NAME, Repository.getNameForNode(this.getInternalNodeService(), nodeRef));
            out.endObject();
        }
        out.endArray();
        out.endValue();
        out.endObject();
        tx.commit();
    } catch (Throwable err) {
        Utils.addErrorMessage("PickerBean exception in getTagNodes()", 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) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap)

Example 8 with JSONWriter

use of org.springframework.extensions.webscripts.json.JSONWriter in project acs-community-packaging by Alfresco.

the class BaseAjaxItemPicker method getItemJson.

/**
 * Returns Json string representing an already-selected item.
 *
 * @return hidden field name
 */
protected String getItemJson(String id, String name, String icon) {
    String itemJson = "";
    StringWriter item = new StringWriter(128);
    JSONWriter json = new JSONWriter(item);
    try {
        json.startObject();
        json.writeValue(ID_ID, id);
        json.writeValue(ID_NAME, name);
        json.writeValue(ID_ICON, (icon != null ? FOLDER_IMAGE_PREFIX + icon + "-16.gif" : getDefaultIcon()));
        json.endObject();
    } catch (Throwable err) {
    }
    itemJson = item.toString();
    return itemJson;
}
Also used : JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) StringWriter(java.io.StringWriter)

Example 9 with JSONWriter

use of org.springframework.extensions.webscripts.json.JSONWriter in project acs-community-packaging by Alfresco.

the class PickerBean method getFolderNodes.

@InvokeCommand.ResponseMimetype(value = MimetypeMap.MIMETYPE_HTML)
public void getFolderNodes() throws Exception {
    FacesContext fc = FacesContext.getCurrentInstance();
    UserTransaction tx = null;
    try {
        tx = Repository.getUserTransaction(FacesContext.getCurrentInstance(), true);
        tx.begin();
        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);
            }
        }
        List<FileInfo> folders = this.getFileFolderService().listFolders(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.endObject();
        out.endValue();
        out.startValue(ID_CHILDREN);
        out.startArray();
        // filter out those children that are not spaces
        for (FileInfo folder : folders) {
            out.startObject();
            out.writeValue(ID_ID, folder.getNodeRef().toString());
            out.writeValue(ID_NAME, (String) folder.getProperties().get(ContentModel.PROP_NAME));
            String icon = (String) folder.getProperties().get(ApplicationModel.PROP_ICON);
            out.writeValue(ID_ICON, FOLDER_IMAGE_PREFIX + (icon != null ? icon + "-16.gif" : BrowseBean.SPACE_SMALL_DEFAULT + ".gif"));
            out.endObject();
        }
        out.endArray();
        out.endValue();
        out.endObject();
        tx.commit();
    } catch (Throwable err) {
        Utils.addErrorMessage("PickerBean exception in getFolderNodes()", 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) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap)

Example 10 with JSONWriter

use of org.springframework.extensions.webscripts.json.JSONWriter 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)

Aggregations

JSONWriter (org.springframework.extensions.webscripts.json.JSONWriter)14 StringWriter (java.io.StringWriter)9 Map (java.util.Map)5 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)5 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 TransferException (org.alfresco.service.cmr.transfer.TransferException)5 FacesContext (javax.faces.context.FacesContext)4 UserTransaction (javax.transaction.UserTransaction)4 MimetypeMap (org.alfresco.repo.content.MimetypeMap)4 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)3 WrappingWebScriptRequest (org.springframework.extensions.webscripts.WrappingWebScriptRequest)3 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)3 IOException (java.io.IOException)2 FileInfo (org.alfresco.service.cmr.model.FileInfo)2 JSONException (org.json.JSONException)2 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 StringTokenizer (java.util.StringTokenizer)1