Example usage for com.mongodb BasicDBObject toString

List of usage examples for com.mongodb BasicDBObject toString

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject toString.

Prototype

@SuppressWarnings("deprecation")
public String toString() 

Source Link

Document

Returns a JSON serialization of this object

The output will look like: {"a":1, "b":["x","y","z"]} }

Usage

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public int insertRow(String jsonRow, String jsonCheck) throws DScabiException, IOException {

    log.debug("insertRow() firstTime is {}", m_firstTime);
    ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames();
    DMJson djson = new DMJson(jsonRow);
    Set<String> st = djson.keySet();
    BasicDBObject document = new BasicDBObject();
    int n = 0;//  w  w  w  .  jav  a 2  s .  co m
    WriteResult result = null;

    DMJson djsonCheck = new DMJson(jsonCheck);
    Set<String> stCheck = djsonCheck.keySet();
    BasicDBObject documentCheck = new BasicDBObject();

    if (false == isEmpty(fieldList)) {
        if (false == fieldList.containsAll(st)) {
            throw new DScabiException(
                    "One or more field name in jsonRow doesn't exist in fieldNames list. jsonRow : " + jsonRow
                            + " Field Names list : " + fieldList,
                    "DBT.IRW.1");
        }
        if (false == fieldList.containsAll(stCheck)) {
            throw new DScabiException(
                    "One or more field name in jsonCheck doesn't exist in fieldNames list. jsonCheck : "
                            + jsonCheck + " Field Names list : " + fieldList,
                    "DBT.IRW.2");
        }
        if (false == st.containsAll(fieldList)) {
            throw new DScabiException(
                    "One or more field name in fieldNames doesn't exist in jsonRow key set. jsonRow : "
                            + jsonRow + " Field Names list : " + fieldList,
                    "DBT.IRW.3");
        }
        if (fieldList.size() != st.size()) {
            throw new DScabiException("Fields count doesn't match. fieldNames : " + fieldList.toString()
                    + " with jsonRow : " + jsonRow, "DBT.IRW.4");
        }
    }

    if (false == isEmpty(fieldList)) {
        for (String fieldName : st) {
            // create a document to store key and value
            String f = djson.getString(fieldName);
            if (null == f) {
                throw new DScabiException("Field name " + fieldName + " doesn't exist in jsonRow : " + jsonRow
                        + " Field Names list : " + fieldList, "DBT.IRW.5");
            }
            document.put(fieldName, f);
        }

        for (String keyCheck : stCheck) {
            // create a document to store key and value            
            String f2 = djsonCheck.getString(keyCheck);
            if (null == f2) {
                throw new DScabiException(
                        "Field name " + keyCheck + " doesn't exist in jsonCheck : " + jsonCheck, "DBT.IRW.6");
            }
            documentCheck.put(keyCheck, f2);
        }

        DBCursor cursorExist = m_table.find(documentCheck);
        n = cursorExist.count();
        if (0 == n) {
            log.debug("insertRow() Inside 0 == n");
            result = m_table.insert(document);
            log.debug("insertRow() result is : {}", result.getN());
            if (result.getN() < 0)
                throw new DScabiException("Insert failed for document : " + document.toString(), "DBT.IRW.7");
        } else if (1 == n) {
            throw new DScabiException("Row already exists. jsonCheck : " + jsonCheck, "DBT.IRW.8"); // already found
        } else {
            throw new DScabiException("Row already exists, multiple matches. jsonCheck : " + jsonCheck,
                    "DBT.IRW.9"); // already found
        }
    } else {
        for (String key : st) {
            // create a document to store key and value
            String f3 = djson.getString(key);
            if (null == f3) {
                throw new DScabiException("Field name " + key + " doesn't exist in jsonRow : " + jsonRow,
                        "DBT.IRW.10");
            }
            document.put(key, djson.getString(key));
        }

        for (String keyCheck : stCheck) {
            // create a document to store key and value            
            String f4 = djsonCheck.getString(keyCheck);
            if (null == f4) {
                throw new DScabiException(
                        "Field name " + keyCheck + " doesn't exist in jsonCheck : " + jsonCheck, "DBT.IRW.11");
            }
            documentCheck.put(keyCheck, djsonCheck.getString(keyCheck));
        }

        DBCursor cursorExist = m_table.find(documentCheck);
        n = cursorExist.count();
        if (0 == n) {
            log.debug("insertRow() Inside 0 == n");
            result = m_table.insert(document);
            log.debug("insertRow() result is : {}", result.getN());
            if (result.getN() < 0)
                throw new DScabiException("Insert failed for document : " + document.toString(), "DBT.IRW.12");
        } else if (1 == n) {
            throw new DScabiException("Row already exists. jsonCheck : " + jsonCheck, "DBT.IRW.13"); // already found
        } else {
            throw new DScabiException("Row already exists, multiple matches. jsonCheck : " + jsonCheck,
                    "DBT.IRW.14"); // already found
        }
    }
    return result.getN();
}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public int executeUpdate(String jsonUpdate, String jsonWhere) throws IOException, DScabiException {
    ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames();
    DMJson djsonWhere = new DMJson(jsonWhere);
    Set<String> stWhere = djsonWhere.keySet();
    BasicDBObject documentWhere = new BasicDBObject();

    DMJson djsonUpdate = new DMJson(jsonUpdate);
    Set<String> stUpdate = djsonUpdate.keySet();
    BasicDBObject documentUpdate = new BasicDBObject();

    if (false == isEmpty(fieldList)) {
        if (false == fieldList.containsAll(stWhere)) {
            throw new DScabiException(
                    "One or more field name in jsonWhere doesn't exist in fieldNames list. jsonWhere : "
                            + jsonWhere + " Field Names list : " + fieldList,
                    "DBT.EUE.1");
        }// w ww  . ja  va  2s  . c  o  m
        if (false == fieldList.containsAll(stUpdate)) {
            throw new DScabiException(
                    "One or more field name in jsonUpdate doesn't exist in fieldNames list. jsonUpdate : "
                            + jsonUpdate + " Field Names list : " + fieldList,
                    "DBT.EUE.2");
        }
    }

    for (String keyWhere : stWhere) {
        // create a document to store key and value
        documentWhere.put(keyWhere, djsonWhere.getString(keyWhere));
    }

    for (String keyUpdate : stUpdate) {
        // create a document to store key and value
        documentUpdate.put(keyUpdate, djsonUpdate.getString(keyUpdate));
    }

    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", documentUpdate);

    WriteResult result = m_table.updateMulti(documentWhere, updateObj);
    log.debug("executeUpdate() result is : {}", result.getN());
    if (result.getN() <= 0)
        throw new DScabiException("Update failed for documentWhere : " + documentWhere.toString()
                + " updateObj : " + updateObj.toString(), "DBT.EUE.3");

    return result.getN();

}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public int executeRemove(String jsonWhere) throws IOException, DScabiException {
    ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames();
    DMJson djsonWhere = new DMJson(jsonWhere);
    Set<String> stWhere = djsonWhere.keySet();
    BasicDBObject documentWhere = new BasicDBObject();

    if (false == isEmpty(fieldList)) {
        if (false == fieldList.containsAll(stWhere)) {
            throw new DScabiException(
                    "One or more field name in jsonWhere doesn't exist in fieldNames list. jsonWhere : "
                            + jsonWhere + " Field Names list : " + fieldList,
                    "DBT.ERE.1");
        }/* ww  w  .  java 2 s  .c  o  m*/
    }

    for (String keyWhere : stWhere) {
        // create a document to store key and value
        documentWhere.put(keyWhere, djsonWhere.getString(keyWhere));
    }

    // DBObject result = table.findAndRemove(documentWhere);
    WriteResult result = m_table.remove(documentWhere);
    log.debug("executeRemove() result is : {}", result.getN());
    if (result.getN() <= 0)
        throw new DScabiException("Remove failed for documentWhere : " + documentWhere.toString(), "DBT.ERE.2");

    return result.getN();

}

From source file:com.ebay.jetstream.config.mongo.MongoDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationDo> findConfigurationByAppNameAndVersion(BasicDBObject query,
        MongoConnection mongoConnection) {

    List<JetStreamBeanConfigurationDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }/*from   w ww  .j a v  a 2 s  .  com*/

    Exception e = null;
    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.config.mongo.MongoDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationDo> findConfigurationByQuery(BasicDBObject query,
        MongoConnection mongoConnection) {

    List<JetStreamBeanConfigurationDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }/*from  ww w  . java  2 s .c om*/

    Exception e = null;

    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
            //beanConfig = (JetStreamBeanConfigurationDo)fromJson(jsonString, JetStreamBeanConfigurationDo.class);
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.configurationmanagement.MongoLogDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationLogDo> findConfigurationByAppNameAndVersion(BasicDBObject query,
        MongoLogConnection mongoConnection) {

    List<JetStreamBeanConfigurationLogDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationLogDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfiglog collection is unknown");
    }//from w  w w. j ava 2 s. co m

    Exception e = null;
    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.configurationmanagement.MongoLogDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationLogDo> findConfigurationByQuery(BasicDBObject query,
        MongoLogConnection mongoLogConnection) {

    List<JetStreamBeanConfigurationLogDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationLogDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoLogConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfigLog collection is unknown");
    }/*w ww.  ja  va 2s  .  co m*/

    Exception e = null;

    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
            // beanConfig =
            // (JetStreamBeanConfigurationDo)fromJson(jsonString,
            // JetStreamBeanConfigurationDo.class);
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.edgytech.umongo.DocView.java

License:Apache License

public void getMore(final int max) {
    new DbJob() {

        @Override/*from   ww w.  ja v  a 2 s. c o  m*/
        public Object doRun() throws IOException {
            int i = 0;
            while (iterator.hasNext() && (i++ < max || max <= 0)) {
                DBObject obj = iterator.next();
                if (obj == null) {
                    break;
                }
                addDocument(obj, null);
            }
            return null;
        }

        @Override
        public String getNS() {
            if (dbcursor != null) {
                return dbcursor.getCollection().getFullName();
            }
            return getLabel();
        }

        @Override
        public String getShortName() {
            return "Find";
        }

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            if (res == null) {
                // res should be null
                // should have a cursor id now
                if (dbcursor != null) {
                    BasicDBObject desc = getDescription(DocView.this.job.getRoot(dbcursor));
                    getTree().label = desc.toString();
                }
                getTree().structureComponent();
                getTree().expandNode(getTree().getTreeNode());

                DocView.this.updateButtons();
            }
        }
    }.addJob();
}

From source file:com.edgytech.umongo.EditCodeDialog.java

License:Apache License

@Override
public void setValue(Object value) {
    Code code = (Code) value;//from   w w  w  . j ava  2 s  .  c o  m
    setStringFieldValue(Item.value, code.getCode());
    if (code instanceof CodeWScope) {
        BasicDBObject scope = (BasicDBObject) ((CodeWScope) code).getScope();
        setStringFieldValue(Item.scope, scope.toString());
    }
}

From source file:com.emuneee.camerasyncmanager.util.DatabaseUtil.java

License:Apache License

/**
 * Updates a list cameras//from  w w  w.  j  a v a 2  s  .  c o m
 * @param cameras
 * @return
 */
public boolean updateCameras(List<Camera> cameras) {
    sLogger.info("Updating " + cameras.size() + " cameras");
    boolean result = false;
    DB db = getDatabase();

    try {
        DBCollection collection = db.getCollection("camera");

        for (Camera camera : cameras) {
            BasicDBObject query = new BasicDBObject("_id", camera.getId());
            BasicDBObject dbObj = cameraToDBObject(camera);
            dbObj.append("updated", new Date());
            sLogger.debug("Updating: " + dbObj.toString());
            collection.update(query, dbObj);
            result = true;
        }
    } catch (Exception e) {
        sLogger.error("Exception inserting cameras");
        sLogger.error(e);
    }

    return result;
}