Search in sources :

Example 1 with RegisteredConstraint

use of org.alfresco.repo.dictionary.constraint.RegisteredConstraint in project alfresco-repository by Alfresco.

the class RepoDictionaryDAOTest method testConstraintsOverrideInheritance.

public void testConstraintsOverrideInheritance() {
    QName baseQName = QName.createQName(TEST_URL, "base");
    QName fileQName = QName.createQName(TEST_URL, "file");
    QName folderQName = QName.createQName(TEST_URL, "folder");
    QName prop1QName = QName.createQName(TEST_URL, "prop1");
    // get the base property
    PropertyDefinition prop1Def = service.getProperty(baseQName, prop1QName);
    assertNotNull(prop1Def);
    List<ConstraintDefinition> prop1Constraints = prop1Def.getConstraints();
    assertEquals("Incorrect number of constraints", 3, prop1Constraints.size());
    assertTrue("Constraint instance incorrect", prop1Constraints.get(0).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(1).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(2).getConstraint() instanceof RegisteredConstraint);
    // check the inherited property on folder (must be same as above)
    prop1Def = service.getProperty(folderQName, prop1QName);
    assertNotNull(prop1Def);
    prop1Constraints = prop1Def.getConstraints();
    assertEquals("Incorrect number of constraints", 3, prop1Constraints.size());
    assertTrue("Constraint instance incorrect", prop1Constraints.get(0).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(1).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(2).getConstraint() instanceof RegisteredConstraint);
    // check the overridden property on file (must be reverse of above)
    prop1Def = service.getProperty(fileQName, prop1QName);
    assertNotNull(prop1Def);
    prop1Constraints = prop1Def.getConstraints();
    assertEquals("Incorrect number of constraints", 3, prop1Constraints.size());
    assertTrue("Constraint instance incorrect", prop1Constraints.get(0).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(1).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(2).getConstraint() instanceof RegisteredConstraint);
}
Also used : RegisteredConstraint(org.alfresco.repo.dictionary.constraint.RegisteredConstraint) QName(org.alfresco.service.namespace.QName) RegexConstraint(org.alfresco.repo.dictionary.constraint.RegexConstraint) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) StringLengthConstraint(org.alfresco.repo.dictionary.constraint.StringLengthConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 2 with RegisteredConstraint

use of org.alfresco.repo.dictionary.constraint.RegisteredConstraint in project alfresco-repository by Alfresco.

the class PropertyFieldProcessor method makeFieldConstraints.

private List<FieldConstraint> makeFieldConstraints(PropertyDefinition propDef) {
    List<FieldConstraint> fieldConstraints = null;
    List<ConstraintDefinition> constraints = propDef.getConstraints();
    if (constraints != null && constraints.size() > 0) {
        fieldConstraints = new ArrayList<FieldConstraint>(constraints.size());
        for (ConstraintDefinition constraintDef : constraints) {
            Constraint constraint = constraintDef.getConstraint();
            String type = constraint.getType();
            Map<String, Object> params = constraint.getParameters();
            // we need to examine the underlying constraint to see if it is a LIST constraint
            if (RegisteredConstraint.class.isAssignableFrom(constraint.getClass())) {
                constraint = ((RegisteredConstraint) constraint).getRegisteredConstraint();
            }
            if (ListOfValuesConstraint.class.isAssignableFrom(constraint.getClass())) {
                ListOfValuesConstraint lovConstraint = (ListOfValuesConstraint) constraint;
                List<String> allowedValues = lovConstraint.getAllowedValues();
                List<String> localisedValues = new ArrayList<String>(allowedValues.size());
                // Look up each localised display-label in turn.
                for (String value : allowedValues) {
                    String displayLabel = lovConstraint.getDisplayLabel(value, dictionaryService);
                    // Change the allowedValue entry to the format the FormsService expects for localised strings: "value|label"
                    // If there is no localisation defined for any value, then this will give us "value|value".
                    localisedValues.add(value + "|" + displayLabel);
                }
                // Now replace the allowedValues param with our localised version.
                params.put(ListOfValuesConstraint.ALLOWED_VALUES_PARAM, localisedValues);
            }
            FieldConstraint fieldConstraint = new FieldConstraint(type, params);
            fieldConstraints.add(fieldConstraint);
        }
    }
    return fieldConstraints;
}
Also used : RegisteredConstraint(org.alfresco.repo.dictionary.constraint.RegisteredConstraint) Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) FieldConstraint(org.alfresco.repo.forms.PropertyFieldDefinition.FieldConstraint) ArrayList(java.util.ArrayList) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) FieldConstraint(org.alfresco.repo.forms.PropertyFieldDefinition.FieldConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 3 with RegisteredConstraint

use of org.alfresco.repo.dictionary.constraint.RegisteredConstraint in project alfresco-repository by Alfresco.

the class RepoDictionaryDAOTest method testConstraints.

public void testConstraints() {
    // Check that the registered constraints are correct
    assertNotNull("Constraint reg1 not registered", ConstraintRegistry.getInstance().getConstraint("cm:reg1"));
    assertNotNull("Constraint reg2 not registered", ConstraintRegistry.getInstance().getConstraint("cm:reg2"));
    QName model = QName.createQName(TEST_URL, "dictionarydaotest");
    Collection<ConstraintDefinition> modelConstraints = service.getConstraints(model);
    // 10 + 7 + 6
    assertEquals(23, modelConstraints.size());
    QName conRegExp1QName = QName.createQName(TEST_URL, "regex1");
    boolean found1 = false;
    QName conStrLen1QName = QName.createQName(TEST_URL, "stringLength1");
    boolean found2 = false;
    for (ConstraintDefinition constraintDef : modelConstraints) {
        if (constraintDef.getName().equals(conRegExp1QName)) {
            assertEquals("Regex1 title", constraintDef.getTitle(service));
            assertEquals("Regex1 description", constraintDef.getDescription(service));
            found1 = true;
        }
        if (constraintDef.getName().equals(conStrLen1QName)) {
            assertNull(constraintDef.getTitle(service));
            assertNull(constraintDef.getDescription(service));
            found2 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    // get the constraints for a property without constraints
    QName propNoConstraintsQName = QName.createQName(TEST_URL, "fileprop");
    PropertyDefinition propNoConstraintsDef = service.getProperty(propNoConstraintsQName);
    assertNotNull("Property without constraints returned null list", propNoConstraintsDef.getConstraints());
    // get the constraints defined for the property
    QName prop1QName = QName.createQName(TEST_URL, "prop1");
    PropertyDefinition propDef = service.getProperty(prop1QName);
    List<ConstraintDefinition> constraints = propDef.getConstraints();
    assertNotNull("Null constraints list", constraints);
    assertEquals("Incorrect number of constraints", 3, constraints.size());
    assertTrue("Constraint instance incorrect", constraints.get(0).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", constraints.get(1).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", constraints.get(2).getConstraint() instanceof RegisteredConstraint);
    // check the individual constraints
    ConstraintDefinition constraintDef = constraints.get(0);
    assertTrue("Constraint anonymous name incorrect", constraintDef.getName().getLocalName().equals("dictionarydaotest_base_prop1_anon_0"));
    // inherit title / description for reference constraint
    assertTrue("Constraint title incorrect", constraintDef.getTitle(service).equals("Regex1 title"));
    assertTrue("Constraint description incorrect", constraintDef.getDescription(service).equals("Regex1 description"));
    constraintDef = constraints.get(1);
    assertTrue("Constraint anonymous name incorrect", constraintDef.getName().getLocalName().equals("dictionarydaotest_base_prop1_anon_1"));
    assertTrue("Constraint title incorrect", constraintDef.getTitle(service).equals("Prop1 Strlen1 title"));
    assertTrue("Constraint description incorrect", constraintDef.getDescription(service).equals("Prop1 Strlen1 description"));
    // check that the constraint implementation is valid (it used a reference)
    Constraint constraint = constraintDef.getConstraint();
    assertNotNull("Reference constraint has no implementation", constraint);
}
Also used : RegisteredConstraint(org.alfresco.repo.dictionary.constraint.RegisteredConstraint) RegisteredConstraint(org.alfresco.repo.dictionary.constraint.RegisteredConstraint) Constraint(org.alfresco.service.cmr.dictionary.Constraint) StringLengthConstraint(org.alfresco.repo.dictionary.constraint.StringLengthConstraint) AbstractConstraint(org.alfresco.repo.dictionary.constraint.AbstractConstraint) UserNameConstraint(org.alfresco.repo.dictionary.constraint.UserNameConstraint) RegexConstraint(org.alfresco.repo.dictionary.constraint.RegexConstraint) QName(org.alfresco.service.namespace.QName) RegexConstraint(org.alfresco.repo.dictionary.constraint.RegexConstraint) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) StringLengthConstraint(org.alfresco.repo.dictionary.constraint.StringLengthConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Aggregations

RegisteredConstraint (org.alfresco.repo.dictionary.constraint.RegisteredConstraint)3 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)3 RegexConstraint (org.alfresco.repo.dictionary.constraint.RegexConstraint)2 StringLengthConstraint (org.alfresco.repo.dictionary.constraint.StringLengthConstraint)2 Constraint (org.alfresco.service.cmr.dictionary.Constraint)2 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)2 QName (org.alfresco.service.namespace.QName)2 ArrayList (java.util.ArrayList)1 AbstractConstraint (org.alfresco.repo.dictionary.constraint.AbstractConstraint)1 ListOfValuesConstraint (org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint)1 UserNameConstraint (org.alfresco.repo.dictionary.constraint.UserNameConstraint)1 FieldConstraint (org.alfresco.repo.forms.PropertyFieldDefinition.FieldConstraint)1