Hello,
Has anyone in the community have been able apply unique constraints in Alfresco. Please let me know.
My project requires to apply unique constraints on one of the property in the model.
Thanks in advance.
There's no out-of-the-box Constraint available to make a property unique across the repository. You'll have to write your custom logic when the property is received by the repository.
Also, making a property unique across the Repository does not make sense in some use cases and could actually restrict your use of Alfresco. E.g. whenever a document is online edited / offline edited, or a new version uploaded via Share (and potentially other APIs), a copy of the node is created with the same metadata. If you now had a unique constraint, that copy would not be allowed to exist, and the whole process of online editing / offline editing etc. would fail / break.
Axel Faust if i create a unique constraint on a property that is in a smart folder will that apply to across the repository. My requirement is to check an id against the database to make sure that the value entered by the user is not already in the database in edit properties section.
Since there is no way to define a unique constraint with default Alfresco, you would have to create a custom implementation. And since that custom implementation can do whatever, it can apply to any context you choose. Though it will actually be hard / impossible to implement any constraint that is context sensitive because the constraint implementations in Alfresco never get sufficient information about the node containing the property value to be validated (as in "no information at all") to determine any context, be it Smart Folder or repository. The only way you could deal with the context is by implementing something completely separate from the concept of constraints in Alfresco (a web script you call from some client-side JavaScript code, e.g. a validator function for the form), and the again you can do whatever you like and are able to check with the information available. If editing a node inside a Smart Folder, you would have the necessary context information via the virtual NodeRef to restrict your validation to the Smart Folder and not the entire repository - if you wanted.
@Axel Faust Can you guide me on how to create the webscripts, I have never done them before.
@Axel Faust Can you guide me on how to create the webscripts, I have never done them before.
The best possible help I can give you is to point you to Jeff Potts' tutorial on the subject, and help you afterwards, if you have problems with specific aspects of developing a web script.
Axel Faust Jeff Potts Going through various discussion and Jeff's Tutorials I have decided it best to go with custom constraint using either list of values or Java class . I have created a java class for the purpose. My question is do i execute this java class using a webscript by creating a Descriptor, Template and Controller or is there any other way I can do it.
Descriptor (loaddata.get.desc.xml)
<webscripts>
<shortname>Load data from DB</shortname>
<description> Load Data from database </description>
<url>/loaddata</url>
<format default="json"></format>
<authentication>user</authentication>
<family> Load Data</family>
</webscripts>
Controller (loaddata.get.js).
package org.alfresco.listofValues;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
public class ListOfValuesQueryConstraint extends ListOfValuesConstraint implements Serializable {
private static final long serialVersionUID=1;
private List<String> allowedLabels;
public void setAllowedValues(List allowedValues) {}
public void setCaseSensitive(boolean caseSensitive) {}
public void initialize() {
super.setCaseSensitive(false);
this.loadDB();
}
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;
}
protected void loadDB() {
String driverName = "org.postgresql.Driver";
String serverName = "localhost:5432";
String mydatabase = "alfresco";
String username = "alfresco";
String password = "admin";
List<String> av = new ArrayList<String>();
List<String> al=new ArrayList<String>();
try {
Connection connection = null;
Class.forName(driverName);
String url = "jdbcostgresql://" + serverName + "/" + mydatabase;
connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select jsid from tb_checkunique");
while (rs.next()) {
av.add(rs.getString("jsid"));
al.add(rs.getString("jsid"));
}
}
catch (Exception e) {}
super.setAllowedValues(av);
this.setAllowedLabels(al);
}
}
Can you please guide me how do i create the template freemarker file as I believe I need to show data the list of values.
Thanks in advance.
I am just seeing this 2 years later. In the past I have used the AttributeService to enforce uniqueness. With the attribute service you can create Key Value pairs. The key can have up to 3 segments. The first two segments could define the context for the unique ids. The third segment would be the actual unique ID. The value would be the NodeRef.
In this case, a content policy would add the entry to the Attribute Service. If there is a duplicate value and exception would be thrown.
If the content policy is providing the unique value, the attribute service would ensure that duplicates do not occur if the same value is generated in separate threads and the code could just pick the "next" value. If there is an external source of the ID, corrective coordiation would need to be coordinated with the system generating the unique value
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.