/**
   * Process the collection harvesting options form.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @param request the Cocoon request object
   * @return A process result's object.
   */
  public static FlowResult processSetupCollectionHarvesting(
      Context context, int collectionID, Request request)
      throws SQLException, IOException, AuthorizeException {
    FlowResult result = new FlowResult();
    HarvestedCollection hc = HarvestedCollection.find(context, collectionID);

    String contentSource = request.getParameter("source");

    // First, if this is not a harvested collection (anymore), set the harvest type to 0; possibly
    // also wipe harvest settings
    if (contentSource.equals("source_normal")) {
      if (hc != null) {
        hc.delete();
      }

      result.setContinue(true);
    } else {
      FlowResult subResult = testOAISettings(context, request);

      // create a new harvest instance if all the settings check out
      if (hc == null) {
        hc = HarvestedCollection.create(context, collectionID);
      }

      // if the supplied options all check out, set the harvesting parameters on the collection
      if (subResult.getErrors().isEmpty()) {
        String oaiProvider = request.getParameter("oai_provider");
        boolean oaiAllSets = "all".equals(request.getParameter("oai-set-setting"));
        String oaiSetId;
        if (oaiAllSets) {
          oaiSetId = "all";
        } else {
          oaiSetId = request.getParameter("oai_setid");
        }

        String metadataKey = request.getParameter("metadata_format");
        String harvestType = request.getParameter("harvest_level");

        hc.setHarvestParams(Integer.parseInt(harvestType), oaiProvider, oaiSetId, metadataKey);
        hc.setHarvestStatus(HarvestedCollection.STATUS_READY);
      } else {
        result.setErrors(subResult.getErrors());
        result.setContinue(false);
        return result;
      }

      hc.update();
    }

    // Save everything
    context.commit();

    // No notice...
    // result.setMessage(new Message("default","Harvesting options successfully modified."));
    result.setOutcome(true);
    result.setContinue(true);

    return result;
  }
  /**
   * Test the supplied OAI settings.
   *
   * @param context
   * @param request
   */
  public static FlowResult testOAISettings(Context context, Request request) {
    FlowResult result = new FlowResult();

    String oaiProvider = request.getParameter("oai_provider");
    String oaiSetId = request.getParameter("oai_setid");
    oaiSetId = request.getParameter("oai-set-setting");
    if (!"all".equals(oaiSetId)) {
      oaiSetId = request.getParameter("oai_setid");
    }
    String metadataKey = request.getParameter("metadata_format");
    String harvestType = request.getParameter("harvest_level");
    int harvestTypeInt = 0;

    if (oaiProvider == null || oaiProvider.length() == 0) {
      result.addError("oai_provider");
    }
    if (oaiSetId == null || oaiSetId.length() == 0) {
      result.addError("oai_setid");
    }
    if (metadataKey == null || metadataKey.length() == 0) {
      result.addError("metadata_format");
    }
    if (harvestType == null || harvestType.length() == 0) {
      result.addError("harvest_level");
    } else {
      harvestTypeInt = Integer.parseInt(harvestType);
    }

    if (result.getErrors() == null) {
      List<String> testErrors =
          OAIHarvester.verifyOAIharvester(oaiProvider, oaiSetId, metadataKey, (harvestTypeInt > 1));
      result.setErrors(testErrors);
    }

    if (result.getErrors() == null || result.getErrors().isEmpty()) {
      result.setOutcome(true);
      // On a successful test we still want to stay in the loop, not continue out of it
      // result.setContinue(true);
      result.setMessage(new Message("default", "Harvesting settings are valid."));
    } else {
      result.setOutcome(false);
      result.setContinue(false);
      // don't really need a message when the errors are highlighted already
      // result.setMessage(new Message("default","Harvesting is not properly configured."));
    }

    return result;
  }