Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

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

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:gMIRC_handler.java

/**
 * Moved active user to a passive user (soon to be deleted)
 *
 * @param username/*  w  ww. j  a v  a2s. c  om*/
 * @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 {//www .  j  av  a  2 s.  c om
        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.  java  2  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(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;/* w  w  w .j  ava2 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:MashUp.java

public ArrayList<String> getApibasedonProtocols(String protocol) {
    ArrayList<String> api = new ArrayList<String>();
    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("protocols", protocol);
    DBCursor cursor = table1.find(whereQuery);
    while (cursor.hasNext()) {
        String temp = cursor.next().get("id").toString();
        api.add(temp);/* w  w  w. j a  va  2  s .  c o m*/
    }

    return api;

}

From source file:MashUp.java

public ArrayList<String> getApibasedonCategory(String category) {
    ArrayList<String> api = new ArrayList<String>();
    BasicDBObject whereQuery = new BasicDBObject();

    whereQuery.put("category", category);
    DBCursor cursor = table1.find(whereQuery);
    while (cursor.hasNext()) {
        String temp = cursor.next().get("id").toString();
        api.add(temp);//from ww w .  j a v a  2s .  co  m
    }

    return api;

}

From source file:MashUp.java

public ArrayList<String> getApibasedonTags(String tags) {
    DBCursor cursor = table1.find();

    ArrayList<String> api = new ArrayList<String>();
    while (cursor.hasNext()) {
        DBObject temp = cursor.next();/* w w  w  .j a  v a 2  s. c  om*/
        String tags_1[] = tags.split(" ");

        for (int i = 0; i < tags_1.length; i++) {
            if (temp.get("Tags").toString().contains(tags_1[i])) {

                api.add(temp.get("id").toString());
            }
        }
    }
    return api;
}

From source file:MashUp.java

public HashMap<String, String> getApibasedonRating(float rating) {
    HashMap<String, String> api = new HashMap<String, String>();
    DBCursor cursor = table1.find();

    while (cursor.hasNext()) {
        DBObject temp = cursor.next();//from w w  w . j  a va  2 s.c  om
        //System.out.println(temp.get("id").toString()+ " "+temp.get("rating").toString());
        if (temp.get("rating").toString() == "") {
            temp.put("rating", "0.0");
        }
        if (Float.parseFloat(temp.get("rating").toString()) > rating) {
            if (api.get("Higher") == null) {

                api.put("Higher", temp.get("id").toString());

            } else {
                if (!api.get("Higher").contains(temp.get("id").toString())) {
                    String temp1 = api.get("Higher") + " " + temp.get("id").toString();
                    api.put("Higher", temp1);
                }
            }
        } else if (Float.parseFloat(temp.get("rating").toString()) < rating) {
            if (api.get("Lower") == null) {
                api.put("Lower", temp.get("id").toString());
            } else {
                if (!api.get("Lower").contains(temp.get("id").toString())) {
                    String temp1 = api.get("Lower") + " " + temp.get("id").toString();
                    api.put("Lower", temp1);
                }
            }
        } else {
            if (api.get("Equals") == null) {
                api.put("Equals", temp.get("id").toString());
            } else {
                if (!api.get("Equals").contains(temp.get("id").toString())) {
                    String temp1 = api.get("Equals") + " " + temp.get("id").toString();
                    api.put("Equals", temp1);
                }
            }
        }
    }
    return api;

}

From source file:MashUp.java

public ArrayList<String> getapibyYear(String year) {
    DBCursor cursor = table1.find();

    ArrayList<String> api = new ArrayList<String>();
    while (cursor.hasNext()) {
        DBObject temp = cursor.next();/*from w  ww  .  ja va2 s . c  o  m*/

        if (temp.get("updated").toString().contains(year)) {

            api.add(temp.get("id").toString());
        }
    }
    return api;
}

From source file:MashUp.java

public HashMap<String, String> getInformation(String keywords) {
    DBCursor cursor = table1.find();

    HashMap<String, String> api = new HashMap<String, String>();
    while (cursor.hasNext()) {
        DBObject temp = cursor.next();/*  w ww  . j  a  v a 2  s  . c o  m*/
        String keywords_list[] = keywords.split(" ");
        for (int i = 0; i < keywords_list.length; i++) {
            if (temp.get("title").toString().contains(keywords_list[i])) {
                if (api.get("title") == null) {

                    api.put("title", "APINAME" + temp.get("id") + "$$" + temp.get("title").toString());
                } else {
                    if (!api.get("title")
                            .contains("APINAME" + temp.get("id") + "$$" + temp.get("title").toString())) {
                        api.put("title", api.get("title") + " " + "APINAME" + temp.get("id") + "$$"
                                + temp.get("title").toString());
                    }
                }

            }
            if (temp.get("summary").toString().contains(keywords_list[i])) {
                if (api.get("summary") == null) {
                    System.out.println("Entering");
                    api.put("summary", "APINAME" + temp.get("id") + "$$" + temp.get("summary").toString());
                } else {
                    if (!api.get("summary")
                            .contains("APINAME" + temp.get("id") + "$$" + temp.get("summary").toString())) {
                        api.put("summary", api.get("summary") + " " + "APINAME" + temp.get("id") + "$$"
                                + temp.get("summary").toString());
                    }
                }

            }
            if (temp.get("description").toString().contains(keywords_list[i])) {
                if (api.get("description") == null) {
                    System.out.println("Entering");
                    api.put("description",
                            "APINAME" + temp.get("id") + "$$" + temp.get("description").toString());
                } else {
                    if (!api.get("description")
                            .contains("APINAME" + temp.get("id") + "$$" + temp.get("description").toString())) {
                        api.put("description", api.get("description") + " " + "APINAME" + temp.get("id") + "$$"
                                + temp.get("description").toString());
                    }
                }

            }
        }
    }
    return api;
}