Custom Constrains in Alfresco Model

Most of the Alfresco development project, or rather I say CMS implementations, starts with content modeling where based on the requirement of organization Developers needs to create content types and related set of properties in to the CMS which can later be used by end user. This is very crucial part of development as our much future development would be dependent on content models. For more details on the content modeling you can refer my previous posts.

Content model in alfresco supports properties with much different types like integer, text, any, long etc…It also support constrains to be applied on the particular property where we want to restrict users to feed specific type of values in property. For instance emailId we want to validate the email id then we need to add constrain to the property associated with it.

Following are out of box constrains available in alfresco

Constrain Name Description Allowed Parameters
REGEX Property value matches regular expression. Regular Expression
LENGTH Text property value length must reside within minimum and maximum length limits. minLength and maxLength
MINMAX Numeric property value must reside within minimum and maximum range limits. minValue and maxValue
LIST  Property value must be one of those specified in the list of values. allowedValues and caseSensitive

Constrains can be defined in model as follow and can be added to multiple property according to requirement.

<constraint name="custom:constrainlist" type="LIST">

        <parameter name="allowedValues">

            <list>

                <value>value1</value>

                <value>value2</value>

            </list>

        </parameter>

     </constraint>

  </constraints>

<property name="custom:testproperty">

              <type>d:text</type>

              <protected>true</protected>

              <default></default>

              <constraints>

                 <constraint ref="customconstrainlist"/>

              </constraints>

           </property>

Multiple constrains can be added to one property and one constrain can also be applied to multiple properties.

There are situation where we want this predefined set of values to be fetched from existing database or some other place where those values are defined. To meet this requirement we need to create our custom constrain which will be extension of LIST constrain type.

Step 1:

Define custom constrain in model

<constraint name="custom:mycustomlist" type="com.company.alfresco.constraints.CustomConstraint" ><parameter name="allowedValues">
  <list></list>
 </parameter>

</constraint>

Step 2:

Create class CustomConstraint which extends org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint

package com.company.alfresco.constraints;

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;

public class CustomConstraint extends ListOfValuesConstraint {

   @Override
    public void setAllowedValues(List allowedValues) {
    }

    @Override
    public void setCaseSensitive(boolean caseSensitive) {
    }

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

    protected void fetchDataFromDb () {
        List<String> allowedValueList = new ArrayList<String>();
        //create db connection and fetch values

      // iterate though list of values and populate above list           

       super.setAllowedValues(allowedValueList);
   }
}

Step 3:

Add custom constrain to property in which you want to use that.

 

<property name="custom:testproperty">

              <type>d:text</type>

              <protected>true</protected>

              <default></default>

              <constraints>

                 <constraint ref="custom:mycustomlist"/>

              </constraints>

 </property>

 

All right now you can go and restart the server and check the effect of this from your user interface for adding properties to user interface please refer my previous posts on content modeling.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight

mitpatoliya

mitpatoliya

I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

Leave a Reply

Your email address will not be published. Required fields are marked *


7 − 3 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>