Search in sources :

Example 81 with Folder

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

the class ConsoleTest method testUserCommand.

@Test
public void testUserCommand() {
    final Console console = new Console(securityContext, ConsoleMode.JavaScript, Collections.emptyMap());
    Principal admin = null;
    try {
        assertEquals("Invalid console execution result", "Mode set to 'AdminShell'. Type 'help' to get a list of commands.\r\n", console.runForTest("Console.setMode('" + ConsoleMode.AdminShell.name() + "')"));
        assertEquals("Invalid console execution result", "\r\n", console.runForTest("user list"));
        // create a user
        assertEquals("Invalid console execution result", "User created.\r\n", console.runForTest("user add tester tester@test.de"));
        assertEquals("Invalid console execution result", "User created.\r\n", console.runForTest("user add admin admin@localhost isAdmin"));
        assertEquals("Invalid console execution result", "User created.\r\n", console.runForTest("user add root isAdmin"));
        // check success
        try (final Tx tx = app.tx()) {
            final User user = app.nodeQuery(User.class).andName("tester").getFirst();
            assertNotNull("Invalid console execution result", user);
            assertEquals("Invalid console execution result", "tester", user.getProperty(User.name));
            assertEquals("Invalid console execution result", "tester@test.de", user.getEMail());
            assertEquals("Invalid console execution result", Boolean.FALSE, user.isAdmin());
            tx.success();
        }
        // check list
        assertEquals("Invalid console execution result", "admin, root, tester\r\n", console.runForTest("user list"));
        // delete user
        assertEquals("Invalid console execution result", "User deleted.\r\n", console.runForTest("user delete tester"));
        // check list
        assertEquals("Invalid console execution result", "admin, root\r\n", console.runForTest("user list"));
        // check "root" user
        try (final Tx tx = app.tx()) {
            final User root = app.nodeQuery(User.class).andName("root").getFirst();
            assertNotNull("Invalid console execution result", root);
            assertEquals("Invalid console execution result", "root", root.getProperty(User.name));
            assertEquals("Invalid console execution result", Boolean.TRUE, root.isAdmin());
            tx.success();
        }
        // make check "admin" user
        try (final Tx tx = app.tx()) {
            admin = app.nodeQuery(User.class).andName("admin").getFirst();
            assertNotNull("Invalid console execution result", admin);
            assertEquals("Invalid console execution result", "admin", admin.getProperty(User.name));
            assertEquals("Invalid console execution result", "admin@localhost", admin.getEMail());
            assertEquals("Invalid console execution result", Boolean.TRUE, admin.isAdmin());
            final Folder folder = app.create(Folder.class, "folder");
            folder.setProperties(folder.getSecurityContext(), new PropertyMap(Folder.owner, admin));
            tx.success();
        }
        final String idHash = admin.getUuid().substring(7, 11);
        // delete user without confirmation
        assertEquals("Invalid console execution result", "User 'admin' has owned nodes, please confirm deletion with 'user delete admin " + idHash + "'.\r\n", console.runForTest("user delete admin"));
        // delete user with confirmation
        assertEquals("Invalid console execution result", "User deleted.\r\n", console.runForTest("user delete admin " + idHash));
        // check list
        assertEquals("Invalid console execution result", "root\r\n", console.runForTest("user list"));
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 82 with Folder

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

the class FileHelper method createFolderPath.

/**
 * Create one folder per path item and return the last folder.
 *
 * F.e.: /a/b/c => Folder["name":"a"] --HAS_CHILD--> Folder["name":"b"]
 * --HAS_CHILD--> Folder["name":"c"], returns Folder["name":"c"]
 *
 * @param securityContext
 * @param path
 * @return folder
 * @throws FrameworkException
 */
public static Folder createFolderPath(final SecurityContext securityContext, final String path) throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    if (path == null) {
        return null;
    }
    Folder folder = (Folder) FileHelper.getFileByAbsolutePath(securityContext, path);
    if (folder != null) {
        return folder;
    }
    String[] parts = PathHelper.getParts(path);
    String partialPath = "";
    for (String part : parts) {
        // ignore ".." and "." in paths
        if ("..".equals(part) || ".".equals(part)) {
            continue;
        }
        Folder parent = folder;
        partialPath += PathHelper.PATH_SEP + part;
        folder = (Folder) FileHelper.getFileByAbsolutePath(securityContext, partialPath);
        if (folder == null) {
            folder = app.create(Folder.class, part);
        }
        if (parent != null) {
            folder.setParent(parent);
        }
    }
    return folder;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Folder(org.structr.web.entity.Folder)

Example 83 with Folder

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

the class FileHelper method getChecksums.

/**
 * Calculate checksums that are configured in settings of parent folder.
 *
 * @param file
 * @param fileOnDisk
 * @return
 * @throws IOException
 */
private static PropertyMap getChecksums(final File file, final java.io.File fileOnDisk) throws IOException {
    final PropertyMap propertiesWithChecksums = new PropertyMap();
    Folder parentFolder = file.getParent();
    String checksums = null;
    while (parentFolder != null && checksums == null) {
        checksums = parentFolder.getEnabledChecksums();
        parentFolder = parentFolder.getParent();
    }
    if (checksums == null) {
        checksums = Settings.DefaultChecksums.getValue();
    }
    // New, very fast xxHash default checksum, will always be calculated
    propertiesWithChecksums.put(StructrApp.key(File.class, "checksum"), FileHelper.getChecksum(fileOnDisk));
    if (StringUtils.contains(checksums, "crc32")) {
        propertiesWithChecksums.put(StructrApp.key(File.class, "crc32"), FileHelper.getChecksum(fileOnDisk));
    }
    if (StringUtils.contains(checksums, "md5")) {
        propertiesWithChecksums.put(StructrApp.key(File.class, "md5"), FileHelper.getMD5Checksum(file));
    }
    if (StringUtils.contains(checksums, "sha1")) {
        propertiesWithChecksums.put(StructrApp.key(File.class, "sha1"), FileHelper.getSHA1Checksum(file));
    }
    if (StringUtils.contains(checksums, "sha512")) {
        propertiesWithChecksums.put(StructrApp.key(File.class, "sha512"), FileHelper.getSHA512Checksum(file));
    }
    return propertiesWithChecksums;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 84 with Folder

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

the class CMISNavigationService method recursivelyCollectFolderTree.

// ----- private methods -----
private void recursivelyCollectFolderTree(final List<ObjectInFolderContainer> list, final Folder child, final int maxDepth, final int depth, final Boolean includeAllowableActions) throws FrameworkException {
    if (depth > maxDepth) {
        return;
    }
    final CMISObjectInFolderWrapper wrapper = new CMISObjectInFolderWrapper(includeAllowableActions);
    final ObjectInFolderContainerImpl impl = new ObjectInFolderContainerImpl();
    final List<ObjectInFolderContainer> childContainerList = new LinkedList<>();
    final String pathSegment = child.getName();
    impl.setObject(wrapper.wrapObjectData(wrapper.wrapGraphObject(child), pathSegment));
    impl.setChildren(childContainerList);
    // add wrapped object to current list
    list.add(impl);
    // fetch and sort children
    final List<Folder> children = Iterables.toList(child.getFolders());
    Collections.sort(children, new GraphObjectComparator(AbstractNode.name, false));
    // descend into children
    for (final Folder folderChild : children) {
        recursivelyCollectFolderTree(childContainerList, folderChild, maxDepth, depth + 1, includeAllowableActions);
    }
}
Also used : ObjectInFolderContainerImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ObjectInFolderContainerImpl) CMISObjectInFolderWrapper(org.structr.files.cmis.wrapper.CMISObjectInFolderWrapper) GraphObjectComparator(org.structr.common.GraphObjectComparator) CMISRootFolder(org.structr.files.cmis.repository.CMISRootFolder) Folder(org.structr.web.entity.Folder) ObjectInFolderContainer(org.apache.chemistry.opencmis.commons.data.ObjectInFolderContainer) LinkedList(java.util.LinkedList)

Example 85 with Folder

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

the class CMISNavigationService method recursivelyCollectDescendants.

private void recursivelyCollectDescendants(final List<ObjectInFolderContainer> list, final AbstractFile child, final int maxDepth, final int depth, final Boolean includeAllowableActions) throws FrameworkException {
    if (depth > maxDepth) {
        return;
    }
    final PropertyKey<Folder> parent = StructrApp.key(AbstractFile.class, "parent");
    final PropertyKey<Boolean> hasParent = StructrApp.key(AbstractFile.class, "hasParent");
    final PropertyKey<Boolean> isThumbnail = StructrApp.key(Image.class, "isThumbnail");
    final CMISObjectInFolderWrapper wrapper = new CMISObjectInFolderWrapper(includeAllowableActions);
    final ObjectInFolderContainerImpl impl = new ObjectInFolderContainerImpl();
    final List<ObjectInFolderContainer> childContainerList = new LinkedList<>();
    final String pathSegment = child.getName();
    impl.setObject(wrapper.wrapObjectData(wrapper.wrapGraphObject(child), pathSegment));
    impl.setChildren(childContainerList);
    // add wrapped object to current list
    list.add(impl);
    if (child.getProperty(AbstractNode.type).equals("Folder")) {
        final App app = StructrApp.getInstance();
        // descend into children
        for (final AbstractFile folderChild : app.nodeQuery(AbstractFile.class).sort(AbstractNode.name).and(parent, (Folder) child).and(isThumbnail, false).getAsList()) {
            recursivelyCollectDescendants(childContainerList, folderChild, maxDepth, depth + 1, includeAllowableActions);
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) AbstractFile(org.structr.web.entity.AbstractFile) ObjectInFolderContainerImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ObjectInFolderContainerImpl) CMISObjectInFolderWrapper(org.structr.files.cmis.wrapper.CMISObjectInFolderWrapper) CMISRootFolder(org.structr.files.cmis.repository.CMISRootFolder) Folder(org.structr.web.entity.Folder) ObjectInFolderContainer(org.apache.chemistry.opencmis.commons.data.ObjectInFolderContainer) LinkedList(java.util.LinkedList)

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