/**
   * take a result object, obtain from it service id get this service from data store update its
   * fields
   *
   * @param result
   */
  public static PsService update(Session session, PsServiceResult result) {

    int serviceId = result.getService_id();

    PsService service = PsDataStore.getService(session, serviceId);

    if (service != null) {

      service.setRunning(false);
      Date prevCheckTime = result.getTime();
      service.setPrevCheckTime(prevCheckTime);

      int checkInterval = service.getCheckInterval();
      Date nextCheckTime =
          new Date(prevCheckTime.getTime() + (long) checkInterval * Timer.ONE_SECOND);
      service.setNextCheckTime(nextCheckTime);

      PsRecentServiceResult recentResult =
          PsServiceResult2RecentServiceResult.copy(session, result);

      service.setResult(recentResult);

      session.save(result);
    }

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

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

      json.put(PsServiceResult.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(PsServiceResult.PARAMETERS, parameters);
    }

    return json;
  }