09-20-2009 01:37 PM
public class CopyOfContentReadAndWrite
{
/** The type of the association we are creating to the new content */
private static final String ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains";
public ArrayList readAndWrite() {
ArrayList answer = new ArrayList();
// Start the session
try {
AuthenticationUtils.startSession("admin", "admin");
} catch (AuthenticationFault e) {
answer.add("Authentication failed!");
}
try
{
// Create a reference to the parent where we want to create content
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home", ASSOC_CONTAINS, "{" + Constants.NAMESPACE_CONTENT_MODEL + "}test.jpg");
// Create the content
NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, "test.jpg")};
CMLCreate create = new CMLCreate("1", companyHomeParent, null, null, null, Constants.TYPE_CONTENT, properties);
CML cml = new CML();
cml.setCreate(new CMLCreate[]{create});
// Construct CML statement to add titled aspect
NamedValue[] titledProps = new NamedValue[2];
titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, "einBild");
titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, "ein bild wie geil");
CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_TITLED, titledProps, null, "1");
cml.setAddAspect(new CMLAddAspect[] {addAspect});
UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
// Get the created node and create the format
Reference newContentNode = result[0].getDestination();
ContentFormat format = new ContentFormat("image/jpeg", "UTF-8");
// Open the file and convert to byte array
InputStream viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg");
byte[] bytes = ContentUtils.convertToByteArray(viewStream);
// Write the content
WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, bytes, format);
}
catch (Exception e){
}
// End the session
AuthenticationUtils.endSession();
return answer;
}
}
09-20-2009 04:33 PM
09-21-2009 07:00 AM
ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home", ASSOC_CONTAINS, "{" + Constants.NAMESPACE_CONTENT_MODEL + "}test.jpg");
InputStream viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg");
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.