Example usage for com.mongodb.util JSON parse

List of usage examples for com.mongodb.util JSON parse

Introduction

In this page you can find the example usage for com.mongodb.util JSON parse.

Prototype

public static Object parse(final String jsonString) 

Source Link

Document

Parses a JSON string and returns a corresponding Java object.

Usage

From source file:eu.cassandra.server.mongo.csn.MongoCluster.java

License:Apache License

/**
 * /*from w w  w.ja v a 2s.  co  m*/
 * @param message
 * @param httpHeaders
 * @return
 */
public DBObject cluster(String message) {
    JSONtoReturn jSON2Rrn = new JSONtoReturn();
    try {
        new JSONValidator().isValid(message, JSONValidator.CLUSTER_PARAM_SCHEMA);
        DBObject params = (DBObject) JSON.parse(message);
        String graph_id = params.get("graph_id").toString();
        String run_id = null;
        String name = params.get("name").toString();
        String clusterbasedon = params.get("clusterbasedon").toString();

        DBObject r = DBConn.getConn().getCollection(MongoGraphs.COL_GRAPHS)
                .findOne(new BasicDBObject("_id", new ObjectId(graph_id)));
        if (r.containsField("run_id")) {
            run_id = r.get("run_id").toString();
        }

        String clusterBasedOn = params.get("clusterbasedon").toString();
        Integer numberOfClusters = Integer.parseInt(params.get("n").toString());
        String clusterMethod = params.get("clustermethod").toString();
        if (clusterMethod.equalsIgnoreCase("kmeans")) {
            return clusterKmeans(message, graph_id, run_id, clusterBasedOn, numberOfClusters, name,
                    clusterbasedon);
        } else if (clusterMethod.equalsIgnoreCase("hierarchical")) {
            return clusterHierarchical(message, graph_id, run_id, clusterBasedOn, numberOfClusters, name,
                    clusterbasedon);
        } else if (clusterMethod.equalsIgnoreCase("graphedgebetweenness")) {
            return clusterGraphEgdetweenness(message, graph_id, run_id, clusterBasedOn, numberOfClusters, name,
                    clusterbasedon);
        } else
            return null;
    } catch (Exception e) {
        e.printStackTrace();
        return jSON2Rrn.createJSONError(message, e);
    }
}

From source file:eu.cassandra.server.mongo.MongoActivities.java

License:Apache License

public static String getParentId(String id) {
    BasicDBList list = ((BasicDBList) ((DBObject) JSON.parse(new MongoActivities().getActivity(null, id)))
            .get("data"));
    if (list == null || list.isEmpty())
        return null;
    return (String) ((DBObject) list.get(0)).get(REF_PERSON);
}

From source file:eu.cassandra.server.mongo.MongoActivities.java

License:Apache License

private String withAddedWarnings(String response, boolean ary) {
    if (Utils.failed(response))
        return response;
    DBObject jsonResponse = (DBObject) JSON.parse(response);
    DBObject data = (DBObject) jsonResponse.get("data");
    String objID = new String();
    if (ary) {//  w  w w.java  2 s . c  o m
        objID = (String) ((DBObject) ((BasicDBList) data).get(0)).get("_id");
    } else {
        objID = (String) data.get("_id");
    }
    BasicDBList list = new BasicDBList();
    DBObject returnQuery = new MongoDBQueries().getEntity(MongoActivityModels.COL_ACTMODELS, "act_id", objID);
    if (returnQuery == null) {
        String warning = "Add at least one activity model for this activity.";
        list.add(warning);
    }

    if (!list.isEmpty()) {
        jsonResponse.put("warnings", list);
    }
    return jsonResponse.toString();
}

From source file:eu.cassandra.server.mongo.MongoActivityModels.java

License:Apache License

public static DBObject createActivityModelObj(String dataToInsert) {
    MongoDBQueries q = new MongoDBQueries();
    DBObject obj = (DBObject) JSON.parse(dataToInsert);
    BasicDBList apps = (BasicDBList) obj.get(REF_CONTAINSAPPLIANCES);
    String act_id = (String) obj.get("act_id");
    boolean checkPass = checkAppliancesExists(apps, act_id);
    DBObject returnObj = null;/*from w w w . ja  v  a  2s.  c o  m*/
    if (checkPass) {
        returnObj = q.insertData(COL_ACTMODELS, dataToInsert, "Activity Model created successfully",
                new String[] { MongoActivities.COL_ACTIVITIES, MongoDistributions.COL_DISTRIBUTIONS,
                        MongoDistributions.COL_DISTRIBUTIONS, MongoDistributions.COL_DISTRIBUTIONS },
                new String[] { "act_id", "duration", "startTime", "repeatsNrOfTime" },
                new boolean[] { false, true, true, true }, JSONValidator.ACTIVITYMODEL_SCHEMA);
        if (Utils.failed(returnObj.toString())) {
            // Perhaps should be added to the user library
            returnObj = q.insertData(COL_ACTMODELS, dataToInsert, "Activity Model created successfully",
                    new String[] { "users" }, new String[] { "act_id" }, new boolean[] { false },
                    JSONValidator.ACTIVITYMODEL_SCHEMA);
        }
    } else {
        returnObj = new JSONtoReturn().createJSONError(
                "Appliances do not exist " + "in the Installation or Activity Model defined inside in the user "
                        + "library should not contain appliances",
                "Activity model appliances error");
    }
    return returnObj;
}

From source file:eu.cassandra.server.mongo.MongoActivityModels.java

License:Apache License

private String withAddedWarnings(String response, boolean ary, boolean appliances) {
    if (Utils.failed(response))
        return response;
    DBObject jsonResponse = (DBObject) JSON.parse(response);
    DBObject data = (DBObject) jsonResponse.get("data");
    if (ary) {/* w w w .j av a  2s  .co m*/
        data = (DBObject) ((BasicDBList) data).get(0);
    }
    BasicDBList list = new BasicDBList();
    if (appliances && (((BasicDBList) data.get("containsAppliances")) == null
            || ((BasicDBList) data.get("containsAppliances")).isEmpty())) {
        String warning = "Add at least one appliance for this activity model.";
        list.add(warning);
    }
    if ((String) data.get("duration") == null
            || ((String) data.get("duration") != null && ((String) data.get("duration")).isEmpty())) {
        String warning = "Add a duration distribution for this activity model.";
        list.add(warning);
    }
    if ((String) data.get("startTime") == null
            || ((String) data.get("startTime") != null && ((String) data.get("startTime")).isEmpty())) {
        String warning = "Add a start time distribution for this activity model.";
        list.add(warning);
    }
    if ((String) data.get("repeatsNrOfTime") == null || ((String) data.get("repeatsNrOfTime") != null
            && ((String) data.get("repeatsNrOfTime")).isEmpty())) {
        String warning = "Add a number of times distribution for this activity model.";
        list.add(warning);
    }
    if (!list.isEmpty()) {
        jsonResponse.put("warnings", list);
    }
    return jsonResponse.toString();
}

From source file:eu.cassandra.server.mongo.MongoActivityModels.java

License:Apache License

/**
 * curl -X PUT -d @activitymodel.json   --header Content-type:application/json   http://localhost:8080/cassandra/api/actmod/4fec8b53df4ffdb8d1d1ce57
 * //from   www  .  j  av a2 s.c o m
 * @param cid
 * @param jsonToUpdate
 * @return
 */
public String updateActivityModel(String id, String jsonToUpdate) {
    MongoDBQueries q = new MongoDBQueries();
    DBObject obj = (DBObject) JSON.parse(jsonToUpdate);
    BasicDBList apps = (BasicDBList) obj.get(REF_CONTAINSAPPLIANCES);
    String act_id = (String) obj.get("act_id");
    boolean checkPass = checkAppliancesExists(apps, act_id);
    String returnMsg = null;
    boolean appliances = true;
    if (checkPass) {
        returnMsg = q
                .updateDocument("_id", id, jsonToUpdate, COL_ACTMODELS, "Activity Model updated successfully",
                        MongoActivities.COL_ACTIVITIES, "act_id", JSONValidator.ACTIVITYMODEL_SCHEMA)
                .toString();
        if (Utils.failed(returnMsg)) {
            // Perhaps should be added to the user library
            returnMsg = q.updateDocument("_id", id, jsonToUpdate, COL_ACTMODELS,
                    "Activity Model updated successfully", "users", "act_id",
                    JSONValidator.ACTIVITYMODEL_SCHEMA).toString();
            appliances = false;
        }
    } else {
        returnMsg = new JSONtoReturn()
                .createJSONError("Appliances do not exist "
                        + "in the Installation or Activity Model defined inside in the user "
                        + "library should not contain appliances", "Activity model appliances error")
                .toString();
    }
    return withAddedWarnings(returnMsg, true, appliances);
}

From source file:eu.cassandra.server.mongo.MongoAppliances.java

License:Apache License

public static String getParentId(String id) {
    BasicDBList list = ((BasicDBList) ((DBObject) JSON.parse(new MongoAppliances().getAppliance(null, id)))
            .get("data"));
    if (list == null || list.isEmpty())
        return null;
    return (String) ((DBObject) list.get(0)).get(REF_INSTALLATION);
}

From source file:eu.cassandra.server.mongo.MongoAppliances.java

License:Apache License

public Response getAppliances(HttpHeaders httpHeaders, String scn_id, String filters, String sort, int limit,
        int skip, boolean count, boolean pertype, boolean lib) {
    // Search for the installations based on the scenario
    String installations = (new MongoInstallations()).getInstallations(httpHeaders, scn_id, null, null, 0, 0,
            false, false);/*from ww  w.j a  va 2s .com*/
    DBObject jsonResponse = (DBObject) JSON.parse(installations);
    BasicDBList list = (BasicDBList) jsonResponse.get("data");
    // Retrieve the ids
    BasicDBList appsList = new BasicDBList();
    int totalCount = 0;
    for (Object o : list) {
        DBObject dbo = (DBObject) o;
        String inst_id = (String) dbo.get("_id");
        // For each one gather the data and load a DBCursor
        totalCount += addToList(inst_id, appsList, filters, sort, count, httpHeaders);
    }
    if (lib) {
        // search also in the corresponding lib
        totalCount += addToList(scn_id, appsList, filters, sort, count, httpHeaders);
    }
    // Use limit and skip for creating a sublist => return it
    Vector<DBObject> vec = new Vector<DBObject>();
    for (Object o : appsList) {
        DBObject dbo = (DBObject) o;
        vec.add(dbo);
    }
    JSONtoReturn jSON2Rrn = new JSONtoReturn();
    String page = jSON2Rrn.createJSON(vec, "Appliances retrieved successfully").toString();
    return Utils.returnResponseWithAppend(page, "total_size", new Integer(totalCount));
}

From source file:eu.cassandra.server.mongo.MongoAppliances.java

License:Apache License

private int addToList(String ref_id, BasicDBList list, String filters, String sort, boolean count,
        HttpHeaders httpHeaders) {//  w  w w.ja  v a  2s  .  c  o  m
    String apps = new MongoDBQueries().getEntity(httpHeaders, COL_APPLIANCES, "inst_id", ref_id, filters, sort,
            0, 0, "Appliances retrieved successfully", count).toString();
    String countResponse = new MongoDBQueries().getEntity(httpHeaders, COL_APPLIANCES, "inst_id", ref_id, null,
            null, 0, 0, "Appliances retrieved successfully", true).toString();
    DBObject response = (DBObject) JSON.parse(countResponse);
    BasicDBList alist = (BasicDBList) response.get("data");
    DBObject object = (DBObject) alist.get(0);
    Integer aint = (Integer) object.get("count");
    DBObject jsonResponse = (DBObject) JSON.parse(apps);
    BasicDBList appsInstList = (BasicDBList) jsonResponse.get("data");
    list.addAll(appsInstList);
    return aint.intValue();
}

From source file:eu.cassandra.server.mongo.MongoAppliances.java

License:Apache License

private String withAddedWarnings(String response, boolean ary) {
    if (Utils.failed(response))
        return response;
    DBObject jsonResponse = (DBObject) JSON.parse(response);
    DBObject data = (DBObject) jsonResponse.get("data");
    String objID = new String();
    if (ary) {//w ww .j av  a  2 s .  c o  m
        objID = (String) ((DBObject) ((BasicDBList) data).get(0)).get("_id");
    } else {
        objID = (String) data.get("_id");
    }
    DBObject returnQuery = new MongoDBQueries().getEntity(MongoConsumptionModels.COL_CONSMODELS, "app_id",
            objID);
    if (returnQuery == null) {
        BasicDBList list = new BasicDBList();
        String warning = "Add a consumption model for this appliance.";
        list.add(warning);
        jsonResponse.put("warnings", list);
    }
    return jsonResponse.toString();
}