Alfresco 3.2 Share: FormService Seems To Be cool. But Whats About Associations?

Alfresco 3.2 bears new cool features to the community, especially an easy way to display custom metadata in Share. Actually it was´nt possible, displaying custom metadata-properties in Share without doing customizations on the underlying Share/SURF-Code.

Well these times are over … almost over!

If you using an association between two nodes, you get not the same feeling about the result as you probably though about it. Because if you looking to you´re document-library in Share, you will receive just a node-reference to the property-association. Oh thats fairly crappy but i guess, its just a temporary problem until Alfresco drops out the next version. Well, in the meantime we have to solve this issue going another way.

The following approach likey differs from a great customizations so we expect to get the solution in less then a few minutes.

At first i will show you my contentmodel i´ve been extended. This content-type is called “myModel”:

...
<associations> 
   <association name="dmc:contactPerson">
      <source>
         <mandatory>false</mandatory>
         <many>true</many>
      </source>
      <target>
         <class>cm:person</class>
         <mandatory>false</mandatory>
         <many>false</many>
      </target>
   </association>
</associations>
...

I´ve just added an association to a person that is targeted as “contact-person”. Using Alfresco-Explorer all works fine and you are able to select a person. Following image depicts our newly created association with an assigned contact-person:

Cool!

If you have such a document-Type in your documentlibrary of share stored, its likely normal to display this property as well.

So, therefore its time to add this newly added property of the the model “myModel” to Share. Therefore we create the file tomcat/shared/classes/alfresco/web-extension/web-framework-config-custom.xml:

...
   <show id="dmc:contactPerson" for-mode="view"/> 
</field-visibility>
<appearance>
   <field id="dmc:contactPerson">
      <control template="/org/alfresco/components/form/controls/association.ftl" />
   </field>

Now this gives a show-up of the the nodeRef as i´ve descriped earlier in that post. Thats not the solution we want.

Change the control-tag attribute to following value:  /org/alfresco/components/form/controls/association_person.ftl

Next we have to extend the form.get.js, responsible for managing all form-properties in Alfresco-Share.

Next we will edit this file, starting with method setupField:

...

 // setup text for the field i.e. the label, description & help
setupFieldText(fieldDef, fieldConfig);// setup constraints for the field

setupFieldConstraints(fieldDef, fieldConfig);

// setup the value for the field
setupFieldValue(formModel, fieldDef, fieldConfig);
//thats the new line of code we´ve just added here
//setup associations for the field
setupAssociations(formModel, fieldDef, fieldConfig);

At the bottom of this file we add the corresonding method:

function setupAssociations(formModel, fieldDef, fieldConfig){
   if(fieldDef.type !== "association")
     return;

   connector = remote.connect("alfresco");
   json = connector.get("/webframework/content/metadata?id=" + fieldDef.value);

   try {

     fieldModel = eval('(' + json + ')');

     if(json.status == 200) {

       props = new Array();

       for (key in fieldModel.properties) {
          trimmed_key = key.substring(key.indexOf('}')+1,key.length)
          props[trimmed_key] = fieldModel.properties[key];
       }

       fieldDef.assocProperties = props

   }
}

 catch(e){
    logger.getSystem().out( e );
}

}

Thats all, we got to do. The method calls the server back due to retrieve more informations about the assosciation-nodeRef. We just call another alfresco-rest-script. Even though this method is not realy the best peace of code i´ve wrote the last years ago, we expect almost a fast solution. For instance, the fully qualified definition-names of all properties will be ereased as freemarker can not handle simplehashmaps with special-chars like ‘}’. So this should be fixed on mapping the fully qualified qnames with its corresponding short-names with character-replacing of ‘:’ to ‘_’.

Finaly we´ve to create a file being called association_person.ftl in /org/alfresco/components/form/controls/association.ftl  for displaying the appropriated association-property :

<#include "common/picker.inc.ftl" />

<#assign controlId = fieldHtmlId + "-cntrl">
  <div class="form-field">
    <#if form.mode == "view">
      <div id="${controlId}" class="viewmode-field">
        <#if field.endpointMandatory && field.value == "">
           <span class="incomplete-warning"><img src="${url.context}/components/form/images/warning-16.png" title="${msg("form.field.incomplete")}" /><span>
        </#if>
        <span class="viewmode-label">${field.label?html}:</span>
        <span id="${controlId}-currentValueDisplay" class="viewmode-value current-values">
           <a href="${url.context}/page/user/${field.assocProperties.userName}/profile">${field.assocProperties.firstName} ${field.assocProperties.lastName}</a>
        </span>
      </div>
    </#if>
</div>

We are done. Try it on refreshing your Share-Webscripts! Its even possible to click on the contactperson where the profile-site of this person gets displayed.

Comments

2 Responses to “Alfresco 3.2 Share: FormService Seems To Be cool. But Whats About Associations?”

  1. Eddie on March 11th, 2010 5:42 pm

    Hi Sebastian,
    Thanks for this posting - I’ve tried to implement this (Alfresco 3.2 Community) & I get the person’s name rendered in the view metadata rather than the nodereference. However, when I edit the metadata, there is no longer any ‘person picker’.

    My web-framework-config-custom.xml has the following declarations:

    What I need is for the picker to be there when I’m editing the metadata, so that I can assign another person. Could you give me any advice?

    Many thanks,
    Eddie

  2. admin on March 11th, 2010 6:28 pm

    Hi Eddie,

    thats not so easy, i guess. You have to implement the whole forms-api for this kind of metadata-construct (association). You have to use (for instance) the people-finder that comes around with Alfresco Share to get users you want to set as a property upon you node-property. This can be done on using YUI-API as well as webscripts (share).

Leave a Reply




Security Code: