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!!!
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!!!
Easy to understand blog.
ReplyDeleteI have made above class.
I am new in alfresco.How to compile above custom class.
Thanks
You can compile it with the alfresco maven project
ReplyDeleteHi, 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"
ReplyDeleteYou need to load the class file into $TOMCAT_HOME/webapps/alfresco/WEB-INF/classes
DeleteAlso you need to create the package folders e.g in your case (mx.com.iikt.atalaya.constraints) -> mx/com/iikt/atalaya/constraints
This comment has been removed by the author.
ReplyDeleteDo we required custom ftl file rendering the dynamic values?
ReplyDeleteNot required. It will automatically use the OOB ftl files if you are using alfresco share
DeleteYou do not need a custom ftl. Alfresco already knows that constraints are rendered as lists.
ReplyDeleteHi Govinda,
ReplyDeleteWould 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.
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
ReplyDeletethe "actedUponNoderef" from within the Constraints class, such as the example above.
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
ReplyDeleteMaxMunus 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/