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:br.bireme.scl.MongoOperations.java

License:Open Source License

public static SearchResult getDocuments(final DBCollection coll, final String docMast, final String docId,
        final String docUrl, final Set<String> centerIds, final boolean decreasingOrder, final int from,
        final int count) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//ww  w  .  jav a  2 s.c o  m
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    final List<IdUrl> lst = new ArrayList<IdUrl>();
    final BasicDBObject query = new BasicDBObject();

    if (docMast != null) {
        query.append(MST_FIELD, docMast);
    }
    if (docId != null) {
        final Pattern pat = Pattern.compile("^" + docId.trim() + "_\\d+");
        query.append(ID_FIELD, pat);
    }
    if (docUrl != null) {
        query.append(BROKEN_URL_FIELD, docUrl.trim());
    }
    if (centerIds != null) {
        final BasicDBList cclst = new BasicDBList();
        for (String centerId : centerIds) {
            cclst.add(centerId);
        }
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        query.append(CENTER_FIELD, in);
    }
    final BasicDBObject sort = new BasicDBObject(DATE_FIELD, decreasingOrder ? -1 : 1);
    final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
    final int size = cursor.count();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    while (cursor.hasNext()) {
        final DBObject doc = cursor.next();
        final BasicDBList ccsLst = (BasicDBList) doc.get(CENTER_FIELD);
        final Set<String> ccs = new TreeSet<String>();

        for (Object cc : ccsLst) {
            ccs.add((String) cc);
        }
        final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD), ccs,
                format.format((Date) (doc.get(DATE_FIELD))), (String) doc.get(MST_FIELD));
        lst.add(iu);
    }
    cursor.close();

    return new SearchResult(size, lst);
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static SearchResult2 getHistoryDocuments(final DBCollection coll, final Element elem, final int from,
        final int count) throws IOException, ParseException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*from   ww w . j  a  v  a  2s . c o  m*/
    if (elem == null) {
        throw new NullPointerException("elem");
    }
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    final List<Element> lst = new ArrayList<Element>();
    final BasicDBObject query = new BasicDBObject();
    final String root = ELEM_LST_FIELD + ".0.";
    final String updated = root + LAST_UPDATE_FIELD;

    if (elem.getDbase() != null) {
        query.append(MST_FIELD, elem.getDbase().trim());
    }
    if (elem.getId() != null) {
        final Pattern pat = Pattern.compile("^" + elem.getId().trim() + "_\\d+");
        query.append(ID_FIELD, pat);
    }
    if (elem.getFurl() != null) {
        query.append(root + FIXED_URL_FIELD, elem.getFurl().trim());
    }
    if (!elem.getCcs().isEmpty()) {
        final BasicDBList cclst = new BasicDBList();
        for (String centerId : elem.getCcs()) {
            cclst.add(centerId.trim());
        }
        final String cc = root + CENTER_FIELD;
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        query.append(cc, in);
    }
    if (elem.getDate() != null) {
        final SimpleDateFormat simple = new SimpleDateFormat("dd-MM-yyyy");
        final Date date = simple.parse(elem.getDate().trim());
        final BasicDBObject qdate = new BasicDBObject("$gte", date);
        query.append(updated, qdate);
    }
    if (elem.getUser() != null) {
        final String user = root + USER_FIELD;
        query.append(user, elem.getUser().trim());
    }

    final BasicDBObject sort = new BasicDBObject(updated, -1);
    final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
    final int size = cursor.count();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    while (cursor.hasNext()) {
        final BasicDBObject hdoc = (BasicDBObject) cursor.next();
        final BasicDBList elst = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
        final BasicDBObject hcurdoc = (BasicDBObject) elst.get(0);
        if (hcurdoc == null) {
            throw new IOException("document last element found.");
        }
        final BasicDBList ccLst = (BasicDBList) hcurdoc.get(CENTER_FIELD);
        final List<String> ccs = Arrays.asList(ccLst.toArray(new String[0]));
        final Element elem2 = new Element(hdoc.getString(ID_FIELD), hcurdoc.getString(BROKEN_URL_FIELD),
                hcurdoc.getString(PRETTY_BROKEN_URL_FIELD), hcurdoc.getString(FIXED_URL_FIELD),
                hdoc.getString(MST_FIELD), format.format((Date) (hcurdoc.get(LAST_UPDATE_FIELD))),
                hcurdoc.getString(USER_FIELD), ccs, hcurdoc.getBoolean(EXPORTED_FIELD));
        lst.add(elem2);
    }
    cursor.close();

    return new SearchResult2(size, lst.size(), lst);
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static boolean undoUpdateDocument2(final DBCollection coll, final DBCollection hcoll,
        final String fromDate) throws IOException, ParseException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*from w w w.java  2  s.co m*/
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
    final Date date = (fromDate == null) ? new Date(0) : simple.parse(fromDate);
    final String updated = ELEM_LST_FIELD + ".0." + LAST_UPDATE_FIELD;
    final BasicDBObject qdate = new BasicDBObject("$gte", date);
    final BasicDBObject query = new BasicDBObject(updated, qdate);
    final BasicDBObject sort = new BasicDBObject(updated, -1);
    final DBCursor cursor = coll.find(query).sort(sort);

    boolean ret = true;

    while (cursor.hasNext()) {
        final BasicDBObject hdoc = (BasicDBObject) cursor.next();
        final BasicDBList lst = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
        final BasicDBObject hcurdoc = (BasicDBObject) lst.remove(0);
        if (hcurdoc == null) {
            throw new IOException("document last element found.");
        }
        final BasicDBObject doc = new BasicDBObject();
        doc.put(DATE_FIELD, hdoc.get(DATE_FIELD));
        doc.put(LAST_UPDATE_FIELD, hcurdoc.get(LAST_UPDATE_FIELD));
        doc.put(MST_FIELD, hdoc.get(MST_FIELD));
        doc.put(ID_FIELD, hdoc.get(ID_FIELD));
        doc.put(BROKEN_URL_FIELD, hcurdoc.get(BROKEN_URL_FIELD));
        doc.put(PRETTY_BROKEN_URL_FIELD, hcurdoc.get(PRETTY_BROKEN_URL_FIELD));
        doc.put(MSG_FIELD, hcurdoc.get(MSG_FIELD));
        doc.put(CENTER_FIELD, hcurdoc.get(CENTER_FIELD));

        final boolean ret1 = coll.save(doc).getLastError().ok();
        final boolean ret2;

        if (lst.isEmpty()) {
            ret2 = hcoll.remove(query, WriteConcern.ACKNOWLEDGED).getLastError().ok();
        } else {
            ret2 = hcoll.save(hdoc, WriteConcern.ACKNOWLEDGED).getLastError().ok();
        }
        final boolean auxret = (ret1 && ret2);
        if (!auxret) {
            System.err.println("doc[" + hdoc.get(ID_FIELD) + "] write error");
        }
        ret &= auxret;
    }

    return ret;
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static void fixMissingHttp(final DBCollection coll, final DBCollection hcoll) throws IOException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//from   w  ww .  j  a  va2s.c o  m
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    final String HTTP = "http://";
    final DBCursor cursor = coll.find();

    while (cursor.hasNext()) {
        final DBObject dbo = cursor.next();
        final String url = ((String) dbo.get(BROKEN_URL_FIELD)).trim();

        if (!url.startsWith(HTTP)) {
            final String fixedUrl = HTTP + url;
            final String id = (String) dbo.get(ID_FIELD);

            if (!CheckUrl.isBroken(CheckUrl.check(fixedUrl))) {
                if (!Tools.isDomain(fixedUrl)) {
                    if (!updateDocument(coll, hcoll, id, fixedUrl, "system", true)) {
                        throw new IOException("could not update document id=" + id);
                    }
                }
            }
        }
    }
    cursor.close();
}

From source file:br.bireme.scl.ResetExportFlag.java

License:Open Source License

private static void reset(final String host, final int port, final String database, final String collection,
        final String sdate) throws UnknownHostException, ParseException {
    assert host != null;
    assert port > 0;
    assert database != null;
    assert collection != null;

    final MongoClient client = new MongoClient(host, port);
    final DB db = client.getDB(database);
    final DBCollection coll = db.getCollection(collection);
    final String prefix = ELEM_LST_FIELD + ".0.";
    final BasicDBObject query;
    final DBCursor cursor;

    if (sdate == null) {
        query = new BasicDBObject(prefix + EXPORTED_FIELD, true);
    } else {//from   w  w w  .j av  a 2  s .c om
        final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
        final Date date = simple.parse(sdate);

        final BasicDBList list = new BasicDBList();
        list.add(new BasicDBObject(prefix + EXPORTED_FIELD, true));
        list.add(new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", date)));
        query = new BasicDBObject("$and", list);
    }
    cursor = coll.find(query);

    while (cursor.hasNext()) {
        final BasicDBObject doc = (BasicDBObject) cursor.next();
        final BasicDBList list = (BasicDBList) doc.get(ELEM_LST_FIELD);
        final BasicDBObject elem = (BasicDBObject) list.get(0);
        elem.put(EXPORTED_FIELD, false);
        coll.save(doc);
    }
    cursor.close();
}

From source file:br.bireme.scl.SendEmailToCcs.java

License:Open Source License

private void prepareEmails(final DBCollection coll, final Map<String, CcProfile> profiles) {
    assert coll != null;
    assert profiles != null;

    final Set<String> ccs = MongoOperations.getCenters(coll);
    final String qry = CENTER_FIELD + ".0";

    for (String cc : ccs) {
        final Set<EmailFrame> eset = new HashSet<EmailFrame>();
        final CcProfile ccp = profiles.get(cc);

        if (ccp != null) {
            final BasicDBObject query = new BasicDBObject(qry, cc);
            final DBCursor cursor = coll.find(query);

            while (cursor.hasNext()) {
                final BasicDBObject doc = (BasicDBObject) cursor.next();
                final String id = doc.getString(ID_FIELD);
                final EmailFrame eframe = new EmailFrame(id.substring(0, id.indexOf('_')),
                        doc.getString(MST_FIELD), doc.getString(BROKEN_URL_FIELD));
                eset.add(eframe);//w ww.jav  a 2 s. co  m
            }
            cursor.close();
            sendEmailToCc(ccp, eset);
        }
    }
}

From source file:br.bireme.scl.ShowFixedLinks.java

License:Open Source License

public List<Element> showExportedLinks(final List<String> ccs, final String fromDate) throws ParseException {
    /*if (ccs == null) {
    throw new NullPointerException("ccs");
    }*/// www .  ja v a 2 s.  co m
    final List<Element> lst = new ArrayList<Element>();
    final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    final Date date = (fromDate == null) ? new Date(0) : simple.parse(fromDate);
    final String updated = ELEM_LST_FIELD + ".0." + LAST_UPDATE_FIELD;
    final BasicDBObject qdate = new BasicDBObject("$gte", date);
    final BasicDBObject query = new BasicDBObject(updated, qdate);
    final BasicDBObject sort = new BasicDBObject(updated, -1);
    final DBCursor cursor = coll.find(query).sort(sort);

    while (cursor.hasNext()) {
        final BasicDBObject doc = (BasicDBObject) cursor.next();
        final BasicDBList elems = (BasicDBList) doc.get(ELEM_LST_FIELD);
        final BasicDBObject upd = (BasicDBObject) elems.get(0);
        final BasicDBList ccLst = (BasicDBList) upd.get(CENTER_FIELD);
        final List<String> ccs2 = new ArrayList<String>();

        for (Object cc : ccLst) {
            ccs2.add((String) cc);
        }

        if (ccs == null) {
            final String id = doc.getString(ID_FIELD);
            final Element elem = new Element(id.substring(0, id.indexOf('_')), upd.getString(BROKEN_URL_FIELD),
                    upd.getString(PRETTY_BROKEN_URL_FIELD), upd.getString(FIXED_URL_FIELD),
                    doc.getString(MST_FIELD), sdf.format(upd.getDate(LAST_UPDATE_FIELD)),
                    upd.getString(USER_FIELD), ccs2, upd.getBoolean(EXPORTED_FIELD));
            lst.add(elem);
        } else {
            for (String cc : ccs) {
                if (ccLst.contains(cc)) {
                    //System.out.println("cc=" + cc + " id=" + doc.getString(ID_FIELD));
                    final String id = doc.getString(ID_FIELD);
                    final Element elem = new Element(id.substring(0, id.indexOf('_')),
                            upd.getString(BROKEN_URL_FIELD), upd.getString(PRETTY_BROKEN_URL_FIELD),
                            upd.getString(FIXED_URL_FIELD), doc.getString(MST_FIELD),
                            sdf.format(upd.getDate(LAST_UPDATE_FIELD)), upd.getString(USER_FIELD), ccs2,
                            upd.getBoolean(EXPORTED_FIELD));
                    lst.add(elem);
                    break;
                }
            }
        }
    }
    cursor.close();

    System.out.println("size=" + lst.size() + "\n");

    return lst;
}

From source file:br.bireme.scl.TabulateField.java

License:Open Source License

public static void tabulate(final String host, final int port, final String user, final String password,
        final String database, final String collection, final String path) throws UnknownHostException {
    if (host == null) {
        throw new NullPointerException("host");
    }//from w ww.  java 2s .  c o  m
    if (port <= 0) {
        throw new IllegalArgumentException("port <= 0");
    }
    if (collection == null) {
        throw new NullPointerException("collection");
    }
    if (path == null) {
        throw new NullPointerException("path");
    }
    final MongoClient mongoClient = new MongoClient(host, port);
    final DB db = mongoClient.getDB(database);
    if (user != null) {
        final boolean auth = db.authenticate(user, password.toCharArray());
        if (!auth) {
            throw new IllegalArgumentException("invalid user/password");
        }
    }
    final DBCollection coll = db.getCollection(collection);
    final DBCursor cursor = coll.find();
    final TreeMap<String, Integer> map1 = new TreeMap<String, Integer>();
    final TreeMap<Integer, String> map2 = new TreeMap<Integer, String>();
    final int ADD_VALUE = 99999999;
    final int size = cursor.size();

    while (cursor.hasNext()) {
        final BasicDBObject doc = (BasicDBObject) cursor.next();
        final String key = getValue(doc, path);

        if (key != null) {
            Integer val = map1.get(key);
            if (val == null) {
                val = 0;
            }
            val += 1;
            map1.put(key, val);
        }
    }
    cursor.close();
    for (Map.Entry<String, Integer> entry : map1.entrySet()) {
        map2.put(ADD_VALUE - entry.getValue(), entry.getKey());
    }

    System.out.println("Total # of docs: " + size + "\n");

    int idx = 0;
    for (Map.Entry<Integer, String> entry : map2.entrySet()) {
        final int val = ADD_VALUE - entry.getKey();
        final String percent = String.format("%.2f", ((float) val / size) * 100);
        System.out.println((++idx) + ") " + entry.getValue() + ": " + val + " (" + percent + "%)");
    }
}

From source file:br.bireme.scl.UndoUpdate.java

License:Open Source License

private static void undo(final String host, final int port, final String database, final String fromDate,
        final String docId) throws UnknownHostException, ParseException {
    assert host != null;
    assert port > 0;
    assert database != null;
    assert (fromDate != null || docId != null);

    final MongoClient client = new MongoClient(host, port);
    final DB db = client.getDB(database);
    final DBCollection from_coll = db.getCollection(HISTORY_COL);
    final DBCollection to_coll = db.getCollection(BROKEN_LINKS_COL);
    final String prefix = ELEM_LST_FIELD + ".0.";
    final BasicDBObject query;

    if (fromDate == null) {
        query = new BasicDBObject("_id", docId);
    } else {/*  w  ww  .ja v a2s .  c  om*/
        final SimpleDateFormat simple = new SimpleDateFormat(
                "yyyyMMdd" + (fromDate.contains(":") ? "-HH:mm:ss" : ""));
        final Date fDate = simple.parse(fromDate);
        query = new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", fDate));
    }
    final DBCursor cursor = from_coll.find(query);
    int total = 0;
    int reverted = 0;

    System.out.println("host=" + host);
    System.out.println("port=" + port);
    System.out.println("database=" + database);
    if (fromDate == null) {
        System.out.println("doc id=" + docId);
    } else {
        System.out.println("from date=" + fromDate);
    }
    System.out.println("found=" + cursor.size());

    while (cursor.hasNext()) {
        final BasicDBObject doc_from = (BasicDBObject) cursor.next();
        final BasicDBObject doc_to = new BasicDBObject(doc_from);
        final String id = doc_from.getString(ID_FIELD);
        final BasicDBList list = (BasicDBList) doc_from.get(ELEM_LST_FIELD);
        final BasicDBObject elem = (BasicDBObject) list.get(0);
        final Date date = elem.getDate(LAST_UPDATE_FIELD);

        doc_to.put(LAST_UPDATE_FIELD, date);
        doc_to.put(BROKEN_URL_FIELD, elem.getString(BROKEN_URL_FIELD));
        doc_to.put(MSG_FIELD, elem.getString(MSG_FIELD));
        doc_to.put(CENTER_FIELD, elem.get(CENTER_FIELD));
        doc_to.removeField(ELEM_LST_FIELD);

        final WriteResult wr = to_coll.save(doc_to, WriteConcern.ACKNOWLEDGED);
        if (wr.getCachedLastError().ok()) {
            final WriteResult wr2 = from_coll.remove(doc_from, WriteConcern.ACKNOWLEDGED);
            if (wr2.getCachedLastError().ok()) {
                reverted++;
                System.out.println("+++id=" + id + " date=" + date);
            } else {
                System.err.println("Document[" + id + "] delete error.");
            }
        } else {
            System.err.println("Document[" + id + "] update error.");
        }
        total++;
    }
    cursor.close();

    System.out.println("total/undo: " + total + "/" + reverted);
}

From source file:br.bireme.tmp.AddPrettyField.java

License:Open Source License

private static void add(final String mongo_host, final int mongo_port, final String mongo_db,
        final String mongo_col) throws UnknownHostException {
    assert mongo_host != null;
    assert mongo_port > 0;
    assert mongo_db != null;
    assert mongo_col != null;

    final MongoClient client = new MongoClient(mongo_host, mongo_port);
    final DB db = client.getDB(mongo_db);
    final DBCollection coll = db.getCollection(HISTORY_COL);
    final DBCursor cursor = coll.find();
    int total = 0;

    System.out.println("host=" + mongo_host);
    System.out.println("port=" + mongo_port);
    System.out.println("database=" + mongo_db);
    System.out.println("collection=" + mongo_col);
    System.out.println("num of documents=" + cursor.size());

    while (cursor.hasNext()) {
        final BasicDBObject doc1 = (BasicDBObject) cursor.next();
        final BasicDBList list = (BasicDBList) doc1.get(ELEM_LST_FIELD);

        for (Object obj : list) {
            final BasicDBObject doc2 = (BasicDBObject) obj;
            if (!doc2.containsField(PRETTY_BROKEN_URL_FIELD)) {
                final String burl = doc2.getString(BROKEN_URL_FIELD);
                try {
                    final String pburl = EncDecUrl.decodeUrl(burl);
                    doc2.append(PRETTY_BROKEN_URL_FIELD, pburl);

                    final WriteResult wr = coll.save(doc1, WriteConcern.ACKNOWLEDGED);
                    if (wr.getCachedLastError().ok()) {
                        total++;/*  w  w  w . j  a v a 2 s  .  c om*/
                    } else {
                        System.err.println("Document[" + doc1.getString("_id") + "] update error.");
                    }
                } catch (IOException ioe) {
                    System.err.println("Document[" + doc1.getString("_id") + "] bad encode conversion"
                            + " url=[" + burl + "]");
                }
            }
        }
    }
    cursor.close();
    System.out.println("num of added fields: " + total);
}