/**
   * convert service result object to JSON
   *
   * @param result
   * @return
   */
  public static JSONObject toJson(PsRecentServiceResult result) {
    JSONObject json = new JSONObject();

    if (result != null) {
      json.put(PsRecentServiceResult.ID, int2String(result.getId()));
      json.put(PsRecentServiceResult.SERVICE_RESULT_ID, int2String(result.getServiceResultId()));
      json.put(PsRecentServiceResult.JOB_ID, int2String(result.getJob_id()));
      json.put(PsRecentServiceResult.SERVICE_ID, int2String(result.getService_id()));
      json.put(PsRecentServiceResult.STATUS, result.getStatus());
      json.put(PsRecentServiceResult.MESSAGE, result.getMessage());

      json.put(PsRecentServiceResult.TIME, IsoDateConverter.dateToString(result.getTime()));

      JSONObject parameters = new JSONObject();
      TreeMap<String, Object> treeMap = result.getParameters();
      for (Map.Entry<String, Object> entry : treeMap.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        parameters.put(key, value);
      }
      json.put(PsRecentServiceResult.PARAMETERS, parameters);
    }

    return json;
  }
  /**
   * Convert perfsonar service to Json object according to specified detail level
   *
   * @param service
   * @return
   */
  public static JSONObject toJson(PsService service, String detailLevel) {

    JSONObject json = new JSONObject();

    if (service != null) {

      json.put(PsService.ID, int2String(service.getId()));
      json.put(PsService.NAME, service.getName());

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

        json.put(PsService.TYPE, service.getType());
        json.put(PsService.DESCRIPTION, service.getDescription());

        // add service parameters

        if (service.getParameters() != null) {
          JSONObject serviceParameters = serviceParametersAsJson(service);
          json.put(PsService.PARAMETERS, serviceParameters);
        }

        json.put(PsService.CHECK_INTERVAL, new Integer(service.getCheckInterval()));
        json.put(PsService.RUNNING, service.isRunning());
        json.put(
            PsService.PREV_CHECK_TIME, IsoDateConverter.dateToString(service.getPrevCheckTime()));
        json.put(
            PsService.NEXT_CHECK_TIME, IsoDateConverter.dateToString(service.getNextCheckTime()));
        json.put(PsService.RUNNING_SINCE, IsoDateConverter.dateToString(service.getRunningSince()));
        json.put(PsService.TIMEOUT, service.getTimeout());

        // add result
        PsRecentServiceResult result = service.getResult();
        JSONObject jsonResult = toJson(result);
        json.put(PsService.RESULT, jsonResult);
      }
    }

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

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

        json.put(PsMatrix.DETAIL_LEVEL, matrix.getDetailLevel());
        json.put(PsMatrix.SERVICE_TYPE_ID, matrix.getMatrixType().getServiceTypeId());

        List<String> statusLabels = matrix.getStatusLabels();
        JSONArray jsonArray = new JSONArray();
        if (statusLabels != null) {
          if (!statusLabels.isEmpty()) {
            for (int i = 0; i < statusLabels.size(); i = i + 1) {
              jsonArray.add(statusLabels.get(i));
            }
          }
        }
        json.put(PsMatrix.STATUS_LABELS, jsonArray);

        Date lastUpdateTime = matrix.getLastUpdateTime();
        json.put(PsMatrix.LAST_UPDATE_TIME, IsoDateConverter.dateToString(lastUpdateTime));

        JSONArray rows = new JSONArray();
        for (int i = 0; i < matrix.getNumberOfRows(); i = i + 1) {
          PsHost currentHost = matrix.getHostInRow(i);
          JSONObject hostAsJson = toJson(currentHost, PsApi.DETAIL_LEVEL_LOW);
          // rows.add(currentHost.getHostname());
          rows.add(hostAsJson);
        }
        json.put(PsMatrix.ROWS, rows);

        JSONArray columns = new JSONArray();
        for (int i = 0; i < matrix.getNumberOfColumns(); i = i + 1) {
          PsHost currentHost = matrix.getHostInColumn(i);
          JSONObject hostAsJson = toJson(currentHost, PsApi.DETAIL_LEVEL_LOW);
          // columns.add(currentHost.getHostname());
          columns.add(hostAsJson);
        }
        json.put(PsMatrix.COLUMNS, columns);

        JSONArray serviceNames = new JSONArray();
        for (int i = 0; i < matrix.getNumberOfServiceNames(); i = i + 1) {
          serviceNames.add(matrix.getServiceNames().get(i));
        }
        json.put(PsMatrix.SERVICE_NAMES, serviceNames);

        JSONArray matrixArray = new JSONArray();
        for (int i = 0; i < matrix.getNumberOfColumns(); i = i + 1) {
          JSONArray rowsArray = new JSONArray();
          for (int j = 0; j < matrix.getNumberOfRows(); j = j + 1) {
            JSONArray serviceArray = new JSONArray();
            for (int k = 0; k < matrix.getNumberOfServiceNames(); k = k + 1) {
              PsService currentService = matrix.getService(i, j, k);

              String serviceDetailLevel = PsApi.DETAIL_LEVEL_HIGH;
              if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) {
                serviceDetailLevel = PsApi.DETAIL_LEVEL_LOW;
              }
              if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) {
                serviceDetailLevel = PsApi.DETAIL_LEVEL_HIGH;
              }
              JSONObject currentServiceJson =
                  JsonConverter.toJson(currentService, serviceDetailLevel);
              serviceArray.add(currentServiceJson);
            }
            rowsArray.add(serviceArray);
          }
          matrixArray.add(rowsArray);
        }
        json.put(PsMatrix.MATRIX, matrixArray);
      }
    }

    return json;
  }