cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom aspect through web service SDK with file upload

fhomasp
Champ in-the-making
Champ in-the-making
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="rlsSmiley TongueostalCode">
               <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="rlsSmiley Very Happyatabases">
               <type>d:text</type>
            </property>
       <property name="rls:Hardware">
               <type>d:text</type>
            </property>
       <property name="rlsSmiley SurprisedS">
               <type>d:text</type>
            </property>
       <property name="rls:Business">
               <type>d:text</type>
       </property>
       <property name="rlsSmiley Surprisedther">
               <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="rlsSmiley Tongueroject-Name">
               <type>d:text</type>
            </property>
       <property name="rlsSmiley Tongueroject-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>
4 REPLIES 4

fhomasp
Champ in-the-making
Champ in-the-making
Well… I made a bit of progress.

I changed: "rls:locatable"
to:
"{realsoftware.be}locatable"

Which doesn't generate an exception, so the system recognises it.  However when I view the uploaded file in the alfresco UI I can't see the extra fields that are supposed to be associated with it.  So there must be something I didn't do.

fhomasp
Champ in-the-making
Champ in-the-making
The aspect is applied when I change:
Predicate predicate = new Predicate(new Reference[]{parentReference}, STORE, null);

to

Predicate predicate = new Predicate(new Reference[]{parentReference}, null, null);

and

CMLAddAspect addAspect = new CMLAddAspect("{realsoftware.be}locatable",titledProps, predicate, null);

to

CMLAddAspect addAspect = new CMLAddAspect("{realsoftware.be}locatable",titledProps, predicate, "1");

However the properties I defined aren't filled in.   I tried to enter as property id "rls:city" as well as "city".  As defined in the NamedValues array.

fhomasp
Champ in-the-making
Champ in-the-making
So making an error in the property description doesn't generate an exception, obviously.  But of course the way a custom aspect is defined correctly is the same way a custom aspect property needs to be defined Smiley Happy

So

titledProps[0] = Utils.createNamedValue("{realsoftware.be}address","testaddress"); 
                titledProps[1] = Utils.createNamedValue("{realsoftware.be}city","testcity");
                titledProps[2] = Utils.createNamedValue("{realsoftware.be}postalCode","testpostalCode");
                titledProps[3] = Utils.createNamedValue("{realsoftware.be}country","testcountry");
               
                CMLAddAspect addAspect = new CMLAddAspect("{realsoftware.be}locatable",titledProps, predicate, "1");

Just FYI in case anyone cares, I certainly don't seem to have that impression Smiley Happy

fhomasp
Champ in-the-making
Champ in-the-making
When I use a search implementation through the SDK or even through the standard UI and search for the newly added values in the new fields of the aspect(s) there's nothing that comes up.  I have the aspect listed in web-client-config-custom in the "string-compare" section.

<alfresco-config>
<!– Model Registration ***************************************** –>
<config evaluator="string-compare" condition="Content Wizards">
      <content-types>
         <type name="rls:cv"/>
      </content-types>
</config>
   <config evaluator="node-type" condition="rls:cv">
      <property-sheet>
         <show-property name="rls:name" />
      </property-sheet>
   </config>

<config evaluator="string-compare" condition="Action Wizards">
      <content-types>
         <type name="rls:cv"/>
      </content-types>
</config>

<!– Aspect Registration **************************************** –>

<config evaluator="aspect-name" condition="rls:locatable">
      <property-sheet>
         <show-property name="rls:address"/>
         <show-property name="rls:city"/>
         <show-property name="rls:postalCode"/>
      <show-property name="rls:country"/>
      </property-sheet>
   </config>

<config evaluator="string-compare" condition="Action Wizards">
      <types>
         <type name="folder" displayLabelId="space"/>
         <type name="content"/>
      </types>
      <aspects> 
         <aspect name="rls:locatable" displayLabel="Locatable"/>
      <aspect name="rls:skillable" displayLabel="Skillable"/>
      <aspect name="rls:experienceable" displayLabel="Experienceable"/>
      </aspects>
</config>

<config evaluator="aspect-name" condition="rls:skillable">
      <property-sheet>
         <show-property name="rls:IT-Languages" display-label="Languages"/>
         <show-property name="rls:Certificate" display-label="Certificates"/>
         <show-property name="rls:Methodologies" display-label="Methodologies"/>
      <show-property name="rls:Databases" display-label="Databases"/>
      <show-property name="rls:Hardware" display-label="Hardware"/>
      <show-property name="rls:OS" display-label="Operating Systems"/>
      <show-property name="rls:Business" display-label="Business Knowledge"/>
      <show-property name="rls:Other" display-label="Other"/>
      <show-property name="rls:Frameworks" display-label="Frameworks"/>
      <show-property name="rls:Tools" display-label="Tools"/>
      </property-sheet>
   </config>

<config evaluator="aspect-name" condition="rls:experienceable" multiple="true">
      <property-sheet>
         <show-property name="rls:Company-Name" display-label="Company Name"/>
         <show-property name="rls:Company-Profile" display-label="Company Profile"/>
         <show-property name="rls:Timeframe" display-label="Year-Year"/>
      <show-property name="rls:Project-Name" display-label="Project Name"/>
      <show-property name="rls:Project-Description" display-label="Project Description"/>
      <show-property name="rls:Technologies" display-label="Technologies"/>
      <show-property name="rls:Role" display-label="Role"/>
      </property-sheet>
   </config>



   <!– Example of overriding the from email address –>
   <!–
   <config>
      <client>
         <from-email-address>someone@your-domain.com</from-email-address>
         <search-max-results>100</search-max-results>
      </client>
   </config>
   –>

   <!– Example of adding languages to the list in the login page –>
   <config evaluator="string-compare" condition="Languages">
      <languages>
         <language locale="ca_ES">Catalan</language>
         <language locale="hr_HR">Croatian</language>
         <language locale="cs_CZ">Czech</language>
         <language locale="da_DK">Danish</language>
         <language locale="de_DE">German</language>
         <language locale="es_ES">Spanish</language>
         <language locale="el_GR">Greek</language>
         <language locale="fi_FI">Finnish</language>
         <language locale="fr_FR">French</language>
         <language locale="it_IT">Italian</language>
         <language locale="ja_JP">Japanese</language>
         <language locale="du_NL">Dutch</language>
         <language locale="pl_PL">Polish</language>
         <language locale="pt_PT">Portuguese</language>
         <language locale="pt_BR">Portuguese (Brazilian)</language>
         <language locale="ru_RU">Russian</language>
         <language locale="sv_SV">Swedish</language>
         <language locale="tr_TR">Turkish</language>
         <language locale="zh_CN">Simplified Chinese</language>
      </languages>
   </config>

   <!– Example of configuring advanced search –>
   <!–
   <config evaluator="string-compare" condition="Advanced Search">
      <advanced-search>
         <content-types>
         </content-types>
         <custom-properties>
            <meta-data aspect="app:simpleworkflow" property="app:approveStep" />
         </custom-properties>
      </advanced-search>
   </config>
   –>

   <!– Example of changing the sort direction for a view in the client –>
   <!–
   <config evaluator="string-compare" condition="Views">
      <views>
         <view-defaults>
            <topic>
               <sort-direction>ascending</sort-direction>
            </topic>
         </view-defaults>
      </views>
   </config>
   –>

   <!– Example of adding a custom icon to the Create Space dialog –>
   <!–
   <config evaluator="string-compare" condition="cm:folder icons">
      <icons>
         <icon name="space-icon-custom" path="/images/icons/space-icon-custom.gif" />
      </icons>
   </config>
   –>

   <!– The config below shows how to incorporate the example model–>
   <!– into the web client, for this to work you will need to –>
   <!– rename example-model-context.xml.sample to example-model-context.xml –>
   <!–
   <config evaluator="string-compare" condition="Content Wizards">
      <content-types>
         <type name="my:sop" />
      </content-types>
   </config>

   <config evaluator="node-type" condition="my:sop">
      <property-sheet>
         <show-property name="mimetype" display-label-id="content_type"
                        component-generator="MimeTypeSelectorGenerator" />
         <show-property name="size" display-label-id="size"
                        converter="org.alfresco.faces.ByteSizeConverter"
                        show-in-edit-mode="false" />              
         <show-property name="my:publishedDate" />
         <show-association name="my:signOff" />
         <show-property name="my:authorisedBy" />
         <show-child-association name="my:processSteps" />
      </property-sheet>
   </config>

   <config evaluator="aspect-name" condition="my:imageClassification">
      <property-sheet>
         <show-property name="my:width"/>
         <show-property name="my:height"/>
         <show-property name="my:resolution"/>
      </property-sheet>
   </config>

   <config evaluator="string-compare" condition="Action Wizards">
      <aspects>
         <aspect name="my:imageClassification"/>
      </aspects>
   </config>

   <config evaluator="string-compare" condition="Advanced Search">
      <advanced-search>
         <content-types>
            <type name="my:sop" />
         </content-types>
         <custom-properties>
            <meta-data type="my:sop" property="my:authorisedBy" />
            <meta-data aspect="my:imageClassification" property="my:resolution" />
         </custom-properties>
      </advanced-search>
   </config>
   –>

</alfresco-config>