Search in sources :

Example 31 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class VersionManagerTest method testDeleteRevisions_withMissingRevisionFile.

/**
 * The test create an original file and 5 versions. It manually
 * delete the physical back up file. We delete the versions
 * of the orginal, 1 and 2. At the end, there is only version 4
 * and 5.
 *
 * @throws IOException
 */
@Test
public void testDeleteRevisions_withMissingRevisionFile() throws IOException {
    OlatRootFolderImpl rootTest = new OlatRootFolderImpl("/ver-" + UUID.randomUUID(), null);
    String filename = getRandomName();
    VFSLeaf file = rootTest.createChildLeaf(filename);
    OutputStream out = file.getOutputStream(false);
    InputStream in = new ByteArrayInputStream("Hello original".getBytes());
    int byteCopied = IOUtils.copy(in, out);
    IOUtils.closeQuietly(in);
    assertFalse(byteCopied == 0);
    assertTrue(file instanceof Versionable);
    // save a first version
    Versionable versionedFile = (Versionable) file;
    InputStream in1 = new ByteArrayInputStream("Hello version 1".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 1", in1);
    IOUtils.closeQuietly(in1);
    // save a second version
    InputStream in2 = new ByteArrayInputStream("Hello version 2".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 2", in2);
    IOUtils.closeQuietly(in2);
    // save a third version
    InputStream in3 = new ByteArrayInputStream("Hello version 3".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 3", in3);
    IOUtils.closeQuietly(in3);
    // save a fourth version
    InputStream in4 = new ByteArrayInputStream("Hello version 4".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 4", in4);
    IOUtils.closeQuietly(in4);
    // save a fourth version
    InputStream in5 = new ByteArrayInputStream("Hello version 5".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 5", in5);
    IOUtils.closeQuietly(in5);
    // delete a specific
    VFSRevision rev3 = versionedFile.getVersions().getRevisions().get(3);
    RevisionFileImpl toDeleteVersionImpl = (RevisionFileImpl) rev3;
    VFSContainer versionContainerAlt = versionManager.getCanonicalVersionFolder(rootTest, false);
    VFSItem itemToDelete = versionContainerAlt.resolve(toDeleteVersionImpl.getFilename());
    itemToDelete.deleteSilently();
    // delete revisions
    List<VFSRevision> toDelete = new ArrayList<>(versionedFile.getVersions().getRevisions().subList(0, 3));
    versionManager.deleteRevisions(versionedFile, toDelete);
    // check number of versions
    VFSItem reloadFile = rootTest.resolve(filename);
    assertTrue(reloadFile instanceof Versionable);
    Versionable reloadedVersionedFile = (Versionable) reloadFile;
    List<VFSRevision> revisions = reloadedVersionedFile.getVersions().getRevisions();
    Assert.assertNotNull(revisions);
    Assert.assertEquals(1, revisions.size());
    VFSRevision revision = revisions.get(0);
    Assert.assertEquals("Version 4", revision.getComment());
    Assert.assertEquals("Version 5", reloadedVersionedFile.getVersions().getComment());
    // check if there is only one backup file
    VFSContainer versionContainer = versionManager.getCanonicalVersionFolder(file.getParentContainer(), false);
    Assert.assertNotNull(versionContainer);
    List<VFSItem> items = versionContainer.getItems(new SystemItemFilter());
    Assert.assertNotNull(items);
    Assert.assertEquals(2, items.size());
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 32 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class VersionManagerTest method testDeleteRevisions_withSameFile.

/**
 * The test create an original file and 3 revisions with exactly
 * the same content. We delete the original and the first version.
 * We check that version 2 and 3 survives and that the file exists.
 *
 * @throws IOException
 */
@Test
public void testDeleteRevisions_withSameFile() throws IOException {
    OlatRootFolderImpl rootTest = new OlatRootFolderImpl("/ver-" + UUID.randomUUID(), null);
    String filename = getRandomName();
    VFSLeaf file = rootTest.createChildLeaf(filename);
    OutputStream out = file.getOutputStream(false);
    InputStream in = VersionManagerTest.class.getResourceAsStream("test.txt");
    int byteCopied = IOUtils.copy(in, out);
    IOUtils.closeQuietly(in);
    assertFalse(byteCopied == 0);
    assertTrue(file instanceof Versionable);
    // save a first version
    Versionable versionedFile = (Versionable) file;
    InputStream in1 = VersionManagerTest.class.getResourceAsStream("test.txt");
    versionedFile.getVersions().addVersion(id2, "Version 1", in1);
    IOUtils.closeQuietly(in1);
    // save a second version
    InputStream in2 = VersionManagerTest.class.getResourceAsStream("test.txt");
    versionedFile.getVersions().addVersion(id2, "Version 2", in2);
    IOUtils.closeQuietly(in2);
    // save a third version
    InputStream in3 = VersionManagerTest.class.getResourceAsStream("test.txt");
    versionedFile.getVersions().addVersion(id2, "Version 3", in2);
    IOUtils.closeQuietly(in3);
    // delete revisions
    versionManager.deleteRevisions(versionedFile, versionedFile.getVersions().getRevisions().subList(0, 2));
    // check number of versions
    VFSItem reloadFile = rootTest.resolve(filename);
    assertTrue(reloadFile instanceof Versionable);
    Versionable reloadedVersionedFile = (Versionable) reloadFile;
    List<VFSRevision> revisions = reloadedVersionedFile.getVersions().getRevisions();
    Assert.assertNotNull(revisions);
    Assert.assertEquals(1, revisions.size());
    // check surviving versions
    Assert.assertEquals("Version 2", revisions.get(0).getComment());
    Assert.assertEquals("Version 3", reloadedVersionedFile.getVersions().getComment());
    // check that the last backup file exists
    RevisionFileImpl revision2 = (RevisionFileImpl) revisions.get(0);
    VFSLeaf revision2File = revision2.getFile();
    Assert.assertNotNull(revision2File);
    Assert.assertTrue(revision2File.exists());
    // check if there is only one backup file
    VFSContainer versionContainer = versionManager.getCanonicalVersionFolder(file.getParentContainer(), false);
    Assert.assertNotNull(versionContainer);
    List<VFSItem> items = versionContainer.getItems(new SystemItemFilter());
    Assert.assertNotNull(items);
    Assert.assertEquals(2, items.size());
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) Test(org.junit.Test)

Example 33 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class LinkFileCombiCalloutController method doOpenFileChanger.

public void doOpenFileChanger(UserRequest ureq, String tool) {
    // close callout and open appropriate file changer controller
    calloutCtr.deactivate();
    Controller toolCtr = null;
    if (tool.equals("chooseLink")) {
        VFSItemFilter filter = new SystemItemFilter();
        FileChooserController fileChooserCtr = FileChooserUIFactory.createFileChooserController(ureq, getWindowControl(), baseContainer, filter, true);
        fileChooserCtr.setShowTitle(true);
        fileChooserCtr.selectPath(relFilePath);
        toolCtr = fileChooserCtr;
    }
    if (tool.equals("createLink")) {
        String folderPath = null;
        if (StringHelper.containsNonWhitespace(relFilePath)) {
            // remove file name from relFilePath to represent directory path
            folderPath = relFilePath.substring(0, relFilePath.lastIndexOf("/"));
        }
        toolCtr = new FileCreatorController(ureq, getWindowControl(), baseContainer, folderPath);
    }
    if (tool.equals("uploadLink")) {
        long quotaLeftKB = VFSManager.getQuotaLeftKB(baseContainer);
        String folderPath = null;
        if (StringHelper.containsNonWhitespace(relFilePath)) {
            // remove file name from relFilePath to represent directory path
            folderPath = relFilePath.substring(0, relFilePath.lastIndexOf("/"));
        }
        toolCtr = new FileUploadController(getWindowControl(), baseContainer, ureq, quotaLeftKB, quotaLeftKB, null, false, true, false, false, true, true, folderPath);
    }
    displayModal(toolCtr);
}
Also used : VFSItemFilter(org.olat.core.util.vfs.filters.VFSItemFilter) FileUploadController(org.olat.core.commons.modules.bc.FileUploadController) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) LayoutMain3ColsPreviewController(org.olat.core.commons.fullWebApp.LayoutMain3ColsPreviewController) HTMLEditorController(org.olat.core.commons.editor.htmleditor.HTMLEditorController) Controller(org.olat.core.gui.control.Controller) BasicController(org.olat.core.gui.control.controller.BasicController) SinglePageController(org.olat.core.commons.modules.singlepage.SinglePageController) CloseableCalloutWindowController(org.olat.core.gui.control.generic.closablewrapper.CloseableCalloutWindowController) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) FileUploadController(org.olat.core.commons.modules.bc.FileUploadController)

Example 34 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class GTACourseNode method archiveNodeData.

@Override
public boolean archiveNodeData(Locale locale, ICourse course, ArchiveOptions options, ZipOutputStream exportStream, String charset) {
    final GTAManager gtaManager = CoreSpringFactory.getImpl(GTAManager.class);
    final ModuleConfiguration config = getModuleConfiguration();
    String prefix;
    if (GTAType.group.name().equals(config.getStringValue(GTACourseNode.GTASK_TYPE))) {
        prefix = "grouptask_";
    } else {
        prefix = "ita_";
    }
    String dirName = prefix + StringHelper.transformDisplayNameToFileSystemName(getShortName()) + "_" + Formatter.formatDatetimeFilesystemSave(new Date(System.currentTimeMillis()));
    TaskList taskList = gtaManager.getTaskList(course.getCourseEnvironment().getCourseGroupManager().getCourseEntry(), this);
    // save assessment datas
    List<Identity> users = null;
    if (config.getBooleanSafe(GTASK_GRADING)) {
        users = ScoreAccountingHelper.loadUsers(course.getCourseEnvironment(), options);
        String courseTitle = course.getCourseTitle();
        String fileName = ExportUtil.createFileNameWithTimeStamp(courseTitle, "xlsx");
        List<AssessableCourseNode> nodes = Collections.<AssessableCourseNode>singletonList(this);
        try (OutputStream out = new ShieldOutputStream(exportStream)) {
            exportStream.putNextEntry(new ZipEntry(dirName + "/" + fileName));
            ScoreAccountingHelper.createCourseResultsOverviewXMLTable(users, nodes, course, locale, out);
            exportStream.closeEntry();
        } catch (Exception e) {
            log.error("", e);
        }
    }
    // copy tasks
    if (taskList != null) {
        if (GTAType.group.name().equals(config.getStringValue(GTACourseNode.GTASK_TYPE))) {
            List<BusinessGroup> selectedGroups;
            if (options != null && options.getGroup() != null) {
                selectedGroups = Collections.singletonList(options.getGroup());
            } else {
                selectedGroups = gtaManager.getBusinessGroups(this);
            }
            for (BusinessGroup businessGroup : selectedGroups) {
                archiveNodeData(course, businessGroup, taskList, dirName, exportStream);
            }
        } else {
            if (users == null) {
                users = ScoreAccountingHelper.loadUsers(course.getCourseEnvironment(), options);
            }
            Set<Identity> uniqueUsers = new HashSet<>(users);
            for (Identity user : uniqueUsers) {
                archiveNodeData(course, user, taskList, dirName, exportStream);
            }
        }
    }
    // copy solutions
    if (config.getBooleanSafe(GTACourseNode.GTASK_SAMPLE_SOLUTION)) {
        VFSContainer solutions = gtaManager.getSolutionsContainer(course.getCourseEnvironment(), this);
        if (solutions.exists()) {
            String solutionDirName = dirName + "/solutions";
            for (VFSItem solution : solutions.getItems(new SystemItemFilter())) {
                ZipUtil.addToZip(solution, solutionDirName, exportStream);
            }
        }
    }
    return true;
}
Also used : ModuleConfiguration(org.olat.modules.ModuleConfiguration) BusinessGroup(org.olat.group.BusinessGroup) TaskList(org.olat.course.nodes.gta.TaskList) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) Date(java.util.Date) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) GTAManager(org.olat.course.nodes.gta.GTAManager) Identity(org.olat.core.id.Identity) HashSet(java.util.HashSet)

Example 35 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class VersionsFileManager method countDirectories.

private int countDirectories(VFSContainer container) {
    // itself
    int count = 1;
    List<VFSItem> children = container.getItems(new SystemItemFilter());
    for (VFSItem child : children) {
        if (child instanceof VFSContainer) {
            count += countDirectories((VFSContainer) child);
        }
    }
    return count;
}
Also used : VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter)

Aggregations

SystemItemFilter (org.olat.core.util.vfs.filters.SystemItemFilter)36 VFSItem (org.olat.core.util.vfs.VFSItem)34 VFSContainer (org.olat.core.util.vfs.VFSContainer)30 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)24 Date (java.util.Date)10 OutputStream (java.io.OutputStream)8 PathSegment (javax.ws.rs.core.PathSegment)8 UriBuilder (javax.ws.rs.core.UriBuilder)8 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 Response (javax.ws.rs.core.Response)6 Test (org.junit.Test)6 RepositoryEntry (org.olat.repository.RepositoryEntry)4 FileVO (org.olat.restapi.support.vo.FileVO)4 LinkVO (org.olat.restapi.support.vo.LinkVO)4 File (java.io.File)2 Collator (java.text.Collator)2 HashSet (java.util.HashSet)2