use of org.alfresco.repo.forms.PropertyFieldDefinition.FieldConstraint 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.forms.PropertyFieldDefinition.FieldConstraint in project alfresco-repository by Alfresco.
the class PropertyFieldProcessor method makePropertyFieldDefinition.
private PropertyFieldDefinition makePropertyFieldDefinition(final PropertyDefinition propDef, FieldGroup group) {
String name = getPrefixedName(propDef);
QName dataType = propDef.getDataType().getName();
PropertyFieldDefinition fieldDef = new PropertyFieldDefinition(name, dataType.getLocalName());
populateFieldDefinition(propDef, fieldDef, group, PROP_DATA_PREFIX);
fieldDef.setDefaultValue(propDef.getDefaultValue());
fieldDef.setMandatory(propDef.isMandatory());
fieldDef.setRepeating(propDef.isMultiValued());
fieldDef.setIndexTokenisationMode(propDef.getIndexTokenisationMode());
// currently enforce this so make sure they are not editable
if (NamespaceService.SYSTEM_MODEL_1_0_URI.equals(propDef.getName().getNamespaceURI())) {
fieldDef.setProtectedField(true);
}
// type parameters object to represent the options and rules
if (dataType.equals(DataTypeDefinition.PERIOD)) {
PeriodDataTypeParameters periodOptions = getPeriodOptions();
fieldDef.setDataTypeParameters(periodOptions);
}
// setup constraints for the property
List<FieldConstraint> fieldConstraints = makeFieldConstraints(propDef);
fieldDef.setConstraints(fieldConstraints);
return fieldDef;
}
use of org.alfresco.repo.forms.PropertyFieldDefinition.FieldConstraint in project alfresco-repository by Alfresco.
the class ActionParameterField method processActionConstraints.
/**
* This method creates a list of {@link FieldConstraint field constraints}, if there are any.
*
* @return a List<FieldConstraint> if there are any, else Collections.emptyList()
*/
private List<FieldConstraint> processActionConstraints(ParameterDefinition parameterDef, ActionService actionService) {
List<FieldConstraint> fieldConstraints = Collections.emptyList();
String paramConstraintName = parameterDef.getParameterConstraintName();
if (paramConstraintName != null) {
ParameterConstraint paramConstraint = actionService.getParameterConstraint(paramConstraintName);
if (paramConstraint == null) {
throw new FormException("ParameterConstraint name '" + paramConstraintName + "' not recognised.");
} else {
// This map is of allowedValue : display label for that value.
Map<String, String> allowableValuesMap = paramConstraint.getAllowableValues();
// We need to concatenate each key-value entry into a String like "value|displaylabel"
// Then the FormService can display the labels but deal with the values as the underlying data.
List<String> pipeSeparatedAllowedValues = new ArrayList<String>(allowableValuesMap.size());
for (Map.Entry<String, String> entry : allowableValuesMap.entrySet()) {
pipeSeparatedAllowedValues.add(entry.getKey() + "|" + entry.getValue());
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("allowedValues", pipeSeparatedAllowedValues);
// Finally wrap it up in a parameter map.
fieldConstraints = new ArrayList<FieldConstraint>(allowableValuesMap.size());
fieldConstraints.add(new FieldConstraint("LIST", params));
}
}
return fieldConstraints;
}
use of org.alfresco.repo.forms.PropertyFieldDefinition.FieldConstraint in project alfresco-repository by Alfresco.
the class FormServiceImplTest method testGetAllDocForm.
@SuppressWarnings("unchecked")
@Test
public void testGetAllDocForm() throws Exception {
Form form = this.formService.getForm(new Item(NODE_FORM_ITEM_KIND, this.document.toString()));
// check a form got returned
assertNotNull("Expecting form to be present", form);
// check item identifier matches
assertEquals(NODE_FORM_ITEM_KIND, form.getItem().getKind());
assertEquals(this.document.toString(), form.getItem().getId());
// check the type is correct
assertEquals(ContentModel.TYPE_CONTENT.toPrefixString(this.namespaceService), form.getItem().getType());
// check there is no group info
assertNull("Expecting the form groups to be null!", form.getFieldGroups());
// check the field definitions
Collection<FieldDefinition> fieldDefs = form.getFieldDefinitions();
assertNotNull("Expecting to find fields", fieldDefs);
assertEquals("Wrong number of fields", 19, fieldDefs.size());
// create a Map of the field definitions
// NOTE: we can safely do this as we know there are no duplicate field names and we're not
// concerned with ordering!
Map<String, FieldDefinition> fieldDefMap = new HashMap<String, FieldDefinition>(fieldDefs.size());
for (FieldDefinition fieldDef : fieldDefs) {
fieldDefMap.put(fieldDef.getName(), fieldDef);
}
// find the fields
PropertyFieldDefinition nameField = (PropertyFieldDefinition) fieldDefMap.get("cm:name");
PropertyFieldDefinition titleField = (PropertyFieldDefinition) fieldDefMap.get("cm:title");
PropertyFieldDefinition descField = (PropertyFieldDefinition) fieldDefMap.get("cm:description");
PropertyFieldDefinition mimetypeField = (PropertyFieldDefinition) fieldDefMap.get("mimetype");
PropertyFieldDefinition encodingField = (PropertyFieldDefinition) fieldDefMap.get("encoding");
PropertyFieldDefinition sizeField = (PropertyFieldDefinition) fieldDefMap.get("size");
PropertyFieldDefinition originatorField = (PropertyFieldDefinition) fieldDefMap.get("cm:originator");
PropertyFieldDefinition addresseeField = (PropertyFieldDefinition) fieldDefMap.get("cm:addressee");
PropertyFieldDefinition addresseesField = (PropertyFieldDefinition) fieldDefMap.get("cm:addressees");
PropertyFieldDefinition subjectField = (PropertyFieldDefinition) fieldDefMap.get("cm:subjectline");
PropertyFieldDefinition sentDateField = (PropertyFieldDefinition) fieldDefMap.get("cm:sentdate");
AssociationFieldDefinition referencesField = (AssociationFieldDefinition) fieldDefMap.get("cm:references");
// check fields are present
assertNotNull("Expecting to find the cm:name field", nameField);
assertNotNull("Expecting to find the cm:title field", titleField);
assertNotNull("Expecting to find the cm:description field", descField);
assertNotNull("Expecting to find the mimetype field", mimetypeField);
assertNotNull("Expecting to find the encoding field", encodingField);
assertNotNull("Expecting to find the size field", sizeField);
assertNotNull("Expecting to find the cm:originator field", originatorField);
assertNotNull("Expecting to find the cm:addressee field", addresseeField);
assertNotNull("Expecting to find the cm:addressees field", addresseesField);
assertNotNull("Expecting to find the cm:subjectline field", subjectField);
assertNotNull("Expecting to find the cm:sentdate field", sentDateField);
assertNotNull("Expecting to find the cm:references field", referencesField);
// check the system properties are not present by default
assertFalse(fieldDefMap.containsKey("sys:node-dbid"));
assertFalse(fieldDefMap.containsKey("sys:store-identifier"));
assertFalse(fieldDefMap.containsKey("sys:node-uuid"));
assertFalse(fieldDefMap.containsKey("sys:store-protocol"));
// check the labels of all the fields
assertEquals("Expecting cm:name label to be " + LABEL_NAME, LABEL_NAME, nameField.getLabel());
assertEquals("Expecting cm:title label to be " + LABEL_TITLE, LABEL_TITLE, titleField.getLabel());
assertEquals("Expecting cm:description label to be " + LABEL_DESCRIPTION, LABEL_DESCRIPTION, descField.getLabel());
assertEquals("Expecting mimetype label to be " + LABEL_MIMETYPE, LABEL_MIMETYPE, mimetypeField.getLabel());
assertEquals("Expecting encoding label to be " + LABEL_ENCODING, LABEL_ENCODING, encodingField.getLabel());
assertEquals("Expecting size label to be " + LABEL_SIZE, LABEL_SIZE, sizeField.getLabel());
assertEquals("Expecting cm:originator label to be " + LABEL_ORIGINATOR, LABEL_ORIGINATOR, originatorField.getLabel());
assertEquals("Expecting cm:addressee label to be " + LABEL_ADDRESSEE, LABEL_ADDRESSEE, addresseeField.getLabel());
assertEquals("Expecting cm:addressees label to be " + LABEL_ADDRESSEES, LABEL_ADDRESSEES, addresseesField.getLabel());
assertEquals("Expecting cm:subjectline label to be " + LABEL_SUBJECT, LABEL_SUBJECT, subjectField.getLabel());
assertEquals("Expecting cm:sentdate label to be " + LABEL_SENT_DATE, LABEL_SENT_DATE, sentDateField.getLabel());
assertEquals("Expecting cm:references label to be " + LABEL_REFERENCES, LABEL_REFERENCES, referencesField.getLabel());
// check details of name field
assertEquals("Expecting cm:name type to be text", "text", nameField.getDataType());
assertTrue("Expecting cm:name to be mandatory", nameField.isMandatory());
assertFalse("Expecting cm:name to be single valued", nameField.isRepeating());
// get the constraint for the name field and check
List<FieldConstraint> constraints = nameField.getConstraints();
assertEquals("Expecting 1 constraint for cm:name", 1, constraints.size());
FieldConstraint constraint = constraints.get(0);
assertEquals("Expecting name of constraint to be 'REGEX'", "REGEX", constraint.getType());
Map<String, Object> params = constraint.getParameters();
assertNotNull("Expecting constraint parameters", params);
assertEquals("Expecting 2 constraint parameters", 2, params.size());
assertNotNull("Expecting an 'expression' constraint parameter", params.get("expression"));
assertNotNull("Expecting an 'requiresMatch' constraint parameter", params.get("requiresMatch"));
// check details of the addressees field
assertEquals("Expecting cm:addressees type to be text", "text", addresseesField.getDataType());
assertFalse("Expecting cm:addressees to be mandatory", addresseesField.isMandatory());
assertTrue("Expecting cm:addressees to be multi valued", addresseesField.isRepeating());
assertNull("Expecting constraints for cm:addressees to be null", addresseesField.getConstraints());
// check the details of the association field
assertEquals("Expecting cm:references endpoint type to be cm:content", "cm:content", referencesField.getEndpointType());
assertEquals("Expecting cm:references endpoint direction to be TARGET", Direction.TARGET.toString(), referencesField.getEndpointDirection().toString());
assertFalse("Expecting cm:references endpoint to be optional", referencesField.isEndpointMandatory());
assertTrue("Expecting cm:references endpoint to be 1 to many", referencesField.isEndpointMany());
// check the form data
FormData data = form.getFormData();
assertNotNull("Expecting form data", data);
assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue());
assertEquals(VALUE_DESCRIPTION, data.getFieldData(descField.getDataKeyName()).getValue());
assertEquals(VALUE_MIMETYPE, data.getFieldData(mimetypeField.getDataKeyName()).getValue());
assertEquals(VALUE_ENCODING, data.getFieldData(encodingField.getDataKeyName()).getValue());
assertEquals(VALUE_ORIGINATOR, data.getFieldData(originatorField.getDataKeyName()).getValue());
assertEquals(VALUE_ADDRESSEE, data.getFieldData(addresseeField.getDataKeyName()).getValue());
assertEquals(VALUE_SUBJECT, data.getFieldData(subjectField.getDataKeyName()).getValue());
assertTrue("Expecting size to be > 0", ((Long) data.getFieldData(sizeField.getDataKeyName()).getValue()).longValue() > 0);
String addressees = (String) data.getFieldData(addresseesField.getDataKeyName()).getValue();
assertNotNull(addressees);
assertTrue("Expecting the addressees value to have at least 1 comma", addressees.indexOf(",") != -1);
String[] addresseesArr = StringUtils.delimitedListToStringArray(addressees, ",");
assertEquals("Expecting 2 addressees", 2, addresseesArr.length);
assertEquals(VALUE_ADDRESSEES1, addresseesArr[0]);
assertEquals(VALUE_ADDRESSEES2, addresseesArr[1]);
Calendar calTestValue = Calendar.getInstance();
calTestValue.setTime(VALUE_SENT_DATE);
Calendar calServiceValue = Calendar.getInstance();
calServiceValue.setTime((Date) data.getFieldData(sentDateField.getDataKeyName()).getValue());
assertEquals(calTestValue.getTimeInMillis(), calServiceValue.getTimeInMillis());
List<String> targets = (List<String>) data.getFieldData(referencesField.getDataKeyName()).getValue();
assertEquals("Expecting 1 target", 1, targets.size());
assertEquals(this.associatedDoc.toString(), targets.get(0));
}
Aggregations