When extending the ListOfValuesConstraint with an id,name structure, how do you modify the search element so that you can search using the name rather than the id?

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Member II

When extending the ListOfValuesConstraint with an id,name structure, how do you modify the search element so that you can search using the name rather than the id?

Jump to solution

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>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
...
1 Solution

Accepted Solutions
Highlighted
Master

Re: When extending the ListOfValuesConstraint with an id,name structure, how do you modify the search element so that you can search using the name rather than the id?

Jump 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.

View solution in original post

1 Reply
Highlighted
Master

Re: When extending the ListOfValuesConstraint with an id,name structure, how do you modify the search element so that you can search using the name rather than the id?

Jump 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.

View solution in original post