Search in sources :

Example 6 with ListOfValuesConstraint

use of org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint in project acs-community-packaging by Alfresco.

the class TextFieldGenerator method createComponent.

@Override
@SuppressWarnings("unchecked")
protected UIComponent createComponent(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
    UIComponent component = null;
    if (propertySheet.inEditMode()) {
        // if the field has the list of values constraint
        // and it is editable a SelectOne component is
        // required otherwise create the standard edit component
        ListOfValuesConstraint constraint = getListOfValuesConstraint(context, propertySheet, item);
        PropertyDefinition propDef = this.getPropertyDefinition(context, propertySheet.getNode(), item.getName());
        if (constraint != null && item.isReadOnly() == false && propDef != null && propDef.isProtected() == false) {
            component = context.getApplication().createComponent(UISelectOne.COMPONENT_TYPE);
            FacesHelper.setupComponentId(context, component, item.getName());
            // create the list of choices
            UISelectItems itemsComponent = (UISelectItems) context.getApplication().createComponent("javax.faces.SelectItems");
            List<String> values = constraint.getAllowedValues();
            List<SelectItem> items = new ArrayList<SelectItem>(values.size());
            for (String value : values) {
                Object obj = null;
                // we need to setup the list with objects of the correct type
                if (propDef.getDataType().getName().equals(DataTypeDefinition.INT)) {
                    obj = Integer.valueOf(value);
                } else if (propDef.getDataType().getName().equals(DataTypeDefinition.LONG)) {
                    obj = Long.valueOf(value);
                } else if (propDef.getDataType().getName().equals(DataTypeDefinition.DOUBLE)) {
                    obj = Double.valueOf(value);
                } else if (propDef.getDataType().getName().equals(DataTypeDefinition.FLOAT)) {
                    obj = Float.valueOf(value);
                } else {
                    obj = value;
                }
                // retrieve the display label
                String label = constraint.getDisplayLabel(value, dataDictionary.getDictionaryService());
                if (label == null) {
                    label = value;
                }
                items.add(new SelectItem(obj, label));
            }
            itemsComponent.setValue(items);
            // add the items as a child component
            component.getChildren().add(itemsComponent);
        } else {
            // use the standard component in edit mode
            component = generate(context, item.getName());
        }
    } else {
        // create an output text component in view mode
        component = createOutputTextComponent(context, item.getName());
    }
    return component;
}
Also used : UISelectItems(javax.faces.component.UISelectItems) SelectItem(javax.faces.model.SelectItem) UIComponent(javax.faces.component.UIComponent) ArrayList(java.util.ArrayList) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Example 7 with ListOfValuesConstraint

use of org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint in project alfresco-remote-api by Alfresco.

the class WorkflowRestImpl method getFormModelElements.

/**
 * @param type the type to get the elements for
 * @param paging Paging
 * @return collection with all valid form-model elements for the given type.
 */
public CollectionWithPagingInfo<FormModelElement> getFormModelElements(TypeDefinition type, Paging paging) {
    Map<QName, PropertyDefinition> taskProperties = type.getProperties();
    Set<QName> typesToExclude = getTypesToExclude(type);
    List<FormModelElement> page = new ArrayList<FormModelElement>();
    for (Entry<QName, PropertyDefinition> entry : taskProperties.entrySet()) {
        String name = entry.getKey().toPrefixString(namespaceService).replace(':', '_');
        // Only add properties which are not part of an excluded type
        if (!typesToExclude.contains(entry.getValue().getContainerClass().getName()) && excludeModelTypes.contains(name) == false) {
            FormModelElement element = new FormModelElement();
            element.setName(name);
            element.setQualifiedName(entry.getKey().toString());
            element.setTitle(entry.getValue().getTitle(dictionaryService));
            element.setRequired(entry.getValue().isMandatory());
            element.setDataType(entry.getValue().getDataType().getName().toPrefixString(namespaceService));
            element.setDefaultValue(entry.getValue().getDefaultValue());
            if (entry.getValue().getConstraints() != null) {
                for (ConstraintDefinition constraintDef : entry.getValue().getConstraints()) {
                    Constraint constraint = constraintDef.getConstraint();
                    if (constraint != null && constraint instanceof ListOfValuesConstraint) {
                        ListOfValuesConstraint valuesConstraint = (ListOfValuesConstraint) constraint;
                        if (valuesConstraint.getAllowedValues() != null && valuesConstraint.getAllowedValues().size() > 0) {
                            element.setAllowedValues(valuesConstraint.getAllowedValues());
                        }
                    }
                }
            }
            page.add(element);
        }
    }
    Map<QName, AssociationDefinition> taskAssociations = type.getAssociations();
    for (Entry<QName, AssociationDefinition> entry : taskAssociations.entrySet()) {
        // Only add associations which are not part of an excluded type
        if (!typesToExclude.contains(entry.getValue().getSourceClass().getName())) {
            FormModelElement element = new FormModelElement();
            element.setName(entry.getKey().toPrefixString(namespaceService).replace(':', '_'));
            element.setQualifiedName(entry.getKey().toString());
            element.setTitle(entry.getValue().getTitle(dictionaryService));
            element.setRequired(entry.getValue().isTargetMandatory());
            element.setDataType(entry.getValue().getTargetClass().getName().toPrefixString(namespaceService));
            page.add(element);
        }
    }
    return CollectionWithPagingInfo.asPaged(paging, page, false, page.size());
}
Also used : FormModelElement(org.alfresco.rest.workflow.api.model.FormModelElement) Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint)

Aggregations

ListOfValuesConstraint (org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint)7 ArrayList (java.util.ArrayList)5 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)5 Constraint (org.alfresco.service.cmr.dictionary.Constraint)4 QName (org.alfresco.service.namespace.QName)4 SelectItem (javax.faces.model.SelectItem)3 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)3 List (java.util.List)2 UIComponent (javax.faces.component.UIComponent)2 UISelectItems (javax.faces.component.UISelectItems)2 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)2 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)2 Entry (java.util.Map.Entry)1 UIInput (javax.faces.component.UIInput)1 UIOutput (javax.faces.component.UIOutput)1 UISelectBoolean (javax.faces.component.UISelectBoolean)1 UISelectOne (javax.faces.component.UISelectOne)1 ValueBinding (javax.faces.el.ValueBinding)1 FormModelElement (org.alfresco.rest.workflow.api.model.FormModelElement)1 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)1