Search in sources :

Example 61 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project alfresco-remote-api by Alfresco.

the class TaskInstancePut method parseTaskProperties.

@SuppressWarnings("unchecked")
private Map<QName, Serializable> parseTaskProperties(JSONObject json, WorkflowTask workflowTask) throws JSONException {
    Map<QName, Serializable> props = new HashMap<QName, Serializable>();
    // gets the array of properties names
    String[] names = JSONObject.getNames(json);
    if (names != null) {
        // array is not empty
        for (String name : names) {
            // build the qname of property
            QName key = QName.createQName(name.replaceFirst("_", ":"), namespaceService);
            Object jsonValue = json.get(name);
            Serializable value = null;
            // process null values
            if (jsonValue.equals(JSONObject.NULL)) {
                props.put(key, null);
            } else {
                // gets the property definition from dictionary
                PropertyDefinition prop = dictionaryService.getProperty(key);
                if (prop != null) {
                    if (prop.isMultiValued() && jsonValue instanceof JSONArray) {
                        value = new ArrayList<Serializable>();
                        for (int i = 0; i < ((JSONArray) jsonValue).length(); i++) {
                            ((List<Serializable>) value).add((Serializable) DefaultTypeConverter.INSTANCE.convert(prop.getDataType(), ((JSONArray) jsonValue).get(i)));
                        }
                    } else {
                        // convert property using its data type specified in model
                        value = (Serializable) DefaultTypeConverter.INSTANCE.convert(prop.getDataType(), jsonValue);
                    }
                } else {
                    // property definition was not found in dictionary
                    if (jsonValue instanceof JSONArray) {
                        value = new ArrayList<String>();
                        for (int i = 0; i < ((JSONArray) jsonValue).length(); i++) {
                            ((List<String>) value).add(((JSONArray) jsonValue).getString(i));
                        }
                    } else {
                        // Otherwise, we try to convert the string
                        if (jsonValue instanceof String) {
                            // Check if the task already has the property, use that type.
                            Serializable existingValue = workflowTask.getProperties().get(key);
                            if (existingValue != null) {
                                try {
                                    value = DefaultTypeConverter.INSTANCE.convert(existingValue.getClass(), jsonValue);
                                } catch (TypeConversionException tce) {
                                // TODO: is this the right approach, ignoring exception?
                                // Ignore the exception, revert to using String-value
                                }
                            } else {
                                // Revert to using string-value
                                value = (String) jsonValue;
                            }
                        } else {
                            // Use the value provided by JSON
                            value = (Serializable) jsonValue;
                        }
                    }
                }
            }
            props.put(key, value);
        }
    }
    return props;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) TypeConversionException(org.alfresco.service.cmr.repository.datatype.TypeConversionException) QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 62 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project alfresco-remote-api by Alfresco.

the class WorkflowModelBuilder method buildQNameProperties.

private Map<String, Object> buildQNameProperties(Map<QName, Serializable> properties, Collection<QName> keys, WorkflowTask task) {
    Map<QName, PropertyDefinition> propDefs = task.getDefinition().getMetadata().getProperties();
    Map<String, Object> model = new HashMap<String, Object>();
    for (QName key : keys) {
        Object value = convertValue(properties.get(key));
        String strKey = qNameConverter.mapQNameToName(key);
        PropertyDefinition propDef = propDefs.get(key);
        if ((value == null) && (propDef != null)) {
            value = propDef.getDefaultValue();
        }
        model.put(strKey, value);
    }
    return model;
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Example 63 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project alfresco-remote-api by Alfresco.

the class WorkflowModelBuilder method buildProperties.

private Map<String, Object> buildProperties(WorkflowTask task, Collection<String> propertyFilters) {
    Map<QName, Serializable> properties = task.getProperties();
    Collection<QName> keys;
    if (propertyFilters == null || propertyFilters.size() == 0) {
        TypeDefinition taskType = task.getDefinition().getMetadata();
        Map<QName, PropertyDefinition> propDefs = taskType.getProperties();
        Map<QName, AssociationDefinition> assocDefs = taskType.getAssociations();
        Set<QName> propKeys = properties.keySet();
        keys = new HashSet<QName>(propDefs.size() + assocDefs.size() + propKeys.size());
        keys.addAll(propDefs.keySet());
        keys.addAll(assocDefs.keySet());
        keys.addAll(propKeys);
        keys.add(WorkflowModel.PROP_HIDDEN_TRANSITIONS);
    } else {
        keys = buildQNameKeys(propertyFilters);
    }
    Map<String, Object> result = buildQNameProperties(properties, keys, task);
    // ALF-18092: Special handling for the "hiddenTransitions" property, as it can be an empty string
    if (keys.contains(WorkflowModel.PROP_HIDDEN_TRANSITIONS)) {
        List<?> hiddenTransitions = getHiddenTransitions(properties);
        if (hiddenTransitions != null) {
            result.put(qNameConverter.mapQNameToName(WorkflowModel.PROP_HIDDEN_TRANSITIONS), hiddenTransitions);
        }
    }
    return result;
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition)

Example 64 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project alfresco-remote-api by Alfresco.

the class AbstractPropertiesGet method executeImpl.

/**
 * Override method from DeclarativeWebScript
 */
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    QName classQName = getClassQName(req);
    String[] names = req.getParameterValues(PARAM_NAME);
    String namespacePrefix = req.getParameter(REQ_URL_TEMPL_VAR_NAMESPACE_PREFIX);
    String namespaceURI = null;
    if (namespacePrefix != null) {
        namespaceURI = this.namespaceService.getNamespaceURI(namespacePrefix);
    }
    Map<QName, PropertyDefinition> propMap = null;
    if (classQName == null) {
        if (names != null) {
            propMap = new HashMap<QName, PropertyDefinition>(names.length);
            for (String name : names) {
                QName propQName = QName.createQName(name, namespaceService);
                PropertyDefinition propDef = dictionaryservice.getProperty(propQName);
                if (propDef != null) {
                    propMap.put(propQName, propDef);
                }
            }
        } else {
            Collection<QName> propQNames = dictionaryservice.getAllProperties(null);
            propMap = new HashMap<QName, PropertyDefinition>(propQNames.size());
            for (QName propQName : propQNames) {
                propMap.put(propQName, dictionaryservice.getProperty(propQName));
            }
        }
    } else {
        // Get all the property definitions for the class
        propMap = dictionaryservice.getClass(classQName).getProperties();
    }
    // Filter the properties by URI
    List<PropertyDefinition> props = new ArrayList<PropertyDefinition>(propMap.size());
    for (Map.Entry<QName, PropertyDefinition> entry : propMap.entrySet()) {
        if ((namespaceURI != null && namespaceURI.equals(entry.getKey().getNamespaceURI()) == true) || namespaceURI == null) {
            props.add(entry.getValue());
        }
    }
    // Filter the properties by the allowed types...
    String[] filterTypes = req.getParameterValues(REQ_PARM_ALLOWED_TYPE);
    if (filterTypes != null && filterTypes.length > 0) {
        List<PropertyDefinition> typeFilteredProps = new ArrayList<PropertyDefinition>(props.size());
        for (PropertyDefinition prop : props) {
            for (String type : filterTypes) {
                DataTypeDefinition dtd = prop.getDataType();
                if (dtd.getName().getPrefixString().equals(type)) {
                    typeFilteredProps.add(prop);
                    break;
                }
            }
        }
        // Important to change the props variable to reference the type filtered properties...
        props = typeFilteredProps;
    }
    // Order property definitions by title
    Collections.sort(props, new DictionaryComparators.PropertyDefinitionComparator(dictionaryservice));
    // Pass list of property definitions to template
    Map<String, Object> model = new HashMap<String, Object>();
    model.put(MODEL_PROP_KEY_PROPERTY_DETAILS, props);
    model.put(MODEL_PROP_KEY_MESSAGE_LOOKUP, dictionaryservice);
    return model;
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) Map(java.util.Map) HashMap(java.util.HashMap)

Example 65 with PropertyDefinition

use of org.alfresco.service.cmr.dictionary.PropertyDefinition in project alfresco-remote-api by Alfresco.

the class AbstractSubClassesGet method executeImpl.

/**
 * Override method from DeclarativeWebScript
 */
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    String name = req.getParameter(REQ_URL_TEMPL_VAR_NAME);
    String namespacePrefix = req.getParameter(REQ_URL_TEMPL_VAR_NAMESPACE_PREFIX);
    String recursiveValue = getValidInput(req.getParameter(REQ_URL_TEMPL_IMMEDIATE_SUB_TYPE_CHILDREN));
    boolean recursive = true;
    Map<QName, ClassDefinition> classdef = new HashMap<QName, ClassDefinition>();
    Map<QName, Collection<PropertyDefinition>> propdef = new HashMap<QName, Collection<PropertyDefinition>>();
    Map<QName, Collection<AssociationDefinition>> assocdef = new HashMap<QName, Collection<AssociationDefinition>>();
    Map<String, Object> model = new HashMap<String, Object>();
    String namespaceUri = null;
    Collection<QName> qname = null;
    boolean ignoreCheck = false;
    // validate recursive parameter => can be either true or false or null
    if (recursiveValue == null) {
        recursive = true;
    } else if (recursiveValue.equalsIgnoreCase("true")) {
        recursive = true;
    } else if (recursiveValue.equalsIgnoreCase("false")) {
        recursive = false;
    } else {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Check the value for the parameter recursive=> " + recursiveValue + "  can only be either true or false");
    }
    qname = getQNameCollection(req, recursive);
    // validate the name parameter
    if (name != null) {
        if (isValidModelName(name) == false) {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, "Check the name parameter - " + name + " in the URL");
        }
    }
    // validate the name parameter
    if (namespacePrefix == null && name != null) {
        namespaceUri = namespaceService.getNamespaceURI(getPrefixFromModelName(name));
    }
    if (namespacePrefix != null && name == null) {
        namespaceUri = namespaceService.getNamespaceURI(namespacePrefix);
    }
    if (namespacePrefix == null && name == null) {
        namespaceUri = null;
        ignoreCheck = true;
    }
    if (namespacePrefix != null && name != null) {
        validateClassname(namespacePrefix, name);
        namespaceUri = namespaceService.getNamespaceURI(namespacePrefix);
    }
    for (QName qnameObj : qname) {
        if ((ignoreCheck == true) || (qnameObj.getNamespaceURI().equals(namespaceUri))) {
            classdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj));
            propdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj).getProperties().values());
            assocdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj).getAssociations().values());
        }
    }
    List<ClassDefinition> classDefinitions = new ArrayList<ClassDefinition>(classdef.values());
    Collections.sort(classDefinitions, new DictionaryComparators.ClassDefinitionComparator(dictionaryservice));
    model.put(MODEL_PROP_KEY_CLASS_DEFS, classDefinitions);
    model.put(MODEL_PROP_KEY_PROPERTY_DETAILS, reorderedValues(classDefinitions, propdef));
    model.put(MODEL_PROP_KEY_ASSOCIATION_DETAILS, reorderedValues(classDefinitions, assocdef));
    model.put(MODEL_PROP_KEY_MESSAGE_LOOKUP, this.dictionaryservice);
    return model;
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Collection(java.util.Collection)

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