/**
   * Creates a {@link hudson.model.StringParameterValue} and adds it to the provided list. If the
   * parameter with the same name already exists in the list it will be replaced by the new
   * parameter, but its description will be used, unless the parameter type is something else than a
   * StringParameterValue.
   *
   * @param parameters the list of existing parameters.
   * @param value the value.
   * @param escapeQuotes if quote characters should be escaped.
   */
  public void setOrCreateStringParameterValue(
      List<ParameterValue> parameters, String value, boolean escapeQuotes) {
    ParameterValue parameter = null;
    for (ParameterValue p : parameters) {
      if (p.getName().toUpperCase().equals(this.name())) {
        parameter = p;
        break;
      }
    }
    String description = null;
    if (parameter != null) {
      if (parameter instanceof StringParameterValue) {
        // Perhaps it is manually added to remind the user of what it is for.
        description = parameter.getDescription();
      }
      parameters.remove(parameter);
    }
    String stringValue;
    if (escapeQuotes) {
      stringValue = StringUtil.escapeQuotes(value);
    } else {
      stringValue = value;
    }
    if (stringValue == null) {
      stringValue = "";
    }

    parameter = new StringParameterValue(this.name(), stringValue, description);
    parameters.add(parameter);
  }
  /**
   * Creates a {@link hudson.model.ParameterValue} and adds it to the provided list. If the
   * parameter with the same name already exists in the list it will be replaced by the new
   * parameter, but its description will be used, unless the parameter type is something else than a
   * StringParameterValue.
   *
   * @param parameters the list of existing parameters.
   * @param value the value.
   * @param escapeQuotes if quote characters should be escaped.
   * @param clazz the class which extends {@link hudson.model.ParameterValue}.
   */
  private void setOrCreateParameterValue(
      List<ParameterValue> parameters,
      String value,
      boolean escapeQuotes,
      Class<? extends StringParameterValue> clazz) {
    ParameterValue parameter = null;
    for (ParameterValue p : parameters) {
      if (p.getName().toUpperCase().equals(this.name())) {
        parameter = p;
        break;
      }
    }
    String description = null;
    if (parameter != null) {
      if (parameter instanceof StringParameterValue) {
        // Perhaps it is manually added to remind the user of what it is for.
        description = parameter.getDescription();
      }
      parameters.remove(parameter);
    }
    String stringValue;
    if (escapeQuotes) {
      stringValue = StringUtil.escapeQuotes(value);
    } else {
      stringValue = value;
    }
    if (stringValue == null) {
      stringValue = "";
    }

    Class<?>[] types = {String.class, String.class, String.class};
    Object[] args = {this.name(), stringValue, description};
    Constructor<? extends StringParameterValue> constructor;
    try {
      constructor = clazz.getConstructor(types);
      parameter = constructor.newInstance(args);
      parameters.add(parameter);
    } catch (Exception ex) {
      parameter = null;
    }
  }
  /**
   * Adds or sets all the Gerrit-parameter values to the provided list.
   *
   * @param gerritEvent the event.
   * @param project the project for which the parameters are being set
   * @param parameters the default parameters
   * @see #setOrCreateStringParameterValue(java.util.List, String, boolean)
   */
  public static void setOrCreateParameters(
      GerritTriggeredEvent gerritEvent, AbstractProject project, List<ParameterValue> parameters) {

    boolean noNameAndEmailParameters = false;
    boolean escapeQuotes = false;
    if (project != null) {
      GerritTrigger trigger = GerritTrigger.getTrigger(project);
      if (trigger != null) {
        noNameAndEmailParameters = trigger.isNoNameAndEmailParameters();
        escapeQuotes = trigger.isEscapeQuotes();
      }
    }

    GERRIT_EVENT_TYPE.setOrCreateStringParameterValue(
        parameters, gerritEvent.getEventType().getTypeValue(), escapeQuotes);
    GERRIT_EVENT_HASH.setOrCreateStringParameterValue(
        parameters, String.valueOf(((java.lang.Object) gerritEvent).hashCode()), escapeQuotes);
    if (gerritEvent instanceof ChangeBasedEvent) {
      ChangeBasedEvent event = (ChangeBasedEvent) gerritEvent;
      GERRIT_BRANCH.setOrCreateStringParameterValue(
          parameters, event.getChange().getBranch(), escapeQuotes);
      GERRIT_TOPIC.setOrCreateStringParameterValue(
          parameters, event.getChange().getTopic(), escapeQuotes);
      GERRIT_CHANGE_NUMBER.setOrCreateStringParameterValue(
          parameters, event.getChange().getNumber(), escapeQuotes);
      GERRIT_CHANGE_ID.setOrCreateStringParameterValue(
          parameters, event.getChange().getId(), escapeQuotes);
      String pNumber = null;
      if (null != event.getPatchSet()) {
        pNumber = event.getPatchSet().getNumber();
        GERRIT_PATCHSET_NUMBER.setOrCreateStringParameterValue(parameters, pNumber, escapeQuotes);
        GERRIT_PATCHSET_REVISION.setOrCreateStringParameterValue(
            parameters, event.getPatchSet().getRevision(), escapeQuotes);
        GERRIT_REFSPEC.setOrCreateStringParameterValue(
            parameters, StringUtil.makeRefSpec(event), escapeQuotes);
      }
      GERRIT_PROJECT.setOrCreateStringParameterValue(
          parameters, event.getChange().getProject(), escapeQuotes);
      if (event instanceof ChangeRestored) {
        if (!noNameAndEmailParameters) {
          GERRIT_CHANGE_RESTORER.setOrCreateStringParameterValue(
              parameters, getNameAndEmail(((ChangeRestored) event).getRestorer()), escapeQuotes);
        }
        GERRIT_CHANGE_RESTORER_NAME.setOrCreateStringParameterValue(
            parameters, getName(((ChangeRestored) event).getRestorer()), escapeQuotes);
        GERRIT_CHANGE_RESTORER_EMAIL.setOrCreateStringParameterValue(
            parameters, getEmail(((ChangeRestored) event).getRestorer()), escapeQuotes);
      }
      GERRIT_CHANGE_SUBJECT.setOrCreateStringParameterValue(
          parameters, event.getChange().getSubject(), escapeQuotes);

      String url = getURL(event, project);

      String commitMessage = event.getChange().getCommitMessage();
      if (commitMessage != null) {
        try {
          byte[] encodedBytes = Base64.encodeBase64(commitMessage.getBytes("UTF-8"));
          GERRIT_CHANGE_COMMIT_MESSAGE.setOrCreateStringParameterValue(
              parameters, new String(encodedBytes), escapeQuotes);
        } catch (UnsupportedEncodingException uee) {
          logger.error("Failed to encode commit message as Base64: ", uee);
        }
      }
      GERRIT_CHANGE_URL.setOrCreateStringParameterValue(parameters, url, escapeQuotes);
      if (event instanceof ChangeAbandoned) {
        if (!noNameAndEmailParameters) {
          GERRIT_CHANGE_ABANDONER.setOrCreateStringParameterValue(
              parameters, getNameAndEmail(((ChangeAbandoned) event).getAbandoner()), escapeQuotes);
        }
        GERRIT_CHANGE_ABANDONER_NAME.setOrCreateStringParameterValue(
            parameters, getName(((ChangeAbandoned) event).getAbandoner()), escapeQuotes);
        GERRIT_CHANGE_ABANDONER_EMAIL.setOrCreateStringParameterValue(
            parameters, getEmail(((ChangeAbandoned) event).getAbandoner()), escapeQuotes);
      }
      if (!noNameAndEmailParameters) {
        GERRIT_CHANGE_OWNER.setOrCreateStringParameterValue(
            parameters, getNameAndEmail(event.getChange().getOwner()), escapeQuotes);
      }
      GERRIT_CHANGE_OWNER_NAME.setOrCreateStringParameterValue(
          parameters, getName(event.getChange().getOwner()), escapeQuotes);
      GERRIT_CHANGE_OWNER_EMAIL.setOrCreateStringParameterValue(
          parameters, getEmail(event.getChange().getOwner()), escapeQuotes);
      Account uploader = findUploader(event);
      if (!noNameAndEmailParameters) {
        GERRIT_PATCHSET_UPLOADER.setOrCreateStringParameterValue(
            parameters, getNameAndEmail(uploader), escapeQuotes);
      }
      GERRIT_PATCHSET_UPLOADER_NAME.setOrCreateStringParameterValue(
          parameters, getName(uploader), escapeQuotes);
      GERRIT_PATCHSET_UPLOADER_EMAIL.setOrCreateStringParameterValue(
          parameters, getEmail(uploader), escapeQuotes);
    } else if (gerritEvent instanceof RefUpdated) {
      RefUpdated event = (RefUpdated) gerritEvent;
      GERRIT_REFNAME.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getRefName(), escapeQuotes);
      GERRIT_PROJECT.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getProject(), escapeQuotes);
      GERRIT_OLDREV.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getOldRev(), escapeQuotes);
      GERRIT_NEWREV.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getNewRev(), escapeQuotes);
    }
    Account account = gerritEvent.getAccount();
    if (account != null) {
      if (!noNameAndEmailParameters) {
        GERRIT_EVENT_ACCOUNT.setOrCreateStringParameterValue(
            parameters, getNameAndEmail(account), escapeQuotes);
      }
      GERRIT_EVENT_ACCOUNT_NAME.setOrCreateStringParameterValue(
          parameters, getName(account), escapeQuotes);
      GERRIT_EVENT_ACCOUNT_EMAIL.setOrCreateStringParameterValue(
          parameters, getEmail(account), escapeQuotes);
    }
    Provider provider = gerritEvent.getProvider();
    if (provider != null) {
      GERRIT_NAME.setOrCreateStringParameterValue(parameters, provider.getName(), escapeQuotes);
      GERRIT_HOST.setOrCreateStringParameterValue(parameters, provider.getHost(), escapeQuotes);
      GERRIT_PORT.setOrCreateStringParameterValue(parameters, provider.getPort(), escapeQuotes);
      GERRIT_SCHEME.setOrCreateStringParameterValue(parameters, provider.getScheme(), escapeQuotes);
      GERRIT_VERSION.setOrCreateStringParameterValue(
          parameters, provider.getVersion(), escapeQuotes);
    }
  }