Search in sources :

Example 6 with CmisConstraintException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException in project structr by structr.

the class CMISObjectService method updateProperties.

@Override
public void updateProperties(final String repositoryId, final Holder<String> objectId, final Holder<String> changeToken, final Properties properties, final ExtensionsData extension) {
    final App app = StructrApp.getInstance();
    final String id = objectId.getValue();
    try (final Tx tx = app.tx()) {
        final AbstractNode obj = app.get(AbstractNode.class, id);
        if (obj != null) {
            final PropertyMap propertyMap = PropertyMap.cmisTypeToJavaType(securityContext, obj.getClass(), properties);
            if (propertyMap != null) {
                obj.setProperties(securityContext, propertyMap);
            }
        } else {
            throw new CmisObjectNotFoundException("Object with ID " + objectId + " does not exist");
        }
        tx.success();
    } catch (FrameworkException fex) {
        throw new CmisConstraintException(fex.getMessage(), fex);
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)

Example 7 with CmisConstraintException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException in project structr by structr.

the class CMISObjectService method moveObject.

@Override
public void moveObject(String repositoryId, final Holder<String> objectId, final String targetFolderId, final String sourceFolderId, final ExtensionsData extension) {
    if (sourceFolderId != null && targetFolderId != null) {
        if (sourceFolderId.equals(targetFolderId)) {
            return;
        }
        final App app = StructrApp.getInstance(securityContext);
        try (final Tx tx = app.tx()) {
            final File file = get(app, File.class, objectId.getValue());
            final Folder parent = file.getParent();
            // check if the file to be moved is filed in the root folder (=> null parent)
            if (CMISInfo.ROOT_FOLDER_ID.equals(sourceFolderId) && parent != null) {
                throw new CmisInvalidArgumentException("Object with ID " + objectId.getValue() + " is not filed in folder with ID " + sourceFolderId);
            }
            // check if the file to be moved is filed in the given source folder
            if (parent != null && !sourceFolderId.equals(parent.getUuid())) {
                throw new CmisInvalidArgumentException("Object with ID " + objectId.getValue() + " is not filed in folder with ID " + sourceFolderId);
            }
            // check if the target folder is the root folder
            if (CMISInfo.ROOT_FOLDER_ID.equals(targetFolderId)) {
                // root folder => null parent
                file.setParent(null);
            } else {
                // get will throw an exception if the folder doesn't exist
                file.setParent(get(app, Folder.class, targetFolderId));
            }
            tx.success();
        } catch (FrameworkException fex) {
            throw new CmisConstraintException(fex.getMessage(), fex);
        }
    } else {
        throw new CmisInvalidArgumentException("Source and target folder must be set");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 8 with CmisConstraintException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException in project structr by structr.

the class CMISObjectService method createDocument.

@Override
public String createDocument(final String repositoryId, final Properties properties, final String folderId, final ContentStream contentStream, final VersioningState versioningState, final List<String> policies, final Acl addAces, final Acl removeAces, final ExtensionsData extension) {
    final App app = StructrApp.getInstance(securityContext);
    File newFile = null;
    String uuid = null;
    try (final Tx tx = app.tx()) {
        final String objectTypeId = getStringValue(properties, PropertyIds.OBJECT_TYPE_ID);
        final String fileName = getStringValue(properties, PropertyIds.NAME);
        final Class type = typeFromObjectTypeId(objectTypeId, BaseTypeId.CMIS_DOCUMENT, File.class);
        // check if type exists
        if (type != null) {
            // check that base type is cmis:folder
            final BaseTypeId baseTypeId = getBaseTypeId(type);
            if (baseTypeId != null && BaseTypeId.CMIS_DOCUMENT.equals(baseTypeId)) {
                final String mimeType = contentStream != null ? contentStream.getMimeType() : null;
                // create file
                newFile = FileHelper.createFile(securityContext, new byte[0], mimeType, type, fileName);
                if (newFile != null) {
                    // find and set parent if it exists
                    if (!CMISInfo.ROOT_FOLDER_ID.equals(folderId)) {
                        final Folder parent = app.get(Folder.class, folderId);
                        if (parent != null) {
                            newFile.setParent(parent);
                        } else {
                            throw new CmisObjectNotFoundException("Folder with ID " + folderId + " does not exist");
                        }
                    }
                    uuid = newFile.getUuid();
                    if (contentStream != null) {
                        final InputStream inputStream = contentStream.getStream();
                        if (inputStream != null) {
                            // copy file and update metadata
                            try (final OutputStream outputStream = newFile.getOutputStream(false, false)) {
                                IOUtils.copy(inputStream, outputStream);
                            }
                            inputStream.close();
                            FileHelper.updateMetadata(newFile);
                        }
                    }
                }
            } else {
                throw new CmisConstraintException("Cannot create cmis:document of type " + objectTypeId);
            }
        } else {
            throw new CmisObjectNotFoundException("Type with ID " + objectTypeId + " does not exist");
        }
        tx.success();
    } catch (Throwable t) {
        throw new CmisRuntimeException("New document could not be created: " + t.getMessage());
    }
    // start indexing after transaction is finished
    if (newFile != null) {
        newFile.notifyUploadCompletion();
    }
    return uuid;
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Tx(org.structr.core.graph.Tx) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) InputStream(java.io.InputStream) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) OutputStream(java.io.OutputStream) BaseTypeId(org.apache.chemistry.opencmis.commons.enums.BaseTypeId) Folder(org.structr.web.entity.Folder) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 9 with CmisConstraintException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException in project structr by structr.

the class CMISObjectService method bulkUpdateProperties.

@Override
public List<BulkUpdateObjectIdAndChangeToken> bulkUpdateProperties(final String repositoryId, final List<BulkUpdateObjectIdAndChangeToken> objectIdsAndChangeTokens, final Properties properties, final List<String> addSecondaryTypeIds, final List<String> removeSecondaryTypeIds, final ExtensionsData extension) {
    final List<BulkUpdateObjectIdAndChangeToken> result = new LinkedList<>();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        for (final BulkUpdateObjectIdAndChangeToken token : objectIdsAndChangeTokens) {
            final AbstractNode obj = app.get(AbstractNode.class, token.getId());
            if (obj != null) {
                final PropertyMap propertyMap = PropertyMap.cmisTypeToJavaType(securityContext, obj.getClass(), properties);
                if (propertyMap != null) {
                    obj.setProperties(securityContext, propertyMap);
                }
                result.add(token);
            }
        }
        tx.success();
    } catch (FrameworkException fex) {
        throw new CmisConstraintException(fex.getMessage(), fex);
    }
    return result;
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) BulkUpdateObjectIdAndChangeToken(org.apache.chemistry.opencmis.commons.data.BulkUpdateObjectIdAndChangeToken) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) LinkedList(java.util.LinkedList)

Example 10 with CmisConstraintException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException in project structr by structr.

the class CMISObjectService method deleteObject.

@Override
public void deleteObject(String repositoryId, String objectId, Boolean allVersions, ExtensionsData extension) {
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        final Principal principal = securityContext.getUser(false);
        final AbstractNode obj = app.get(AbstractNode.class, objectId);
        if (obj != null) {
            if (principal.isGranted(Permission.delete, securityContext)) {
                if (obj.isNode()) {
                    // getSyncNode() returns the node or null
                    app.delete(obj.getSyncNode());
                } else {
                    // getSyncRelationship() return the relationship or null
                    app.delete(obj.getSyncRelationship());
                }
            } else {
                throw new CmisPermissionDeniedException("Cannot delete object with ID " + objectId);
            }
        } else {
            throw new CmisObjectNotFoundException("Object with ID " + objectId + " does not exist");
        }
        tx.success();
    } catch (FrameworkException fex) {
        throw new CmisConstraintException(fex.getMessage(), fex);
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Tx(org.structr.core.graph.Tx) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) Principal(org.structr.core.entity.Principal)

Aggregations

CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)18 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)6 App (org.structr.core.app.App)6 StructrApp (org.structr.core.app.StructrApp)6 Tx (org.structr.core.graph.Tx)6 Document (org.apache.chemistry.opencmis.client.api.Document)5 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)5 File (java.io.File)4 HashMap (java.util.HashMap)4 Folder (org.apache.chemistry.opencmis.client.api.Folder)4 FrameworkException (org.structr.common.error.FrameworkException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)3 AbstractNode (org.structr.core.entity.AbstractNode)3 AbstractFile (org.structr.web.entity.AbstractFile)3 Folder (org.structr.web.entity.Folder)3 InputStream (java.io.InputStream)2 FileChannel (java.nio.channels.FileChannel)2 ObjectId (org.apache.chemistry.opencmis.client.api.ObjectId)2