Example usage for com.mongodb BasicDBObject getString

List of usage examples for com.mongodb BasicDBObject getString

Introduction

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

Prototype

public String getString(final String key) 

Source Link

Document

Returns the value of a field as a string

Usage

From source file:MDBInt.DBMongo.java

License:Apache License

public void insertFederatedCloud(String dbName, String collectionName, String docJSON) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, collectionName);
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    String userName;/*from  ww w .  j av a 2 s .com*/
    userName = obj.getString("cloudId");
    obj.append("_id", userName);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);
}

From source file:mini_mirc_server.miniIRCHandler.java

public String GetMessage(String username) {
    String ret = "";
    try {//  w  w w .ja  v a 2 s . c  o m
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("inbox");
        BasicDBObject query = new BasicDBObject("target", username);
        JSONObject obj = new JSONObject();
        JSONArray arr = new JSONArray();
        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                BasicDBObject temp = (BasicDBObject) cursor.next();
                JSONObject sav = new JSONObject();
                sav.put("target", temp.getString("target"));
                sav.put("username", temp.getString("username"));
                sav.put("channel", temp.getString("channel"));
                sav.put("message", temp.getString("message"));
                sav.put("timestamp", temp.getLong("timestamp"));
                arr.add(sav);
                coll.remove(temp);
            }
            obj.put("msg", arr);
            ret = obj.toJSONString();
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    UpdateLastActive(username);
    return ret;
}

From source file:mini_mirc_server.miniIRCHandler.java

private int PutMessageWild(String username, String msg) {
    int ret = 0;/*w  w  w.j  av  a  2  s  . c o  m*/

    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll2 = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", username);
        System.out.println("Wild message appear from " + username + " !");
        DBCursor cursor = coll2.find(query);
        try {
            while (cursor.hasNext()) {
                ret = 1;
                BasicDBObject temp = (BasicDBObject) cursor.next();
                String channelname = temp.getString("channel");
                ret = PutMessage(username, channelname, msg);
            }
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

    return 0;
}

From source file:mini_mirc_server.Mini_mirc_server.java

public static void cleaner() {
    try {/*from  ww w.ja v a2 s . c  o  m*/
        boolean hard_clean = false;
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");

        DBCursor cursor = coll.find();
        try {
            Date now = new Date();
            long timestamp_now = now.getTime();
            long treshold = timestamp_now - (1000 * 60 * 5); //5 minutes
            while (cursor.hasNext()) {
                hard_clean = true;
                BasicDBObject temp = (BasicDBObject) cursor.next();
                Date time_temp = (Date) temp.get("timestamp");
                long timestamp_temp = time_temp.getTime();
                if (timestamp_temp < treshold) {
                    String target = temp.getString("username");
                    handler.SoftDelete(target);
                }
            }
            HardClean();
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(Mini_mirc_server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:mini_mirc_server.Mini_mirc_server.java

public static void HardClean() {
    try {//from  w w w  .  ja  v  a 2  s.c o m
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll[] = new DBCollection[4];
        coll[0] = db.getCollection("channelCollection");
        coll[1] = db.getCollection("inbox");
        coll[2] = db.getCollection("activeUser");
        coll[3] = db.getCollection("passiveUser");

        DBCursor cursor = coll[3].find();

        try {
            while (cursor.hasNext()) {
                BasicDBObject temp = (BasicDBObject) cursor.next();
                String username = temp.getString("username");
                BasicDBObject query = new BasicDBObject("username", username);
                System.out.println("cleaning " + username);
                for (int i = 0; i < 4; i++) {
                    DBCursor cursor2 = coll[i].find(query);

                    try {
                        while (cursor2.hasNext()) {
                            DBObject temp2 = cursor2.next();
                            coll[i].remove(temp2);
                        }
                    } finally {
                        cursor2.close();
                    }
                }
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(Mini_mirc_server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Modelo.DAO.MongoDAO.java

@SuppressWarnings("empty-statement")
public static List<String> AllCamara() {

    List<BasicDBObject> marca = new ArrayList<>();
    List<String> marca1 = new ArrayList<>();

    try {// w w w .j  av a 2  s  . c  o m
        Conexion con = new Conexion();
        mongoClient = con.conectarMongo();
    } catch (UnknownHostException ex) {
        System.out.println("Imposible realizar la conexin: " + ex.getMessage());
    }
    DB db = mongoClient.getDB("test");

    DBCollection docs = db.getCollection("fotitos");

    BasicDBObject query = new BasicDBObject();

    BasicDBObject fields = new BasicDBObject("marca", true).append("_id", false);

    try (DBCursor cursor = docs.find(query, fields)) {
        while (cursor.hasNext()) {
            marca.add((BasicDBObject) cursor.next());

        }

        for (BasicDBObject objeto : marca) {
            if (objeto.getString("marca") != null && !marca1.contains(objeto.getString("marca"))) {
                marca1.add(objeto.getString("marca"));

            }
            ;
        }
    }

    return (marca1);
}

From source file:Modelo.DAO.MongoDAO.java

public static List<String> AllModelo(String camara) {

    List<BasicDBObject> modelo1 = new ArrayList<>();
    List<String> modelo = new ArrayList<>();

    try {//from  ww  w . j  a  v a2s. c o m
        Conexion con = new Conexion();
        mongoClient = con.conectarMongo();
    } catch (UnknownHostException ex) {
        System.out.println("Imposible realizar la conexin: " + ex.getMessage());
    }
    DB db = mongoClient.getDB("test");
    DBCollection docs = db.getCollection("fotitos");

    try (DBCursor cur = docs.find(new BasicDBObject("marca", camara), new BasicDBObject("modelo", 1))) {
        while (cur.hasNext()) {
            modelo1.add((BasicDBObject) cur.next());

        }
        for (BasicDBObject objeto : modelo1) {
            if (objeto.getString("modelo") != null && !modelo.contains(objeto.getString("modelo"))) {

                modelo.add(objeto.getString("modelo"));

            }
        }
    }

    return (modelo);
}

From source file:mx.edu.cide.justiciacotidiana.v1.model.Comentario.java

License:Open Source License

/**
 * Genera un BasicDBObject a partir de una cadena JSON que representa un comentario.
 * @param content Cadena con el JSON del comentario.
 * @return BasicDBObject si el comentario es vlido y contiene al menos los campos requeridos, null en otro caso.
 *//*from   w w  w.  ja  v a  2  s .c  o  m*/
public static BasicDBObject parse(String content) {
    BasicDBObject obj = (BasicDBObject) JSON.parse(content);
    BasicDBObject from = (BasicDBObject) obj.get(FIELDS.FROM);
    String checkedVal = "";

    List<String> params = new ArrayList<String>();
    params.add(FIELDS.MESSAGE);
    params.add(FIELDS.PROPOSALID);

    if (!Utils.validateEmptyStringFields(obj, params)) {
        return null;
    }

    if (null == from) {
        return null;
    }

    checkedVal = from.getString(FIELDS.FACEBOOKUSER);
    if (null == checkedVal || checkedVal.length() == 0)
        return null;

    //Remove unmodifiable fields
    checkedVal = obj.getString(FIELDS.CREATED);
    if (null != checkedVal)
        obj.remove(FIELDS.CREATED);
    checkedVal = obj.getString(FIELDS.UPDATED);
    if (null != checkedVal)
        obj.remove(FIELDS.UPDATED);
    checkedVal = obj.getString(FIELDS.SWBPAGE);
    if (null != checkedVal)
        obj.remove(FIELDS.SWBPAGE);
    return obj;
}

From source file:mx.edu.cide.justiciacotidiana.v1.model.Voto.java

License:Open Source License

/**
 * Genera un BasicDBObject a partir de una cadena JSON que representa un voto.
 * @param content Cadena con el JSON del voto.
 * @return BasicDBObject si el voto es vlido y contiene al menos los campos requeridos, null en otro caso.
 *//*from   w  ww.j  a v a2 s  .co  m*/
public static BasicDBObject parse(String content) {
    BasicDBObject obj = (BasicDBObject) JSON.parse(content);
    List<String> params = new ArrayList<String>();
    params.add(FIELDS.PROPOSALID);
    params.add(FIELDS.VALUE);
    params.add(FIELDS.FACEBOOKUSER);

    if (!Utils.validateEmptyStringFields(obj, params)) {
        return null;
    }

    String val = obj.getString(FIELDS.VALUE);
    if (!LIKE.equalsIgnoreCase(val) && !DISLIKE.equalsIgnoreCase(val) && !REFRAIN.equalsIgnoreCase(val))
        return null;

    //Remove unmodifiable fields
    if (null == obj.getString(FIELDS.CREATED))
        obj.remove(FIELDS.CREATED);

    return obj;
}

From source file:mx.edu.cide.justiciacotidiana.v1.mongo.MongoInterface.java

License:Open Source License

/**
 * Agrega un elemento a una coleccin./*from  w  ww .  ja  v a2  s  .com*/
 * @param collectionName Nombre de la coleccin donde se agregar el elemento.
 * @param item Elemento a insertar.
 * @return ID del elemento insertado. null en otro caso.
 */
public String addItem(String collectionName, BasicDBObject item) {
    DBCollection tCol = mongoDB.getCollection(collectionName);
    item.put(FIELD_CREATED, new Date());

    //Eliminar id y updated, si es que viene en el documento.
    item.remove(FIELD_ID);
    item.remove(FIELD_UPDATED);
    try {
        tCol.insert(item);
    } catch (MongoException ex) {
        return null;
    }
    return item.getString(FIELD_ID);
}