private static Object getValue(Object value, final boolean includeType) {

    Object returnValue = value;

    // if the includeType is set to true then show the data types of the properties
    if (includeType) {

      // type will be one of: map, list, string, long, int, double, float.
      // in the event of a complex object it will call a toString and store as a
      // string
      String type = determineType(value);

      ObjectNode valueAndType = jsonNodeFactory.objectNode();
      valueAndType.put(GraphSONTokens.TYPE, type);

      if (type.equals(GraphSONTokens.TYPE_LIST)) {

        // values of lists must be accumulated as ObjectNode objects under the value key.
        // will return as a ArrayNode. called recursively to traverse the entire
        // object graph of each item in the array.
        ArrayNode list = (ArrayNode) value;

        // there is a set of values that must be accumulated as an array under a key
        ArrayNode valueArray = valueAndType.putArray(GraphSONTokens.VALUE);
        for (int ix = 0; ix < list.size(); ix++) {
          // the value of each item in the array is a node object from an ArrayNode...must
          // get the value of it.
          addObject(valueArray, getValue(getTypedValueFromJsonNode(list.get(ix)), includeType));
        }

      } else if (type.equals(GraphSONTokens.TYPE_MAP)) {

        // maps are converted to a ObjectNode.  called recursively to traverse
        // the entire object graph within the map.
        ObjectNode convertedMap = jsonNodeFactory.objectNode();
        ObjectNode jsonObject = (ObjectNode) value;
        Iterator keyIterator = jsonObject.getFieldNames();
        while (keyIterator.hasNext()) {
          Object key = keyIterator.next();

          // no need to getValue() here as this is already a ObjectNode and should have type info
          convertedMap.put(key.toString(), jsonObject.get(key.toString()));
        }

        valueAndType.put(GraphSONTokens.VALUE, convertedMap);
      } else {

        // this must be a primitive value or a complex object.  if a complex
        // object it will be handled by a call to toString and stored as a
        // string value
        putObject(valueAndType, GraphSONTokens.VALUE, value);
      }

      // this goes back as a JSONObject with data type and value
      returnValue = valueAndType;
    }

    return returnValue;
  }
  public ObjectNode toJson() {
    try {
      JsonNodeFactory nf = JsonNodeFactory.instance;
      ObjectNode all = nf.objectNode();
      all.put("allconvs", mAllConvs);
      all.put("convs", mConvs);
      all.put("clicks", mClicks);
      all.put("imps", mImps);
      all.put("bids", mBids);
      all.put("aucs", mAucs);
      all.put("uniques", mUniques);
      ArrayNode winNodes = new ArrayNode(nf);
      Iterator<Double> winIter = mPercentKey.iterator();
      while (winIter.hasNext()) {
        Double winPercent = winIter.next();
        Integer winCount = mWinPercent.get(winPercent);
        ObjectNode winCountN = nf.objectNode();
        winCountN.put("winPercent", winPercent);
        winCountN.put("winCount", winCount);
        winNodes.add(winCountN);
      }
      Iterator<Double> bidIter = mPercentKey.iterator();
      while (bidIter.hasNext()) {
        Double bidPercent = bidIter.next();
        Integer bidCount = mBidPercent.get(bidPercent);
        ObjectNode bidCountN = nf.objectNode();
        bidCountN.put("bidPercent", bidPercent);
        bidCountN.put("bidCount", bidCount);
        winNodes.add(bidCountN);
      }
      all.put("WinBidPercent", winNodes);
      if (mShwhr != null && mShwhr.length() > 0) {
        all.put("shwhr", mShwhr);
      }

      return all;
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      return null;
    }
  }
 private static ArrayNode createJSONList(
     final List list, final List<String> propertyKeys, final boolean showTypes) {
   final ArrayNode jsonList = jsonNodeFactory.arrayNode();
   for (Object item : list) {
     if (item instanceof Element) {
       jsonList.add(createJSONElementAsObjectNode((Element) item, propertyKeys, showTypes));
     } else if (item instanceof List) {
       jsonList.add(createJSONList((List) item, propertyKeys, showTypes));
     } else if (item instanceof Map) {
       jsonList.add(createJSONMap((Map) item, propertyKeys, showTypes));
     } else if (item != null && item.getClass().isArray()) {
       jsonList.add(createJSONList(convertArrayToList(item), propertyKeys, showTypes));
     } else {
       addObject(jsonList, item);
     }
   }
   return jsonList;
 }
  private static ObjectNode createJSONMap(
      final Map map, final List<String> propertyKeys, final boolean showTypes) {
    final ObjectNode jsonMap = jsonNodeFactory.objectNode();
    for (Object key : map.keySet()) {
      Object value = map.get(key);
      if (value != null) {
        if (value instanceof List) {
          value = createJSONList((List) value, propertyKeys, showTypes);
        } else if (value instanceof Map) {
          value = createJSONMap((Map) value, propertyKeys, showTypes);
        } else if (value instanceof Element) {
          value = createJSONElementAsObjectNode((Element) value, propertyKeys, showTypes);
        } else if (value.getClass().isArray()) {
          value = createJSONList(convertArrayToList(value), propertyKeys, showTypes);
        }
      }

      putObject(jsonMap, key.toString(), getValue(value, showTypes));
    }
    return jsonMap;
  }
 private static Object wrapSingleton(Object o) {
   JsonNodeFactory jnf = JsonNodeFactory.instance;
   Object r;
   if (o == null || o instanceof Null) {
     r = makeResultObjectNode(jnf, jnf.nullNode());
   } else if (o instanceof Privacy || o instanceof CharSequence || o instanceof Character) {
     r = makeResultObjectNode(jnf, jnf.textNode(o.toString()));
   } else if (o instanceof Boolean) {
     r = makeResultObjectNode(jnf, jnf.booleanNode((Boolean) o));
   } else if (o instanceof Byte
       || o instanceof Short
       || o instanceof Integer
       || o instanceof Long) {
     r = makeResultObjectNode(jnf, jnf.numberNode(((Number) o).longValue()));
   } else if (o instanceof Float || o instanceof Double) {
     r = makeResultObjectNode(jnf, jnf.numberNode(((Number) o).doubleValue()));
   } else {
     r = o;
   }
   return r;
 }
  @Override
  public void execute() {
    Connection dbConn = null;
    CallableStatement proc = null;
    try {
      dbConn = PostgresConnection.getDataSource().getConnection();
      dbConn.setAutoCommit(true);
      if (map.containsKey("image_url")) {
        proc = dbConn.prepareCall("{? = call create_dm(?,?,?,now()::timestamp,?))}");

      } else {
        proc = dbConn.prepareCall("{? = call create_dm(?,?,?,now()::timestamp)}");
      }

      proc.setPoolable(true);
      proc.registerOutParameter(1, Types.BOOLEAN);
      proc.setInt(2, Integer.parseInt(map.get("sender_id")));
      proc.setInt(3, Integer.parseInt(map.get("reciever_id")));
      proc.setString(4, map.get("dm_text"));
      if (map.containsKey("image_url")) {
        proc.setString(5, map.get("image_url"));
      }
      proc.execute();

      boolean sent = proc.getBoolean(1);

      if (sent) {
        MyObjectMapper mapper = new MyObjectMapper();
        JsonNodeFactory nf = JsonNodeFactory.instance;
        ObjectNode root = nf.objectNode();
        root.put("app", map.get("app"));
        root.put("method", map.get("method"));
        root.put("status", "ok");
        root.put("code", "200");
        try {
          CommandsHelp.submit(
              map.get("app"), mapper.writeValueAsString(root), map.get("correlation_id"), LOGGER);
        } catch (JsonGenerationException e) {
          LOGGER.log(Level.SEVERE, e.getMessage(), e);
        } catch (JsonMappingException e) {
          LOGGER.log(Level.SEVERE, e.getMessage(), e);
        } catch (IOException e) {
          LOGGER.log(Level.SEVERE, e.getMessage(), e);
        }
      } else {
        CommandsHelp.handleError(
            map.get("app"),
            map.get("method"),
            "You can not dm a user who is not following you",
            map.get("correlation_id"),
            LOGGER);
      }

    } catch (PSQLException e) {
      if (e.getMessage().contains("value too long")) {
        CommandsHelp.handleError(
            map.get("app"),
            map.get("method"),
            "DM length cannot exceed 140 character",
            map.get("correlation_id"),
            LOGGER);
      } else {
        CommandsHelp.handleError(
            map.get("app"), map.get("method"), e.getMessage(), map.get("correlation_id"), LOGGER);
      }

      LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } catch (SQLException e) {
      CommandsHelp.handleError(
          map.get("app"), map.get("method"), e.getMessage(), map.get("correlation_id"), LOGGER);
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } finally {
      PostgresConnection.disconnect(null, proc, dbConn);
    }
  }
  @Override
  public void execute() {
    Connection dbConn = null;
    CallableStatement proc = null;
    try {
      dbConn = PostgresConnection.getDataSource().getConnection();
      dbConn.setAutoCommit(true);
      proc = dbConn.prepareCall("{call create_list(?,?,?,?,now()::timestamp)}");
      proc.setPoolable(true);

      proc.setString(1, map.get("name"));
      proc.setString(2, map.get("description"));
      proc.setInt(3, Integer.parseInt(map.get("creator_id")));
      proc.setBoolean(4, Boolean.parseBoolean(map.get("private")));
      proc.execute();

      MyObjectMapper mapper = new MyObjectMapper();
      JsonNodeFactory nf = JsonNodeFactory.instance;
      ObjectNode root = nf.objectNode();
      root.put("app", map.get("app"));
      root.put("method", map.get("method"));
      root.put("status", "ok");
      root.put("code", "200");
      try {
        CommandsHelp.submit(
            map.get("app"), mapper.writeValueAsString(root), map.get("correlation_id"), LOGGER);
      } catch (JsonGenerationException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
      } catch (JsonMappingException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
      } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
      }
    } catch (PSQLException e) {
      if (e.getMessage().contains("unique constraint")) {
        if (e.getMessage().contains("(name)")) {
          CommandsHelp.handleError(
              map.get("app"),
              map.get("method"),
              "List name already exists",
              map.get("correlation_id"),
              LOGGER);
        }
      }
      if (e.getMessage().contains("value too long")) {
        CommandsHelp.handleError(
            map.get("app"), map.get("method"), "Too long input", map.get("correlation_id"), LOGGER);
      }
      CommandsHelp.handleError(
          map.get("app"),
          map.get("method"),
          "List name already exists",
          map.get("correlation_id"),
          LOGGER);
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } catch (SQLException e) {
      CommandsHelp.handleError(
          map.get("app"),
          map.get("method"),
          "List name already exists",
          map.get("correlation_id"),
          LOGGER);
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } finally {
      PostgresConnection.disconnect(null, proc, dbConn);
    }
  }
  @Override
  public void execute() {
    Connection dbConn = null;
    CallableStatement proc = null;
    ResultSet set = null;
    try {
      dbConn = PostgresConnection.getDataSource().getConnection();
      dbConn.setAutoCommit(false);
      proc = dbConn.prepareCall("{? = call get_user_favorites(?)}");
      proc.setPoolable(true);
      proc.registerOutParameter(1, Types.OTHER);
      proc.setInt(2, Integer.parseInt(map.get("user_id")));
      proc.execute();

      set = (ResultSet) proc.getObject(1);

      MyObjectMapper mapper = new MyObjectMapper();
      JsonNodeFactory nf = JsonNodeFactory.instance;
      ObjectNode root = nf.objectNode();
      ArrayNode tweets = nf.arrayNode();
      root.put("app", map.get("app"));
      root.put("method", map.get("method"));
      root.put("status", "ok");
      root.put("code", "200");

      while (set.next()) {
        Integer id = set.getInt(1);
        String tweet = set.getString(2);
        String image_url = set.getString(3);
        Timestamp created_at = set.getTimestamp(4);
        String creator_name = set.getString(5);
        String creator_username = set.getString(6);
        String creator_avatar = set.getString(7);

        Tweet t = new Tweet();
        t.setId(id);
        t.setTweetText(tweet);
        t.setImageUrl(image_url);
        t.setCreatedAt(created_at);
        User creator = new User();
        creator.setName(creator_name);
        creator.setAvatarUrl(creator_avatar);
        creator.setUsername(creator_username);
        t.setCreator(creator);

        tweets.addPOJO(t);
      }

      root.put("favorites", tweets);
      try {
        CommandsHelp.submit(
            map.get("app"), mapper.writeValueAsString(root), map.get("correlation_id"), LOGGER);
      } catch (JsonGenerationException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
      } catch (JsonMappingException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
      } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
      }

      dbConn.commit();
    } catch (PSQLException e) {
      CommandsHelp.handleError(
          map.get("app"), map.get("method"), e.getMessage(), map.get("correlation_id"), LOGGER);
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } catch (SQLException e) {
      CommandsHelp.handleError(
          map.get("app"), map.get("method"), e.getMessage(), map.get("correlation_id"), LOGGER);
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } finally {
      PostgresConnection.disconnect(set, proc, dbConn);
    }
  }
  public static Object to(Object o, int type) {
    if (o == null) return null;

    switch (type) {
      case BOOLEAN:
        {
          if (o instanceof Boolean) return o;
          else if (o instanceof Number) return ((Number) o).longValue() != 0L;
          else if (o instanceof CharSequence || o instanceof Character)
            return Boolean.parseBoolean(o.toString());
          else throw new IllegalArgumentException();
        }

      case INT:
        {
          if (o instanceof Boolean) return ((Boolean) o) ? 1L : 0L;
          else if (o instanceof Number) return ((Number) o).longValue();
          else if (o instanceof CharSequence || o instanceof Character)
            return Long.parseLong(o.toString());
          else throw new IllegalArgumentException();
        }

      case FLOAT:
        {
          if (o instanceof Boolean) return ((Boolean) o) ? 1.0 : 0.0;
          else if (o instanceof Number) return ((Number) o).doubleValue();
          else if (o instanceof CharSequence || o instanceof Character)
            return Double.parseDouble(o.toString());
          else throw new IllegalArgumentException();
        }

      case STRING:
        return ObjectUtils.toString(o, "null");

      case JSON:
        {
          JsonNodeFactory jnf = JsonNodeFactory.instance;
          if (o instanceof Boolean) return jnf.booleanNode((Boolean) o);
          else if (o instanceof Number) return jnf.numberNode(((Number) o).longValue());
          else if (o instanceof CharSequence || o instanceof Character)
            try {
              return JsonHelper.parse(o.toString());
            } catch (Exception e) {
              return jnf.nullNode();
            }
          else if (o instanceof JsonNode) return o;
          else if (o instanceof Collection) {
            ArrayNode an = jnf.arrayNode();
            for (Object e : (Collection) o) {
              Object v = ValuesNewAccount.to(e, JSON);
              an.add(v instanceof JsonNode ? (JsonNode) v : jnf.nullNode());
            }
            return an;
          } else if (o instanceof Iterator) {
            ArrayNode an = jnf.arrayNode();
            Iterator iter = (Iterator) o;
            while (iter.hasNext()) {
              Object v = ValuesNewAccount.to(iter.next(), JSON);
              an.add(v instanceof JsonNode ? (JsonNode) v : jnf.nullNode());
            }
            return an;
          } else if (o instanceof Map) {
            ObjectNode on = jnf.objectNode();
            for (Object e0 : ((Map) o).entrySet()) {
              Map.Entry e = (Map.Entry) e0;
              Object v = ValuesNewAccount.to(e.getValue(), JSON);
              on.put(e.getKey().toString(), v instanceof JsonNode ? (JsonNode) v : jnf.nullNode());
            }
            return on;
          }
        }
        break;
    }
    return o;
  }
 private static JsonNode makeResultObjectNode(JsonNodeFactory jnf, JsonNode jn) {
   ObjectNode r = jnf.objectNode();
   r.put("result", jn);
   return r;
 }