/**
  * Add literal data to the request
  *
  * @param parameterID the ID of the input paramter according to the describe process
  * @param value the value. other types than strings have to be converted to string. The datatype
  *     is automatically determined and set accordingly to the process description
  */
 public void addLiteralData(String parameterID, String value) {
   InputDescriptionType inputDesc = this.getParameterDescription(parameterID);
   if (inputDesc == null) {
     throw new IllegalArgumentException("inputDesription is null for: " + parameterID);
   }
   if (inputDesc.getLiteralData() == null) {
     throw new IllegalArgumentException(
         "inputDescription is not of type literalData: " + parameterID);
   }
   InputType input = execute.getExecute().getDataInputs().addNewInput();
   input.addNewIdentifier().setStringValue(parameterID);
   input.addNewData().addNewLiteralData().setStringValue(value);
   DomainMetadataType dataType = inputDesc.getLiteralData().getDataType();
   if (dataType != null) {
     input.getData().getLiteralData().setDataType(dataType.getReference());
   }
 }
  /**
   * Sets a reference to input data
   *
   * @param parameterID ID of the input element
   * @param value reference URL
   * @param schema schema if applicable otherwise null
   * @param encoding encoding if applicable (typically not), otherwise null
   * @param mimetype mimetype of the input according to the process description. has to be set
   */
  public void addComplexDataReference(
      String parameterID, String value, String schema, String encoding, String mimetype) {
    InputDescriptionType inputDesc = getParameterDescription(parameterID);
    if (inputDesc == null) {
      throw new IllegalArgumentException("inputDesription is null for: " + parameterID);
    }
    if (inputDesc.getComplexData() == null) {
      throw new IllegalArgumentException(
          "inputDescription is not of type complexData: " + parameterID);
    }

    InputType input = execute.getExecute().getDataInputs().addNewInput();
    input.addNewIdentifier().setStringValue(parameterID);
    input.addNewReference().setHref(value);
    if (schema != null) {
      input.getReference().setSchema(schema);
    }

    if (encoding != null) {
      input.getReference().setEncoding(encoding);
    }
    if (mimetype != null) {
      input.getReference().setMimeType(mimetype);
    }
  }
  /**
   * add an input element. sets the data in the xml request
   *
   * @param parameterID the ID of the input (see process description)
   * @param value the actual value (for xml data xml for binary data is should be base64 encoded
   *     data)
   * @param schema schema if applicable otherwise null
   * @param encoding encoding if not the default encoding (for default encoding set it to null)
   *     (i.e. binary data, use base64)
   * @param mimeType mimetype of the data, has to be set
   * @throws WPSClientException
   */
  public void addComplexData(
      String parameterID, IData value, String schema, String encoding, String mimeType)
      throws WPSClientException {
    GeneratorFactory fac = StaticDataHandlerRepository.getGeneratorFactory();
    InputDescriptionType inputDesc = getParameterDescription(parameterID);
    if (inputDesc == null) {
      throw new IllegalArgumentException("inputDesription is null for: " + parameterID);
    }
    if (inputDesc.getComplexData() == null) {
      throw new IllegalArgumentException(
          "inputDescription is not of type ComplexData: " + parameterID);
    }

    LOGGER.debug(
        "Looking for matching Generator ..."
            + " schema: "
            + schema
            + " mimeType: "
            + mimeType
            + " encoding: "
            + encoding);

    IGenerator generator = fac.getGenerator(schema, mimeType, encoding, value.getClass());

    if (generator == null) {
      // generator is still null
      throw new IllegalArgumentException(
          "Could not find an appropriate generator for parameter: " + parameterID);
    }

    InputStream stream = null;

    InputType input = execute.getExecute().getDataInputs().addNewInput();
    input.addNewIdentifier().setStringValue(inputDesc.getIdentifier().getStringValue());
    // encoding is UTF-8 (or nothing and we default to UTF-8)
    // everything that goes to this condition should be inline xml data
    try {

      if (encoding == null
          || encoding.equals("")
          || encoding.equalsIgnoreCase(IOHandler.DEFAULT_ENCODING)) {
        stream = generator.generateStream(value, mimeType, schema);

      } else if (encoding.equalsIgnoreCase("base64")) {
        stream = generator.generateBase64Stream(value, mimeType, schema);
      } else {
        throw new WPSClientException("Encoding not supported");
      }
      ComplexDataType data = input.addNewData().addNewComplexData();
      data.set(XmlObject.Factory.parse(stream));
      if (schema != null) {
        data.setSchema(schema);
      }
      if (mimeType != null) {
        data.setMimeType(mimeType);
      }
      if (encoding != null) {
        data.setEncoding(encoding);
      }
    } catch (XmlException e) {
      throw new IllegalArgumentException("error inserting node into execute request", e);
    } catch (IOException e) {
      throw new IllegalArgumentException("error reading generator output", e);
    }
  }
  /**
   * return a KVP representation for the created execute document.
   *
   * @return KVP request string
   */
  public String getExecuteAsGETString() {
    String request = "?service=wps&request=execute&version=1.0.0&identifier=";
    request = request + processDesc.getIdentifier().getStringValue();
    request = request + "&DataInputs=";
    InputType[] inputs = execute.getExecute().getDataInputs().getInputArray();
    int inputCounter = 0;
    for (InputType input : inputs) {

      request = request + input.getIdentifier().getStringValue();

      if (input.isSetReference()) {
        // reference
        InputReferenceType reference = input.getReference();
        request = request + "=" + "@xlink:href=" + URLEncoder.encode(reference.getHref());
        if (reference.isSetEncoding()) {
          request = request + "@encoding=" + reference.getEncoding();
        }
        if (reference.isSetMimeType()) {
          request = request + "@format=" + reference.getMimeType();
        }
        if (reference.isSetEncoding()) {
          request = request + "@schema=" + reference.getSchema();
        }
      }
      if (input.isSetData()) {
        if (input.getData().isSetComplexData()) {
          // complex
          ComplexDataType complexData = input.getData().getComplexData();
          request = request + "=" + URLEncoder.encode(input.getData().getComplexData().xmlText());
          if (complexData.isSetEncoding()) {
            request = request + "@encoding=" + complexData.getEncoding();
          }
          if (complexData.isSetMimeType()) {
            request = request + "@format=" + complexData.getMimeType();
          }
          if (complexData.isSetEncoding()) {
            request = request + "@schema=" + complexData.getSchema();
          }
        }
        if (input.getData().isSetLiteralData()) {
          // literal
          LiteralDataType literalData = input.getData().getLiteralData();
          request = request + "=" + literalData.getStringValue();
          if (literalData.isSetDataType()) {
            request = request + "@datatype=" + literalData.getDataType();
          }
          if (literalData.isSetUom()) {
            request = request + "@datatype=" + literalData.getUom();
          }
        }
      }
      // concatenation for next input element
      inputCounter = inputCounter + 1;
      if (inputCounter < inputs.length) {
        request = request + ";";
      }
    }
    if (execute.getExecute().getResponseForm().getResponseDocument() == null) {
      throw new RuntimeException("Responresponsedocument=se Form missing");
    }
    DocumentOutputDefinitionType[] outputs =
        execute.getExecute().getResponseForm().getResponseDocument().getOutputArray();
    int outputCounter = 0;
    if (execute.getExecute().getResponseForm().isSetRawDataOutput()) {
      request = request + "&rawdataoutput=";
    } else {
      request = request + "&responsedocument=";
    }
    for (DocumentOutputDefinitionType output : outputs) {
      request = request + output.getIdentifier().getStringValue();
      if (output.isSetEncoding()) {
        request = request + "@encoding=" + output.getEncoding();
      }
      if (output.isSetMimeType()) {
        request = request + "@format=" + output.getMimeType();
      }
      if (output.isSetEncoding()) {
        request = request + "@schema=" + output.getSchema();
      }
      if (output.isSetUom()) {
        request = request + "@datatype=" + output.getUom();
      }
      // concatenation for next output element
      outputCounter = outputCounter + 1;
      if (outputCounter < outputs.length) {
        request = request + ";";
      }
    }

    if (execute.getExecute().getResponseForm().getResponseDocument().isSetStoreExecuteResponse()) {
      request = request + "&storeExecuteResponse=true";
    }
    if (execute.getExecute().getResponseForm().getResponseDocument().isSetStatus()) {
      request = request + "&status=true";
    }
    if (execute.getExecute().getResponseForm().getResponseDocument().isSetLineage()) {
      request = request + "&lineage=true";
    }

    return request;
  }