AnsweredAssumed Answered

Add custom aspect through web service SDK with file upload

Question asked by fhomasp on Jan 23, 2008
Latest reply on Jan 25, 2008 by fhomasp
Hey,

Believe it or not, I've been able to upload a file  :shock:
Now I want to add a customly designed aspect to the uploaded file.  Here's the code I'm using, which doesn't work for the custom aspect.
Note that I defined the custom aspect in the xml config files and I'm able to add the aspects through traditional means.  Now of course doing it the traditional way pretty much eliminates the purpose of having customly written classes.

I know my code isn't exactly a school example of good programming, so any criticism in that departement is also appreciated.


public static boolean write(File inputFile) throws Exception
       {
           AuthenticationUtils.startSession(USERNAME, PASSWORD);
          
           String filename = inputFile.getName();
          
           String file = inputFile.getPath();
          

           String filetype = "text/html";
                
             try {
           
       
                ParentReference parentReference = new ParentReference(STORE, null, location ,ASSOC_CONTAINS, ASSOC_CONTAINS);
                
              
              
                NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, filename)};
              
                CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_CONTENT, properties);
                           CML cml = new CML();
                cml.setCreate(new CMLCreate[]{create});
               
                NamedValue[] titledProps = new NamedValue[1];
                Predicate predicate = new Predicate(new Reference[]{parentReference}, STORE, null);
               
                titledProps[0] = Utils.createNamedValue("rls:city","testcity");
               
//I tried using {http://www.alfresco.org/model/content/1.0}<namespace:aspectname> as well, no luck.
                CMLAddAspect addAspect = new CMLAddAspect("rls:locatable",titledProps, predicate, null);
              
                cml.setAddAspect(new CMLAddAspect[] {addAspect});
               
               
                UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
              
               
              
                Reference newContentNode = result[0].getDestination();
               
               
                             
                ContentFormat format = new ContentFormat(filetype, "UTF-8"); 
             
                InputStream viewStream = new FileInputStream(inputFile);
                byte[] bytes = ContentUtils.convertToByteArray(viewStream);
                
                WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, bytes, format);
                
              
                
                  } catch (Exception e) {
              e.printStackTrace();
              return false;  //will cause the calling class to delete the file if written in the repository
           } finally {
              AuthenticationUtils.endSession();
              return true;  //this gives me a warning, and rightly so.
           }

And here is the customly written xml config file.  I'm not going to include the other config files as I guess you'd only need its definition.

<?xml version="1.0" encoding="UTF-8"?>

<!– Definition of new Model –>

<!– The important part here is the name - Note: the use of the my: namespace
     which is defined further on in the document –>
<model name="rls:cvmodel" xmlns="http://www.alfresco.org/model/dictionary/1.0">

   <!– Optional meta-data about the model –>  
   <description>Example custom Model</description>
   <author></author>
   <version>1.0</version>

   <!– Imports are required to allow references to definitions in other models –>  
   <imports>
        <!– Import Alfresco Dictionary Definitions –>
      <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
      <!– Import Alfresco Content Domain Model Definitions –>
      <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/>
   </imports>

   <!– Introduction of new namespaces defined by this model –>
   <!– NOTE: The following namespace my.new.model should be changed to reflect your own namespace –>
   <namespaces>
      <namespace uri="realsoftware.be" prefix="rls"/>
   </namespaces>
  
   <types>
  
      <!– Definition of new Content Type: Real CV –>
      <type name="rls:cv">
         <title>Real CV</title>
         <parent>cm:content</parent>
         <properties>
            <property name="rls:name">
               <type>d:text</type>
            </property>
         </properties>
         <mandatory-aspects>
            <aspect>cm:versionable</aspect>
            <aspect>rls:locatable</aspect>           
         </mandatory-aspects>
      </type>
     
    </types>
   
    <aspects>
     
      <!– Definition of new Content Aspect: Locatable –>
      <aspect name="rls:locatable">
         <title>Locatable</title>
         <properties>
            <property name="rls:address">
               <type>d:text</type>
            </property>
            <property name="rls:city">
               <type>d:text</type>
            </property>
         <property name="rls:postalCode">
               <type>d:text</type>
            </property>
            <property name="rls:country">
               <type>d:text</type>
            </property>
         </properties>
      </aspect>

      <!– Definition of new Content Aspect: Skillable –>
      <aspect name="rls:skillable">
         <title>Skillable</title>
         <properties>
            <property name="rls:IT-Languages">
               <type>d:text</type>
            </property>
            <property name="rls:Certificate">
               <type>d:text</type>
            </property>
       <property name="rls:Methodologies">
               <type>d:text</type>
            </property>
            <property name="rls:Databases">
               <type>d:text</type>
            </property>
       <property name="rls:Hardware">
               <type>d:text</type>
            </property>
       <property name="rls:OS">
               <type>d:text</type>
            </property>
       <property name="rls:Business">
               <type>d:text</type>
       </property>
       <property name="rls:Other">
               <type>d:text</type>          
            </property>
         </properties>
      </aspect>

<!– Definition of new Content Aspect: Experienceable –>
      <aspect name="rls:experienceable">
         <title>Experienceable</title>
         <properties>
            <property name="rls:Company-Name">
               <type>d:text</type>
            </property>
            <property name="rls:Company-Profile">
               <type>d:text</type>
            </property>
       <property name="rls:Timeframe">
               <type>d:text</type>
            </property>
            <property name="rls:Project-Name">
               <type>d:text</type>
            </property>
       <property name="rls:Project-Description">
               <type>d:text</type>
            </property>
       <property name="rls:Technologies">
               <type>d:text</type>
            </property>
       <property name="rls:Role">
               <type>d:text</type>
       </property>
        </properties>
      </aspect>

     
   </aspects>
  
</model>

Outcomes