Unique Constraints in Alfresco

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

Unique Constraints in Alfresco

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.

8 Replies
Highlighted
Senior Member

Re: Unique Constraints in Alfresco

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.

Regards,
Abbas
Highlighted
Master

Re: Unique Constraints in Alfresco

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.

Highlighted
Active Member II

Re: Unique Constraints in Alfresco

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.

Highlighted
Master

Re: Unique Constraints in Alfresco

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.

Highlighted
Active Member II

Re: Unique Constraints in Alfresco

@Axel Faust  Can you guide me on how to create the webscripts, I have never done them before.

Highlighted
Active Member II

Re: Unique Constraints in Alfresco

@Axel Faust  Can you guide me on how to create the webscripts, I have never done them before.

Highlighted
Master

Re: Unique Constraints in Alfresco

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.

Highlighted
Active Member II

Re: Unique Constraints in Alfresco

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 = "jdbcSmiley Tongueostgresql://" + 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.