/** Do our best to get the value out. There might be a better way to do this. */
 protected String getStringValue(ParameterDefinition definition) {
   if (definition instanceof ChoiceParameterDefinition) {
     return ((ChoiceParameterDefinition) definition).getChoicesText();
   } else {
     ParameterValue value = definition.getDefaultParameterValue();
     return getStringValue(value);
   }
 }
  public List<ParameterValue> createDefaultValues() {
    List<ParameterValue> paramValues = new ArrayList<ParameterValue>();

    if (parameterDefinitions != null && !parameterDefinitions.isEmpty()) {
      for (ParameterDefinition d : parameterDefinitions) {
        paramValues.add(d.getDefaultParameterValue());
      }
    }
    return paramValues;
  }
 private ArrayList<ParameterValue> getDefaultBuildParameters() {
   ArrayList<ParameterValue> values = new ArrayList<ParameterValue>();
   ParametersDefinitionProperty property = project.getProperty(ParametersDefinitionProperty.class);
   if (property != null) {
     for (ParameterDefinition pd : property.getParameterDefinitions()) {
       values.add(pd.getDefaultParameterValue());
     }
   }
   return values;
 }
  /**
   * Actually build a project, passing in parameters where appropriate
   *
   * @param project
   * @return
   */
  protected final boolean performBuildProject(AbstractProject<?, ?> project) {
    if (!project.hasPermission(AbstractProject.BUILD)) {
      LOGGER.log(Level.WARNING, "Insufficient permission to build job '" + project.getName() + "'");
      return false;
    }

    if (action.equals(BuildAction.POLL_SCM)) {
      project.schedulePolling();
      return true;
    }

    // no user parameters provided, just build it
    if (param == null) {
      project.scheduleBuild(new Cause.UserIdCause());
      return true;
    }

    ParametersDefinitionProperty pp =
        (ParametersDefinitionProperty) project.getProperty(ParametersDefinitionProperty.class);

    // project does not except any parameters, just build it
    if (pp == null) {
      project.scheduleBuild(new Cause.UserIdCause());
      return true;
    }

    List<ParameterDefinition> parameterDefinitions = pp.getParameterDefinitions();
    List<ParameterValue> values = new ArrayList<ParameterValue>();

    for (ParameterDefinition paramDef : parameterDefinitions) {

      if (!(paramDef instanceof StringParameterDefinition)) {
        // TODO add support for other parameter types
        values.add(paramDef.getDefaultParameterValue());
        continue;
      }

      StringParameterDefinition stringParamDef = (StringParameterDefinition) paramDef;
      ParameterValue value;

      // Did user supply this parameter?
      if (param.containsKey(paramDef.getName())) {
        value = stringParamDef.createValue(param.get(stringParamDef.getName()));
      } else {
        // No, then use the default value
        value = stringParamDef.createValue(stringParamDef.getDefaultValue());
      }

      values.add(value);
    }

    Jenkins.getInstance().getQueue().schedule(pp.getOwner(), 1, new ParametersAction(values));
    return true;
  }
  private static ParametersAction getDefaultParameters(AbstractProject<?, ?> project) {
    ParametersDefinitionProperty property = project.getProperty(ParametersDefinitionProperty.class);
    if (property == null) {
      return null;
    }

    List<ParameterValue> parameters = new ArrayList<ParameterValue>();
    for (ParameterDefinition pd : property.getParameterDefinitions()) {
      ParameterValue param = pd.getDefaultParameterValue();
      if (param != null) parameters.add(param);
    }

    return new ParametersAction(parameters);
  }
  /*
   * Determine the Hudson parameter values from the OSLC parameter instances
   * in the AutomationRequest
   */
  private List<ParameterValue> getParameterValues(
      AbstractProject<?, ?> project, ParameterInstance[] parameters) {
    ParametersDefinitionProperty pp = project.getProperty(ParametersDefinitionProperty.class);
    if (pp == null) {
      LOG.log(Level.FINE, "Job does not take parameters: " + project.getName());
      throw HttpResponses.status(
          HttpServletResponse.SC_BAD_REQUEST); // This build is not parameterized.
    }

    HashMap<String, String> inputMap = new HashMap<String, String>();
    for (ParameterInstance param : parameters) {
      inputMap.put(param.getName(), param.getValue());
    }

    List<ParameterValue> values = new ArrayList<ParameterValue>();
    for (ParameterDefinition def : pp.getParameterDefinitions()) {
      String inputValue = inputMap.get(def.getName());
      if (inputValue == null) {
        ParameterValue defaultValue = def.getDefaultParameterValue();
        if (defaultValue == null) {
          LOG.log(
              Level.FINE, "Missing parameter " + def.getName() + " for job " + project.getName());
          throw HttpResponses.status(HttpServletResponse.SC_BAD_REQUEST);
        }

        values.add(defaultValue);
      } else {
        if (def instanceof SimpleParameterDefinition) {
          SimpleParameterDefinition simple = (SimpleParameterDefinition) def;
          values.add(simple.createValue(inputValue));
        } else {
          LOG.log(
              Level.WARNING,
              "Unsupported parameter type with name "
                  + def.getName()
                  + " for project "
                  + project.getName());
          throw HttpResponses.status(HttpServletResponse.SC_NOT_IMPLEMENTED);
        }
      }
    }

    return values;
  }
  /*
   * Convert an individual Hudson parameter definition to an OSLC Property.
   */
  private Property toProperty(StaplerRequest request, ParameterDefinition def)
      throws URISyntaxException {
    Property prop = new Property();
    prop.setName(def.getName());
    prop.setDescription(def.getDescription());

    if (def instanceof BooleanParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDboolean.getURI()));
    } else if (def instanceof StringParameterDefinition
        || def instanceof PasswordParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDstring.getURI()));
    } else if (def instanceof ChoiceParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDstring.getURI()));
      ChoiceParameterDefinition choices = (ChoiceParameterDefinition) def;
      prop.setAllowedValuesCollection(choices.getChoices());
    }
    // TODO: Other types?

    ParameterValue defaultValue = def.getDefaultParameterValue();
    if (defaultValue == null) {
      prop.setOccurs(Occurs.ExactlyOne);
    } else {
      prop.setOccurs(Occurs.ZeroOrOne);
      if (!defaultValue.isSensitive()) {
        if (defaultValue instanceof BooleanParameterValue) {
          BooleanParameterValue bool = (BooleanParameterValue) defaultValue;
          prop.setDefaultValue(bool.value);
        } else if (defaultValue instanceof StringParameterValue) {
          StringParameterValue str = (StringParameterValue) defaultValue;
          prop.setDefaultValue(str.value);
        }
        // TODO: Other types?
      }
    }

    return prop;
  }