Example #1
0
  protected void runCLI(String[] args) {
    // prepare CLI and parse arguments
    Options options = createOptions();
    CommandLineParser parser = new PosixParser();
    CommandLine line = null;
    try {
      line = parser.parse(options, args);
    } catch (ParseException ex) {
      usage(options);
      System.err.println();
      System.err.println(ex.getMessage());
      log.fatal(ex);
      System.exit(1);
    }

    String[] remainingArgs = line.getArgs();
    if (remainingArgs.length > 0) {
      this.usage(options);
      System.err.println();
      StringBuilder builder = new StringBuilder(100);
      for (String argument : remainingArgs) {
        if (builder.length() > 0) builder.append(", ");
        builder.append(argument);
      }
      String argumentsLine = builder.toString().trim();
      System.err.print("Cannot recognize the following argument");
      if (remainingArgs.length >= 2) System.err.print("s");
      System.err.println(": " + argumentsLine + ".");
      System.exit(1);
    }

    // set member variables depending on CLI arguments.
    if (line.hasOption("verbose")) {
      setVerbose(true);
    }

    if (line.hasOption("dry-run")) {
      setDryrun(true);
    }

    if (line.hasOption("stdout")) {
      setStdout(true);
    }

    // check mutual exclusive arguments
    if (line.hasOption("delete") && line.hasOption("delete-all")) {
      usage(options);
      System.err.println(
          "\n\nYou cannot use the options --delete <handle> " + "and --delete-all together.");
      System.exit(1);
    }

    if (line.hasOption("convert-all")
        && (line.hasOption("delete") || line.hasOption("delete-all"))) {
      usage(options);
      System.err.println(
          "\n\nYou cannot use the option --convert-all "
              + "together with --delete or --delete-all.");
      System.exit(1);
    }
    if (line.hasOption("identifiers")
        && (line.hasOption("delete") || line.hasOption("delete-all"))) {
      usage(options);
      System.err.println(
          "\n\nYou cannot use the option --identifiers <handle> "
              + "together with --delete or --delete-all.");
      System.exit(1);
    }
    if (line.hasOption("stdout") && (line.hasOption("delete") || line.hasOption("delete-all"))) {
      usage(options);
      System.err.println(
          "\n\nYou cannot use the option --stdout together " + "with --delete or --deleta-all.");
      System.exit(1);
    }

    // Run commands depending on CLI arguments.
    // process help first to prevent further evaluation of given options.
    if (line.hasOption('h')) {
      usage(options);
      System.exit(0);
    }

    if (line.hasOption("delete")) {
      String[] identifiers = line.getOptionValues("delete");
      for (String identifier : identifiers) {
        if (!StringUtils.startsWithIgnoreCase(identifier, "hdl:")) {
          if (!this.dryrun) {
            storage.delete(identifier);
          }
          if (this.verbose) {
            System.err.println("Deleted " + identifier + ".");
          }
          continue;
        }
        String handle = identifier.substring(4);

        log.debug("Trying to resolve identifier " + handle + ".");

        DSpaceObject dso = resolveHandle(handle);
        if (dso == null) {
          // resolveHandle reports problems and return null in case
          // of an error or an unresolvable handle.
          // Don't report it a second time, just continue...
          continue;
        }

        log.debug(
            "Resolved identifier "
                + handle
                + " as "
                + contentServiceFactory.getDSpaceObjectService(dso).getTypeText(dso)
                + " "
                + dso.getID());

        try {
          this.delete(dso, true);
        } catch (SQLException ex) {
          log.error(ex);
          System.err.println(
              "A problem with the database connection " + "occurred. Canceled pending actions.");
          System.err.println(ex.getMessage());
          ex.printStackTrace(System.err);
          System.exit(1);
        }
      }
      System.exit(0);
    }

    if (line.hasOption("delete-all")) {
      this.deleteAll();
      System.exit(0);
    }

    if (line.hasOption("identifiers")) {
      String[] identifiers = line.getOptionValues("identifiers");
      report("Starting conversion of specified DSpaceObjects...");

      this.processed.clear();
      for (String handle : identifiers) {
        log.debug("Trying to resolve identifier " + handle + ".");

        DSpaceObject dso = resolveHandle(handle);
        if (dso == null) {
          // resolveHandle reports problems and return null in case
          // of an error or an unresolvable handle.
          // Don't report it a second time, just continue...
          continue;
        }

        try {
          this.convert(dso, false);
        } catch (SQLException ex) {
          log.error(ex);
          System.err.println(
              "A problem with the database connection " + "occurred. Canceled pending actions.");
          System.err.println(ex.getMessage());
          ex.printStackTrace(System.err);
          System.exit(1);
        }
      }
      report("Conversion ended.");
      System.exit(0);
    }

    if (line.hasOption("convert-all")) {
      try {
        this.convertAll();
      } catch (SQLException ex) {
        log.error(ex);
        System.err.println(
            "A problem with the database connection " + "occurred. Canceled pending actions.");
        System.err.println(ex.getMessage());
        ex.printStackTrace(System.err);
        System.exit(1);
      }
      System.exit(0);
    }

    this.usage(options);
    System.exit(0);
  }
Example #2
0
 /**
  * Deletes all data stored in the triplestore (drops all named graphs and cleans the default
  * graph).
  */
 public void deleteAll() {
   report("Sending delete command to the triple store.");
   if (!this.dryrun) storage.deleteAll();
   report("Deleted all data from the triplestore.");
 }