Search in sources :

Example 21 with TypeDefinitionWrapper

use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.

the class AlfrescoCmisServiceImpl method createDocument.

@Override
public String createDocument(String repositoryId, final Properties properties, String folderId, final ContentStream contentStream, VersioningState versioningState, final List<String> policies, final Acl addAces, final Acl removeAces, ExtensionsData extension) {
    checkRepositoryId(repositoryId);
    // get the parent folder node ref
    final CMISNodeInfo parentInfo = getOrCreateFolderInfo(folderId, "Parent folder");
    // get name and type
    final String name = connector.getNameProperty(properties, null);
    final String objectTypeId = connector.getObjectTypeIdProperty(properties);
    final TypeDefinitionWrapper type = connector.getTypeForCreate(objectTypeId, BaseTypeId.CMIS_DOCUMENT);
    connector.checkChildObjectType(parentInfo, type.getTypeId());
    DocumentTypeDefinition docType = (DocumentTypeDefinition) type.getTypeDefinition(false);
    if ((docType.getContentStreamAllowed() == ContentStreamAllowed.NOTALLOWED) && (contentStream != null)) {
        throw new CmisConstraintException("This document type does not support content!");
    }
    if ((docType.getContentStreamAllowed() == ContentStreamAllowed.REQUIRED) && (contentStream == null)) {
        throw new CmisConstraintException("This document type does requires content!");
    }
    if (!docType.isVersionable() && (versioningState != VersioningState.NONE)) {
        throw new CmisConstraintException("This document type is not versionable!");
    }
    versioningState = getDocumentDefaultVersioningState(versioningState, type);
    FileInfo fileInfo = connector.getFileFolderService().create(parentInfo.getNodeRef(), name, type.getAlfrescoClass());
    NodeRef nodeRef = fileInfo.getNodeRef();
    connector.setProperties(nodeRef, type, properties, new String[] { PropertyIds.NAME, PropertyIds.OBJECT_TYPE_ID });
    connector.applyPolicies(nodeRef, type, policies);
    connector.applyACL(nodeRef, type, addAces, removeAces);
    // handle content
    if (contentStream != null) {
        // write content
        String mimeType = parseMimeType(contentStream);
        String encoding = getEncoding(contentStream.getStream(), mimeType);
        ContentWriter writer = connector.getFileFolderService().getWriter(nodeRef);
        writer.setMimetype(mimeType);
        writer.setEncoding(encoding);
        writer.putContent(contentStream.getStream());
    }
    connector.extractMetadata(nodeRef);
    // generate "doclib" thumbnail asynchronously
    connector.createThumbnails(nodeRef, Collections.singleton("doclib"));
    connector.applyVersioningState(nodeRef, versioningState);
    String objectId = connector.createObjectId(nodeRef);
    connector.getActivityPoster().postFileFolderAdded(nodeRef);
    return objectId;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) DocumentTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileInfo(org.alfresco.service.cmr.model.FileInfo) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CMISNodeInfo(org.alfresco.opencmis.dictionary.CMISNodeInfo)

Example 22 with TypeDefinitionWrapper

use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.

the class AlfrescoCmisServiceImpl method createItem.

@Override
public String createItem(String repositoryId, Properties properties, String folderId, List<String> policies, Acl addAces, Acl removeAces, ExtensionsData extension) {
    checkRepositoryId(repositoryId);
    // get the parent folder node ref
    final CMISNodeInfo parentInfo = getOrCreateFolderInfo(folderId, "Folder");
    // get name and type
    final String name = connector.getNameProperty(properties, null);
    final String objectTypeId = connector.getObjectTypeIdProperty(properties);
    final TypeDefinitionWrapper type = connector.getTypeForCreate(objectTypeId, BaseTypeId.CMIS_ITEM);
    connector.checkChildObjectType(parentInfo, type.getTypeId());
    /**
     * The above code specifies a folder - so the contents of a folder (as defined by the alfresco model) are
     * ASSOC cm:contains to a TYPE sys:base
     */
    QName assocQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(name));
    Map<QName, Serializable> props = new HashMap<QName, Serializable>(11);
    props.put(ContentModel.PROP_NAME, (Serializable) assocQName.getLocalName());
    ChildAssociationRef newRef = connector.getNodeService().createNode(parentInfo.getNodeRef(), ContentModel.ASSOC_CONTAINS, assocQName, type.getAlfrescoClass(), props);
    NodeRef nodeRef = newRef.getChildRef();
    connector.setProperties(nodeRef, type, properties, new String[] { PropertyIds.NAME, PropertyIds.OBJECT_TYPE_ID });
    connector.getNodeService().setProperty(nodeRef, ContentModel.PROP_NAME, assocQName.getLocalName());
    connector.applyPolicies(nodeRef, type, policies);
    connector.applyACL(nodeRef, type, addAces, removeAces);
    String objectId = connector.createObjectId(nodeRef);
    return objectId;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) QName(org.alfresco.service.namespace.QName) CMISNodeInfo(org.alfresco.opencmis.dictionary.CMISNodeInfo) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 23 with TypeDefinitionWrapper

use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.

the class CMISConnector method addSecondaryTypes.

public void addSecondaryTypes(NodeRef nodeRef, List<String> secondaryTypes) {
    if (secondaryTypes != null && secondaryTypes.size() > 0) {
        for (String secondaryType : secondaryTypes) {
            TypeDefinitionWrapper type = getType(secondaryType);
            if (type == null) {
                throw new CmisInvalidArgumentException("Invalid secondaryType: " + secondaryType);
            }
            nodeService.addAspect(nodeRef, type.getAlfrescoName(), null);
        }
    }
}
Also used : ItemTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.ItemTypeDefinitionWrapper) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) DocumentTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.DocumentTypeDefinitionWrapper) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) PropertyString(org.apache.chemistry.opencmis.commons.data.PropertyString)

Example 24 with TypeDefinitionWrapper

use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper 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)

Example 25 with TypeDefinitionWrapper

use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.

the class CMISConnector method getContentStream.

/**
 * Gets the content from the repository.
 */
public ContentStream getContentStream(CMISNodeInfo info, String streamId, BigInteger offset, BigInteger length) {
    // get the type and check if the object can have content
    TypeDefinitionWrapper type = info.getType();
    checkDocumentTypeForContent(type);
    // looks like a document, now get the content
    ContentStreamImpl result = new ContentStreamImpl();
    result.setFileName(info.getName());
    // if streamId is set, fetch other content
    NodeRef streamNodeRef = info.getNodeRef();
    if ((streamId != null) && (streamId.length() > 0)) {
        CMISNodeInfo streamInfo = createNodeInfo(streamId);
        if (!streamInfo.isVariant(CMISObjectVariant.CURRENT_VERSION)) {
            throw new CmisInvalidArgumentException("Stream id is invalid: " + streamId + ", expected variant " + CMISObjectVariant.CURRENT_VERSION + ", got variant " + streamInfo.getObjectVariant());
        }
        streamNodeRef = streamInfo.getNodeRef();
        type = streamInfo.getType();
        checkDocumentTypeForContent(type);
    }
    // get the stream now
    try {
        ContentReader contentReader = contentService.getReader(streamNodeRef, ContentModel.PROP_CONTENT);
        if (contentReader == null) {
            throw new CmisConstraintException("Document has no content!");
        }
        result.setMimeType(contentReader.getMimetype());
        long contentSize = contentReader.getSize();
        if ((offset == null) && (length == null)) {
            result.setStream(contentReader.getContentInputStream());
            result.setLength(BigInteger.valueOf(contentSize));
            publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), null);
        } else {
            long off = (offset == null ? 0 : offset.longValue());
            long len = (length == null ? contentSize : length.longValue());
            if (off + len > contentSize) {
                len = contentReader.getSize() - off;
            }
            result.setStream(new RangeInputStream(contentReader.getContentInputStream(), off, len));
            result.setLength(BigInteger.valueOf(len));
            publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), off + " - " + len);
        }
    } catch (Exception e) {
        if (e instanceof CmisBaseException) {
            throw (CmisBaseException) e;
        } else {
            StringBuilder msg = new StringBuilder("Failed to retrieve content: " + e.getMessage());
            Throwable cause = e.getCause();
            if (cause != null) {
                // add the cause to the CMIS exception
                msg.append(", ");
                msg.append(cause.getMessage());
            }
            throw new CmisRuntimeException(msg.toString(), e);
        }
    }
    return result;
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) ItemTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.ItemTypeDefinitionWrapper) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) DocumentTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.DocumentTypeDefinitionWrapper) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) ContentReader(org.alfresco.service.cmr.repository.ContentReader) CMISNodeInfo(org.alfresco.opencmis.dictionary.CMISNodeInfo) CmisContentAlreadyExistsException(org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) InvalidAspectException(org.alfresco.service.cmr.dictionary.InvalidAspectException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) CmisStreamNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisStreamNotSupportedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)

Aggregations

TypeDefinitionWrapper (org.alfresco.opencmis.dictionary.TypeDefinitionWrapper)33 CMISNodeInfo (org.alfresco.opencmis.dictionary.CMISNodeInfo)13 DocumentTypeDefinitionWrapper (org.alfresco.opencmis.dictionary.DocumentTypeDefinitionWrapper)13 ItemTypeDefinitionWrapper (org.alfresco.opencmis.dictionary.ItemTypeDefinitionWrapper)13 NodeRef (org.alfresco.service.cmr.repository.NodeRef)12 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)11 ArrayList (java.util.ArrayList)10 PropertyString (org.apache.chemistry.opencmis.commons.data.PropertyString)9 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)9 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)9 Serializable (java.io.Serializable)8 QName (org.alfresco.service.namespace.QName)8 PropertyDefinitionWrapper (org.alfresco.opencmis.dictionary.PropertyDefinitionWrapper)7 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)5 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 List (java.util.List)4 FileInfo (org.alfresco.service.cmr.model.FileInfo)4 ObjectData (org.apache.chemistry.opencmis.commons.data.ObjectData)4