Search in sources :

Example 16 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class Importer method createFileNode.

private File createFileNode(final String path, final String contentType, final long size, final long checksum, final Class fileClass) throws FrameworkException {
    final PropertyKey<Integer> versionKey = StructrApp.key(File.class, "version");
    final PropertyKey<Folder> parentKey = StructrApp.key(File.class, "parent");
    final PropertyKey<String> contentTypeKey = StructrApp.key(File.class, "contentType");
    final PropertyKey<String> pathKey = StructrApp.key(File.class, "path");
    final PropertyKey<Long> checksumKey = StructrApp.key(File.class, "checksum");
    final PropertyKey<Long> sizeKey = StructrApp.key(File.class, "size");
    return app.create(fileClass != null ? fileClass : File.class, new NodeAttribute(AbstractNode.name, PathHelper.getName(path)), new NodeAttribute(parentKey, FileHelper.createFolderPath(securityContext, PathHelper.getFolderPath(path))), new NodeAttribute(contentTypeKey, contentType), new NodeAttribute(sizeKey, size), new NodeAttribute(checksumKey, checksum), new NodeAttribute(versionKey, 1), new NodeAttribute(AbstractNode.visibleToPublicUsers, publicVisible), new NodeAttribute(AbstractNode.visibleToAuthenticatedUsers, authVisible));
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File)

Example 17 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class DirectFileImportCommand method createFileOrFolder.

private FileVisitResult createFileOrFolder(final SecurityContext ctx, final App app, final Path path, final Path file, final BasicFileAttributes attrs, final String sourcePath, final String targetPath, final Mode mode, final Existing existing, final boolean doIndex) {
    ctx.setDoTransactionNotifications(false);
    final String name = file.getFileName().toString();
    try (final Tx tx = app.tx()) {
        final String relativePath = PathHelper.getRelativeNodePath(path.toString(), file.toString());
        String parentPath = targetPath + PathHelper.getFolderPath(relativePath);
        // fix broken path concatenation
        if (parentPath.startsWith("//")) {
            parentPath = parentPath.substring(1);
        }
        if (attrs.isDirectory()) {
            final Folder newFolder = app.create(Folder.class, new NodeAttribute(Folder.name, name), new NodeAttribute(StructrApp.key(File.class, "parent"), FileHelper.createFolderPath(securityContext, parentPath)));
            folderCount++;
            logger.info("Created folder " + newFolder.getPath());
        } else if (attrs.isRegularFile()) {
            final File existingFile = app.nodeQuery(File.class).and(StructrApp.key(AbstractFile.class, "path"), parentPath + name).getFirst();
            if (existingFile != null) {
                switch(existing) {
                    case SKIP:
                        logger.info("Skipping import of {}, file exists and mode is SKIP.", parentPath + name);
                        return FileVisitResult.CONTINUE;
                    case OVERWRITE:
                        logger.info("Overwriting {}, file exists and mode is OVERWRITE.", parentPath + name);
                        app.delete(existingFile);
                        break;
                    case RENAME:
                        logger.info("Renaming existing file {}, file exists and mode is RENAME.", parentPath + name);
                        existingFile.setProperty(AbstractFile.name, existingFile.getProperty(AbstractFile.name).concat("_").concat(FileHelper.getDateString()));
                        break;
                }
            }
            final String contentType = FileHelper.getContentMimeType(file.toFile(), file.getFileName().toString());
            boolean isImage = (contentType != null && contentType.startsWith("image"));
            boolean isVideo = (contentType != null && contentType.startsWith("video"));
            Class cls = null;
            if (isImage) {
                cls = Image.class;
            } else if (isVideo) {
                cls = SchemaHelper.getEntityClassForRawType("VideoFile");
                if (cls == null) {
                    logger.warn("Unable to create entity of type VideoFile, class is not defined.");
                }
            } else {
                cls = File.class;
            }
            final File newFile = (File) app.create(cls, new NodeAttribute(File.name, name), new NodeAttribute(StructrApp.key(File.class, "parent"), FileHelper.createFolderPath(securityContext, parentPath)), new NodeAttribute(AbstractNode.type, cls.getSimpleName()));
            final java.io.File fileOnDisk = newFile.getFileOnDisk(false);
            final Path fullFolderPath = fileOnDisk.toPath();
            Files.createDirectories(fullFolderPath.getParent());
            switch(mode) {
                case MOVE:
                    Files.move(file, fullFolderPath);
                    break;
                case COPY:
                    Files.copy(file, fullFolderPath);
                    break;
            }
            FileHelper.updateMetadata(newFile);
            if (doIndex) {
                indexer.addToFulltextIndex(newFile);
            }
            fileCount++;
        }
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.debug("File: " + name + ", path: " + sourcePath, ex);
    }
    return FileVisitResult.CONTINUE;
}
Also used : Path(java.nio.file.Path) NodeAttribute(org.structr.core.graph.NodeAttribute) AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) Image(org.structr.web.entity.Image) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 18 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class LogEventFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 4)) {
        final String action = sources[0].toString();
        final String message = sources[1].toString();
        final LogEvent logEvent = StructrApp.getInstance().create(LogEvent.class, new NodeAttribute(LogEvent.actionProperty, action), new NodeAttribute(LogEvent.messageProperty, message), new NodeAttribute(LogEvent.timestampProperty, new Date()));
        switch(sources.length) {
            case 4:
                final String object = sources[3].toString();
                logEvent.setProperties(logEvent.getSecurityContext(), new PropertyMap(LogEvent.objectProperty, object));
            case 3:
                final String subject = sources[2].toString();
                logEvent.setProperties(logEvent.getSecurityContext(), new PropertyMap(LogEvent.subjectProperty, subject));
                break;
        }
        return logEvent;
    } else if (sources.length == 1 && sources[0] instanceof Map) {
        // support javascript objects here
        final Map map = (Map) sources[0];
        final String action = DOMNode.objectToString(map.get("action"));
        final String message = DOMNode.objectToString(map.get("message"));
        final String subject = DOMNode.objectToString(map.get("subject"));
        final String object = DOMNode.objectToString(map.get("object"));
        return StructrApp.getInstance().create(LogEvent.class, new NodeAttribute(LogEvent.actionProperty, action), new NodeAttribute(LogEvent.messageProperty, message), new NodeAttribute(LogEvent.timestampProperty, new Date()), new NodeAttribute(LogEvent.subjectProperty, subject), new NodeAttribute(LogEvent.objectProperty, object));
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyMap(org.structr.core.property.PropertyMap) LogEvent(org.structr.rest.logging.entity.LogEvent) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) Date(java.util.Date)

Example 19 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class FileOrFolder method mkdir.

@Override
public boolean mkdir() {
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        logger.info("mkdir() Folder");
        AbstractFile existing = FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(), newPath);
        if (existing != null) {
            logger.warn("File {} already exists.", newPath);
            return false;
        }
        final Folder parentFolder = (Folder) FileHelper.getFileByAbsolutePath(securityContext, StringUtils.substringBeforeLast(newPath, "/"));
        try {
            Folder newFolder = (Folder) app.command(CreateNodeCommand.class).execute(new NodeAttribute(AbstractNode.type, Folder.class.getSimpleName()), new NodeAttribute(AbstractNode.owner, owner.getStructrUser()), new NodeAttribute(AbstractNode.name, getName()));
            if (parentFolder != null) {
                newFolder.setParent(parentFolder);
            }
        } catch (FrameworkException ex) {
            logger.error("", ex);
            return false;
        }
        tx.success();
        return true;
    } catch (FrameworkException ex) {
        logger.error("", ex);
        return false;
    }
}
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) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder)

Example 20 with NodeAttribute

use of org.structr.core.graph.NodeAttribute 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

NodeAttribute (org.structr.core.graph.NodeAttribute)73 Tx (org.structr.core.graph.Tx)55 FrameworkException (org.structr.common.error.FrameworkException)54 Test (org.junit.Test)45 App (org.structr.core.app.App)23 StructrApp (org.structr.core.app.StructrApp)23 StructrUiTest (org.structr.web.StructrUiTest)21 LinkedList (java.util.LinkedList)19 PropertyKey (org.structr.core.property.PropertyKey)17 SchemaNode (org.structr.core.entity.SchemaNode)14 NodeInterface (org.structr.core.graph.NodeInterface)13 Principal (org.structr.core.entity.Principal)12 PropertyMap (org.structr.core.property.PropertyMap)12 StringProperty (org.structr.core.property.StringProperty)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 File (org.structr.web.entity.File)10 Folder (org.structr.web.entity.Folder)9 AbstractFile (org.structr.web.entity.AbstractFile)8 List (java.util.List)7 StructrTest (org.structr.common.StructrTest)7