Search in sources :

Example 1 with ListOfValuesConstraint

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

the class WorkflowModelBuilder method buildPropertyLabels.

private Map<String, String> buildPropertyLabels(WorkflowTask task, Map<String, Object> properties) {
    TypeDefinition taskType = task.getDefinition().getMetadata();
    final Map<QName, PropertyDefinition> propDefs = taskType.getProperties();
    return CollectionUtils.transform(properties, new Function<Entry<String, Object>, Pair<String, String>>() {

        @Override
        public Pair<String, String> apply(Entry<String, Object> entry) {
            String propName = entry.getKey();
            PropertyDefinition propDef = propDefs.get(qNameConverter.mapNameToQName(propName));
            if (propDef != null) {
                List<ConstraintDefinition> constraints = propDef.getConstraints();
                for (ConstraintDefinition constraintDef : constraints) {
                    Constraint constraint = constraintDef.getConstraint();
                    if (constraint instanceof ListOfValuesConstraint) {
                        ListOfValuesConstraint listConstraint = (ListOfValuesConstraint) constraint;
                        String label = listConstraint.getDisplayLabel(String.valueOf(entry.getValue()), dictionaryService);
                        return new Pair<String, String>(propName, label);
                    }
                }
            }
            return null;
        }
    });
}
Also used : Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) QName(org.alfresco.service.namespace.QName) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) Entry(java.util.Map.Entry) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair)

Example 2 with ListOfValuesConstraint

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

the class UISearchCustomProperties method generateControl.

/**
 * Generates an appropriate control for the given property
 *
 * @param context       JSF context
 * @param propDef       The definition of the property to create the control for
 * @param displayLabel  Display label for the component
 * @param beanBinding   Combined name of the value bound bean and variable used for value binding expression
 *
 * @return UIComponent
 */
@SuppressWarnings("unchecked")
private UIComponent generateControl(FacesContext context, PropertyDefinition propDef, String displayLabel, String beanBinding) {
    UIComponent control = null;
    DataTypeDefinition dataTypeDef = propDef.getDataType();
    QName typeName = dataTypeDef.getName();
    javax.faces.application.Application facesApp = context.getApplication();
    // create default value binding to a Map of values with a defined name
    ValueBinding vb = facesApp.createValueBinding("#{" + beanBinding + "[\"" + propDef.getName().toString() + "\"]}");
    // generate the appropriate input field
    if (typeName.equals(DataTypeDefinition.BOOLEAN)) {
        control = (UISelectBoolean) facesApp.createComponent(ComponentConstants.JAVAX_FACES_SELECT_BOOLEAN);
        control.setRendererType(ComponentConstants.JAVAX_FACES_CHECKBOX);
        control.setValueBinding(VALUE, vb);
    } else if (typeName.equals(DataTypeDefinition.CATEGORY)) {
        control = (UICategorySelector) facesApp.createComponent(RepoConstants.ALFRESCO_FACES_TAG_SELECTOR);
        control.setValueBinding(VALUE, vb);
    } else if (typeName.equals(DataTypeDefinition.DATETIME) || typeName.equals(DataTypeDefinition.DATE)) {
        Boolean showTime = Boolean.valueOf(typeName.equals(DataTypeDefinition.DATETIME));
        // create value bindings for the start year and year count attributes
        ValueBinding startYearBind = null;
        ValueBinding yearCountBind = null;
        if (showTime) {
            startYearBind = facesApp.createValueBinding("#{DateTimePickerGenerator.startYear}");
            yearCountBind = facesApp.createValueBinding("#{DateTimePickerGenerator.yearCount}");
        } else {
            startYearBind = facesApp.createValueBinding("#{DatePickerGenerator.startYear}");
            yearCountBind = facesApp.createValueBinding("#{DatePickerGenerator.yearCount}");
        }
        // Need to output component for From and To date selectors and labels
        // also neeed checkbox for enable/disable state - requires an outer wrapper component
        control = (UIPanel) facesApp.createComponent(ComponentConstants.JAVAX_FACES_PANEL);
        control.setRendererType(ComponentConstants.JAVAX_FACES_GRID);
        control.getAttributes().put("columns", Integer.valueOf(2));
        // enabled state checkbox
        UIInput checkbox = (UIInput) facesApp.createComponent(ComponentConstants.JAVAX_FACES_SELECT_BOOLEAN);
        checkbox.setRendererType(ComponentConstants.JAVAX_FACES_CHECKBOX);
        checkbox.setId(context.getViewRoot().createUniqueId());
        ValueBinding vbCheckbox = facesApp.createValueBinding("#{" + beanBinding + "[\"" + propDef.getName().toString() + "\"]}");
        checkbox.setValueBinding(VALUE, vbCheckbox);
        control.getChildren().add(checkbox);
        // main display label
        UIOutput label = (UIOutput) context.getApplication().createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
        label.setId(context.getViewRoot().createUniqueId());
        label.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
        label.setValue(displayLabel + ":");
        control.getChildren().add(label);
        // from date label
        UIOutput labelFromDate = (UIOutput) context.getApplication().createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
        labelFromDate.setId(context.getViewRoot().createUniqueId());
        labelFromDate.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
        labelFromDate.setValue(Application.getMessage(context, MSG_FROM));
        control.getChildren().add(labelFromDate);
        // from date control
        UIInput inputFromDate = (UIInput) facesApp.createComponent(ComponentConstants.JAVAX_FACES_INPUT);
        inputFromDate.setId(context.getViewRoot().createUniqueId());
        inputFromDate.setRendererType(RepoConstants.ALFRESCO_FACES_DATE_PICKER_RENDERER);
        inputFromDate.setValueBinding("startYear", startYearBind);
        inputFromDate.setValueBinding("yearCount", yearCountBind);
        inputFromDate.getAttributes().put("initialiseIfNull", Boolean.TRUE);
        inputFromDate.getAttributes().put("showTime", showTime);
        ValueBinding vbFromDate = facesApp.createValueBinding("#{" + beanBinding + "[\"" + PREFIX_DATE_FROM + propDef.getName().toString() + "\"]}");
        inputFromDate.setValueBinding(VALUE, vbFromDate);
        control.getChildren().add(inputFromDate);
        // to date label
        UIOutput labelToDate = (UIOutput) context.getApplication().createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
        labelToDate.setId(context.getViewRoot().createUniqueId());
        labelToDate.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
        labelToDate.setValue(Application.getMessage(context, MSG_TO));
        control.getChildren().add(labelToDate);
        // to date control
        UIInput inputToDate = (UIInput) facesApp.createComponent(ComponentConstants.JAVAX_FACES_INPUT);
        inputToDate.setId(context.getViewRoot().createUniqueId());
        inputToDate.setRendererType(RepoConstants.ALFRESCO_FACES_DATE_PICKER_RENDERER);
        inputToDate.setValueBinding("startYear", startYearBind);
        inputToDate.setValueBinding("yearCount", yearCountBind);
        inputToDate.getAttributes().put("initialiseIfNull", Boolean.TRUE);
        inputToDate.getAttributes().put("showTime", showTime);
        ValueBinding vbToDate = facesApp.createValueBinding("#{" + beanBinding + "[\"" + PREFIX_DATE_TO + propDef.getName().toString() + "\"]}");
        inputToDate.setValueBinding(VALUE, vbToDate);
        control.getChildren().add(inputToDate);
    } else if (typeName.equals(DataTypeDefinition.NODE_REF)) {
        control = (UISpaceSelector) facesApp.createComponent(RepoConstants.ALFRESCO_FACES_SPACE_SELECTOR);
        control.setValueBinding(VALUE, vb);
    } else {
        ListOfValuesConstraint constraint = getListOfValuesConstraint(propDef);
        if (constraint != null && propDef != null && propDef.isProtected() == false) {
            control = (UISelectOne) facesApp.createComponent(UISelectOne.COMPONENT_TYPE);
            UISelectItems itemsComponent = (UISelectItems) facesApp.createComponent(ComponentConstants.JAVAX_FACES_SELECT_ITEMS);
            List<SelectItem> items = new ArrayList<SelectItem>();
            List<String> values = constraint.getAllowedValues();
            for (String value : values) {
                items.add(new SelectItem(value, value));
            }
            itemsComponent.setValue(items);
            // add the items as a child component
            control.getChildren().add(itemsComponent);
            ValueBinding vbItemList = facesApp.createValueBinding("#{" + beanBinding + "[\"" + PREFIX_LOV_ITEM + propDef.getName().toString() + "\"]}");
            control.setValueBinding(VALUE, vbItemList);
        } else {
            // any other type is represented as an input text field
            control = (UIInput) facesApp.createComponent(ComponentConstants.JAVAX_FACES_INPUT);
            control.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
            control.setValueBinding("size", facesApp.createValueBinding("#{TextFieldGenerator.size}"));
            control.setValueBinding("maxlength", facesApp.createValueBinding("#{TextFieldGenerator.maxLength}"));
            control.setValueBinding(VALUE, vb);
        }
    }
    // set up the common aspects of the control
    control.setId(context.getViewRoot().createUniqueId());
    return control;
}
Also used : UISelectItems(javax.faces.component.UISelectItems) QName(org.alfresco.service.namespace.QName) ValueBinding(javax.faces.el.ValueBinding) UIComponent(javax.faces.component.UIComponent) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) UIInput(javax.faces.component.UIInput) UIOutput(javax.faces.component.UIOutput) SelectItem(javax.faces.model.SelectItem) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) ArrayList(java.util.ArrayList) List(java.util.List) UISelectBoolean(javax.faces.component.UISelectBoolean) UISelectOne(javax.faces.component.UISelectOne)

Example 3 with ListOfValuesConstraint

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

the class TextFieldGenerator method getListOfValuesConstraint.

/**
 * Retrieves the list of values constraint for the item, if it has one
 *
 * @param context FacesContext
 * @param propertySheet The property sheet being generated
 * @param item The item being generated
 * @return The constraint if the item has one, null otherwise
 */
protected ListOfValuesConstraint getListOfValuesConstraint(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
    ListOfValuesConstraint lovConstraint = null;
    // get the property definition for the item
    PropertyDefinition propertyDef = getPropertyDefinition(context, propertySheet.getNode(), item.getName());
    if (propertyDef != null) {
        // go through the constaints and see if it has the
        // list of values constraint
        List<ConstraintDefinition> constraints = propertyDef.getConstraints();
        for (ConstraintDefinition constraintDef : constraints) {
            Constraint constraint = constraintDef.getConstraint();
            if (constraint instanceof ListOfValuesConstraint) {
                lovConstraint = (ListOfValuesConstraint) constraint;
                break;
            }
        }
    }
    return lovConstraint;
}
Also used : Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 4 with ListOfValuesConstraint

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

the class UISearchCustomProperties method createComponentsFromConfig.

/**
 * Build the components from the Advanced Search config entries
 *
 * @param context FacesContext
 */
@SuppressWarnings("unchecked")
private void createComponentsFromConfig(FacesContext context) {
    DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService();
    AdvancedSearchConfigElement config = (AdvancedSearchConfigElement) Application.getConfigService(context).getConfig("Advanced Search").getConfigElement(AdvancedSearchConfigElement.CONFIG_ELEMENT_ID);
    // create an appropriate component for each custom property
    // using the DataDictionary to look-up labels and value types
    String beanBinding = (String) getAttributes().get("bean") + '.' + (String) getAttributes().get("var");
    List<CustomProperty> props = config.getCustomProperties();
    if (props != null) {
        for (CustomProperty property : props) {
            try {
                // try to find the Property definition for the specified Type or Aspect
                PropertyDefinition propDef = null;
                if (property.Type != null) {
                    QName type = Repository.resolveToQName(property.Type);
                    TypeDefinition typeDef = dd.getType(type);
                    if (typeDef == null) {
                        logger.warn("No Type Definition found for: " + property.Type + " - Was an Aspect expected?");
                        continue;
                    }
                    propDef = typeDef.getProperties().get(Repository.resolveToQName(property.Property));
                } else if (property.Aspect != null) {
                    QName aspect = Repository.resolveToQName(property.Aspect);
                    AspectDefinition aspectDef = dd.getAspect(aspect);
                    if (aspectDef == null) {
                        logger.warn("No Aspect Definition found for: " + property.Aspect + " - Was a Type expected?");
                        continue;
                    }
                    propDef = aspectDef.getProperties().get(Repository.resolveToQName(property.Property));
                }
                // if we found a def, then we can build components to represent it
                if (propDef != null) {
                    // resolve display label I18N message
                    String label;
                    if (property.LabelId != null && property.LabelId.length() != 0) {
                        label = Application.getMessage(context, property.LabelId);
                    } else {
                        // or use dictionary label or QName as last resort
                        label = propDef.getTitle(dd) != null ? propDef.getTitle(dd) : propDef.getName().getLocalName();
                    }
                    // special handling for Date and DateTime
                    DataTypeDefinition dataTypeDef = propDef.getDataType();
                    if (DataTypeDefinition.DATE.equals(dataTypeDef.getName()) || DataTypeDefinition.DATETIME.equals(dataTypeDef.getName())) {
                        getChildren().add(generateControl(context, propDef, label, beanBinding));
                    } else {
                        // add ListOfValues constraint components
                        ListOfValuesConstraint constraint = getListOfValuesConstraint(propDef);
                        if (constraint != null && propDef != null && propDef.isProtected() == false) {
                            getChildren().add(generateCheck(context, propDef, beanBinding));
                            getChildren().add(generateLabel(context, label + ": "));
                        } else {
                            getChildren().add(generateLabel(context, ""));
                            getChildren().add(generateLabel(context, label + ": "));
                        }
                        getChildren().add(generateControl(context, propDef, null, beanBinding));
                    }
                }
            } catch (DictionaryException ddErr) {
                logger.warn("Error building custom properties for Advanced Search: " + ddErr.getMessage());
            }
        }
    }
}
Also used : DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) QName(org.alfresco.service.namespace.QName) CustomProperty(org.alfresco.web.config.AdvancedSearchConfigElement.CustomProperty) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) AdvancedSearchConfigElement(org.alfresco.web.config.AdvancedSearchConfigElement) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException)

Example 5 with ListOfValuesConstraint

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

the class UIStoreSelector method createList.

/**
 * @return List of SelectItem components
 */
protected List<SelectItem> createList() {
    List<SelectItem> items = new ArrayList<SelectItem>(5);
    Constraint storesConstraint = ConstraintRegistry.getInstance().getConstraint("defaultStoreSelector");
    for (String store : ((ListOfValuesConstraint) storesConstraint).getAllowedValues()) {
        items.add(new SelectItem(store, store));
    }
    // make sure the list is sorted by the values
    QuickSort sorter = new QuickSort(items, "label", true, IDataContainer.SORT_CASEINSENSITIVE);
    sorter.sort();
    return items;
}
Also used : QuickSort(org.alfresco.web.data.QuickSort) Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) SelectItem(javax.faces.model.SelectItem) ArrayList(java.util.ArrayList) 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