Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

In this page you can find the example usage for com.mongodb DBCollection insert.

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

From source file:mini_mirc_server.miniIRCHandler.java

/**
 * Add a user to a channel (can check if the channel is a new channel)
 * @param Username//from  w  w w .j a v a2 s  . c  om
 * @param Channel
 * @return code
 */
private int AddChannel(String Username, String Channel) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                ret = 1;
                System.err.println(Username + " has joined Channel : " + Channel + "!");
            } else {
                query = new BasicDBObject("channel", Channel);
                DBCursor cursor2 = coll.find(query);
                try {
                    if (!cursor2.hasNext()) {
                        ret = 2;
                    }
                } finally {
                    cursor2.close();
                }
                BasicDBObject doc = new BasicDBObject("username", Username).append("channel", Channel);
                coll.insert(doc);
                System.out.println(Username + " joined Channel : " + Channel);
            }
        } finally {
            cursor.close();
        }

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

From source file:mini_mirc_server.miniIRCHandler.java

/**
 * Register a user to the server (used in RegUser)
 * @param username/*from   w w  w.j a v a  2 s . co m*/
 * @return code
 */
private int AddUser(String username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                Date now = new Date();
                long timestamp_now = now.getTime();
                long treshold = timestamp_now - (1000 * 10); //10
                BasicDBObject temp = (BasicDBObject) cursor.next();
                Date time_temp = (Date) temp.get("timestamp");
                long timestamp_temp = time_temp.getTime();
                if (timestamp_temp < treshold) {
                    ret = 2;
                    System.out.println(username + " has joined back!");
                } else {
                    ret = 1;
                    System.err.println(username + " has been used !");
                }
            } else {
                java.util.Date date = new java.util.Date();
                BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date);
                coll.insert(doc);
                System.out.println(username + " online !");
            }
        } finally {
            cursor.close();
        }

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

From source file:mini_mirc_server.miniIRCHandler.java

private int UpdateLastActive(String username) {
    int ret = 0;//from   w w w.  j a v  a2  s .co  m
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                DBObject temp = cursor.next();
                java.util.Date date = new java.util.Date();
                temp.put("timestamp", date);
                coll.save(temp);
            } else {
                java.util.Date date = new java.util.Date();
                BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date);
                coll.insert(doc);
                System.out.println(username + " online !");
            }
        } finally {
            cursor.close();
        }

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

From source file:mini_mirc_server.miniIRCHandler.java

/**
 * Moved active user to a passive user (soon to be deleted)
 * @param username/*from   w w  w  .ja v a 2 s  .  c  o  m*/
 * @return code
 */
public int SoftDelete(String username) {
    int ret = 0;
    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        DBCollection coll2 = db.getCollection("passiveUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                DBObject temp = cursor.next();
                coll2.insert(temp);
                coll.remove(temp);
            } else {
                ret = 1;
            }
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:mini_mirc_server.miniIRCHandler.java

private int PutMessage(String username, String channelname, String msg) {
    int ret = 0;//from   w w w .ja  v a2  s  .c  o m
    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("inbox");
        DBCollection coll2 = db.getCollection("channelCollection");
        BasicDBObject query2 = new BasicDBObject("channel", channelname);
        BasicDBObject query = new BasicDBObject("channel", channelname).append("username", username);
        DBCursor cursor = coll2.find(query);
        try {
            if (cursor.hasNext()) {
                DBCursor cursor2 = coll2.find(query2);
                System.out.println("Got message from " + username);
                try {
                    java.util.Date date = new java.util.Date();
                    while (cursor2.hasNext()) {
                        ret = 1;
                        BasicDBObject temp = (BasicDBObject) cursor2.next();
                        String target = temp.get("username").toString();
                        BasicDBObject put = new BasicDBObject("target", target).append("username", username)
                                .append("channel", channelname).append("message", msg)
                                .append("timestamp", date.getTime());
                        coll.insert(put);
                        ret = 0;
                    }
                } finally {
                    cursor2.close();
                }
            } else {
                ret = 2;
                System.out.println(username + " not registered to Channel : " + channelname);
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        ret = 1;
        Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:model.DocumentoDAO.java

public static void salvarDocumento(Documento d) {
    DB db = ConnectionFactory.create();/*from   w w w  .  ja  va2 s  . c om*/
    DBCollection coll = db.getCollection("documentos");
    coll.insert(d.toJSON());
}

From source file:Modelo.DAO.MongoDAO.java

public static void insertarFoto(BasicDBObject foto) {

    try {/*  www.  ja  v  a  2 s.  c  om*/
        Conexion c = new Conexion();
        mongoClient = c.conectarMongo();
    } catch (UnknownHostException ex) {
        System.out.println("Imposible realizar la conexin: " + ex.getMessage());
    }
    DB db = mongoClient.getDB("test");
    DBCollection coll = db.getCollection("fotitos");
    coll.insert(foto);
}

From source file:models.OntoProcessor.java

License:Open Source License

protected void saveReport() {
    DBCollection reportColl = db.getCollection("report");
    reportColl.drop();/*from  w  w  w . j av a2 s  . c  om*/

    BasicDBObject dbObject = (BasicDBObject) JSON.parse("{status:" + toJson(getStatus()) + ",messages:"
            + toJson(OntoProcessorMessageStore.getInstance()).toString() + "}");

    reportColl.insert(dbObject);
}

From source file:models.OntoProcessor.java

License:Open Source License

protected void saveCategories() throws JsonProcessingException {
    BasicDBObject dbObject;//from ww  w  . j ava2s . c o  m

    CarbonOntology ontology = CarbonOntology.getInstance();
    DBCollection categoriesColl = db.getCollection("categories");
    categoriesColl.drop();

    String serialized = mapper.writeValueAsString(ontology.getCategoryTree());
    dbObject = (BasicDBObject) JSON.parse(serialized);

    categoriesColl.insert(dbObject);
}

From source file:models.OntoProcessor.java

License:Open Source License

protected void saveImpactAndFlowTypes() throws JsonProcessingException {
    DBCollection impactAndFlowTypesColl = db.getCollection("impactAndFlowTypes");
    impactAndFlowTypesColl.drop();//from w w w .  j ava 2 s . co m

    String impactTypesSerialized = mapper.writeValueAsString(ontology.getImpactTypes());
    String elementaryFlowTypesSerialized = mapper.writeValueAsString(ontology.getElementaryFlowTypes());

    BasicDBObject dbObject = (BasicDBObject) JSON.parse(
            "{impactTypes:" + impactTypesSerialized + ", flowTypes:" + elementaryFlowTypesSerialized + "}");
    impactAndFlowTypesColl.insert(dbObject);
}