Search in sources :

Example 71 with App

use of org.structr.core.app.App 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)

Example 72 with App

use of org.structr.core.app.App in project structr by structr.

the class UiSyncCommand method doExport.

// ----- private methods -----
private void doExport(final String fileName) throws FrameworkException {
    // collect all nodes etc that belong to the frontend (including files)
    // and export them to the given output file
    final Set<RelationshipInterface> rels = new LinkedHashSet<>();
    final Set<NodeInterface> nodes = new LinkedHashSet<>();
    final Set<String> filePaths = new LinkedHashSet<>();
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        // collect folders that are marked for export
        for (final Folder folder : app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "includeInFrontendExport"), true).getAsList()) {
            collectDataRecursively(app, folder, nodes, rels, filePaths);
        }
        // collect pages (including files, shared components etc.)
        for (final Page page : app.nodeQuery(Page.class).getAsList()) {
            collectDataRecursively(app, page, nodes, rels, filePaths);
        }
        SyncCommand.exportToFile(fileName, nodes, rels, filePaths, true);
        tx.success();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) RelationshipInterface(org.structr.core.graph.RelationshipInterface) Page(org.structr.web.entity.dom.Page) Folder(org.structr.web.entity.Folder) NodeInterface(org.structr.core.graph.NodeInterface)

Example 73 with App

use of org.structr.core.app.App in project structr by structr.

the class UiSyncCommand method doImport.

private void doImport(final String fileName) throws FrameworkException {
    final App app = StructrApp.getInstance();
    final DatabaseService graphDb = app.getDatabaseService();
    SyncCommand.importFromFile(graphDb, securityContext, fileName, true);
    // import done, now the ShadowDocument needs some special care. :(
    try (final Tx tx = app.tx()) {
        final List<ShadowDocument> shadowDocuments = app.nodeQuery(ShadowDocument.class).includeDeletedAndHidden().getAsList();
        if (shadowDocuments.size() > 1) {
            final PropertyKey<List<DOMNode>> elementsKey = StructrApp.key(Page.class, "elements");
            final List<DOMNode> collectiveChildren = new LinkedList<>();
            // sort by node id (higher node ID is newer entity)
            Collections.sort(shadowDocuments, new Comparator<ShadowDocument>() {

                @Override
                public int compare(final ShadowDocument t1, final ShadowDocument t2) {
                    return Long.valueOf(t2.getId()).compareTo(t1.getId());
                }
            });
            final ShadowDocument previousShadowDoc = shadowDocuments.get(0);
            final ShadowDocument newShadowDoc = shadowDocuments.get(1);
            // collect children of both shadow documents
            collectiveChildren.addAll(previousShadowDoc.getProperty(elementsKey));
            collectiveChildren.addAll(newShadowDoc.getProperty(elementsKey));
            // delete old shadow document
            app.delete(previousShadowDoc);
            // add children to new shadow document
            newShadowDoc.setProperties(securityContext, new PropertyMap(elementsKey, collectiveChildren));
        }
        tx.success();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) DatabaseService(org.structr.api.DatabaseService) LinkedList(java.util.LinkedList) PropertyMap(org.structr.core.property.PropertyMap) LinkedList(java.util.LinkedList) List(java.util.List) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 74 with App

use of org.structr.core.app.App in project structr by structr.

the class UserConsoleCommand method handleDelete.

private void handleDelete(final SecurityContext securityContext, final Writable writable, final String name, final String confirm) throws FrameworkException, IOException {
    if (StringUtils.isEmpty(name)) {
        throw new FrameworkException(422, "Missing user name for delete command.");
    }
    final Class<? extends NodeInterface> type = StructrApp.getConfiguration().getNodeEntityClass("User");
    final App app = StructrApp.getInstance(securityContext);
    if (type != null) {
        try (final Tx tx = app.tx()) {
            NodeInterface user = app.nodeQuery(type).andName(name).getFirst();
            if (user == null) {
                user = app.get(type, name);
            }
            if (user != null) {
                if (user.getProperty(Principal.ownedNodes).isEmpty()) {
                    app.delete(user);
                    writable.println("User deleted.");
                } else {
                    final String hash = user.getUuid().substring(7, 11);
                    if (confirm == null || !confirm.equals(hash)) {
                        writable.print("User '");
                        writable.print(name);
                        writable.print("' has owned nodes, please confirm deletion with 'user delete ");
                        writable.print(name);
                        writable.print(" ");
                        writable.print(hash);
                        writable.println("'.");
                    } else {
                        app.delete(user);
                        writable.println("User deleted.");
                    }
                }
            } else {
                throw new FrameworkException(422, "User " + name + " not found.");
            }
            tx.success();
        }
    } else {
        throw new FrameworkException(422, "Cannot delete user, no User class found.");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) NodeInterface(org.structr.core.graph.NodeInterface)

Example 75 with App

use of org.structr.core.app.App in project structr by structr.

the class AbstractTabCompletionProvider method getTabCompletionForUUIDs.

protected List<TabCompletionResult> getTabCompletionForUUIDs(final SecurityContext securityContext, final String token, final String suffix) {
    final List<TabCompletionResult> results = new LinkedList<>();
    if (token.length() >= 3) {
        final App app = StructrApp.getInstance(securityContext);
        try (final Tx tx = app.tx()) {
            final String tenantIdentifier = app.getDatabaseService().getTenantIdentifier();
            final StringBuilder buf = new StringBuilder();
            buf.append("MATCH (n");
            if (tenantIdentifier != null) {
                buf.append(":");
                buf.append(tenantIdentifier);
            }
            buf.append(") WHERE n.id STARTS WITH {part} RETURN n");
            for (final GraphObject obj : app.cypher(buf.toString(), toMap("part", token))) {
                results.add(getCompletion(obj.getUuid(), token, suffix));
            }
            tx.success();
        } catch (FrameworkException ignore) {
        }
    }
    return results;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList)

Aggregations

App (org.structr.core.app.App)296 StructrApp (org.structr.core.app.StructrApp)294 Tx (org.structr.core.graph.Tx)201 FrameworkException (org.structr.common.error.FrameworkException)176 LinkedList (java.util.LinkedList)60 SecurityContext (org.structr.common.SecurityContext)56 PropertyMap (org.structr.core.property.PropertyMap)41 Folder (org.structr.web.entity.Folder)38 GraphObject (org.structr.core.GraphObject)35 Principal (org.structr.core.entity.Principal)31 IOException (java.io.IOException)30 AbstractFile (org.structr.web.entity.AbstractFile)27 AbstractNode (org.structr.core.entity.AbstractNode)26 Test (org.junit.Test)24 NodeAttribute (org.structr.core.graph.NodeAttribute)24 File (org.structr.web.entity.File)23 NodeInterface (org.structr.core.graph.NodeInterface)22 SchemaNode (org.structr.core.entity.SchemaNode)19 PropertyKey (org.structr.core.property.PropertyKey)17 Map (java.util.Map)16