private void processReturnFile(
      AssessableCourseNode courseNode,
      BulkAssessmentRow row,
      UserCourseEnvironment uce,
      File assessedFolder) {
    String assessedId = row.getAssessedId();
    Identity identity = uce.getIdentityEnvironment().getIdentity();
    VFSContainer returnBox = getReturnBox(uce, courseNode, identity);
    if (returnBox != null) {
      for (String returnFilename : row.getReturnFiles()) {
        File returnFile = new File(assessedFolder, returnFilename);
        VFSItem currentReturnLeaf = returnBox.resolve(returnFilename);
        if (currentReturnLeaf != null) {
          // remove the current file (delete make a version if it is enabled)
          currentReturnLeaf.delete();
        }

        VFSLeaf returnLeaf = returnBox.createChildLeaf(returnFilename);
        if (returnFile.exists()) {
          try {
            InputStream inStream = new FileInputStream(returnFile);
            VFSManager.copyContent(inStream, returnLeaf);
          } catch (FileNotFoundException e) {
            log.error("Cannot copy return file " + returnFilename + " from " + assessedId, e);
          }
        }
      }
    }
  }
  private void doCreateSolutionEditor(UserRequest ureq, Solution solution) {
    String documentName = solution.getFilename();
    VFSItem item = solutionContainer.resolve(documentName);
    if (item == null) {
      solutionContainer.createChildLeaf(documentName);
    } else {
      documentName = VFSManager.rename(solutionContainer, documentName);
      solutionContainer.createChildLeaf(documentName);
    }

    newSolutionEditorCtrl =
        WysiwygFactory.createWysiwygController(
            ureq, getWindowControl(), solutionContainer, documentName, "media", true, true);
    newSolutionEditorCtrl.getRichTextConfiguration().disableMedia();
    newSolutionEditorCtrl.getRichTextConfiguration().setAllowCustomMediaFactory(false);
    newSolutionEditorCtrl.setNewFile(true);
    newSolutionEditorCtrl.setUserObject(solution);
    listenTo(newSolutionEditorCtrl);

    cmc =
        new CloseableModalController(
            getWindowControl(), "close", newSolutionEditorCtrl.getInitialComponent());
    listenTo(cmc);
    cmc.activate();
  }
Exemple #3
0
  /** @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;
    }

    // The following code block eliminates directory scans on file-systems,
    // which are done in the original code in the next block, which is left
    // there as a fall-back in case there are non-file-system implementations
    // of OLAT-VFS.
    // OLAT file-systems can be very large and directories can contain
    // quite numerous files. Scanning these can take a lot of time.
    // Just put together the paths of both arguments
    // and ask the file-system whether such an entry
    // exists. If yes, this entry must be exactly what is
    // 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;
  }
  @Override
  public boolean restore(Versionable currentVersion, VFSRevision version, String comment) {
    VFSLeaf currentFile = (VFSLeaf) currentVersion;
    if (!VFSManager.exists(currentFile)) {
      return false;
    }

    // add current version to versions file
    if (addToRevisions(currentVersion, null, comment)) {
      // copy the content of the new file to the old
      if (VFSManager.copyContent(version.getInputStream(), currentFile)) {
        return true;
      }
    } else {
      log.error("Cannot create a version of this file: " + currentVersion);
    }

    return false;
  }
 @Override
 public boolean addVersion(
     Versionable currentVersion, Identity identity, String comment, InputStream newFile) {
   VFSLeaf currentFile = (VFSLeaf) currentVersion;
   if (addToRevisions(currentVersion, identity, comment)) {
     // copy the content of the new file to the old
     boolean closeInputStream =
         !(newFile instanceof net.sf.jazzlib.ZipInputStream
             || newFile instanceof java.util.zip.ZipInputStream);
     if (VFSManager.copyContent(newFile, currentFile, closeInputStream)) {
       return true;
     }
   } else {
     log.error("Cannot create a version of this file: " + currentVersion);
   }
   return false;
 }
  private boolean copyRevision(
      VFSRevision revision, VFSLeaf fNewVersions, VersionsFileImpl targetVersions) {
    if (!(revision instanceof RevisionFileImpl)) {
      logWarn("Copy only copy persisted revisions", null);
    }

    RevisionFileImpl revisionImpl = (RevisionFileImpl) revision;
    String revUuid = revisionImpl.getUuid();
    for (VFSRevision rev : targetVersions.getRevisions()) {
      if (rev instanceof RevisionFileImpl) {
        RevisionFileImpl fRev = (RevisionFileImpl) rev;
        if (StringHelper.containsNonWhitespace(fRev.getUuid()) && fRev.getUuid().equals(revUuid)) {
          return true;
        }
      }
    }

    String uuid = UUID.randomUUID().toString().replace("-", "") + "_" + revision.getName();

    RevisionFileImpl newRevision = new RevisionFileImpl();
    newRevision.setName(revision.getName());
    newRevision.setFilename(uuid);
    newRevision.setRevisionNr(getNextRevisionNr(targetVersions));
    newRevision.setComment(revision.getComment());
    newRevision.setAuthor(revision.getAuthor());
    newRevision.setLastModified(revision.getLastModified());
    newRevision.setUuid(revUuid);

    // copy -> the files revision
    InputStream revisionIn = revision.getInputStream();

    VFSLeaf target = fNewVersions.getParentContainer().createChildLeaf(uuid);
    if (VFSManager.copyContent(revisionIn, target)) {
      targetVersions.setComment(revision.getComment());
      targetVersions.getRevisions().add(newRevision);
      targetVersions.setRevisionNr(getNextRevisionNr(targetVersions));
      targetVersions.setAuthor(revision.getAuthor());
      return true;
    }
    return false;
  }
 @Override
 public boolean restore(VFSContainer container, VFSRevision revision) {
   String filename = revision.getName();
   VFSItem restoredItem = container.resolve(filename);
   if (restoredItem == null) {
     restoredItem = container.createChildLeaf(filename);
   }
   if (restoredItem instanceof VFSLeaf) {
     VFSLeaf restoredLeaf = (VFSLeaf) restoredItem;
     InputStream inStream = revision.getInputStream();
     if (VFSManager.copyContent(inStream, restoredLeaf)) {
       VFSLeaf versionFile = getCanonicalVersionXmlFile(restoredLeaf, true);
       Versions versions = readVersions(restoredLeaf, versionFile);
       if (versions instanceof VersionsFileImpl) {
         versions.getRevisions().remove(revision);
         ((VersionsFileImpl) versions).setRevisionNr(getNextRevisionNr(versions));
       }
       XStreamHelper.writeObject(mystream, versionFile, versions);
       return true;
     }
   }
   return false;
 }
Exemple #8
0
 /** @see org.olat.core.util.vfs.VFSItem#canRename() */
 public VFSStatus canRename() {
   VFSContainer inheritingContainer = VFSManager.findInheritingSecurityCallbackContainer(this);
   if (inheritingContainer != null && !inheritingContainer.getLocalSecurityCallback().canWrite())
     return VFSConstants.NO_SECURITY_DENIED;
   return VFSConstants.YES;
 }
  /**
   * @see
   *     org.olat.core.util.vfs.version.VersionsManager#addToRevisions(org.olat.core.util.vfs.version.Versionable,
   *     org.olat.core.id.Identity, java.lang.String)
   */
  @Override
  public boolean addToRevisions(Versionable currentVersion, Identity identity, String comment) {
    int maxNumOfVersions = versioningConfigurator.getMaxNumOfVersionsAllowed();
    if (maxNumOfVersions == 0) {
      return true; // deactivated, return all ok
    }

    VFSLeaf currentFile = (VFSLeaf) currentVersion;

    VFSLeaf versionFile = getCanonicalVersionXmlFile(currentFile, true);
    if (versionFile == null) {
      return false; // cannot do something with the current file
    }

    VFSContainer versionContainer = versionFile.getParentContainer();

    String name = currentFile.getName();

    // read from the
    Versions v = readVersions(currentFile, versionFile);
    if (!(v instanceof VersionsFileImpl)) {
      log.error("Wrong implementation of Versions: " + v);
      return false;
    }
    VersionsFileImpl versions = (VersionsFileImpl) v;
    boolean sameFile = isSameFile(currentFile, versions);
    String uuid =
        sameFile ? getLastRevisionFilename(versions) : UUID.randomUUID().toString() + "_" + name;

    String versionNr = getNextRevisionNr(versions);
    String currentAuthor = versions.getAuthor();
    long lastModifiedDate = 0;
    if (currentFile instanceof MetaTagged) {
      MetaInfo metaInfo = ((MetaTagged) currentFile).getMetaInfo();
      if (metaInfo != null) {
        metaInfo.clearThumbnails();
        if (currentAuthor == null) {
          currentAuthor = metaInfo.getAuthor();
        }
        lastModifiedDate = metaInfo.getLastModified();
      }
    }

    if (lastModifiedDate <= 0) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(new Date());
      lastModifiedDate = cal.getTimeInMillis();
    }

    RevisionFileImpl newRevision = new RevisionFileImpl();
    newRevision.setUuid(UUID.randomUUID().toString());
    newRevision.setName(name);
    newRevision.setFilename(uuid);
    newRevision.setRevisionNr(versionNr);
    newRevision.setComment(versions.getComment());
    newRevision.setAuthor(currentAuthor);
    newRevision.setLastModified(lastModifiedDate);

    if (versions.getRevisions().isEmpty() && currentVersion instanceof MetaTagged) {
      MetaTagged metaTagged = (MetaTagged) currentVersion;
      versions.setCreator(metaTagged.getMetaInfo().getAuthor());
    }

    if (sameFile || VFSManager.copyContent(currentFile, versionContainer.createChildLeaf(uuid))) {
      if (identity != null) {
        versions.setAuthor(identity.getName());
      }

      if (maxNumOfVersions >= 0 && versions.getRevisions().size() >= maxNumOfVersions) {
        List<VFSRevision> revisions = versions.getRevisions();
        int numOfVersionsToDelete =
            Math.min(revisions.size(), (revisions.size() - maxNumOfVersions) + 1);
        if (numOfVersionsToDelete > 0) {
          List<VFSRevision> versionsToDelete = revisions.subList(0, numOfVersionsToDelete);
          deleteRevisions(currentVersion, versionsToDelete);
          versions = (VersionsFileImpl) currentVersion.getVersions();
        }
      }
      versions.setComment(comment);
      versions.getRevisions().add(newRevision);
      versions.setRevisionNr(getNextRevisionNr(versions));
      XStreamHelper.writeObject(mystream, versionFile, versions);
      if (currentVersion.getVersions() instanceof VersionsFileImpl) {
        ((VersionsFileImpl) currentVersion.getVersions()).update(versions);
      }
      return true;
    } else {
      log.error("Cannot create a version of this file: " + currentVersion);
    }
    return false;
  }