Search in sources :

Example 36 with Folder

use of org.structr.web.entity.Folder in project structr by structr.

the class CMISObjectService method deleteTree.

@Override
public FailedToDeleteData deleteTree(final String repositoryId, final String folderId, final Boolean allVersions, final UnfileObject unfileObjects, final Boolean continueOnFailure, final ExtensionsData extension) {
    if (UnfileObject.UNFILE.equals(unfileObjects)) {
        throw new CmisNotSupportedException("Unfiling not supported");
    }
    final App app = StructrApp.getInstance(securityContext);
    final FailedToDeleteDataImpl result = new FailedToDeleteDataImpl();
    result.setIds(new LinkedList<String>());
    try (final Tx tx = app.tx()) {
        final Folder folder = app.get(Folder.class, folderId);
        if (folder != null) {
            recursivelyCheckAndDeleteFiles(app, result, folder, continueOnFailure);
        } else {
            throw new CmisObjectNotFoundException("Folder with ID " + folderId + " does not exist");
        }
        tx.success();
    } catch (final FrameworkException fex) {
        logger.warn("", fex);
    }
    return result;
}
Also used : CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) 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) FailedToDeleteDataImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.FailedToDeleteDataImpl) Folder(org.structr.web.entity.Folder)

Example 37 with Folder

use of org.structr.web.entity.Folder 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 38 with Folder

use of org.structr.web.entity.Folder 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 39 with Folder

use of org.structr.web.entity.Folder in project structr by structr.

the class DirectoryWatchService method startService.

@Override
public void startService() throws Exception {
    try {
        final Path dir = Paths.get(Settings.FilesPath.getValue());
        this.watchService = dir.getFileSystem().newWatchService();
        logger.info("Watch service successfully registered");
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    final PropertyKey<String> mountTargetKey = StructrApp.key(Folder.class, "mountTarget");
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx(false, false, false)) {
        // find all folders with mount targets and mount them
        for (final Folder folder : app.nodeQuery(Folder.class).notBlank(mountTargetKey).getAsList()) {
            mountFolder(folder);
        }
        tx.success();
    }
    running = true;
    this.start();
}
Also used : Path(java.nio.file.Path) StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder)

Example 40 with Folder

use of org.structr.web.entity.Folder in project structr by structr.

the class FileSyncWatchEventListener method getOrCreate.

private AbstractFile getOrCreate(final Folder parentFolder, final Path fullPath, final Path relativePath, final boolean doCreate) throws FrameworkException {
    final PropertyKey<Boolean> isExternalKey = StructrApp.key(AbstractFile.class, "isExternal");
    final PropertyKey<Folder> parentKey = StructrApp.key(AbstractFile.class, "parent");
    final String fileName = relativePath.getFileName().toString();
    final boolean isFile = !Files.isDirectory(fullPath);
    final Class<? extends AbstractFile> type = isFile ? org.structr.web.entity.File.class : Folder.class;
    final App app = StructrApp.getInstance();
    AbstractFile file = app.nodeQuery(type).and(AbstractFile.name, fileName).and(parentKey, parentFolder).getFirst();
    if (file == null && doCreate) {
        file = app.create(type, new NodeAttribute<>(AbstractFile.name, fileName), new NodeAttribute<>(parentKey, parentFolder), new NodeAttribute<>(isExternalKey, true));
    }
    return file;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NodeAttribute(org.structr.core.graph.NodeAttribute) AbstractFile(org.structr.web.entity.AbstractFile) Folder(org.structr.web.entity.Folder)

Aggregations

Folder (org.structr.web.entity.Folder)95 Tx (org.structr.core.graph.Tx)64 FrameworkException (org.structr.common.error.FrameworkException)60 AbstractFile (org.structr.web.entity.AbstractFile)42 App (org.structr.core.app.App)35 StructrApp (org.structr.core.app.StructrApp)35 File (org.structr.web.entity.File)34 Test (org.junit.Test)23 StructrUiTest (org.structr.web.StructrUiTest)21 IOException (java.io.IOException)20 PropertyMap (org.structr.core.property.PropertyMap)16 Path (java.nio.file.Path)14 LinkedList (java.util.LinkedList)10 NodeAttribute (org.structr.core.graph.NodeAttribute)9 InputStream (java.io.InputStream)5 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)5 CMISRootFolder (org.structr.files.cmis.repository.CMISRootFolder)5 User (org.structr.web.entity.User)5 Page (org.structr.web.entity.dom.Page)5 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4