cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Aspect Property reading Values from a database Field

ctraversa
Champ in-the-making
Champ in-the-making
Hi all.
I'd like to have a suggestion on the best way to implement Custom Aspect Property whose values come from a database field.
In other words the possible values of the custom aspect property cannot be entered freely but come from a database field with more then 5000 entries.
Does exist a best practice to follow to put in place a such procedure?
Or does anyone have any idea on how to do it?
Thanks in advance,
Carlo.
12 REPLIES 12

acis
Champ in-the-making
Champ in-the-making
Hi ctraversa,

You have to create a new custom constraint for your property.

So your model should look like





<constraints>
      <constraint name="tc:CodeLabel" type="org.alfresco.sample.CodeLabelExtend.ListOfValuesQueryConstraint" />
   </constraints>
   
   <types>
      <type name="tc:codecontent">
         <title>Contenu étendu</title>
         <parent>cm:content</parent>
         <properties>
            <property name="tc:codestr">
               <title>Code str</title>
               <type>d:text</type>
               <constraints>
                  <constraint ref="tc:CodeLabel" />
               </constraints>
            </property>
         </properties>
      </type>
   </types>




After you have to define your ListOfValuesQueryConstraint class like this :



package org.alfresco.sample.CodeLabelExtend;

import java.util.ArrayList;
import java.util.List;
import java.sql.*;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.web.bean.generator.BaseComponentGenerator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import javax.faces.model.SelectItem;

public class ListOfValuesQueryConstraint extends ListOfValuesConstraint implements Serializable {

   private static Log logger = LogFactory.getLog(BaseComponentGenerator.class);
   
   private static final long serialVersionUID=1;
   
   private List<String> allowedLabels;
   
   public void setAllowedValues(List allowedValues) {}
   public void setCaseSensitive(boolean caseSensitive) {}

   public void initialize() {
       super.setCaseSensitive(false);
       this.loadDB();
    }
   
   public List<String> getAllowedLabels() {
      return this.allowedLabels;
   }
   
   public void setAllowedLabels(List<String> allowedLabels) {
      this.allowedLabels=allowedLabels;
   }
   
    public List<SelectItem> getSelectItemList() {
      List<SelectItem> result = new ArrayList<SelectItem>(this.getAllowedValues().size());
      for(int i=0;i<this.getAllowedValues().size();i++) {
         result.add(new SelectItem((Object)this.getAllowedValues().get(i),this.allowedLabels.get(i)));
      }
      return result;
   }
   
    protected void loadDB() {
       
       String driverName = "org.gjt.mm.mysql.Driver";
        String serverName = "localhost";
        String mydatabase = "alfresco";
        String username = "alfresco";
        String password = "alfresco";
       
        List<String> av = new ArrayList<String>();
        List<String> al=new ArrayList<String>();
       
       
        try {
           Connection connection = null;
            Class.forName(driverName);
            String url = "jdbc:mysql://" + serverName +  "/" + mydatabase;
            connection = DriverManager.getConnection(url, username, password);
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("select code,label from codelabel");
            while (rs.next()) {
                av.add(rs.getString("code"));
                al.add(rs.getString("label"));
            }
        }
        catch (Exception e) {}
       
      super.setAllowedValues(av);
      this.setAllowedLabels(al);
   }
}


And that's it.

After you can create a component generator and a converter to custom displaying in the web client but it's not compulsary.

marcostopper
Champ in-the-making
Champ in-the-making
I have a db with 3 different fields. Is possible to change value of a property when the user select a value from one of the fields. When the user insert a document he choose from a name from the combo box. Is possible to view the other related information in the next text box? I suppose I have to make a query using value selected from the user. How can I get this value to make the query? Have you any example?

archana_bonkanp
Champ in-the-making
Champ in-the-making
Hi All,

I just dont want the values to be displayed fro the db.
but i just want to add some static values in the drop down list ,so that user selects them instaed of typing.
please hel[p me in implementing the same.

Thanks & regards,
Archana

jpfi
Champ in-the-making
Champ in-the-making
Hi,
a simple list constraint solves your prob. here is an example:
http://wiki.alfresco.com/wiki/Content_Model_Constraints
Cheers, Jan

archana_bonkanp
Champ in-the-making
Champ in-the-making
HI Jan,

Thanks for providing the link.but i was confused in which file to add the constraint list.
I need a list with "DG" title and values "ARC" ,"ARC1","ARC2".
i have aded following in the custommodel.xml file

   <aspects>
   <aspect name="custom:CustomerDetails">
   <title>Lessons Learned Database</title>
   <properties>
   <property name="custom:deliverygroup">
   <title>Delivery Group</title>
   <type>d:text</type>
   </property>
   <property name="custom:domain">
   <title>Domain</title>
   <type>d:text</type>
   </property>
   <property name="customSmiley Tongueroducttype">
   <title>Produst Type</title>
   <type>d:text</type>         
   </property>
   <property name="customSmiley Tonguehase">
   <title>Phase</title>
   <type>d:text</type>
   </property>
   <property name="custom:dropdowntest">
   <title>Dropdownlist</title>
   <type>d:category</type>
   </property>
   </properties>
</aspect>
</aspects>

and in webclient config custom,we added


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


<config evaluator="aspect-name" condition="custom:CustomerDetails">
<property-sheet>
<separator name="sepCust1" display-label="Lessons Learned Database"
          component-generator="HeaderSeparatorGenerator"/>

<show-property name="custom:deliverygroup"/>
<show-property name="custom:domain"/>
<show-property name="customSmiley Tongueroducttype"/>
<show-property name="customSmiley Tonguehase"/>
<show-property name="custom:dropdowntest">
<list>
                <value>BFSI</value>
                <value>SAM</value>
         </list>

</show-property>

</property-sheet>
</config>

<config evaluator="string-compare" condition="Advanced Search">
<advanced-search>
<content-types>
<type name="custom:CustomerDetails"/>
</content-types>

<custom-properties>

<meta-data aspect="custom:CustomerDetails"
property="custom:deliverygroup"/>
<meta-data aspect="custom:CustomerDetails"
property="custom:domain"/>
<meta-data aspect="custom:CustomerDetails"
property="customSmiley Tongueroducttype"/>
<meta-data aspect="custom:CustomerDetails"
property="customSmiley Tonguehase"/>
<meta-data aspect="custom:CustomerDetails"
property="custom:dropdowntest"/>

</custom-properties>
</advanced-search>
</config>

please solve my issue
its urgent

Regards,
Archana

archana_bonkanp
Champ in-the-making
Champ in-the-making
Hi

how to implement drop down box box custom proeprties,please help.
we need only static values to be define in the combo box.
Its urgent

dgenard
Champ on-the-rise
Champ on-the-rise
Simply set a LIST constraint on the property in your model.
Here is an example :

            <property name="acme:type">
               <type>d:text</type>
               <mandatory>true</mandatory>
               <default>simple</default>
               <constraints>
                  <constraint type="LIST">
                     <parameter name="allowedValues">
                        <list>
                           <value>simple</value>
                           <value>double</value>
                           <value>triple</value>
                        </list>
                     </parameter>
                  </constraint>
               </constraints>
            </property>
Cheers,
Denis

archana_bonkanp
Champ in-the-making
Champ in-the-making
HI Denis,

Please give us the detail list of the steps to be written in the custom model.xml,web-cleint -config-cutom.xml file   to get the drop down list of static values .

im getting an error if add above code

dgenard
Champ on-the-rise
Champ on-the-rise
For complete reference, all details are here :
http://wiki.alfresco.com/wiki/Data_Dictionary_Guide#Step_by_Step_Model_Definition

Without any details on your error (model file and log extract), it won't be easy to give you more precise help…

Regards,
Denis