/**
   * static method to convert host to JsonObject
   *
   * @param host
   * @return
   */
  public static JSONObject psHost2Json(PsHost host, String detailLevel) {
    JSONObject json = new JSONObject();

    json.put(PsHost.ID, int2String(host.getId()));
    json.put(PsHost.HOSTNAME, host.getHostname());

    if (!PsApi.DETAIL_LEVEL_LOW.equals(detailLevel)) {

      json.put(PsHost.IPV4, host.getIpv4());
      json.put(PsHost.IPV6, host.getIpv6());

      JSONArray services = new JSONArray();
      Iterator<PsService> iter = host.serviceIterator();
      while (iter.hasNext()) {

        PsService service = (PsService) iter.next();
        if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) {
          JSONObject serviceObject = toJson(service, PsApi.DETAIL_LEVEL_LOW);
          services.add(serviceObject);
        }
        if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) {
          JSONObject serviceObject = toJson(service, PsApi.DETAIL_LEVEL_HIGH);
          services.add(serviceObject);
        }
      }
      json.put(PsHost.SERVICES, services);
    }

    return json;
  }
  public static JSONObject toJson(PsSite site, String detailLevel) {
    JSONObject json = new JSONObject();
    if (site != null) {
      json.put(PsSite.ID, int2String(site.getId()));
      json.put(PsSite.NAME, site.getName());

      if (!PsApi.DETAIL_LEVEL_LOW.equals(detailLevel)) {

        json.put(PsSite.DESCRIPTION, site.getDescription());
        json.put(PsSite.STATUS, site.getStatus());

        JSONArray listOfHosts = new JSONArray();
        Collection<PsHost> listOfHostsInSite = site.getHosts();
        Iterator<PsHost> iter = listOfHostsInSite.iterator();
        while (iter.hasNext()) {
          PsHost currentHost = (PsHost) iter.next();
          if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) {
            JSONObject currentHostJson = toJson(currentHost, PsApi.DETAIL_LEVEL_LOW);
            listOfHosts.add(currentHostJson);
          }
          if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) {
            JSONObject currentHostJson = toJson(currentHost, PsApi.DETAIL_LEVEL_HIGH);
            listOfHosts.add(currentHostJson);
          }
        }
        json.put(PsSite.HOSTS, listOfHosts);
      }
    }
    return json;
  }
  private static JSONObject serviceParametersAsJson(PsJob job) {
    JSONObject serviceParameters = new JSONObject();

    Iterator iter = job.getParameters().keySet().iterator();
    while (iter.hasNext()) {
      String key = (String) iter.next();

      Object value = job.getParameters().get(key);
      serviceParameters.put(key, value);
    }
    return serviceParameters;
  }
  /**
   * get service parameters of this particular service and convert them into JSON object
   *
   * @param service
   * @return
   */
  public static JSONObject serviceParametersAsJson(PsService service) {
    JSONObject serviceParameters = new JSONObject();

    Iterator iter = service.getParameters().keySet().iterator();
    while (iter.hasNext()) {
      String key = (String) iter.next();

      Object value = service.getParameters().get(key);

      // check if parameter name indicates that it is an object id
      if (thisIsObjectId(key)) {
        serviceParameters.put(key, value + "");
      } else {
        serviceParameters.put(key, value);
      }
    }
    return serviceParameters;
  }
  public static JSONObject toJson(PsServiceType serviceType) {
    JSONObject json = new JSONObject();
    // warning: externally id means 'service type id'
    json.put(PsServiceType.ID, serviceType.getServiceTypeId());
    // internal id is the id in the database
    json.put(PsServiceType.INTERNAL_ID, int2String(serviceType.getId()));
    json.put(PsServiceType.JOB_TYPE, serviceType.getJobType());
    json.put(PsServiceType.NAME, serviceType.getName());

    // get serviceParameterInfo
    TreeMap<String, PsParameterInfo> serviceParameterInfo = serviceType.getServiceParameterInfo();
    JSONObject serviceParameterInfoJson = new JSONObject();
    if (serviceParameterInfo != null) {
      Iterator keyIterator = serviceParameterInfo.keySet().iterator();
      while (keyIterator.hasNext()) {
        String currentParameterName = (String) keyIterator.next();
        PsParameterInfo currentParameterInfo = serviceParameterInfo.get(currentParameterName);
        JSONObject currentParameterInfoAsJson = toJson(currentParameterInfo);
        serviceParameterInfoJson.put(currentParameterName, currentParameterInfoAsJson);
      }
    }
    json.put(PsServiceType.SERVICE_PARAMETER_INFO, serviceParameterInfoJson);

    // get result parameter info
    TreeMap<String, PsParameterInfo> resultParameterInfo = serviceType.getResultParameterInfo();
    JSONObject resultParameterInfoJson = new JSONObject();

    if (resultParameterInfo != null) {
      Iterator keyIterator2 = resultParameterInfo.keySet().iterator();
      while (keyIterator2.hasNext()) {
        String currentParameterName = (String) keyIterator2.next();
        PsParameterInfo currentParameterInfo = resultParameterInfo.get(currentParameterName);
        JSONObject currentParameterInfoAsJson = toJson(currentParameterInfo);
        resultParameterInfoJson.put(currentParameterName, currentParameterInfoAsJson);
      }
    }
    json.put(PsServiceType.RESULT_PARAMETER_INFO, resultParameterInfoJson);
    return json;
  }
  public static JSONObject toJson(PsCloud cloud, String detailLevel) {
    JSONObject json = new JSONObject();
    if (cloud != null) {
      json.put(PsCloud.ID, int2String(cloud.getId()));
      json.put(PsCloud.NAME, cloud.getName());

      if (!PsApi.DETAIL_LEVEL_LOW.equals(detailLevel)) {

        json.put(PsCloud.STATUS, cloud.getStatus());

        JSONArray sites = new JSONArray();
        Iterator<PsSite> iter = cloud.sitesIterator();
        while (iter.hasNext()) {
          PsSite currentSite = (PsSite) iter.next();
          if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) {
            sites.add(toJson(currentSite, PsApi.DETAIL_LEVEL_LOW));
          }
          if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) {
            sites.add(toJson(currentSite, PsApi.DETAIL_LEVEL_HIGH));
          }
        }
        json.put(PsCloud.SITES, sites);

        JSONArray matrices = new JSONArray();
        Iterator<PsMatrix> iter2 = cloud.matrixIterator();
        while (iter2.hasNext()) {
          PsMatrix currentMatrix = (PsMatrix) iter2.next();
          if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) {
            matrices.add(toJson(currentMatrix, PsApi.DETAIL_LEVEL_LOW));
          }
          if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) {
            matrices.add(toJson(currentMatrix, PsApi.DETAIL_LEVEL_HIGH));
          }
        }
        json.put(PsCloud.MATRICES, matrices);
      }
    }
    return json;
  }