/**
  * Get an {@link OriginalFile} object based on its id. Returns null if the file does not exist or
  * does not belong to this repo.
  *
  * @param id long, db id of original file.
  * @return OriginalFile object.
  */
 private CheckedPath checkId(final long id, final Ice.Current curr)
     throws SecurityViolation, ValidationException {
   // TODO: could getOriginalFile and getFile be reduced to a single call?
   final FsFile file = this.repositoryDao.getFile(id, curr, this.repoUuid);
   if (file == null) {
     throw new SecurityViolation(null, null, "FileNotFound: " + id);
   }
   final OriginalFile originalFile = this.repositoryDao.getOriginalFile(id, curr);
   if (originalFile == null) {
     /* reachable even if file != null because getFile uses SQL,
      * evading the filter on the HQL used here by getOriginalFile */
     throw new SecurityViolation(null, null, "FileNotAccessible: " + id);
   }
   final CheckedPath checked =
       new CheckedPath(
           this.serverPaths, file.toString(), checksumProviderFactory, originalFile.getHasher());
   checked.setId(id);
   return checked;
 }
  protected OriginalFile findOrCreateInDb(
      CheckedPath checked, String mode, String mimetype, Ice.Current curr) throws ServerError {

    OriginalFile ofile = findInDb(checked, mode, curr);
    if (ofile != null) {
      return ofile;
    }

    if (checked.exists()) {
      omero.grid.UnregisteredFileException ufe = new omero.grid.UnregisteredFileException();
      ofile = (OriginalFile) new IceMapper().map(checked.asOriginalFile(mimetype));
      ufe.file = ofile;
      throw ufe;
    }

    ofile = repositoryDao.register(repoUuid, checked, null, curr);
    final long originalFileId = ofile.getId().getValue();
    setOriginalFileHasherToSHA1(originalFileId, curr);
    checked.setId(originalFileId);
    return ofile;
  }
  /**
   * Find the given path in the DB or create.
   *
   * <p>"requiresWrite" is set to true unless the mode is "r". If requiresWrite is true, then the
   * caller needs the file to be modifiable (both on disk and the DB). If this doesn't hold, then a
   * SecurityViolation will be thrown.
   */
  protected OriginalFile findInDb(CheckedPath checked, String mode, Ice.Current current)
      throws ServerError {

    final OriginalFile ofile = repositoryDao.findRepoFile(repoUuid, checked, null, current);

    if (ofile == null) {
      return null; // EARLY EXIT!
    }

    boolean requiresWrite = true;
    if ("r".equals(mode)) {
      requiresWrite = false;
    }

    checked.setId(ofile.getId().getValue());
    boolean canUpdate = repositoryDao.canUpdate(ofile, current);
    if (requiresWrite && !canUpdate) {
      throw new omero.SecurityViolation(null, null, "requiresWrite is true but cannot modify");
    }

    return ofile;
  }