Search in sources :

Example 1 with PropertyDefinition

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

the class RestVariableHelper method setVariableValueAndType.

/**
 * Sets the variable value with possible conversion to the correct format to be used in the response and sets
 * the type accordingly. If the variables is defined on the {@link TypeDefinition}, the data-type is used. If it's not
 * defined, the type is deducted from the raw variable value.
 */
protected void setVariableValueAndType(Variable variable, Object value, TypeDefinitionContext context) {
    PropertyDefinition propDef = context.getPropertyDefinition(variable.getName());
    if (propDef != null) {
        variable.setValue(getSafePropertyValue(value));
        variable.setType(propDef.getDataType().getName().toPrefixString(namespaceService));
    } else {
        // Not defined as a property, check if it's an association
        AssociationDefinition assocDef = context.getAssociationDefinition(variable.getName());
        if (assocDef == null) {
            // Try to get an association definition through dictionary
            String[] prefixLocalName = variable.getName().split("_");
            if (prefixLocalName.length == 2) {
                QName qName = QName.createQName(prefixLocalName[0], prefixLocalName[1], namespaceService);
                assocDef = dictionaryService.getAssociation(qName);
            }
        }
        if (assocDef != null) {
            // Type of variable is the target class-name
            variable.setType(assocDef.getTargetClass().getName().toPrefixString(namespaceService));
            variable.setValue(getAssociationRepresentation(value, assocDef));
        } else {
            // Variable is not declared as property or association type-def. Use actual raw value as base for conversion.
            variable.setValue(getSafePropertyValue(value));
            variable.setType(extractTypeStringFromValue(value));
        }
    }
}
Also used : AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) QName(org.alfresco.service.namespace.QName) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Example 2 with PropertyDefinition

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

the class AdminWebScriptTest method testResidualProperties.

@Test
public // ALF-21950 We check now if the property belongs to the type of the node
void testResidualProperties() throws Exception {
    NodeBrowserPost nodeBrowserPost = new NodeBrowserPost();
    DictionaryService dictionaryService = mock(DictionaryService.class);
    NodeService nodeService = mock(NodeService.class);
    nodeBrowserPost.setDictionaryService(dictionaryService);
    nodeBrowserPost.setNodeService(nodeService);
    // make own class definition from origin type
    ClassDefinition classDefinition = mock(ClassDefinition.class);
    when(dictionaryService.getClass(any())).thenReturn(classDefinition);
    QName qnameResidualFalse1 = QName.createQName("testResidualProperties", "residualFalse1");
    QName qnameResidualFalse2 = QName.createQName("testResidualProperties", "residualFalse2");
    QName qnameResidualTrue1 = QName.createQName("testResidualProperties", "residualTrue1");
    QName qnameResidualTrue2 = QName.createQName("testResidualProperties", "residualTrue2");
    // Define residual False properties inside of class definition
    // That simulates the belonging of the properties to a given type of a node
    Map<QName, PropertyDefinition> properties = new HashMap<>();
    properties.put(qnameResidualFalse1, new SimplePropertyDefinition(false));
    properties.put(qnameResidualFalse2, new SimplePropertyDefinition(true));
    when(classDefinition.getProperties()).thenReturn(properties);
    when(dictionaryService.getProperty(eq(qnameResidualFalse1))).thenReturn(new SimplePropertyDefinition(false));
    when(dictionaryService.getProperty(eq(qnameResidualFalse2))).thenReturn(new SimplePropertyDefinition(true));
    when(dictionaryService.getProperty(eq(qnameResidualTrue1))).thenReturn(new SimplePropertyDefinition(true));
    when(dictionaryService.getProperty(eq(qnameResidualTrue2))).thenReturn(new SimplePropertyDefinition(false));
    // property found in definition so it is not residual
    String value = "abc";
    NodeBrowserPost.Property property = nodeBrowserPost.new Property(qnameResidualFalse1, value, null);
    assertFalse(property.getResidual());
    // property belongs to an aspect so it is not residual
    property = nodeBrowserPost.new Property(qnameResidualFalse2, value, null);
    assertFalse(property.getResidual());
    // property not found in definition but it is an aspect so it is not residual
    property = nodeBrowserPost.new Property(qnameResidualTrue1, value, null);
    assertFalse(property.getResidual());
    // property not found in definition so it is residual
    property = nodeBrowserPost.new Property(qnameResidualTrue2, value, null);
    assertTrue(property.getResidual());
}
Also used : DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) NodeService(org.alfresco.service.cmr.repository.NodeService) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) BaseWebScriptTest(org.alfresco.repo.web.scripts.BaseWebScriptTest) Test(org.junit.Test)

Example 3 with PropertyDefinition

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

the class NodesImpl method mapToNodeProperties.

public Map<QName, Serializable> mapToNodeProperties(Map<String, Object> props) {
    Map<QName, Serializable> nodeProps = new HashMap<>(props.size());
    for (Entry<String, Object> entry : props.entrySet()) {
        String propName = entry.getKey();
        QName propQName = createQName(propName);
        PropertyDefinition pd = dictionaryService.getProperty(propQName);
        if (pd != null) {
            Serializable value = null;
            if (entry.getValue() != null) {
                if (pd.getDataType().getName().equals(DataTypeDefinition.NODE_REF)) {
                    String nodeRefString = (String) entry.getValue();
                    if (!NodeRef.isNodeRef(nodeRefString)) {
                        value = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, nodeRefString);
                    } else {
                        value = new NodeRef(nodeRefString);
                    }
                } else {
                    value = (Serializable) entry.getValue();
                }
            }
            nodeProps.put(propQName, value);
        } else {
            throw new InvalidArgumentException("Unknown property: " + propName);
        }
    }
    return nodeProps;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Example 4 with PropertyDefinition

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

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

the class PropPatchMethod method patchProperties.

protected void patchProperties(FileInfo nodeInfo, String path) throws WebDAVServerException {
    failedProperty = null;
    for (PropertyAction action : m_propertyActions) {
        if (action.getProperty().isProtected()) {
            failedProperty = action.getProperty();
            break;
        }
    }
    Map<QName, String> deadProperties = null;
    for (PropertyAction propertyAction : m_propertyActions) {
        int statusCode;
        String statusCodeDescription;
        WebDAVProperty property = propertyAction.getProperty();
        if (failedProperty == null) {
            PropertyDefinition propDef = getDAVHelper().getDictionaryService().getProperty(property.createQName());
            boolean deadProperty = propDef == null || (!propDef.getContainerClass().isAspect() && !getDAVHelper().getDictionaryService().isSubClass(getNodeService().getType(nodeInfo.getNodeRef()), propDef.getContainerClass().getName()));
            if (deadProperty && deadProperties == null) {
                deadProperties = loadDeadProperties(nodeInfo.getNodeRef());
            }
            if (PropertyAction.SET == propertyAction.getAction()) {
                if (deadProperty) {
                    deadProperties.put(property.createQName(), property.getValue());
                } else {
                    getNodeService().setProperty(nodeInfo.getNodeRef(), property.createQName(), property.getValue());
                }
            } else if (PropertyAction.REMOVE == propertyAction.getAction()) {
                if (deadProperty) {
                    deadProperties.remove(property.createQName());
                } else {
                    getNodeService().removeProperty(nodeInfo.getNodeRef(), property.createQName());
                }
            } else {
                throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
            }
            statusCode = HttpServletResponse.SC_OK;
            statusCodeDescription = WebDAV.SC_OK_DESC;
        } else if (failedProperty == property) {
            statusCode = HttpServletResponse.SC_FORBIDDEN;
            statusCodeDescription = WebDAV.SC_FORBIDDEN_DESC;
        } else {
            statusCode = WebDAV.WEBDAV_SC_FAILED_DEPENDENCY;
            statusCodeDescription = WebDAV.WEBDAV_SC_FAILED_DEPENDENCY_DESC;
        }
        propertyAction.setResult(statusCode, statusCodeDescription);
    }
    if (deadProperties != null) {
        persistDeadProperties(nodeInfo.getNodeRef(), deadProperties);
    }
}
Also used : QName(org.alfresco.service.namespace.QName) 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