Saturday

Dynamic List Constraints in Alfresco

In Alfresco Share, how do I populate the drop down values from a database. This is something possible using dynamic constraint. So here I go.

To achieve this you need to extend the "ListOfValuesConstraint" class which present in org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint package.

Step-1 (Extend the class):

package org.alfresco.diary.constraint;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;


public class CustomConstraint extends ListOfValuesConstraint implements Serializable {

   private final Logger logger = Logger.getLogger(CustomConstraint.class);
   private static final long serialVersionUID=1;

   @Override
    public void setAllowedValues(List allowedValues) {
    }

    @Override
    public void setCaseSensitive(boolean caseSensitive) {
    }

   public void initialize() {
       super.setCaseSensitive(false);
       this.getDataFromDb();
    }

    protected void getDataFromDb() {

String driverName = "org.gjt.mm.mysql.Driver";
        String serverName = "localhost:3306";
        String dbName = "alfdiary";
        String userName = "alfdiary";
        String password = "alfdiary";

        List<String> allowedValue = new ArrayList<String>();

        try {
            Connection connection = null;
            Class.forName(driverName);
            String url = "jdbc:mysql://" + serverName +  "/" + dbName;
            connection = DriverManager.getConnection(url, userName, password);
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("select dropdowns from alfdiary");
            while (rs.next()) {
                allowedValue.add(rs.getString("dropdowns"));
            }
        }
        catch (Exception e) {}
super.setAllowedValues(allowedValue);
   }
}

Step-2 (Create Metadata Model):

Add the following configurations in your meta model.

<constraint name="alfdiary:dropDowns" type="org.alfresco.diary.constraint.CustomConstraint">
<parameter name="allowedValues">
<list></list>
</parameter>
</constraint>

Apply the above constraint to the metadata

Thats it!!!

11 comments:

  1. Easy to understand blog.
    I have made above class.
    I am new in alfresco.How to compile above custom class.
    Thanks

    ReplyDelete
  2. You can compile it with the alfresco maven project

    ReplyDelete
  3. Hi, I got my class and xml model but when trying to deploy got this error "Caused by: org.alfresco.service.cmr.dictionary.DictionaryException: 04200000 Constraint type 'mx.com.iikt.atalaya.constraints.CustomDyniamicConstraints' on constraint 'atalaya:custDynConstraint' is not a well-known type or a valid Constraint implementation"

    ReplyDelete
    Replies
    1. You need to load the class file into $TOMCAT_HOME/webapps/alfresco/WEB-INF/classes

      Also you need to create the package folders e.g in your case (mx.com.iikt.atalaya.constraints) -> mx/com/iikt/atalaya/constraints

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Do we required custom ftl file rendering the dynamic values?

    ReplyDelete
    Replies
    1. Not required. It will automatically use the OOB ftl files if you are using alfresco share

      Delete
  6. You do not need a custom ftl. Alfresco already knows that constraints are rendered as lists.

    ReplyDelete
  7. Hi Govinda,

    Would you care to share your bean config file please, I am getting an error where it say the the list of allowed values is empty.

    ReplyDelete
  8. I would like to create a custom constraints class that returns a list of files within the "actedUponNoderef". The problem is that I don't know how to get
    the "actedUponNoderef" from within the Constraints class, such as the example above.

    ReplyDelete
  9. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Alfresco, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on in Alfresco. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Nitesh Kumar
    MaxMunus
    E-mail: nitesh@maxmunus.com
    Skype id: nitesh_maxmunus
    Ph:(+91) 8553912023
    http://www.maxmunus.com/


    ReplyDelete