Search in sources :

Example 6 with CmisExtensionElement

use of org.apache.chemistry.opencmis.commons.data.CmisExtensionElement in project alfresco-repository by Alfresco.

the class CMISTest method testMNT10021.

/**
 * CMIS 1.0 aspect properties should provide the following CMIS attributes:
 * propertyDefinitionId, displayName, localName, queryName
 */
@Test
public void testMNT10021() throws Exception {
    final String folderName = "testfolder." + GUID.generate();
    final String docName = "testdoc.txt." + GUID.generate();
    AuthenticationUtil.pushAuthentication();
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    try {
        transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                NodeRef companyHomeNodeRef = repositoryHelper.getCompanyHome();
                FileInfo folderInfo = fileFolderService.create(companyHomeNodeRef, folderName, ContentModel.TYPE_FOLDER);
                nodeService.setProperty(folderInfo.getNodeRef(), ContentModel.PROP_NAME, folderName);
                assertNotNull(folderInfo);
                FileInfo document = fileFolderService.create(folderInfo.getNodeRef(), docName, ContentModel.TYPE_CONTENT);
                assertNotNull(document);
                nodeService.setProperty(document.getNodeRef(), ContentModel.PROP_NAME, docName);
                // lock adds aspects to the document with properties: lockIsDeep, lockOwner, lockType, expiryDate, lockLifetime
                lockService.lock(document.getNodeRef(), LockType.READ_ONLY_LOCK, 0, true);
                return null;
            }
        });
        final ObjectData objectData = withCmisService(new CmisServiceCallback<ObjectData>() {

            @Override
            public ObjectData execute(CmisService cmisService) {
                List<RepositoryInfo> repositories = cmisService.getRepositoryInfos(null);
                assertTrue(repositories.size() > 0);
                RepositoryInfo repo = repositories.get(0);
                String repositoryId = repo.getId();
                ObjectData objectData = cmisService.getObjectByPath(repositoryId, "/" + folderName + "/" + docName, null, true, IncludeRelationships.NONE, null, false, true, null);
                return objectData;
            }
        }, CmisVersion.CMIS_1_0);
        List<CmisExtensionElement> propertyExtensionList = objectData.getProperties().getExtensions();
        assertEquals("propertyExtensionList should be singletonList", propertyExtensionList.size(), 1);
        List<CmisExtensionElement> extensions = propertyExtensionList.iterator().next().getChildren();
        for (CmisExtensionElement extension : extensions) {
            if ("properties".equals(extension.getName())) {
                // check properties extension
                List<CmisExtensionElement> propExtensions = extension.getChildren();
                assertTrue("cmisObject should contain aspect properties", propExtensions.size() > 0);
                for (CmisExtensionElement prop : propExtensions) {
                    Map<String, String> cmisAspectProperty = prop.getAttributes();
                    Set<String> cmisAspectPropertyNames = cmisAspectProperty.keySet();
                    assertTrue("propertyDefinitionId attribute should be present", cmisAspectPropertyNames.contains("propertyDefinitionId"));
                    assertTrue("queryName attribute should be present", cmisAspectPropertyNames.contains("queryName"));
                    // optional values that are present for test document
                    assertTrue("displayName attribute should be present for property of test node", cmisAspectPropertyNames.contains("displayName"));
                    assertTrue("localName attribute should be present for property of test node", cmisAspectPropertyNames.contains("localName"));
                    assertEquals(cmisAspectPropertyNames.size(), 4);
                    // check values
                    for (String aspectPropertyName : cmisAspectPropertyNames) {
                        String value = cmisAspectProperty.get(aspectPropertyName);
                        assertTrue("value for " + aspectPropertyName + " should be present", value != null && value.length() > 0);
                    }
                }
            }
        }
    } catch (CmisConstraintException e) {
        fail(e.toString());
    } finally {
        AuthenticationUtil.popAuthentication();
    }
}
Also used : RepositoryInfo(org.apache.chemistry.opencmis.commons.data.RepositoryInfo) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) ObjectData(org.apache.chemistry.opencmis.commons.data.ObjectData) NodeRef(org.alfresco.service.cmr.repository.NodeRef) CmisExtensionElement(org.apache.chemistry.opencmis.commons.data.CmisExtensionElement) FileInfo(org.alfresco.service.cmr.model.FileInfo) CmisService(org.apache.chemistry.opencmis.commons.server.CmisService) ObjectInFolderList(org.apache.chemistry.opencmis.commons.data.ObjectInFolderList) ArrayList(java.util.ArrayList) ObjectList(org.apache.chemistry.opencmis.commons.data.ObjectList) List(java.util.List) VersionableAspectTest(org.alfresco.repo.version.VersionableAspectTest) Test(org.junit.Test)

Example 7 with CmisExtensionElement

use of org.apache.chemistry.opencmis.commons.data.CmisExtensionElement in project alfresco-repository by Alfresco.

the class CMISTest method testExtensionDataIsReturnedViaCmis1_1.

/**
 * MNT-11876 : Test that Alfresco CMIS 1.1 Implementation is returning aspect and aspect properties as extension data
 * @throws Exception
 */
@Test
public void testExtensionDataIsReturnedViaCmis1_1() throws Exception {
    AuthenticationUtil.pushAuthentication();
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    final String FOLDER = "testExtensionDataIsReturnedViaCmis1_1-" + GUID.generate();
    final String CONTENT = FOLDER + "-file";
    try {
        transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                // create folder
                FileInfo folderInfo = fileFolderService.create(repositoryHelper.getCompanyHome(), FOLDER, ContentModel.TYPE_FOLDER);
                nodeService.setProperty(folderInfo.getNodeRef(), ContentModel.PROP_NAME, FOLDER);
                assertNotNull(folderInfo);
                // create document
                FileInfo document = fileFolderService.create(folderInfo.getNodeRef(), CONTENT, ContentModel.TYPE_CONTENT);
                assertNotNull(document);
                nodeService.setProperty(document.getNodeRef(), ContentModel.PROP_NAME, CONTENT);
                // apply aspect with properties
                Map<QName, Serializable> props = new HashMap<QName, Serializable>();
                props.put(ContentModel.PROP_LATITUDE, Double.valueOf(1.0d));
                props.put(ContentModel.PROP_LONGITUDE, Double.valueOf(1.0d));
                nodeService.addAspect(document.getNodeRef(), ContentModel.ASPECT_GEOGRAPHIC, props);
                return null;
            }
        });
        final ObjectData objectData = withCmisService(new CmisServiceCallback<ObjectData>() {

            @Override
            public ObjectData execute(CmisService cmisService) {
                List<RepositoryInfo> repositories = cmisService.getRepositoryInfos(null);
                assertTrue(repositories.size() > 0);
                RepositoryInfo repo = repositories.get(0);
                String repositoryId = repo.getId();
                // get object data
                ObjectData objectData = cmisService.getObjectByPath(repositoryId, "/" + FOLDER + "/" + CONTENT, null, true, IncludeRelationships.NONE, null, false, true, null);
                return objectData;
            }
        }, CmisVersion.CMIS_1_1);
        // get extension data from object properties
        List<CmisExtensionElement> extensions = objectData.getProperties().getExtensions().iterator().next().getChildren();
        Set<String> appliedAspects = new HashSet<String>();
        Set<String> aspectProperties = new HashSet<String>();
        for (CmisExtensionElement extension : extensions) {
            if (CMISConnector.PROPERTIES.equals(extension.getName())) {
                // check properties extension
                List<CmisExtensionElement> propExtensions = extension.getChildren();
                assertTrue("cmisObject should contain aspect properties", propExtensions.size() > 0);
                for (CmisExtensionElement prop : propExtensions) {
                    Map<String, String> cmisAspectProperty = prop.getAttributes();
                    Set<String> cmisAspectPropertyNames = cmisAspectProperty.keySet();
                    assertTrue("propertyDefinitionId attribute should be present", cmisAspectPropertyNames.contains("propertyDefinitionId"));
                    aspectProperties.add(cmisAspectProperty.get("propertyDefinitionId"));
                }
            } else if (CMISConnector.APPLIED_ASPECTS.equals(extension.getName())) {
                appliedAspects.add(extension.getValue());
            }
        }
        // extension data should contain applied aspects and aspect properties
        assertTrue("Extensions should contain " + ContentModel.ASPECT_GEOGRAPHIC, appliedAspects.contains("P:cm:geographic"));
        assertTrue("Extensions should contain " + ContentModel.PROP_LATITUDE, aspectProperties.contains("cm:latitude"));
        assertTrue("Extensions should contain " + ContentModel.PROP_LONGITUDE, aspectProperties.contains("cm:longitude"));
    } finally {
        AuthenticationUtil.popAuthentication();
    }
}
Also used : Serializable(java.io.Serializable) RepositoryInfo(org.apache.chemistry.opencmis.commons.data.RepositoryInfo) QName(org.alfresco.service.namespace.QName) ObjectData(org.apache.chemistry.opencmis.commons.data.ObjectData) CmisExtensionElement(org.apache.chemistry.opencmis.commons.data.CmisExtensionElement) FileInfo(org.alfresco.service.cmr.model.FileInfo) CmisService(org.apache.chemistry.opencmis.commons.server.CmisService) ObjectInFolderList(org.apache.chemistry.opencmis.commons.data.ObjectInFolderList) ArrayList(java.util.ArrayList) ObjectList(org.apache.chemistry.opencmis.commons.data.ObjectList) List(java.util.List) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet) VersionableAspectTest(org.alfresco.repo.version.VersionableAspectTest) Test(org.junit.Test)

Example 8 with CmisExtensionElement

use of org.apache.chemistry.opencmis.commons.data.CmisExtensionElement in project alfresco-remote-api by Alfresco.

the class RepoService method getProperties.

/*
     * Get (CMIS) node properties
     */
private Properties getProperties(NodeRef nodeRef) {
    CMISNodeInfoImpl nodeInfo = cmisConnector.createNodeInfo(nodeRef);
    final Properties properties = cmisConnector.getNodeProperties(nodeInfo, null);
    // fake the title property, which CMIS doesn't give us
    String title = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_TITLE);
    final PropertyStringImpl titleProp = new PropertyStringImpl(ContentModel.PROP_TITLE.toString(), title);
    Properties wrapProperties = new Properties() {

        @Override
        public List<CmisExtensionElement> getExtensions() {
            return properties.getExtensions();
        }

        @Override
        public void setExtensions(List<CmisExtensionElement> extensions) {
            properties.setExtensions(extensions);
        }

        @Override
        public Map<String, PropertyData<?>> getProperties() {
            Map<String, PropertyData<?>> updatedProperties = new HashMap<String, PropertyData<?>>(properties.getProperties());
            updatedProperties.put(titleProp.getId(), titleProp);
            return updatedProperties;
        }

        @Override
        public List<PropertyData<?>> getPropertyList() {
            List<PropertyData<?>> propertyList = new ArrayList<PropertyData<?>>(properties.getPropertyList());
            propertyList.add(titleProp);
            return propertyList;
        }
    };
    return wrapProperties;
}
Also used : CmisExtensionElement(org.apache.chemistry.opencmis.commons.data.CmisExtensionElement) PropertyData(org.apache.chemistry.opencmis.commons.data.PropertyData) CMISNodeInfoImpl(org.alfresco.opencmis.CMISNodeInfoImpl) PropertyStringImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyStringImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Properties(org.apache.chemistry.opencmis.commons.data.Properties)

Example 9 with CmisExtensionElement

use of org.apache.chemistry.opencmis.commons.data.CmisExtensionElement in project alfresco-repository by Alfresco.

the class CMISConnector method setProperties.

/**
 * Sets property values.
 */
@SuppressWarnings({ "rawtypes" })
public void setProperties(NodeRef nodeRef, TypeDefinitionWrapper type, Properties properties, String... exclude) {
    if (properties == null) {
        return;
    }
    Map<String, PropertyData<?>> incomingPropsMap = properties.getProperties();
    if (incomingPropsMap == null) {
        return;
    }
    // extract property data into an easier to use form
    Map<String, Pair<TypeDefinitionWrapper, Serializable>> propsMap = new HashMap<String, Pair<TypeDefinitionWrapper, Serializable>>();
    for (String propertyId : incomingPropsMap.keySet()) {
        PropertyData<?> property = incomingPropsMap.get(propertyId);
        PropertyDefinitionWrapper propDef = type.getPropertyById(property.getId());
        if (propDef == null) {
            propDef = getOpenCMISDictionaryService().findProperty(propertyId);
            if (propDef == null) {
                throw new CmisInvalidArgumentException("Property " + property.getId() + " is unknown!");
            }
        }
        Boolean isOnWorkingCopy = checkOutCheckInService.isWorkingCopy(nodeRef);
        Updatability updatability = propDef.getPropertyDefinition().getUpdatability();
        if (!isUpdatable(updatability, isOnWorkingCopy)) {
            throw new CmisInvalidArgumentException("Property " + propertyId + " is read-only!");
        }
        TypeDefinitionWrapper propType = propDef.getOwningType();
        Serializable value = getValue(property, propDef.getPropertyDefinition().getCardinality() == Cardinality.MULTI);
        Pair<TypeDefinitionWrapper, Serializable> pair = new Pair<TypeDefinitionWrapper, Serializable>(propType, value);
        propsMap.put(propertyId, pair);
    }
    // Need to do deal with secondary types first
    Pair<TypeDefinitionWrapper, Serializable> pair = propsMap.get(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
    Serializable secondaryTypesProperty = (pair != null ? pair.getSecond() : null);
    if (secondaryTypesProperty != null) {
        if (!(secondaryTypesProperty instanceof List)) {
            throw new CmisInvalidArgumentException("Secondary types must be a list!");
        }
        List secondaryTypes = (List) secondaryTypesProperty;
        if (secondaryTypes != null && secondaryTypes.size() > 0) {
            // add/remove secondary types/aspects
            processSecondaryTypes(nodeRef, secondaryTypes, propsMap);
        }
    }
    for (String propertyId : propsMap.keySet()) {
        if (propertyId.equals(PropertyIds.SECONDARY_OBJECT_TYPE_IDS)) {
            // already handled above
            continue;
        }
        pair = propsMap.get(propertyId);
        TypeDefinitionWrapper propType = pair.getFirst();
        Serializable value = pair.getSecond();
        if (Arrays.binarySearch(exclude, propertyId) < 0) {
            setProperty(nodeRef, propType, propertyId, value);
        }
    }
    List<CmisExtensionElement> extensions = properties.getExtensions();
    if (extensions != null) {
        boolean isNameChanging = properties.getProperties().containsKey(PropertyIds.NAME);
        for (CmisExtensionElement extension : extensions) {
            if (ALFRESCO_EXTENSION_NAMESPACE.equals(extension.getNamespace()) && SET_ASPECTS.equals(extension.getName())) {
                setAspectProperties(nodeRef, isNameChanging, extension);
                break;
            }
        }
    }
}
Also used : Serializable(java.io.Serializable) PropertyData(org.apache.chemistry.opencmis.commons.data.PropertyData) AbstractPropertyData(org.apache.chemistry.opencmis.commons.impl.dataobjects.AbstractPropertyData) HashMap(java.util.HashMap) ItemTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.ItemTypeDefinitionWrapper) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) DocumentTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.DocumentTypeDefinitionWrapper) PropertyString(org.apache.chemistry.opencmis.commons.data.PropertyString) Updatability(org.apache.chemistry.opencmis.commons.enums.Updatability) CmisExtensionElement(org.apache.chemistry.opencmis.commons.data.CmisExtensionElement) PropertyDefinitionWrapper(org.alfresco.opencmis.dictionary.PropertyDefinitionWrapper) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) Collections.singletonList(java.util.Collections.singletonList) ObjectList(org.apache.chemistry.opencmis.commons.data.ObjectList) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair)

Example 10 with CmisExtensionElement

use of org.apache.chemistry.opencmis.commons.data.CmisExtensionElement in project alfresco-repository by Alfresco.

the class CMISConnector method getAspectExtensions.

/**
 * Builds aspect extension.
 */
public List<CmisExtensionElement> getAspectExtensions(CMISNodeInfo info, String filter, Set<String> alreadySetProperties) {
    List<CmisExtensionElement> extensions = new ArrayList<CmisExtensionElement>();
    Set<String> propertyIds = new HashSet<String>(alreadySetProperties);
    List<CmisExtensionElement> propertyExtensionList = new ArrayList<CmisExtensionElement>();
    Set<String> filterSet = splitFilter(filter);
    Set<QName> aspects = info.getNodeAspects();
    for (QName aspect : aspects) {
        TypeDefinitionWrapper aspectType = getOpenCMISDictionaryService().findNodeType(aspect);
        if (aspectType == null) {
            continue;
        }
        AspectDefinition aspectDefinition = dictionaryService.getAspect(aspect);
        Map<QName, org.alfresco.service.cmr.dictionary.PropertyDefinition> aspectProperties = aspectDefinition.getProperties();
        extensions.add(new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, APPLIED_ASPECTS, null, aspectType.getTypeId()));
        for (PropertyDefinitionWrapper propDef : aspectType.getProperties()) {
            boolean addPropertyToExtensionList = getRequestCmisVersion().equals(CmisVersion.CMIS_1_1) && aspectProperties.keySet().contains(propDef.getAlfrescoName());
            // MNT-11876 : add property to extension even if it has been returned (CMIS 1.1)
            if (propertyIds.contains(propDef.getPropertyId()) && !addPropertyToExtensionList) {
                // skip properties that have already been added
                continue;
            }
            if ((filterSet != null) && (!filterSet.contains(propDef.getPropertyDefinition().getQueryName()))) {
                // skip properties that are not in the filter
                continue;
            }
            Serializable value = propDef.getPropertyAccessor().getValue(info);
            propertyExtensionList.add(createAspectPropertyExtension(propDef.getPropertyDefinition(), value));
            // mark property as 'added'
            propertyIds.add(propDef.getPropertyId());
        }
    }
    if (!propertyExtensionList.isEmpty()) {
        CmisExtensionElementImpl propertiesExtension = new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, "properties", null, propertyExtensionList);
        extensions.addAll(Collections.singletonList(propertiesExtension));
    }
    return extensions;
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ItemTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.ItemTypeDefinitionWrapper) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) DocumentTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.DocumentTypeDefinitionWrapper) ArrayList(java.util.ArrayList) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) PropertyString(org.apache.chemistry.opencmis.commons.data.PropertyString) PropertyDefinition(org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition) CmisExtensionElementImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.CmisExtensionElementImpl) CmisExtensionElement(org.apache.chemistry.opencmis.commons.data.CmisExtensionElement) PropertyDefinitionWrapper(org.alfresco.opencmis.dictionary.PropertyDefinitionWrapper) HashSet(java.util.HashSet)

Aggregations

CmisExtensionElement (org.apache.chemistry.opencmis.commons.data.CmisExtensionElement)11 ArrayList (java.util.ArrayList)10 List (java.util.List)8 HashMap (java.util.HashMap)7 ObjectList (org.apache.chemistry.opencmis.commons.data.ObjectList)7 Serializable (java.io.Serializable)6 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)5 QName (org.alfresco.service.namespace.QName)5 CmisService (org.apache.chemistry.opencmis.commons.server.CmisService)5 Test (org.junit.Test)5 Collections.singletonList (java.util.Collections.singletonList)4 Map (java.util.Map)4 FileInfo (org.alfresco.service.cmr.model.FileInfo)4 RepositoryInfo (org.apache.chemistry.opencmis.commons.data.RepositoryInfo)4 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)4 CmisExtensionElementImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.CmisExtensionElementImpl)4 HashSet (java.util.HashSet)3 MimetypeMap (org.alfresco.repo.content.MimetypeMap)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 ObjectData (org.apache.chemistry.opencmis.commons.data.ObjectData)3