Search in sources :

Example 21 with ContentWriter

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

the class EditSearchDialog method saveEditSearchOK.

public String saveEditSearchOK(FacesContext newContext, String newOutcome) {
    String outcome = newOutcome;
    final SearchContext search = this.navigator.getSearchContext();
    if (search != null) {
        try {
            final FacesContext context = newContext;
            RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

                public Object execute() throws Throwable {
                    // handle Edit e.g. Overwrite of existing search
                    // detect if was previously selected saved search (e.g.
                    // NodeRef not null)
                    NodeRef searchRef = new NodeRef(Repository.getStoreRef(), properties.getSavedSearch());
                    if (getNodeService().exists(searchRef)) {
                        Map<QName, Serializable> props = getNodeService().getProperties(searchRef);
                        props.put(ContentModel.PROP_NAME, properties.getSearchName());
                        props.put(ContentModel.PROP_DESCRIPTION, properties.getSearchDescription());
                        getNodeService().setProperties(searchRef, props);
                        ContentService contentService = Repository.getServiceRegistry(context).getContentService();
                        ContentWriter writer = contentService.getWriter(searchRef, ContentModel.PROP_CONTENT, true);
                        // get a writer to our new node ready for XML
                        // content
                        writer.setMimetype(MimetypeMap.MIMETYPE_XML);
                        writer.setEncoding("UTF-8");
                        // output an XML serialized version of the
                        // SearchContext object
                        writer.putContent(search.toXML());
                    }
                    return null;
                }
            };
            callback.execute();
            properties.getCachedSavedSearches().clear();
            properties.setSavedSearch(null);
        } catch (Throwable e) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(newContext, MSG_ERROR_SAVE_SEARCH), e.getMessage()), e);
            outcome = null;
            this.isFinished = false;
            ReportedException.throwIfNecessary(e);
        }
    }
    return outcome;
}
Also used : FacesContext(javax.faces.context.FacesContext) Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ContentService(org.alfresco.service.cmr.repository.ContentService) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)

Example 22 with ContentWriter

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

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

the class EditPostDialog method finishImpl.

@Override
protected String finishImpl(FacesContext context, String outcome) throws Exception {
    // remove link breaks and replace with <br>
    this.content = Utils.replaceLineBreaks(this.content, false);
    // update the content
    NodeRef postNode = this.browseBean.getDocument().getNodeRef();
    // check that the name of this post does not contain the :
    // character (used in previous versions), if it does rename
    // the post.
    String name = (String) this.getNodeService().getProperty(postNode, ContentModel.PROP_NAME);
    if (name.indexOf(":") != -1) {
        String newName = name.replace(':', '-');
        this.getFileFolderService().rename(postNode, newName);
    }
    ContentWriter writer = this.getContentService().getWriter(postNode, ContentModel.PROP_CONTENT, true);
    if (writer != null) {
        writer.putContent(this.content);
    }
    return outcome;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter)

Example 24 with ContentWriter

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

the class CheckinCheckoutDialog method updateFileOK.

/**
 * Action called upon completion of the Update File page
 */
public String updateFileOK(final FacesContext context, String outcome) {
    // NOTE: for update the document node _is_ the working document!
    final Node node = property.getDocument();
    if (node != null && this.getFileName() != null) {
        try {
            RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
            RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

                public Object execute() throws Throwable {
                    if (logger.isDebugEnabled())
                        logger.debug("Trying to update content node Id: " + node.getId());
                    // get an updating writer that we can use to modify the content on the current node
                    ContentWriter writer = property.getContentService().getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, true);
                    // also update the mime type in case a different type of file is uploaded
                    String mimeType = Repository.getMimeTypeForFileName(context, property.getFileName());
                    writer.setMimetype(mimeType);
                    writer.putContent(property.getFile());
                    return null;
                }
            };
            txnHelper.doInTransaction(callback);
            // clear action context
            property.setDocument(null);
            resetState();
            outcome = AlfrescoNavigationHandler.CLOSE_DIALOG_OUTCOME;
        } catch (Throwable err) {
            Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_UPDATE) + err.getMessage(), err);
            ReportedException.throwIfNecessary(err);
        }
    } else {
        logger.warn("WARNING: updateFileOK called without a current Document!");
    }
    return outcome;
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) Node(org.alfresco.web.bean.repository.Node)

Example 25 with ContentWriter

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

the class ApplyFixMob1573Get method writeCustomContentModel.

private void writeCustomContentModel(M2Model deserializedModel) {
    ContentWriter writer = contentService.getWriter(RM_CUSTOM_MODEL_NODE_REF, ContentModel.TYPE_CONTENT, true);
    writer.setMimetype(MimetypeMap.MIMETYPE_XML);
    writer.setEncoding("UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    deserializedModel.toXML(baos);
    String updatedModelXml;
    try {
        updatedModelXml = baos.toString("UTF-8");
        writer.putContent(updatedModelXml);
    // putContent closes all resources.
    // so we don't have to.
    } catch (UnsupportedEncodingException uex) {
        throw new AlfrescoRuntimeException("Exception when writing custom model xml.", uex);
    }
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)76 NodeRef (org.alfresco.service.cmr.repository.NodeRef)50 HashMap (java.util.HashMap)31 Serializable (java.io.Serializable)22 QName (org.alfresco.service.namespace.QName)21 ContentReader (org.alfresco.service.cmr.repository.ContentReader)20 Test (org.junit.Test)18 FileContentWriter (org.alfresco.repo.content.filestore.FileContentWriter)14 InputStream (java.io.InputStream)13 AlfrescoDocument (org.alfresco.cmis.client.AlfrescoDocument)13 AlfrescoFolder (org.alfresco.cmis.client.AlfrescoFolder)13 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)13 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)13 CmisSession (org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession)13 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)13 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)13 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)12 CmisUpdateConflictException (org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException)12 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)11 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)11