cancel
Showing results for 
Search instead for 
Did you mean: 

How can I use specific properties

z3r0
Champ in-the-making
Champ in-the-making
Hi,
I have a class which upload a file to Alfresco. Moreover, I have uploaded an own ContentModel to the folder "Company Home -> Data Dictionary -> Models". What do I need to change in my class, that the stored content use my specific model? In my code, I raised the standard features such as name and description. How can I address the properties of my content model?
Here is my code.

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;
    } 
}
2 REPLIES 2

norgan
Champ in-the-making
Champ in-the-making
You have to specialize your content to the content-type in question. Then you have the standard access method "namespaceSmiley Tongueropertyname".

You can specialize via action or businss rule and also via API on uploading.

z3r0
Champ in-the-making
Champ in-the-making
Hi, thanks for your answer. I've forgotten to specify the correct ContentType in the create method. Now it is.
In my example, a test.jpg file is uploaded. How to upload my own images?
My image is located at my project-folder. What is the path?
What I must change here

ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home", ASSOC_CONTAINS, "{" + Constants.NAMESPACE_CONTENT_MODEL + "}test.jpg");

and

InputStream viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg");

?
And where exactly is the file test.jpg? I have searched using the Windows search for it, but I could not find it.