Example usage for com.mongodb BasicDBList get

List of usage examples for com.mongodb BasicDBList get

Introduction

In this page you can find the example usage for com.mongodb BasicDBList get.

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value at an index.

Usage

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

License:Open Source License

Collection<Element> getNotExportedElements(final DBCollection coll, final DBCursor cursor) throws IOException {
    assert coll != null;
    assert cursor != null;

    final Collection<Element> col = new ArrayList<Element>();

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final String id = obj.getString(ID_FIELD);
        final BasicDBList lst = (BasicDBList) obj.get(ELEM_LST_FIELD);
        if (lst == null) {
            throw new NullPointerException("Elem list espected");
        }/*from  w ww . ja  va2s.  co  m*/
        final BasicDBObject lelem = (BasicDBObject) lst.get(0);
        if (lelem == null) {
            throw new NullPointerException("Elem element espected");
        }
        if (!lelem.getBoolean(EXPORTED_FIELD)) {
            final Element elem = new Element(id, lelem.getString(BROKEN_URL_FIELD),
                    lelem.getString(PRETTY_BROKEN_URL_FIELD), lelem.getString(FIXED_URL_FIELD),
                    obj.getString(MST_FIELD), lelem.getDate(LAST_UPDATE_FIELD).toString(),
                    lelem.getString(USER_FIELD), null, false);
            col.add(elem);

            lelem.put(EXPORTED_FIELD, true);
            final WriteResult res = coll.save(obj, WriteConcern.ACKNOWLEDGED);

            if (!res.getCachedLastError().ok()) {
                throw new IOException("write doc[" + obj.getString(ID_FIELD) + "] failed");
            }
        }
    }

    return col;
}

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

License:Open Source License

public static Set<String> getCenters(final DBCollection coll) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//from  w  w w  . j ava 2s  . c o m
    final Set<String> set = new TreeSet<String>();
    final DBCursor cursor = coll.find();

    while (cursor.hasNext()) {
        final BasicDBList lst = (BasicDBList) cursor.next().get(CENTER_FIELD);
        set.add((String) lst.get(0));
    }
    cursor.close();

    return set;
}

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 w w  w.j  a  va 2 s .  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.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.  ja  v a 2s . c o  m*/
        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.ShowBrokenLinks.java

License:Open Source License

public Map<String, String> statistics() throws IOException {
    final Map<String, String> ret = new TreeMap<String, String>();
    final Map<String, Integer> map = new HashMap<String, Integer>();
    final DBCursor cursor = coll.find();

    for (DBObject obj : cursor) {
        final BasicDBObject dobj = (BasicDBObject) obj;
        final BasicDBList ccList = (BasicDBList) dobj.get(CENTER_FIELD);
        final String cc = (String) ccList.get(0);

        if (cc == null) {
            throw new IOException("null center field. Id=" + dobj.getString(ID_FIELD));
        }//  w ww  .j  av a  2s.  co  m
        Integer val = map.get(cc);
        if (val == null) {
            val = 0;
        }
        map.put(cc, val + 1);
    }
    cursor.close();

    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        final String key = entry.getKey();
        final int value = entry.getValue();
        final int dif = 999999 - value;
        ret.put(dif + "_" + value + "_" + key, key);
    }

    return ret;
}

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");
    }*///from www  .  j ava 2 s  .  c om
    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

private static String getValue(final DBObject obj, final String path) {
    assert obj != null;
    assert path != null;

    final String[] split = path.split(" */ *");
    final Matcher mat = Pattern.compile("([^\\[]+)\\[(\\d+)\\]").matcher("");
    final int len = split.length;
    final String str;
    DBObject dbo = obj;//from  w  ww.j  a v  a 2 s  .  c  o  m

    for (int idx = 0; idx < len - 1; idx++) {
        final String elem = split[idx];

        mat.reset(elem);
        if (mat.matches()) {
            //System.out.println("0:" + mat.group(0) + " 1:" + mat.group(1) + " 2:" + mat.group(2));
            final BasicDBList lst = (BasicDBList) dbo.get(mat.group(1));
            dbo = (DBObject) lst.get(mat.group(2));
            if (dbo == null) {
                break;
            }
        } else {
            dbo = (DBObject) dbo.get(elem);
        }
    }
    if (dbo == null) {
        str = null;
    } else {
        final String elem = split[len - 1];

        mat.reset(elem);
        if (mat.matches()) {
            final BasicDBList lst = (BasicDBList) dbo.get(mat.group(1));
            str = lst.get(mat.group(2)).toString();
        } else {
            str = dbo.get(elem).toString();
        }
    }

    return str;
}

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 {/*from w w w  .  ja v a 2 s  . 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:ch.agent.crnickl.mongodb.ReadMethodsForSchema.java

License:Apache License

private Collection<AttributeDefinition<?>> attributeDefinitions(Surrogate schemaSurrogate, int seriesNr,
        Database db, BasicDBList list) throws T2DBException {
    Collection<AttributeDefinition<?>> result = new ArrayList<AttributeDefinition<?>>(list.size());
    for (int i = 0; i < list.size(); i++) {
        result.add(attributeDefinition(schemaSurrogate, seriesNr, db, (BasicDBObject) list.get(i)));
    }// w w  w  .j a v  a 2 s  .c om
    return result;
}

From source file:ch.agent.crnickl.mongodb.ReadMethodsForSchema.java

License:Apache License

private Collection<SeriesDefinition> seriesDefinitions(Surrogate schemaSurrogate, Database db, BasicDBList list)
        throws T2DBException {
    Collection<SeriesDefinition> result = new ArrayList<SeriesDefinition>(list.size());
    for (int i = 0; i < list.size(); i++) {
        result.add(seriesDefinition(schemaSurrogate, db, (BasicDBObject) list.get(i)));
    }//from w  w w .  j  ava  2s.c  om
    return result;
}