mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
- Added writeContent statement to CML
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@3615 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
*/
|
||||
package org.alfresco.repo.webservice;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -35,11 +37,15 @@ import org.alfresco.repo.webservice.types.CMLRemoveAspect;
|
||||
import org.alfresco.repo.webservice.types.CMLRemoveAssociation;
|
||||
import org.alfresco.repo.webservice.types.CMLRemoveChild;
|
||||
import org.alfresco.repo.webservice.types.CMLUpdate;
|
||||
import org.alfresco.repo.webservice.types.CMLWriteContent;
|
||||
import org.alfresco.repo.webservice.types.ContentFormat;
|
||||
import org.alfresco.repo.webservice.types.NamedValue;
|
||||
import org.alfresco.repo.webservice.types.ParentReference;
|
||||
import org.alfresco.repo.webservice.types.Predicate;
|
||||
import org.alfresco.repo.webservice.types.Reference;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.CopyService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
@@ -64,12 +70,14 @@ public class CMLUtil
|
||||
private static final String REMOVE_CHILD = "removeChild";
|
||||
private static final String CREATE_ASSOCIATION = "createAssociation";
|
||||
private static final String REMOVE_ASSOCIATION = "removeAssociation";
|
||||
private static final String WRITE_CONTENT = "writeContent";
|
||||
|
||||
private NodeService nodeService;
|
||||
private SearchService searchService;
|
||||
private NamespaceService namespaceService;
|
||||
private CopyService copyService;
|
||||
private DictionaryService dictionaryService;
|
||||
private ContentService contentService;
|
||||
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
@@ -96,6 +104,11 @@ public class CMLUtil
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
public void setContentService(ContentService contentService)
|
||||
{
|
||||
this.contentService = contentService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a cml update query.
|
||||
*
|
||||
@@ -147,6 +160,15 @@ public class CMLUtil
|
||||
}
|
||||
}
|
||||
|
||||
CMLWriteContent[] writes = cml.getWriteContent();
|
||||
if (writes != null)
|
||||
{
|
||||
for (CMLWriteContent write : writes)
|
||||
{
|
||||
executeCMLWriteContent(write, context, results);
|
||||
}
|
||||
}
|
||||
|
||||
// Execute delete
|
||||
CMLDelete[] deletes = cml.getDelete();
|
||||
if (deletes != null)
|
||||
@@ -375,6 +397,34 @@ public class CMLUtil
|
||||
}
|
||||
}
|
||||
|
||||
private void executeCMLWriteContent(CMLWriteContent write, ExecutionContext context, List<UpdateResult> results)
|
||||
{
|
||||
// Get the nodes and content property
|
||||
List<NodeRef> nodeRefs = getNodeRefList(write.getWhere_id(), write.getWhere(), context);
|
||||
QName property = QName.createQName(write.getProperty());
|
||||
ContentFormat format = write.getFormat();
|
||||
byte[] content = write.getContent();
|
||||
|
||||
for (NodeRef nodeRef : nodeRefs)
|
||||
{
|
||||
//Get the content writer
|
||||
ContentWriter writer = this.contentService.getWriter(nodeRef, property, true);
|
||||
|
||||
// Set the content format details (if they have been specified)
|
||||
if (format != null)
|
||||
{
|
||||
writer.setEncoding(format.getEncoding());
|
||||
writer.setMimetype(format.getMimetype());
|
||||
}
|
||||
|
||||
// Write the content
|
||||
InputStream is = new ByteArrayInputStream(content);
|
||||
writer.putContent(is);
|
||||
|
||||
results.add(createResult(WRITE_CONTENT, null, nodeRef, nodeRef));
|
||||
}
|
||||
}
|
||||
|
||||
private void executeCMLDelete(CMLDelete delete, ExecutionContext context, List<UpdateResult> results)
|
||||
{
|
||||
List<NodeRef> nodeRefs = Utils.resolvePredicate(delete.getWhere(), this.nodeService, this.searchService, this.namespaceService);
|
||||
|
@@ -34,6 +34,8 @@ import org.alfresco.repo.webservice.types.CMLRemoveAspect;
|
||||
import org.alfresco.repo.webservice.types.CMLRemoveAssociation;
|
||||
import org.alfresco.repo.webservice.types.CMLRemoveChild;
|
||||
import org.alfresco.repo.webservice.types.CMLUpdate;
|
||||
import org.alfresco.repo.webservice.types.CMLWriteContent;
|
||||
import org.alfresco.repo.webservice.types.ContentFormat;
|
||||
import org.alfresco.repo.webservice.types.NamedValue;
|
||||
import org.alfresco.repo.webservice.types.ParentReference;
|
||||
import org.alfresco.repo.webservice.types.Predicate;
|
||||
@@ -41,6 +43,8 @@ import org.alfresco.repo.webservice.types.Reference;
|
||||
import org.alfresco.service.cmr.repository.AssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ContentData;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
@@ -57,6 +61,7 @@ public class CMLUtilTest extends BaseSpringTest
|
||||
{
|
||||
private static final ContentData CONTENT_DATA_TEXT_UTF8 = new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "UTF-8");
|
||||
private static final ContentData CONTENT_DATA_HTML_UTF16 = new ContentData(null, MimetypeMap.MIMETYPE_HTML, 0L, "UTF-16");
|
||||
private static final String TEST_CONTENT = "This is some test content";
|
||||
|
||||
private CMLUtil cmlUtil;
|
||||
private NodeService nodeService;
|
||||
@@ -67,6 +72,7 @@ public class CMLUtilTest extends BaseSpringTest
|
||||
private SearchService searchService;
|
||||
private NodeRef folderNodeRef;
|
||||
private AuthenticationComponent authenticationComponent;
|
||||
private ContentService contentService;
|
||||
|
||||
@Override
|
||||
protected String[] getConfigLocations()
|
||||
@@ -82,6 +88,7 @@ public class CMLUtilTest extends BaseSpringTest
|
||||
this.searchService = (SearchService)this.applicationContext.getBean("searchService");
|
||||
this.namespaceService = (NamespaceService)this.applicationContext.getBean("namespaceService");
|
||||
this.authenticationComponent = (AuthenticationComponent) this.applicationContext.getBean("authenticationComponent");
|
||||
this.contentService = (ContentService)this.applicationContext.getBean("contentService");
|
||||
|
||||
this.authenticationComponent.setSystemUserAsCurrentUser();
|
||||
|
||||
@@ -212,6 +219,32 @@ public class CMLUtilTest extends BaseSpringTest
|
||||
assertEquals(CONTENT_DATA_HTML_UTF16, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_CONTENT));
|
||||
}
|
||||
|
||||
public void testWriteContent()
|
||||
{
|
||||
CMLWriteContent write = new CMLWriteContent();
|
||||
write.setWhere(createPredicate(this.nodeRef));
|
||||
write.setProperty(ContentModel.PROP_CONTENT.toString());
|
||||
ContentFormat format = new ContentFormat(MimetypeMap.MIMETYPE_TEXT_PLAIN, "UTF-8");
|
||||
write.setFormat(format);
|
||||
write.setContent(TEST_CONTENT.getBytes());
|
||||
|
||||
CML cml = new CML();
|
||||
cml.setWriteContent(new CMLWriteContent[]{write});
|
||||
|
||||
UpdateResult[] result = this.cmlUtil.executeCML(cml);
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.length);
|
||||
|
||||
UpdateResult updateResult = result[0];
|
||||
assertEquals("writeContent", updateResult.getStatement());
|
||||
assertNotNull(updateResult.getSource());
|
||||
assertNotNull(updateResult.getDestination());
|
||||
|
||||
ContentReader reader = this.contentService.getReader(this.nodeRef, ContentModel.PROP_CONTENT);
|
||||
assertNotNull(reader);
|
||||
assertEquals(reader.getContentString(), TEST_CONTENT);
|
||||
}
|
||||
|
||||
public void testDelete()
|
||||
{
|
||||
CMLDelete delete = new CMLDelete();
|
||||
|
@@ -49,6 +49,9 @@
|
||||
<property name="dictionaryService">
|
||||
<ref bean="DictionaryService"/>
|
||||
</property>
|
||||
<property name="contentService">
|
||||
<ref bean="ContentService"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- Implementations of each exposed web service -->
|
||||
|
Reference in New Issue
Block a user