Search in sources :

Example 1 with OlatRootFileImpl

use of org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl in project OpenOLAT by OpenOLAT.

the class FileArtefactHandler method prefillArtefactAccordingToSource.

/**
 * @see org.olat.portfolio.EPAbstractHandler#prefillArtefactAccordingToSource(org.olat.portfolio.model.artefacts.AbstractArtefact,
 *      java.lang.Object)
 */
@Override
public void prefillArtefactAccordingToSource(AbstractArtefact artefact, Object source) {
    super.prefillArtefactAccordingToSource(artefact, source);
    if (source instanceof VFSItem) {
        VFSItem fileSource = (VFSItem) source;
        ((FileArtefact) artefact).setFilename(fileSource.getName());
        MetaInfo meta = null;
        if (fileSource instanceof MetaTagged) {
            meta = ((MetaTagged) fileSource).getMetaInfo();
        }
        if (meta != null && StringHelper.containsNonWhitespace(meta.getTitle())) {
            artefact.setTitle(meta.getTitle());
        } else {
            artefact.setTitle(fileSource.getName());
        }
        if (meta != null && StringHelper.containsNonWhitespace(meta.getComment())) {
            artefact.setDescription(meta.getComment());
        }
        artefact.setSignature(60);
        String path = ((OlatRootFileImpl) fileSource).getRelPath();
        String[] pathElements = path.split("/");
        String finalBusinessPath = null;
        String sourceInfo = null;
        // used to rebuild businessPath and source for a file:
        if (pathElements[1].equals("homes") && meta != null && pathElements[2].equals(meta.getAuthor())) {
            // from users briefcase
            String lastParts = "/";
            for (int i = 4; i < (pathElements.length - 1); i++) {
                lastParts = lastParts + pathElements[i] + "/";
            }
            sourceInfo = "Home -> " + pathElements[3] + " -> " + lastParts + fileSource.getName();
        } else if (pathElements[3].equals("BusinessGroup")) {
            // out of a businessgroup
            String lastParts = "/";
            for (int i = 5; i < (pathElements.length - 1); i++) {
                lastParts = lastParts + pathElements[i] + "/";
            }
            BusinessGroup bGroup = CoreSpringFactory.getImpl(BusinessGroupService.class).loadBusinessGroup(new Long(pathElements[4]));
            if (bGroup != null) {
                sourceInfo = bGroup.getName() + " -> " + lastParts + " -> " + fileSource.getName();
            }
            finalBusinessPath = "[BusinessGroup:" + pathElements[4] + "][toolfolder:0][path=" + lastParts + fileSource.getName() + ":0]";
        } else if (pathElements[4].equals("coursefolder")) {
            // the course folder
            sourceInfo = RepositoryManager.getInstance().lookupDisplayNameByOLATResourceableId(new Long(pathElements[2])) + " -> " + fileSource.getName();
        } else if (pathElements[1].equals("course") && pathElements[3].equals("foldernodes")) {
            // folders inside a course
            sourceInfo = RepositoryManager.getInstance().lookupDisplayNameByOLATResourceableId(new Long(pathElements[2])) + " -> " + pathElements[4] + " -> " + fileSource.getName();
            finalBusinessPath = "[RepositoryEntry:" + pathElements[2] + "][CourseNode:" + pathElements[4] + "]";
        }
        if (sourceInfo == null) {
            // unknown source, keep full path
            sourceInfo = VFSManager.getRealPath(fileSource.getParentContainer()) + "/" + fileSource.getName();
        }
        artefact.setBusinessPath(finalBusinessPath);
        artefact.setSource(sourceInfo);
    }
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) VFSItem(org.olat.core.util.vfs.VFSItem) OlatRootFileImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl)

Example 2 with OlatRootFileImpl

use of org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl in project OpenOLAT by OpenOLAT.

the class FileCopyController method event.

@Override
public void event(UserRequest ureq, Controller source, Event event) {
    if (source instanceof FileLinkChooserController) {
        if (event == Event.DONE_EVENT || event == Event.CANCELLED_EVENT) {
            fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
        } else if (event instanceof URLChoosenEvent) {
            URLChoosenEvent choosenEvent = (URLChoosenEvent) event;
            String url = choosenEvent.getURL();
            if (url.indexOf("://") < 0) {
                VFSContainer cContainer = folderComponent.getExternContainerForCopy();
                VFSItem item = cContainer.resolve(url);
                if (item instanceof VFSLeaf) {
                    sourceLeaf = (VFSLeaf) item;
                    String filename = sourceLeaf.getName();
                    VFSContainer tContainer = folderComponent.getCurrentContainer();
                    newFile = tContainer.createChildLeaf(filename);
                    if (newFile == null) {
                        existingVFSItem = (VFSLeaf) tContainer.resolve(filename);
                        fileAlreadyExists(ureq);
                    } else {
                        finishUpload(ureq);
                    }
                } else {
                    fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
                }
            } else {
                fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
            }
        }
    } else if (source == overwriteDialog) {
        if (event instanceof ButtonClickedEvent) {
            ButtonClickedEvent buttonClickedEvent = (ButtonClickedEvent) event;
            if (buttonClickedEvent.getPosition() == 0) {
                // ok
                if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
                    // new version
                    String relPath = null;
                    if (existingVFSItem instanceof OlatRootFileImpl) {
                        relPath = ((OlatRootFileImpl) existingVFSItem).getRelPath();
                    }
                    int maxNumOfRevisions = FolderConfig.versionsAllowed(relPath);
                    if (maxNumOfRevisions == 0) {
                        // someone play with the configuration
                        // Overwrite...
                        String fileName = existingVFSItem.getName();
                        existingVFSItem.delete();
                        newFile = folderComponent.getCurrentContainer().createChildLeaf(fileName);
                        // ... and notify listeners.
                        finishUpload(ureq);
                    } else {
                        removeAsListenerAndDispose(commentVersionCtr);
                        boolean locked = vfsLockManager.isLocked(existingVFSItem);
                        commentVersionCtr = new VersionCommentController(ureq, getWindowControl(), locked, true);
                        listenTo(commentVersionCtr);
                        removeAsListenerAndDispose(commentVersionDialogBox);
                        commentVersionDialogBox = new CloseableModalController(getWindowControl(), translate("save"), commentVersionCtr.getInitialComponent());
                        listenTo(commentVersionDialogBox);
                        commentVersionDialogBox.activate();
                    }
                } else {
                    // if the file is locked, ask for unlocking it
                    if (vfsLockManager.isLocked(existingVFSItem)) {
                        removeAsListenerAndDispose(unlockCtr);
                        unlockCtr = new VersionCommentController(ureq, getWindowControl(), true, false);
                        listenTo(unlockCtr);
                        removeAsListenerAndDispose(unlockDialogBox);
                        unlockDialogBox = new CloseableModalController(getWindowControl(), translate("ok"), unlockCtr.getInitialComponent());
                        listenTo(unlockDialogBox);
                        unlockDialogBox.activate();
                    } else {
                        // Overwrite...
                        String fileName = existingVFSItem.getName();
                        existingVFSItem.delete();
                        newFile = folderComponent.getCurrentContainer().createChildLeaf(fileName);
                        // ... and notify listeners.
                        finishUpload(ureq);
                    }
                }
            } else if (buttonClickedEvent.getPosition() == 1) {
                // not ok
                // make newFile with the proposition of filename
                newFile = folderComponent.getCurrentContainer().createChildLeaf(renamedFilename);
                // ... and notify listeners.
                finishUpload(ureq);
            } else if (buttonClickedEvent.getPosition() == 2) {
            // cancel
            // cancel -> do nothing
            } else {
                throw new RuntimeException("Unknown button number " + buttonClickedEvent.getPosition());
            }
        }
    } else if (source == lockedFileDialog) {
        if (event instanceof ButtonClickedEvent) {
            ButtonClickedEvent buttonClickedEvent = (ButtonClickedEvent) event;
            switch(buttonClickedEvent.getPosition()) {
                case 0:
                    {
                        // ... and notify listeners.
                        newFile = existingVFSItem;
                        finishUpload(ureq);
                        break;
                    }
                case 1:
                    {
                        // cancel
                        fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
                        break;
                    }
                default:
                    throw new RuntimeException("Unknown button number " + buttonClickedEvent.getPosition());
            }
        }
    } else if (source == commentVersionCtr) {
        String comment = commentVersionCtr.getComment();
        Roles roles = ureq.getUserSession().getRoles();
        boolean locked = vfsLockManager.isLocked(existingVFSItem);
        if (locked && !commentVersionCtr.keepLocked()) {
            vfsLockManager.unlock(existingVFSItem, getIdentity(), roles);
        }
        commentVersionDialogBox.deactivate();
        if (revisionListDialogBox != null) {
            revisionListDialogBox.deactivate();
        }
        // ok, new version of the file
        Versionable existingVersionableItem = (Versionable) existingVFSItem;
        boolean ok = existingVersionableItem.getVersions().addVersion(ureq.getIdentity(), comment, sourceLeaf.getInputStream());
        if (ok) {
            newFile = existingVFSItem;
        }
        finishSuccessfullUpload(existingVFSItem.getName(), ureq);
    } else if (source == unlockCtr) {
        // Overwrite...
        if (!unlockCtr.keepLocked()) {
            vfsLockManager.unlock(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
        }
        unlockDialogBox.deactivate();
        newFile = existingVFSItem;
        // ... and notify listeners.
        finishSuccessfullUpload(existingVFSItem.getName(), ureq);
    } else if (source == revisionListCtr) {
        if (FolderCommandStatus.STATUS_CANCELED == revisionListCtr.getStatus()) {
            revisionListDialogBox.deactivate();
            // don't want to delete revisions
            fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
        } else {
            if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
                revisionListDialogBox.deactivate();
                Versionable versionable = (Versionable) existingVFSItem;
                Versions versions = versionable.getVersions();
                int maxNumOfRevisions = FolderConfig.versionsAllowed(null);
                if (maxNumOfRevisions < 0 || maxNumOfRevisions > versions.getRevisions().size()) {
                    removeAsListenerAndDispose(commentVersionCtr);
                    boolean locked = vfsLockManager.isLocked(existingVFSItem);
                    commentVersionCtr = new VersionCommentController(ureq, getWindowControl(), locked, true);
                    listenTo(commentVersionCtr);
                    removeAsListenerAndDispose(commentVersionDialogBox);
                    commentVersionDialogBox = new CloseableModalController(getWindowControl(), translate("save"), commentVersionCtr.getInitialComponent());
                    listenTo(commentVersionDialogBox);
                    commentVersionDialogBox.activate();
                } else {
                    removeAsListenerAndDispose(revisionListCtr);
                    revisionListCtr = new RevisionListController(ureq, getWindowControl(), versionable, false);
                    listenTo(revisionListCtr);
                    removeAsListenerAndDispose(revisionListDialogBox);
                    revisionListDialogBox = new CloseableModalController(getWindowControl(), translate("delete"), revisionListCtr.getInitialComponent());
                    listenTo(revisionListDialogBox);
                    revisionListDialogBox.activate();
                }
            }
        }
    }
}
Also used : ButtonClickedEvent(org.olat.core.gui.control.generic.modal.ButtonClickedEvent) FileLinkChooserController(org.olat.core.commons.controllers.linkchooser.FileLinkChooserController) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) Roles(org.olat.core.id.Roles) OlatRootFileImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl) RevisionListController(org.olat.core.commons.modules.bc.version.RevisionListController) Versionable(org.olat.core.util.vfs.version.Versionable) VersionCommentController(org.olat.core.commons.modules.bc.version.VersionCommentController) Versions(org.olat.core.util.vfs.version.Versions) URLChoosenEvent(org.olat.core.commons.controllers.linkchooser.URLChoosenEvent)

Example 3 with OlatRootFileImpl

use of org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl in project OpenOLAT by OpenOLAT.

the class FileCopyController method finishSuccessfullUpload.

private void finishSuccessfullUpload(String fileName, UserRequest ureq) {
    VFSContainer currentContainer = folderComponent.getCurrentContainer();
    VFSItem item = currentContainer.resolve(fileName);
    if (item instanceof OlatRootFileImpl) {
        OlatRootFileImpl relPathItem = (OlatRootFileImpl) item;
        // create meta data
        MetaInfo meta = CoreSpringFactory.getImpl(MetaInfoFactory.class).createMetaInfoFor(relPathItem);
        meta.setAuthor(ureq.getIdentity());
        // if overwrite an older file
        meta.clearThumbnails();
        meta.write();
    }
    ThreadLocalUserActivityLogger.log(FolderLoggingAction.FILE_COPIED, getClass(), CoreLoggingResourceable.wrapUploadFile(fileName));
    // Notify listeners about upload
    fireEvent(ureq, new FolderEvent(FolderEvent.NEW_FILE_EVENT, newFile.getName()));
    fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
}
Also used : VFSContainer(org.olat.core.util.vfs.VFSContainer) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) VFSItem(org.olat.core.util.vfs.VFSItem) MetaInfoFactory(org.olat.core.commons.modules.bc.meta.MetaInfoFactory) OlatRootFileImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl)

Example 4 with OlatRootFileImpl

use of org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl in project OpenOLAT by OpenOLAT.

the class FileCopyController method fileAlreadyExists.

private void fileAlreadyExists(UserRequest ureq) {
    renamedFilename = proposedRenamedFilename(existingVFSItem);
    boolean locked = vfsLockManager.isLockedForMe(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
    if (locked) {
        // the file is locked and cannot be overwritten
        removeAsListenerAndDispose(lockedFileDialog);
        lockedFileDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.lockedFile.title"), translate("ul.lockedFile.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
        listenTo(lockedFileDialog);
        lockedFileDialog.activate();
    } else if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
        Versionable versionable = (Versionable) existingVFSItem;
        Versions versions = versionable.getVersions();
        String relPath = null;
        if (existingVFSItem instanceof OlatRootFileImpl) {
            relPath = ((OlatRootFileImpl) existingVFSItem).getRelPath();
        }
        int maxNumOfRevisions = FolderConfig.versionsAllowed(relPath);
        if (maxNumOfRevisions == 0) {
            // it's possible if someone change the configuration
            // let calling method decide what to do.
            removeAsListenerAndDispose(overwriteDialog);
            overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.overwrite.threeoptions.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.overwrite"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
            listenTo(overwriteDialog);
            overwriteDialog.activate();
        } else if (versions.getRevisions().isEmpty() || maxNumOfRevisions < 0 || maxNumOfRevisions > versions.getRevisions().size()) {
            // let calling method decide what to do.
            removeAsListenerAndDispose(overwriteDialog);
            overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.versionoroverwrite", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.newVersion"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
            listenTo(overwriteDialog);
            overwriteDialog.activate();
        } else {
            String title = translate("ul.tooManyRevisions.title", new String[] { Integer.toString(maxNumOfRevisions), Integer.toString(versions.getRevisions().size()) });
            String description = translate("ul.tooManyRevisions.description", new String[] { Integer.toString(maxNumOfRevisions), Integer.toString(versions.getRevisions().size()) });
            removeAsListenerAndDispose(revisionListCtr);
            revisionListCtr = new RevisionListController(ureq, getWindowControl(), versionable, null, description, false);
            listenTo(revisionListCtr);
            removeAsListenerAndDispose(revisionListDialogBox);
            revisionListDialogBox = new CloseableModalController(getWindowControl(), translate("delete"), revisionListCtr.getInitialComponent(), true, title);
            listenTo(revisionListDialogBox);
            revisionListDialogBox.activate();
        }
    } else {
        // let calling method decide what to do.
        // for this, we put a list with "existing name" and "new name"
        overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.overwrite.threeoptions.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.overwrite"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
        listenTo(overwriteDialog);
        overwriteDialog.activate();
    }
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) Versions(org.olat.core.util.vfs.version.Versions) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) OlatRootFileImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl) RevisionListController(org.olat.core.commons.modules.bc.version.RevisionListController)

Example 5 with OlatRootFileImpl

use of org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl in project OpenOLAT by OpenOLAT.

the class VFSManager method resolveFile.

/**
 * @see org.olat.core.util.vfs.VFSItem#resolveFile(java.lang.String)
 */
public static VFSItem resolveFile(VFSContainer rootContainer, String path) {
    path = VFSManager.sanitizePath(path);
    if (path.equals("/")) {
        // slash or empty path -> return this vfsitem
        return rootContainer;
    }
    // to be returned as, the proper type of, VFSItem.
    if (rootContainer instanceof LocalFolderImpl) {
        String childName = extractChild(path);
        LocalFolderImpl l = (LocalFolderImpl) rootContainer;
        File t = new File(l.getBasefile().getAbsolutePath(), childName);
        if (t.exists()) {
            String bcroot = FolderConfig.getCanonicalRoot();
            String fsPath = t.getAbsolutePath();
            if (t.isDirectory()) {
                VFSContainer subContainer;
                if (fsPath.startsWith(bcroot)) {
                    fsPath = fsPath.substring(bcroot.length(), fsPath.length());
                    subContainer = new OlatRootFolderImpl(fsPath, rootContainer);
                } else {
                    subContainer = new LocalFolderImpl(t, rootContainer);
                }
                String subPath = path.substring(childName.length() + 1);
                return resolveFile(subContainer, subPath);
            } else {
                if (fsPath.startsWith(bcroot)) {
                    fsPath = fsPath.replace(bcroot, "");
                    return new OlatRootFileImpl(fsPath, rootContainer);
                } else {
                    return new LocalFileImpl(t, rootContainer);
                }
            }
        } else {
            return null;
        }
    }
    // leave original code block as fall-back for non-file-system-based implementations
    String childName = extractChild(path);
    List<VFSItem> children = rootContainer.getItems();
    for (VFSItem child : children) {
        String curName = child.getName();
        if (childName.equals(curName)) {
            // found , let child further resolve if needed
            return child.resolve(path.substring(childName.length() + 1));
        }
    }
    return null;
}
Also used : OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) OlatRootFileImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl) File(java.io.File) ContainerAndFile(org.olat.core.util.vfs.util.ContainerAndFile)

Aggregations

OlatRootFileImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl)18 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 File (java.io.File)4 IOException (java.io.IOException)4 RevisionListController (org.olat.core.commons.modules.bc.version.RevisionListController)4 CloseableModalController (org.olat.core.gui.control.generic.closablewrapper.CloseableModalController)4 Roles (org.olat.core.id.Roles)4 VFSContainer (org.olat.core.util.vfs.VFSContainer)4 Versionable (org.olat.core.util.vfs.version.Versionable)4 Versions (org.olat.core.util.vfs.version.Versions)4 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 HashSet (java.util.HashSet)2 ZipInputStream (java.util.zip.ZipInputStream)2 BaseSecurity (org.olat.basesecurity.BaseSecurity)2 FileLinkChooserController (org.olat.core.commons.controllers.linkchooser.FileLinkChooserController)2 URLChoosenEvent (org.olat.core.commons.controllers.linkchooser.URLChoosenEvent)2