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.MongoCopyEntities.java

License:Apache License

/**
 * /*from   w w w  .j a va 2 s .  c  om*/
 * @param fromInstID
 * @param toScnID
 * @return
 */
public String copyInstallationToScenario(String instID, String toScnID, DBObject answer, boolean maintainApps) {
    DBObject fromObj = DBConn.getConn().getCollection(MongoInstallations.COL_INSTALLATIONS)
            .findOne(new BasicDBObject("_id", new ObjectId(instID)));
    String oldInstallationID = ((ObjectId) fromObj.get("_id")).toString();
    fromObj.put(MongoInstallations.REF_SCENARIO, toScnID);
    if (answer == null) {
        copyOf(fromObj);
    }
    DBObject res = MongoInstallations.createInstallationObj(fromObj.toString());
    String newID = ((DBObject) res.get("data")).get("_id").toString();
    addInfoForCascadedCopy(res, answer, newID);
    //Copy Appliances of the Installation
    DBObject q = new BasicDBObject(MongoAppliances.REF_INSTALLATION, oldInstallationID);
    DBCursor cursorDoc = DBConn.getConn().getCollection(MongoAppliances.COL_APPLIANCES).find(q);
    Map<String, String> addedAppliances = new HashMap<String, String>();
    while (cursorDoc.hasNext()) {
        DBObject obj = cursorDoc.next();
        String childID = obj.get("_id").toString();
        String ret = copyApplianceToInstallation(childID, newID, res);
        DBObject newApp = (DBObject) JSON.parse(ret);
        String newAppID = ((DBObject) newApp.get("data")).get("_id").toString();
        addedAppliances.put(childID, newAppID);
    }
    //Copy Persons of the Installation
    q = new BasicDBObject(MongoPersons.REF_INSTALLATION, oldInstallationID);
    cursorDoc = DBConn.getConn().getCollection(MongoPersons.COL_PERSONS).find(q);
    while (cursorDoc.hasNext()) {
        DBObject obj = cursorDoc.next();
        String childID = obj.get("_id").toString();
        copyPersonToInstallation(childID, newID, res, maintainApps, addedAppliances);
    }
    return PrettyJSONPrinter.prettyPrint(res);
}

From source file:eu.cassandra.server.mongo.MongoInstallations.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");
    System.out.println(response);
    String objID = new String();
    if (ary) {/*from w  w w .  jav a2s  .com*/
        objID = (String) ((DBObject) ((BasicDBList) data).get(0)).get("_id");
    } else {
        objID = (String) data.get("_id");
    }
    BasicDBList list = new BasicDBList();
    DBObject returnQuery = new MongoDBQueries().getEntity(MongoPersons.COL_PERSONS,
            MongoPersons.REF_INSTALLATION, objID);
    if (returnQuery == null) {
        String warning = "Add at least one person for this installation.";
        list.add(warning);
    }
    returnQuery = new MongoDBQueries().getEntity(MongoAppliances.COL_APPLIANCES,
            MongoAppliances.REF_INSTALLATION, objID);
    if (returnQuery == null) {
        String warning = "Add at least one appliance for this installation.";
        list.add(warning);
    }
    if (!list.isEmpty()) {
        jsonResponse.put("warnings", list);
    }
    return jsonResponse.toString();
}

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

License:Apache License

public static String getParentId(String id) {
    BasicDBList list = ((BasicDBList) ((DBObject) JSON.parse(new MongoPersons().getPerson(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.MongoPersons.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) {//from   w  w  w  .  j av  a2  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(MongoActivities.COL_ACTIVITIES, "pers_id", objID);
    if (returnQuery == null) {
        String warning = "Add at least one activity for this person.";
        list.add(warning);
    }

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

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

License:Apache License

public Response getPersons(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   w  ww . ja v  a  2  s  . co m*/
    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, "Persons retrieved successfully").toString();
    return Utils.returnResponseWithAppend(page, "total_size", new Integer(totalCount));
}

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

License:Apache License

private int addToList(String ref_id, BasicDBList list, String filters, String sort, boolean count,
        HttpHeaders httpHeaders) {//from www. j  ava2 s . c  o m
    String apps = new MongoDBQueries().getEntity(httpHeaders, COL_PERSONS, "inst_id", ref_id, filters, sort, 0,
            0, "Appliances retrieved successfully", count).toString();
    String countResponse = new MongoDBQueries().getEntity(httpHeaders, COL_PERSONS, "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.MongoProjects.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) {/*from w w  w . jav a  2s.  c o m*/
        objID = (String) ((DBObject) ((BasicDBList) data).get(0)).get("_id");
    } else {
        objID = (String) data.get("_id");
    }
    DBObject returnQuery = new MongoDBQueries().getEntity(MongoScenarios.COL_SCENARIOS,
            MongoScenarios.REF_PROJECT, objID);
    System.out.println(returnQuery);
    if (returnQuery == null) {
        BasicDBList list = new BasicDBList();
        String warning = "Add at least one scenario for this project.";
        list.add(warning);
        jsonResponse.put("warnings", list);
    }
    return jsonResponse.toString();
}

From source file:eu.cassandra.server.mongo.MongoScenarios.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) {//from  w w w.  j  a va 2s  .  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(MongoInstallations.COL_INSTALLATIONS,
            MongoInstallations.REF_SCENARIO, objID);
    if (returnQuery == null) {
        String warning = "Add at least one installation for this scenario.";
        list.add(warning);
    }
    returnQuery = new MongoDBQueries().getEntity(MongoSimParam.COL_SIMPARAM, "scn_id", objID);
    if (returnQuery == null) {
        String warning = "Add at least one set of simulation parameters for this scenario.";
        list.add(warning);
    }
    if (!list.isEmpty()) {
        jsonResponse.put("warnings", list);
    }
    return jsonResponse.toString();
}

From source file:eu.cassandra.server.mongo.util.JSONValidator.java

License:Apache License

/**
 * //from   ww w  .  j  a  v  a2s  . co  m
 * @param jsonText
 * @param schemaType
 * @param isUpdate
 * @return
 * @throws IOException
 * @throws JSONSchemaNotValidException
 */
public boolean isValid(String jsonText, int schemaType, boolean isUpdate)
        throws IOException, JSONSchemaNotValidException {
    ObjectMapper mapper = new ObjectMapper();
    JSONSchemaProvider schemaProvider = new JacksonSchemaProvider(mapper);

    String jsonSchema = readFile(getSchemaFileName(schemaType));
    if (isUpdate) {

        //         DBObject jsonSchemaObj = (DBObject)JSON.parse(jsonSchema);
        //         DBObject oidObject = new BasicDBObject("type","string").append("optional", "false").append("properties", new BasicDBObject("$oid",new BasicDBObject("type","string").append("optional", "false")));
        //         DBObject schemaProperties = (DBObject)jsonSchemaObj.get("properties");
        //         schemaProperties.put("_id", oidObject);
        //         jsonSchemaObj.put("properties", schemaProperties);
        //         jsonSchema = jsonSchemaObj.toString();
        DBObject jsonSchemaObj = (DBObject) JSON.parse(jsonSchema);
        jsonSchema = removeKeyFromInternalFields(jsonSchemaObj, "optional").toString();
    }
    JSONSchema schema = schemaProvider.getSchema(jsonSchema);
    List<String> errors = schema.validate(jsonText);
    StringBuilder errorMessage = new StringBuilder();
    for (int i = 0; i < errors.size(); i++) {
        String s = errors.get(i);
        if (i == errors.size() - 1)
            errorMessage.append(s);
        else
            errorMessage.append(s + "\n");
    }
    if (errors.size() != 0)
        throw new JSONSchemaNotValidException(errorMessage.toString());
    else
        return true;
}

From source file:eu.cassandra.server.mongo.util.MongoDBQueries.java

License:Apache License

/**
 * //from  w w w.  ja  va  2s . c  o  m
 * @param dataToInsert
 * @param coll
 * @param refKeyName
 * @param schemaType
 * @return
 */
public DBObject insertNestedDocument(String dataToInsert, String coll, String refKeyName, int schemaType) {
    DBObject data;
    String _id;
    try {
        data = (DBObject) JSON.parse(dataToInsert);
        new JSONValidator().isValid(dataToInsert, schemaType);
        if (!data.containsField("cid"))
            data.put("cid", new ObjectId());
        ensureThatRefKeyExists(data, coll, refKeyName, false);
        _id = data.get(refKeyName).toString();
        DBObject q = new BasicDBObject("_id", new ObjectId(_id));
        DBObject o = new BasicDBObject().append("$set", new BasicDBObject("sim_param", data));
        DBConn.getConn().getCollection(coll).update(q, o);
    } catch (Exception e) {
        return jSON2Rrn.createJSONError(dataToInsert, e);
    }
    return jSON2Rrn.createJSONInsertPostMessage(coll + " with _id=" + _id + " updated with the following data",
            data);
}