Example usage for com.mongodb WriteResult toString

List of usage examples for com.mongodb WriteResult toString

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * PUT a json file to the database/*from  w ww.  j  a  v  a  2s .c  om*/
 * @param collName the name of the collection
 * @param docID the docid of the resource 
 * @param json the json to put there
 * @return the server response
 */
@Override
public String putToDb(String collName, String docID, String json) throws DbException {
    try {
        DBObject doc = (DBObject) JSON.parse(json);
        doc.put(JSONKeys.DOCID, docID);
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(JSONKeys.DOCID, docID);
        WriteResult result = coll.update(query, doc, true, false);
        //return removeFromDb( path );
        return result.toString();
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * PUT a json file to the database using dbase, docid and version
 * @param collName the name of the collection
 * @param dbase the name of the database 
 * @param docid the document identifier/*from w w  w  .ja  v a 2s  .c om*/
 * @param version the version of the document
 * @param json the json to put there
 * @return the server response
 */
@Override
public String putToDb(String collName, String dbase, String docid, String version, String json)
        throws DbException {
    try {
        DBObject doc = (DBObject) JSON.parse(json);
        doc.put(JSONKeys.DOCID, docid);
        connect();
        DBObject query = getThreeFieldQuery(JSONKeys.DBASE, dbase, JSONKeys.DOCID, docid, JSONKeys.VERSION1,
                version);
        DBCollection coll = getCollectionFromName(collName);
        WriteResult result = coll.update(query, doc, true, false);
        //return removeFromDb( path );
        return result.toString();
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * PUT a new json file to the database//from  ww w  . ja  v  a2  s .  co  m
 * @param collName the name of the collection
 * @param json the json to put there
 * @return the server response
 */
@Override
public String addToDb(String collName, String json) throws DbException {
    try {
        DBObject doc = (DBObject) JSON.parse(json);
        connect();
        DBCollection coll = getCollectionFromName(collName);
        if (doc.containsField(JSONKeys._ID)) {
            Object id = doc.get(JSONKeys._ID);
            DBObject query = new BasicDBObject(JSONKeys._ID, id);
            if (query != null) {
                WriteResult result = coll.update(query, doc, true, false);
                return result.toString();
            } else
                throw new Exception("Failed to update object " + id);
        } else {
            WriteResult result = coll.insert(doc, WriteConcern.ACKNOWLEDGED);
            // return the new document's id
            ObjectId id = (ObjectId) doc.get("_id");
            JSONObject jDoc = (JSONObject) JSONValue.parse(result.toString());
            jDoc.put("_id", id.toString());
            return jDoc.toJSONString();
        }
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

private String removeFromDbByThreeFields(String collName, String field1, String value1, String field2,
        String value2, String field3, String value3) throws DbException {
    try {/*from  www . j  a va2s. c o  m*/
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = getThreeFieldQuery(field1, value1, field2, value2, field3, value3);
        WriteResult result = coll.remove(query);
        if (result != null)
            return result.toString();
        else
            return null;
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * Remove a document from the database by a unique field value
 * @param collName name of the collection
 * @param field the name of the field /*from   w w w. j av  a  2s.  c o  m*/
 * @param value the value of the field
 * @return the server response
 */
@Override
public String removeFromDbByField(String collName, String field, String value) throws DbException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        //new BasicDBObject("_id", new ObjectId(idString));
        Object obj = value;
        if (field.equals(JSONKeys._ID))
            obj = new ObjectId(value);
        DBObject query = new BasicDBObject(field, obj);
        WriteResult result = coll.remove(query);
        return result.toString();
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * Remove an entire set of documents that match a regular expression.
 * @param collName the collection to remove from
 * @param key the key field to match/*from  ww  w . j a  v a 2 s  .  co  m*/
 * @param expr the regular expression for key's values
 * @return the result
 * @throws DbException 
 */
public String removeFromDbByExpr(String collName, String key, String expr) throws DbException {
    try {
        DBCollection coll = getCollectionFromName(collName);
        if (coll != null) {
            BasicDBObject q = new BasicDBObject();
            q.put(key, Pattern.compile(expr));
            WriteResult result = coll.remove(q);
            return result.toString();
        } else
            throw new Exception("Collection " + collName + " not found");
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * PUT a json file to the database//from  w  w  w  .j a  va2  s.c o m
 * @param collName the name of the collection
 * @param docID the docid of the resource 
 * @param json the json to put there
 * @return the server response
 */
@Override
public String putToDb(String collName, String docID, String json) throws AeseException {
    try {
        docIDCheck(collName, docID);
        DBObject doc = (DBObject) JSON.parse(json);
        doc.put(JSONKeys.DOCID, docID);
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(JSONKeys.DOCID, docID);
        WriteResult result = coll.update(query, doc, true, false);
        //return removeFromDb( path );
        return result.toString();
    } catch (Exception e) {
        throw new AeseException(e);
    }
}

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Remove a document from the database/*from ww w  .ja v a  2  s.  co  m*/
 * @param collName name of the collection
 * @param docID the docid of the resource 
 * @param json the json to put there
 * @return the server response
 */
@Override
public String removeFromDb(String collName, String docID) throws AeseException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(JSONKeys.DOCID, docID);
        WriteResult result = coll.remove(query);
        return result.toString();
    } catch (Exception e) {
        throw new AeseException(e);
    }
}

From source file:com.andreig.jetty.AdminServlet.java

License:GNU General Public License

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doDelete()");

    if (!can_admin(req)) {
        res.sendError(SC_UNAUTHORIZED);/*from  w  w w.  jav  a  2 s . c o  m*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String user = req.getParameter("user");
    if (db_name == null || user == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);
    WriteResult o = db.removeUser(user);

    out_str(req, o.toString(), "application/json");

}

From source file:com.andreig.jetty.AdminServlet.java

License:GNU General Public License

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPut()");

    if (!can_admin(req)) {
        res.sendError(SC_UNAUTHORIZED);/*from www .  jav  a2  s. c o  m*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (db_name == null || user == null || passwd == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }
    boolean read_only = Boolean.parseBoolean(req.getParameter("readonly"));

    DB db = mongo.getDB(db_name);
    WriteResult o = db.addUser(user, passwd.toCharArray(), read_only);

    out_str(req, o.toString(), "application/json");

}