Search in sources :

Example 46 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project acs-community-packaging by Alfresco.

the class TransientNode method createNew.

/**
 * Construct a transient node for an item yet to be created in the Repository.
 *
 * This will apply any one-time initialisation required upon creation of the node
 * e.g. assignment of default values.
 *
 * @param dictionaryService dictionary service
 * @param typeDef The type definition this node will represent
 * @param name The name of the node
 * @param data The properties and associations this node will have
 * @return  transient node
 */
public static TransientNode createNew(DictionaryService dictionaryService, TypeDefinition typeDef, String name, Map<QName, Serializable> data) {
    // build a complete anonymous type for the start task
    List<AspectDefinition> aspects = typeDef.getDefaultAspects();
    List<QName> aspectNames = new ArrayList<QName>(aspects.size());
    getMandatoryAspects(typeDef, aspectNames);
    ClassDefinition startTaskDef = dictionaryService.getAnonymousType(typeDef.getName(), aspectNames);
    // initialise start task values
    Map<QName, Serializable> startValues = new HashMap<QName, Serializable>();
    if (data != null) {
        startValues.putAll(data);
    }
    // apply default values
    Map<QName, PropertyDefinition> propertyDefs = startTaskDef.getProperties();
    for (Map.Entry<QName, PropertyDefinition> entry : propertyDefs.entrySet()) {
        String defaultValue = entry.getValue().getDefaultValue();
        if (defaultValue != null) {
            if (startValues.get(entry.getKey()) == null) {
                startValues.put(entry.getKey(), (Serializable) DefaultTypeConverter.INSTANCE.convert(entry.getValue().getDataType(), defaultValue));
            }
        }
    }
    return new TransientNode(typeDef.getName(), name, startValues);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project acs-community-packaging by Alfresco.

the class EditContentPropertiesDialog method finishImpl.

@Override
protected String finishImpl(FacesContext context, String outcome) throws Exception {
    NodeRef nodeRef = this.editableNode.getNodeRef();
    Map<String, Object> editedProps = this.editableNode.getProperties();
    // get the name and move the node as necessary
    String name = (String) editedProps.get(ContentModel.PROP_NAME);
    if (name != null) {
        getFileFolderService().rename(nodeRef, name);
    }
    // we need to put all the properties from the editable bag back into
    // the format expected by the repository
    Map<QName, Serializable> repoProps = this.getNodeService().getProperties(nodeRef);
    // Extract and deal with the special mimetype property for ContentData
    String mimetype = (String) editedProps.get(TEMP_PROP_MIMETYPE);
    if (mimetype != null) {
        // remove temporary prop from list so it isn't saved with the others
        editedProps.remove(TEMP_PROP_MIMETYPE);
        ContentData contentData = (ContentData) editedProps.get(ContentModel.PROP_CONTENT);
        if (contentData != null) {
            contentData = ContentData.setMimetype(contentData, mimetype);
            editedProps.put(ContentModel.PROP_CONTENT.toString(), contentData);
        }
    }
    // Extract and deal with the special encoding property for ContentData
    String encoding = (String) editedProps.get(TEMP_PROP_ENCODING);
    if (encoding != null) {
        // remove temporary prop from list so it isn't saved with the others
        editedProps.remove(TEMP_PROP_ENCODING);
        ContentData contentData = (ContentData) editedProps.get(ContentModel.PROP_CONTENT);
        if (contentData != null) {
            contentData = ContentData.setEncoding(contentData, encoding);
            editedProps.put(ContentModel.PROP_CONTENT.toString(), contentData);
        }
    }
    // add the "author" aspect if required, properties will get set below
    if (this.getNodeService().hasAspect(nodeRef, ContentModel.ASPECT_AUTHOR) == false) {
        this.getNodeService().addAspect(nodeRef, ContentModel.ASPECT_AUTHOR, null);
    }
    // add the "titled" aspect if required, properties will get set below
    if (this.getNodeService().hasAspect(nodeRef, ContentModel.ASPECT_TITLED) == false) {
        getNodeService().addAspect(nodeRef, ContentModel.ASPECT_TITLED, null);
    }
    // add the remaining properties
    Iterator<String> iterProps = editedProps.keySet().iterator();
    while (iterProps.hasNext()) {
        String propName = iterProps.next();
        QName qname = QName.createQName(propName);
        // make sure the property is represented correctly
        Serializable propValue = (Serializable) editedProps.get(propName);
        // check for empty strings when using number types, set to null in this case
        if (propValue instanceof String) {
            PropertyDefinition propDef = this.getDictionaryService().getProperty(qname);
            if (((String) propValue).length() == 0) {
                if (propDef != null) {
                    if (propDef.getDataType().getName().equals(DataTypeDefinition.DOUBLE) || propDef.getDataType().getName().equals(DataTypeDefinition.FLOAT) || propDef.getDataType().getName().equals(DataTypeDefinition.INT) || propDef.getDataType().getName().equals(DataTypeDefinition.LONG)) {
                        propValue = null;
                    }
                }
            } else // handle locale strings to Locale objects
            if (propDef != null && propDef.getDataType().getName().equals(DataTypeDefinition.LOCALE)) {
                propValue = I18NUtil.parseLocale((String) propValue);
            }
        }
        repoProps.put(qname, propValue);
    }
    // send the properties back to the repository
    this.getNodeService().setProperties(nodeRef, repoProps);
    // we also need to persist any association changes that may have been made
    // add any associations added in the UI
    Map<String, Map<String, AssociationRef>> addedAssocs = this.editableNode.getAddedAssociations();
    for (Map<String, AssociationRef> typedAssoc : addedAssocs.values()) {
        for (AssociationRef assoc : typedAssoc.values()) {
            this.getNodeService().createAssociation(assoc.getSourceRef(), assoc.getTargetRef(), assoc.getTypeQName());
        }
    }
    // remove any association removed in the UI
    Map<String, Map<String, AssociationRef>> removedAssocs = this.editableNode.getRemovedAssociations();
    for (Map<String, AssociationRef> typedAssoc : removedAssocs.values()) {
        for (AssociationRef assoc : typedAssoc.values()) {
            this.getNodeService().removeAssociation(assoc.getSourceRef(), assoc.getTargetRef(), assoc.getTypeQName());
        }
    }
    // add any child associations added in the UI
    Map<String, Map<String, ChildAssociationRef>> addedChildAssocs = this.editableNode.getAddedChildAssociations();
    for (Map<String, ChildAssociationRef> typedAssoc : addedChildAssocs.values()) {
        for (ChildAssociationRef assoc : typedAssoc.values()) {
            this.getNodeService().addChild(assoc.getParentRef(), assoc.getChildRef(), assoc.getTypeQName(), assoc.getTypeQName());
        }
    }
    // remove any child association removed in the UI
    Map<String, Map<String, ChildAssociationRef>> removedChildAssocs = this.editableNode.getRemovedChildAssociations();
    for (Map<String, ChildAssociationRef> typedAssoc : removedChildAssocs.values()) {
        for (ChildAssociationRef assoc : typedAssoc.values()) {
            this.getNodeService().removeChild(assoc.getParentRef(), assoc.getChildRef());
        }
    }
    return outcome;
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentData(org.alfresco.service.cmr.repository.ContentData) Map(java.util.Map)

Example 48 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition 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 49 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project acs-community-packaging by Alfresco.

the class DataDictionary method getPropertyDefinition.

/**
 * Returns the property definition for the given property on the given node
 *
 * @param node The node from which to get the property
 * @param property The property to find the definition for
 * @return The property definition or null if the property is not known
 */
public PropertyDefinition getPropertyDefinition(Node node, String property) {
    PropertyDefinition propDef = null;
    TypeDefinition typeDef = getTypeDef(node.getType(), node.getAspects());
    if (typeDef != null) {
        Map<QName, PropertyDefinition> properties = typeDef.getProperties();
        propDef = properties.get(Repository.resolveToQName(property));
    }
    return propDef;
}
Also used : QName(org.alfresco.service.namespace.QName) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition)

Example 50 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project acs-community-packaging by Alfresco.

the class NodePropertyComparator method compare.

@SuppressWarnings({ "unchecked", "rawtypes" })
public int compare(Object node1, Object node2) {
    Map<String, Object> nodeProperties1 = ((Node) node1).getProperties();
    Map<String, Object> nodeProperties2 = ((Node) node2).getProperties();
    PropertyDefinition pd1 = dataDictionary.getPropertyDefinition((Node) node1, propertyName);
    PropertyDefinition pd2 = dataDictionary.getPropertyDefinition((Node) node2, propertyName);
    Comparable propertyValue1, propertyValue2;
    if ((pd1 != null) && (pd2 != null)) {
        String typeName = pd1.getDataType().getName().getLocalName();
        if (typeName.equals("datetime")) {
            propertyValue1 = (Date) nodeProperties1.get(propertyName);
            propertyValue2 = (Date) nodeProperties2.get(propertyName);
        } else if (typeName.equals("long")) {
            propertyValue1 = (Long) nodeProperties1.get(propertyName);
            propertyValue2 = (Long) nodeProperties2.get(propertyName);
        } else if (typeName.equals("boolean")) {
            propertyValue1 = (Boolean) nodeProperties1.get(propertyName);
            propertyValue2 = (Boolean) nodeProperties2.get(propertyName);
        } else // string types: text, mltext
        // non comparable types: locale, content
        {
            propertyValue1 = nodeProperties1.get(propertyName).toString();
            propertyValue2 = nodeProperties2.get(propertyName).toString();
        }
    } else // additional properties doesn't contains in the node properties
    // their type can't be resolved using DataDictionary
    // QNameNodeMap resolves them on first invocation and puts them into the map of node properties
    {
        if (propertyName.equals("size")) {
            propertyValue1 = (Long) nodeProperties1.get(propertyName);
            propertyValue2 = (Long) nodeProperties2.get(propertyName);
        } else {
            propertyValue1 = nodeProperties1.get(propertyName).toString();
            propertyValue2 = nodeProperties2.get(propertyName).toString();
        }
    }
    if (isAscending) {
        return propertyValue1.compareTo(propertyValue2);
    }
    return propertyValue2.compareTo(propertyValue1);
}
Also used : Node(org.alfresco.web.bean.repository.Node) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Aggregations

PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)77 QName (org.alfresco.service.namespace.QName)51 HashMap (java.util.HashMap)25 ArrayList (java.util.ArrayList)24 NodeRef (org.alfresco.service.cmr.repository.NodeRef)16 Map (java.util.Map)15 Serializable (java.io.Serializable)14 AssociationDefinition (org.alfresco.service.cmr.dictionary.AssociationDefinition)11 ClassDefinition (org.alfresco.service.cmr.dictionary.ClassDefinition)11 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)9 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)9 Collection (java.util.Collection)7 List (java.util.List)7 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)7 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)7 BooleanQuery (org.apache.lucene.search.BooleanQuery)7 Builder (org.apache.lucene.search.BooleanQuery.Builder)7 Constraint (org.alfresco.service.cmr.dictionary.Constraint)6 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6