Search in sources :

Example 76 with Folder

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

the class UiTest method testAutoRenameFileWhenMovingToFolderWhereIdenticalFilenameExists.

@Test
public void testAutoRenameFileWhenMovingToFolderWhereIdenticalFilenameExists() {
    Settings.UniquePaths.setValue(Boolean.TRUE);
    Folder folder1 = null;
    Folder folder2 = null;
    File file1 = null;
    File file2 = null;
    try (final Tx tx = app.tx()) {
        folder1 = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/my/test/folder");
        assertNotNull(folder1);
        assertEquals(folder1.getPath(), "/my/test/folder");
        folder2 = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/another/directory");
        assertNotNull(folder2);
        assertEquals(folder2.getPath(), "/another/directory");
        tx.success();
        file1 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"), new NodeAttribute<>(StructrApp.key(File.class, "parent"), folder1));
        assertNotNull(file1);
        assertEquals("Testfolder 1 should have exactly one child", 1, Iterables.count(folder1.getChildren()));
        file2 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"), new NodeAttribute<>(StructrApp.key(File.class, "parent"), folder2));
        assertNotNull(file2);
        assertEquals("Testfolder 2 should have exactly one child", 1, Iterables.count(folder2.getChildren()));
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        folder2.treeRemoveChild(file2);
        folder1.treeAppendChild(file2);
        assertEquals("Testfolder 1 should have exactly two children", 2, Iterables.count(folder1.getChildren()));
        assertEquals("Testfolder 2 should have no children", 0, Iterables.count(folder2.getChildren()));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
    }
    assertNotEquals(file1.getName(), file2.getName());
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 77 with Folder

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

the class UiTest method testFolderPath.

@Test
public void testFolderPath() {
    try (final Tx tx = app.tx()) {
        FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/a/a");
        FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/c/b/a");
        FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/b/a");
        FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/a/b/c");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        Folder a = (Folder) FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(), "/a");
        assertNotNull(a);
        assertEquals(a.getPath(), "/a");
        Folder b = (Folder) FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(), "/a/b");
        assertNotNull(b);
        assertEquals(b.getPath(), "/a/b");
        Folder c = (Folder) FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(), "/a/b/c");
        assertNotNull(c);
        assertEquals(c.getPath(), "/a/b/c");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 78 with Folder

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

the class DirectoryWatchServiceTest method testNestedMountedFolder.

@Test
public void testNestedMountedFolder() {
    final String dirName = "mountServiceTest2";
    final Path base = Paths.get(basePath);
    Path testDir = null;
    try {
        testDir = Files.createDirectory(base.resolve(dirName));
        createTestFile(testDir.resolve(Paths.get("test1.txt")), "test file content 1");
        createTestFile(testDir.resolve(Paths.get("test2.txt")), "test file content 2");
        createTestFile(testDir.resolve(Paths.get("test3.txt")), "test file content 3");
    } catch (IOException ioex) {
        fail("Unable to create test files.");
    }
    // mount directory
    try (final Tx tx = app.tx()) {
        final Folder parent1 = app.create(Folder.class, "parent");
        final Folder parent2 = app.create(Folder.class, new NodeAttribute<>(Folder.name, "parent"), new NodeAttribute<>(StructrApp.key(Folder.class, "parent"), parent1));
        app.create(Folder.class, new NodeAttribute<>(Folder.name, "mounted2"), new NodeAttribute<>(StructrApp.key(Folder.class, "parent"), parent2), new NodeAttribute<>(StructrApp.key(Folder.class, "mountWatchContents"), true), new NodeAttribute<>(StructrApp.key(Folder.class, "mountTarget"), testDir.toString()));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
    // wait for DirectoryWatchService to start and scan
    try {
        Thread.sleep(5000);
    } catch (InterruptedException ex) {
    }
    // verify mount point
    try (final Tx tx = app.tx()) {
        assertNotNull("Folder should have been created by import", app.nodeQuery(Folder.class).and(StructrApp.key(File.class, "path"), "/parent/parent/mounted2").getFirst());
        final File file1 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/parent/parent/mounted2/test1.txt").getFirst();
        final File file2 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/parent/parent/mounted2/test2.txt").getFirst();
        final File file3 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/parent/parent/mounted2/test3.txt").getFirst();
        assertNotNull("Test file should have been created by import", file1);
        assertNotNull("Test file should have been created by import", file2);
        assertNotNull("Test file should have been created by import", file3);
        assertEquals("Imported test file content does not match source", "test file content 1", getContent(file1));
        assertEquals("Imported test file content does not match source", "test file content 2", getContent(file2));
        assertEquals("Imported test file content does not match source", "test file content 3", getContent(file3));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 79 with Folder

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

the class DirectoryWatchServiceTest method testExternalChangesOfMountedDirectory.

@Test
public void testExternalChangesOfMountedDirectory() {
    Path root = null;
    java.io.File file1 = null;
    java.io.File file2 = null;
    java.io.File file3 = null;
    try {
        logger.info("Creating directory to mount..");
        // create some files and folders on disk
        root = Files.createTempDirectory("structr-mount-test");
        root.resolve("parent1/child1/grandchild1").toFile().mkdirs();
        root.resolve("parent2/child1/grandchild1").toFile().mkdirs();
        root.resolve("parent3/child1/grandchild1").toFile().mkdirs();
        logger.info("Creating files to mount..");
        file1 = root.resolve("parent1/child1/grandchild1/test1.txt").toFile();
        file2 = root.resolve("parent2/child1/grandchild1/test2.txt").toFile();
        file3 = root.resolve("parent3/child1/grandchild1/test3.txt").toFile();
        writeFile(file1, "test1 - before change");
        writeFile(file2, "test2 - before change");
        writeFile(file3, "test3 - before change");
        // mount folder
        try (final Tx tx = app.tx()) {
            logger.info("Mounting directory..");
            app.create(Folder.class, new NodeAttribute<>(Folder.name, "mounted3"), new NodeAttribute<>(StructrApp.key(Folder.class, "mountWatchContents"), true), new NodeAttribute<>(StructrApp.key(Folder.class, "mountTarget"), root.toString()), new NodeAttribute<>(StructrApp.key(Folder.class, "mountScanInterval"), 2));
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
        // wait some time
        try {
            Thread.sleep(5000);
        } catch (Throwable t) {
        }
        // check that all files and folders exist
        try (final Tx tx = app.tx()) {
            logger.info("Checking directory..");
            final File check1 = app.nodeQuery(File.class).andName("test1.txt").getFirst();
            final File check2 = app.nodeQuery(File.class).andName("test2.txt").getFirst();
            final File check3 = app.nodeQuery(File.class).andName("test3.txt").getFirst();
            assertEquals("Invalid mount result", "/mounted3/parent1/child1/grandchild1/test1.txt", check1.getPath());
            assertEquals("Invalid mount result", "/mounted3/parent2/child1/grandchild1/test2.txt", check2.getPath());
            assertEquals("Invalid mount result", "/mounted3/parent3/child1/grandchild1/test3.txt", check3.getPath());
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
        // test external changes to files
        writeFile(file2, "test2 - AFTER change");
        // wait some time
        try {
            Thread.sleep(4000);
        } catch (InterruptedException ignore) {
        }
        // check that external changes are present in the file
        try (final Tx tx = app.tx()) {
            logger.info("Checking directory..");
            final File check2 = app.nodeQuery(File.class).andName("test2.txt").getFirst();
            assertEquals("Invalid checksum of externally modified file", FileHelper.getChecksum(file2), check2.getChecksum());
            assertEquals("Invalid content of externally modified file", "test2 - AFTER change", readFile(check2.getFileOnDisk(false)));
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
        // unmount folder
        try (final Tx tx = app.tx()) {
            logger.info("Unmounting directory..");
            final Folder mounted = app.nodeQuery(Folder.class).and(Folder.name, "mounted3").getFirst();
            mounted.setProperty(StructrApp.key(Folder.class, "mountTarget"), null);
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
    } catch (IOException ioex) {
        fail("Unexpected exception.");
    } finally {
        try {
            // cleanup
            Files.walkFileTree(root, new FileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    try {
                        Files.delete(file);
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    try {
                        Files.delete(dir);
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 80 with Folder

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

the class UnarchiveCommand method handleFile.

private void handleFile(final SecurityContext securityContext, final InputStream in, final Folder existingParentFolder, final String entryPath) throws FrameworkException, IOException {
    final PropertyKey<Folder> parentKey = StructrApp.key(AbstractFile.class, "parent");
    final PropertyKey<Boolean> hasParentKey = StructrApp.key(AbstractFile.class, "hasParent");
    final String filePath = (existingParentFolder != null ? existingParentFolder.getPath() : "") + PathHelper.PATH_SEP + PathHelper.clean(entryPath);
    final String name = PathHelper.getName(entryPath);
    final AbstractFile newFile = ImageHelper.isImageType(name) ? ImageHelper.createImage(securityContext, in, null, Image.class, name, false) : FileHelper.createFile(securityContext, in, null, File.class, name);
    final String folderPath = StringUtils.substringBeforeLast(filePath, PathHelper.PATH_SEP);
    final Folder parentFolder = FileHelper.createFolderPath(securityContext, folderPath);
    if (parentFolder != null) {
        final PropertyMap properties = new PropertyMap();
        properties.put(parentKey, parentFolder);
        properties.put(hasParentKey, true);
        newFile.setProperties(securityContext, properties);
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) PropertyMap(org.structr.core.property.PropertyMap) Folder(org.structr.web.entity.Folder) Image(org.structr.web.entity.Image) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) SevenZFile(org.apache.commons.compress.archivers.sevenz.SevenZFile)

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