Example usage for com.mongodb DBCursor close

List of usage examples for com.mongodb DBCursor close

Introduction

In this page you can find the example usage for com.mongodb DBCursor close.

Prototype

@Override
    public void close() 

Source Link

Usage

From source file:gMIRC_handler.java

/**
 * Add a user to a channel (can check if the channel is a new channel)
 *
 * @param Username/* w  ww. j  av  a 2 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(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Delete a user from a channel (used in leave method)
 *
 * @param Channel//from w w w  .j av  a 2 s  .  c  o  m
 * @param Username
 * @return code
 */
private int DeleteChannelUser(String Channel, String Username) {
    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 {
            ret = 1;
            while (cursor.hasNext()) {
                ret = 1;
                coll.remove(cursor.next());
                ret = 0;
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has leaved Channel : " + Channel);
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Delete a user in all channel (used in cleaning)
 *
 * @param Username/*from w w w  .jav a 2s .com*/
 * @return code
 */
public int DeleteUserInChannel(String Username) {
    int ret = 0;
    try {

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

        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                coll.remove(cursor.next());
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has been deleted in Channel Collection!");
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Register a user to the server (used in RegUser)
 *
 * @param username//from w  ww  .j ava  2s  . c o  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(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

private int UpdateLastActive(String username) {
    int ret = 0;/*from   w ww. j  a v  a  2s .com*/
    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(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return 0;
}

From source file:gMIRC_handler.java

/**
 * Moved active user to a passive user (soon to be deleted)
 *
 * @param username/*ww  w.  jav  a2 s  . co m*/
 * @return code
 */
public static 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(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

public String GetMessage(String username) {
    String ret = "";
    try {// ww  w . ja  v a  2s  . 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(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    UpdateLastActive(username);
    return ret;
}

From source file:gMIRC_handler.java

private int PutMessage(String username, String channelname, String msg) {
    int ret = 0;//from w  w  w  . ja  v  a 2 s.  co  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(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

private int PutMessageWild(String username, String msg) {
    int ret = 1;//www  .ja  v a2  s  . co  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 = ret & PutMessage(username, channelname, msg);
            }
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }

    return ret;
}

From source file:RestrictedService.java

License:Open Source License

/**
 * @param textToAnalize// ww  w . j  a  v  a  2 s  . c  om
 * @param collectionName
 * @return
 * @throws Exception
 */
public static HttpEntity callANEW(String textToAnalize, String collectionName) throws Exception {
    String[] wordsInText = (textToAnalize + " ").split(" ");
    String wordAndValues[][] = new String[wordsInText.length][5]; //initialize the wordAndValues
    int position = 0; //Aux variable to see the position of each word inside the document
    String polarityAndValue[] = new String[2];

    DBCollection coll = db.getCollection(collectionName);
    Double positive = 0.0;
    Double negative = 0.0;
    for (int i = 0; i < wordsInText.length; i++) { //For each word
        wordAndValues[i][0] = wordsInText[i]; //We save the word
        wordAndValues[i][1] = Integer.toString(position); //add its initial 
        position += (wordsInText[i].length() - 1);
        wordAndValues[i][2] = Integer.toString(position);//and final position
        position += 2; //This is where the next word starts
        //We check if this word has a positive or negative annotation and set its value and polarity.
        BasicDBObject query = new BasicDBObject("Word", wordsInText[i].toLowerCase());
        DBCursor cursor = coll.find(query);
        Double value = 5.0;
        try {
            while (cursor.hasNext()) {
                value = (Double) cursor.next().get("ValMn");
            }
        } finally {
            cursor.close();
        }
        if (value > 5) {
            wordAndValues[i][3] = "1.0";
            wordAndValues[i][4] = "Positive";
            positive++;
        } else if (value < 5) {
            wordAndValues[i][3] = "-1.0";
            wordAndValues[i][4] = "Negative";
            negative++;
        } else {
            wordAndValues[i][3] = "0.0";
            wordAndValues[i][4] = "Neutral";
        }
        polarityAndValue[0] = Double.toString(0);
        if ((positive + negative) != 0) {
            polarityAndValue[0] = Double.toString((positive - negative) / (positive + negative));
        }
        if (new Double(polarityAndValue[0]) > 0) {
            polarityAndValue[1] = "Positive";
        } else if (new Double(polarityAndValue[0]) < 0) {
            polarityAndValue[1] = "Negative";
        } else {
            polarityAndValue[1] = "Neutral";
        }
    }

    return callMarl(textToAnalize, wordAndValues, polarityAndValue);
}