/**
   * Copies the file bundle into the repository
   *
   * @param bundle
   * @param bundlePath
   * @param bundlePathName
   * @param file
   * @param comment
   */
  protected boolean copyFileToRepository(
      final ImportSource.IRepositoryFileBundle bundle,
      final String bundlePathName,
      final String repositoryPath,
      final RepositoryFile file,
      final String comment) {
    // Compute the file extension
    final String name = bundle.getFile().getName();
    final String ext = RepositoryFilenameUtils.getExtension(name);
    if (StringUtils.isEmpty(ext)) {
      log.debug("Skipping file without extension: " + bundlePathName);
      return false;
    }

    // Check the mime type
    final String mimeType = bundle.getMimeType();
    if (mimeType == null) {
      log.debug("Skipping file without mime-type: " + bundlePathName);
      return false;
    }

    // Find the converter
    final Converter converter = converters.get(ext);
    if (converter == null) {
      log.debug("Skipping file without converter: " + bundlePathName);
      return false;
    }

    // Copy the file into the repository
    try {
      log.trace("copying file to repository: " + bundlePathName);
      IRepositoryFileData data =
          converter.convert(bundle.getInputStream(), bundle.getCharset(), mimeType);
      if (null == file) {
        final boolean hidden = !executableTypes.contains(ext.toLowerCase());
        log.trace("\tsetting hidden=" + hidden + " for file with extension " + ext.toLowerCase());
        createFile(bundle, repositoryPath, hidden, data, comment);
      } else {
        repository.updateFile(file, data, comment);
      }
      return true;
    } catch (IOException e) {
      log.warn(
          messages.getString("DefaultImportHandler.WARN_0003_IOEXCEPTION", name),
          e); // TODO make sure string
      // exists
      return false;
    }
  }
 /**
  * Computes the bundle path from the bundle
  *
  * @param bundle
  * @return
  */
 protected String computeBundlePath(final ImportSource.IRepositoryFileBundle bundle) {
   String bundlePath = bundle.getPath();
   bundlePath = RepositoryFilenameUtils.separatorsToRepository(bundlePath);
   if (bundlePath.startsWith(RepositoryFile.SEPARATOR)) {
     bundlePath = bundlePath.substring(1);
   }
   return bundlePath;
 }
 /**
  * Creates a new file in the repository
  *
  * @param bundle
  * @param destinationPath
  * @param bundlePath
  * @param ext
  * @param data
  * @param comment
  */
 protected RepositoryFile createFile(
     final ImportSource.IRepositoryFileBundle bundle,
     final String destinationPath,
     final boolean hidden,
     final IRepositoryFileData data,
     final String comment) {
   final RepositoryFile file =
       new RepositoryFile.Builder(bundle.getFile())
           .hidden(hidden)
           .title(RepositoryFile.DEFAULT_LOCALE, getTitle(bundle.getFile().getName()))
           .versioned(true)
           .build();
   final Serializable parentId = getParentId(destinationPath);
   final RepositoryFileAcl acl = bundle.getAcl();
   if (null == acl) {
     return repository.createFile(parentId, file, data, comment);
   } else {
     return repository.createFile(parentId, file, data, acl, comment);
   }
 }
  /**
   * Processes the list of files and performs any processing required to import that data into the
   * repository. If during processing it handles file(s) which should not be handled by downstream
   * import handlers, then it should remove them from the set of files provided.
   *
   * @param importFileSet the set of files to be imported - any files handled to completion by this
   *     Import Handler should remove this files from this list
   * @param comment the import comment provided
   * @param overwrite indicates if the process is authorized to overwrite existing content in the
   *     repository
   * @throws ImportException indicates a significant error during import processing
   */
  @Override
  public void doImport(
      final Iterable<ImportSource.IRepositoryFileBundle> importFileSet,
      final String destinationPath,
      final String comment,
      final boolean overwrite)
      throws ImportException {
    if (null == importFileSet || StringUtils.isEmpty(destinationPath)) {
      throw new IllegalArgumentException();
    }

    // Ensure the destination path is valid
    Assert.notNull(
        getParentId(RepositoryFilenameUtils.normalize(destinationPath + RepositoryFile.SEPARATOR)));

    // Iterate through the file set
    for (Iterator<IRepositoryFileBundle> iterator = importFileSet.iterator();
        iterator.hasNext(); ) {
      final ImportSource.IRepositoryFileBundle bundle =
          (ImportSource.IRepositoryFileBundle) iterator.next();

      // Make sure we don't try to do anything in a system-defined folder
      final String bundlePathName =
          RepositoryFilenameUtils.concat(computeBundlePath(bundle), bundle.getFile().getName());
      if (isSystemPath(bundlePathName)) {
        log.trace("Skipping [" + bundlePathName + "] since it is in admin / system folders");
        continue;
      }
      final String repositoryFilePath =
          RepositoryFilenameUtils.concat(destinationPath, bundlePathName);
      log.trace("Processing [" + bundlePathName + "]");

      // See if the destination already exists in the repository
      final RepositoryFile file = repository.getFile(repositoryFilePath);
      if (file != null) {
        if (file.isFolder() != bundle.getFile().isFolder()) {
          log.warn(
              "Entry already exists in the repository - but it is a "
                  + (file.isFolder() ? "folder" : "file")
                  + " and the entry to be imported is a "
                  + (bundle.getFile().isFolder() ? "folder" : "file"));
        }

        if (!overwrite) {
          log.trace("File already exists in repository and overwrite is false - skipping");
        } else if (file.isFolder()) {
          log.trace("Folder already exists - skip");
        } else {
          // It is a file we can overwrite...
          log.trace("Updating...");
          copyFileToRepository(bundle, bundlePathName, repositoryFilePath, file, comment);
        }
        // We handled this file (even if by doing nothing)
        iterator.remove();
        continue;
      }

      // The file doesn't exist - if it is a folder then this is easy
      if (bundle.getFile().isFolder()) {
        log.trace("Creating folder [" + bundlePathName + "]");
        final Serializable parentId = getParentId(repositoryFilePath);
        if (bundle.getAcl() != null) {
          repository.createFolder(parentId, bundle.getFile(), bundle.getAcl(), comment);
        } else {
          repository.createFolder(parentId, bundle.getFile(), comment);
        }
        iterator.remove();
      } else {
        // It is a file ...
        if (copyFileToRepository(bundle, bundlePathName, repositoryFilePath, null, comment)) {
          iterator.remove();
        }
      }
    }
  }