Search in sources :

Example 31 with ContentWriter

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

the class CustomEmailMappingServiceImpl method saveConfig.

/**
 * @param customMappingsToSave
 */
private void saveConfig(Set<CustomMapping> customMappingsToSave) {
    if (!nodeService.exists(CONFIG_NODE_REF)) {
        // create the config node
        Map<QName, Serializable> properties = new HashMap<QName, Serializable>(2);
        properties.put(ContentModel.PROP_NAME, CONFIG_NAME);
        properties.put(ContentModel.PROP_NODE_UUID, CONFIG_NODE_REF.getId());
        nodeService.createNode(CONFIG_FOLDER_NODE_REF, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, CONFIG_NAME), ContentModel.TYPE_CONTENT, properties);
    }
    // build JSON array of mappings
    JSONArray jsonMappings = new JSONArray();
    try {
        for (CustomMapping mapping : customMappingsToSave) {
            JSONObject obj = new JSONObject();
            obj.put("from", mapping.getFrom());
            obj.put("to", mapping.getTo());
            jsonMappings.put(obj);
        }
    } catch (JSONException je) {
        throw new AlfrescoRuntimeException("Unable to create JSON email mapping configuration during save.", je);
    }
    // update the content
    ContentWriter writer = this.contentService.getWriter(CONFIG_NODE_REF, ContentModel.PROP_CONTENT, true);
    writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
    writer.setEncoding("UTF-8");
    writer.putContent(jsonMappings.toString());
}
Also used : Serializable(java.io.Serializable) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 32 with ContentWriter

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

the class RecordableVersionsBaseTest method createDocumentWithRecordVersions.

/**
 * Creates a document with three versions (1.0, 1.1, 1.2) all of which
 * are recorded.
 *
 * @return  NodeRef node reference
 */
protected NodeRef createDocumentWithRecordVersions() {
    // create document and initial version (1.0)
    final NodeRef myDocument = doTestInTransaction(new Test<NodeRef>() {

        @Override
        public NodeRef run() throws Exception {
            // create a document
            NodeRef testDoc = fileFolderService.create(dmFolder, GUID.generate(), ContentModel.TYPE_CONTENT).getNodeRef();
            ContentWriter writer = fileFolderService.getWriter(testDoc);
            writer.setEncoding("UTF-8");
            writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
            writer.putContent(GUID.generate());
            // make versionable
            Map<QName, Serializable> props = new HashMap<QName, Serializable>(2);
            props.put(RecordableVersionModel.PROP_RECORDABLE_VERSION_POLICY, RecordableVersionPolicy.ALL);
            props.put(RecordableVersionModel.PROP_FILE_PLAN, filePlan);
            nodeService.addAspect(testDoc, RecordableVersionModel.ASPECT_VERSIONABLE, props);
            nodeService.addAspect(testDoc, ContentModel.ASPECT_VERSIONABLE, null);
            return testDoc;
        }
    });
    // create 1.1
    doTestInTransaction(new Test<Void>() {

        @Override
        public Void run() throws Exception {
            // update content
            ContentWriter writer = fileFolderService.getWriter(myDocument);
            writer.putContent(GUID.generate());
            return null;
        }
    });
    // create 1.2
    doTestInTransaction(new Test<Void>() {

        @Override
        public Void run() throws Exception {
            // update content
            ContentWriter writer = fileFolderService.getWriter(myDocument);
            writer.putContent(GUID.generate());
            return null;
        }
    });
    // we do these checks to ensure that the test data is in the correct state before we
    // start to manipulate the versions and execute tests
    doTestInTransaction(new Test<Void>() {

        @Override
        public Void run() throws Exception {
            // verify that the version history looks as expected
            VersionHistory versionHistory = versionService.getVersionHistory(myDocument);
            assertNotNull(versionHistory);
            Collection<Version> versions = versionHistory.getAllVersions();
            assertEquals(3, versions.size());
            // verify 1.2 setup as expected
            Version version12 = versionHistory.getHeadVersion();
            assertEquals("1.2", version12.getVersionLabel());
            NodeRef recordVersion12 = recordableVersionService.getVersionRecord(version12);
            assertNotNull(recordVersion12);
            assertTrue(relationshipService.getRelationshipsTo(recordVersion12, "versions").isEmpty());
            Set<Relationship> from12 = relationshipService.getRelationshipsFrom(recordVersion12, "versions");
            assertEquals(1, from12.size());
            // verify 1.1 setup as expected
            Version version11 = versionHistory.getPredecessor(version12);
            assertEquals("1.1", version11.getVersionLabel());
            NodeRef recordVersion11 = recordableVersionService.getVersionRecord(version11);
            assertNotNull(recordVersion11);
            Set<Relationship> to11 = relationshipService.getRelationshipsTo(recordVersion11, "versions");
            assertEquals(1, to11.size());
            assertEquals(recordVersion12, to11.iterator().next().getSource());
            Set<Relationship> from11 = relationshipService.getRelationshipsFrom(recordVersion11, "versions");
            assertEquals(1, from11.size());
            // verify 1.0 setup as expected
            Version version10 = versionHistory.getPredecessor(version11);
            assertEquals("1.0", version10.getVersionLabel());
            NodeRef recordVersion10 = recordableVersionService.getVersionRecord(version10);
            assertNotNull(recordVersion10);
            Set<Relationship> to10 = relationshipService.getRelationshipsTo(recordVersion10, "versions");
            assertEquals(1, to10.size());
            assertEquals(recordVersion11, to10.iterator().next().getSource());
            assertTrue(relationshipService.getRelationshipsFrom(recordVersion10, "versions").isEmpty());
            return null;
        }
    });
    return myDocument;
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) HashSet(java.util.HashSet) QName(org.alfresco.service.namespace.QName) VersionHistory(org.alfresco.service.cmr.version.VersionHistory) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) Version(org.alfresco.service.cmr.version.Version) Collection(java.util.Collection) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) Map(java.util.Map) PropertyMap(org.alfresco.util.PropertyMap)

Example 33 with ContentWriter

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

the class RecordsManagementAdminBase method writeCustomContentModel.

/**
 * Updates the content of the custom model
 *
 * @param modelRef The node reference of the model
 * @param deserializedModel The deserialized model
 */
protected void writeCustomContentModel(NodeRef modelRef, M2Model deserializedModel) {
    ContentWriter writer = getContentService().getWriter(modelRef, 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(I18NUtil.getMessage(MSG_ERROR_WRITE_CUSTOM_MODEL, modelRef.toString()), uex);
    }
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 34 with ContentWriter

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

the class SplitEmailAction method createAttachment.

/**
 * Create attachment from Mime Message Part
 * @param messageNodeRef - the node ref of the mime message
 * @param parentNodeRef - the node ref of the parent folder
 * @param part
 * @throws MessagingException
 * @throws IOException
 */
private void createAttachment(NodeRef messageNodeRef, NodeRef parentNodeRef, Part part) throws MessagingException, IOException {
    String fileName = part.getFileName();
    try {
        fileName = MimeUtility.decodeText(fileName);
    } catch (UnsupportedEncodingException e) {
        if (logger.isWarnEnabled()) {
            logger.warn("Cannot decode file name '" + fileName + "'", e);
        }
    }
    Map<QName, Serializable> messageProperties = getNodeService().getProperties(messageNodeRef);
    String messageTitle = (String) messageProperties.get(ContentModel.PROP_NAME);
    if (messageTitle == null) {
        messageTitle = fileName;
    } else {
        messageTitle = messageTitle + " - " + fileName;
    }
    ContentType contentType = new ContentType(part.getContentType());
    Map<QName, Serializable> docProps = new HashMap<QName, Serializable>(1);
    docProps.put(ContentModel.PROP_NAME, messageTitle + " - " + fileName);
    docProps.put(ContentModel.PROP_TITLE, fileName);
    /**
     * Create an attachment node in the same folder as the message
     */
    ChildAssociationRef attachmentRef = getNodeService().createNode(parentNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, fileName), ContentModel.TYPE_CONTENT, docProps);
    /**
     * Write the content into the new attachment node
     */
    ContentWriter writer = getContentService().getWriter(attachmentRef.getChildRef(), ContentModel.PROP_CONTENT, true);
    writer.setMimetype(contentType.getBaseType());
    OutputStream os = writer.getContentOutputStream();
    FileCopyUtils.copy(part.getInputStream(), os);
    /**
     * Create a link from the message to the attachment
     */
    createRMReference(messageNodeRef, attachmentRef.getChildRef());
}
Also used : Serializable(java.io.Serializable) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentType(javax.mail.internet.ContentType) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) OutputStream(java.io.OutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 35 with ContentWriter

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

the class RecordsManagementSearchServiceImpl method saveSearch.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService#saveSearch(org.alfresco.module.org_alfresco_module_rm.search.SavedSearchDetails)
 */
@Override
public SavedSearchDetails saveSearch(final SavedSearchDetails savedSearchDetails) {
    // Check for mandatory parameters
    ParameterCheck.mandatory("savedSearchDetails", savedSearchDetails);
    // Get the root saved search container
    final String siteId = savedSearchDetails.getSiteId();
    NodeRef container = siteService.getContainer(siteId, SEARCH_CONTAINER);
    if (container == null) {
        container = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() {
                return siteService.createContainer(siteId, SEARCH_CONTAINER, null, null);
            }
        }, AuthenticationUtil.getSystemUserName());
    }
    // Get the private container for the current user
    if (!savedSearchDetails.isPublic()) {
        final String userName = AuthenticationUtil.getFullyAuthenticatedUser();
        NodeRef userContainer = fileFolderService.searchSimple(container, userName);
        if (userContainer == null) {
            final NodeRef parentContainer = container;
            userContainer = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

                @Override
                public NodeRef doWork() {
                    return fileFolderService.create(parentContainer, userName, ContentModel.TYPE_FOLDER).getNodeRef();
                }
            }, AuthenticationUtil.getSystemUserName());
        }
        container = userContainer;
    }
    // Get the saved search node
    NodeRef searchNode = fileFolderService.searchSimple(container, savedSearchDetails.getName());
    if (searchNode == null) {
        final NodeRef searchContainer = container;
        searchNode = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() {
                return fileFolderService.create(searchContainer, savedSearchDetails.getName(), ContentModel.TYPE_CONTENT).getNodeRef();
            }
        }, AuthenticationUtil.getSystemUserName());
    }
    nodeService.addAspect(searchNode, ASPECT_SAVED_SEARCH, null);
    // Write the JSON content to search node
    final NodeRef writableSearchNode = searchNode;
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        @Override
        public Void doWork() {
            ContentWriter writer = fileFolderService.getWriter(writableSearchNode);
            writer.setEncoding("UTF-8");
            writer.setMimetype(MimetypeMap.MIMETYPE_JSON);
            writer.putContent(savedSearchDetails.toJSONString());
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());
    return savedSearchDetails;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)

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