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);
}
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;
}
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);
}
Aggregations