Hi All,
I have successfuly created a dynamic list constraint by extending the ListOfValuesConstraint.
The database table (custom_entities) has two columns (id,name) as follows:
id, name
1, EntityA
2, EntityB
3, EntityC
...
The property stores the id value (1), but share shows the name label (EntityA) throughout. This is how you would expect it to work.
However, I am not sure how to make the search work.
For example the following search will find all Entities with id 1:
ukl:entity:'1'
However I want a way to be able to use the name field, like so:
ukl:entity:'EntityA'
Since the id is the only value stored clearly this won't work out of the box.
Can somebody please provide some suggestions on a potential way forward with this?
I want to avoid using the name field as reference, which would have been the easy way out.
Best Regards,
Adam
// ListOfEntitiesConstraint
public class ListOfEntitiesConstraint extends ListOfValuesConstraint implements Serializable {
private static final long serialVersionUID = 1;
private List<String> allowedLabels;
@Override
public void setAllowedValues(List allowedValues) {
}
@Override
public void setCaseSensitive(boolean caseSensitive) {
}
@Override
public void initialize() {
super.setCaseSensitive(false);
this.loadDB();
}
@Override
public List<String> getAllowedValues() {
this.loadDB();
return super.getAllowedValues();
}
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;
}
@Override
public String getDisplayLabel(String constraintAllowableValue, MessageLookup messageLookup) {
if (!super.getAllowedValues().contains(constraintAllowableValue))
{
return null;
}
String message = this.getAllowedLabels().get(super.getAllowedValues().indexOf(constraintAllowableValue));
return message == null ? constraintAllowableValue : message;
//return super.getDisplayLabel(constraintAllowableValue, messageLookup);
}
protected void loadDB() {
String driverName = "org.postgresql.Driver";
String serverName = "alfresco.meab.local";
String mydatabase = "alfresco";
String username = "postgres";
String password = "password";
List<String> av = new ArrayList<String>();
List<String> al = new ArrayList<String>();
try {
Connection connection = null;
Class.forName(driverName);
String url = "jdbc:postgresql://" + serverName + "/" + mydatabase;
connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select id, name from custom_entities order by name ASC");
while (rs.next()) {
av.add(rs.getString("id"));
al.add(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
super.setAllowedValues(av);
this.setAllowedLabels(al);
}
}
<!-- Relevant parts of model -->
...
<constraints>
<constraint name="ukl:entityList_constraint" type="com.acme.cmspoc2.demoamp.ListOfEntitiesConstraint">
<parameter name="allowedValues">
<list></list>
</parameter>
<parameter name="caseSensitive">
<value>true</value>
</parameter>
</constraint>
</constraints>
...
<type name="ukl:doc">
<title>General Document</title>
<parent>cm:content</parent>
<properties>
<property name="ukl:entity">
<title>Entity</title>
<type>d:text</type>
<multiple>true</multiple>
<constraints>
<constraint ref="ukl:entityList_constraint" />
</constraints>
</property>
</properties>
...
</type>
...
Solved! Go to Solution.
In short, there is no way to achieve this. The value is just the id, so you must use that for search. For the user you will almost always provide this via a select list that uses the name for the display label, so it is not an issue. For the developer - well, they will just have to use the correct value when writing a query.
In short, there is no way to achieve this. The value is just the id, so you must use that for search. For the user you will almost always provide this via a select list that uses the name for the display label, so it is not an issue. For the developer - well, they will just have to use the correct value when writing a query.
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.