Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject(final String key, final Object value) 

Source Link

Document

Creates an object with the given key/value

Usage

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

License:Open Source License

Collection<Element> loadRecords(final String host, final int port, final String user, final String password)
        throws IOException {
    assert host != null;
    assert port > 0;

    final MongoClient mongoClient = new MongoClient(host, port);
    final DB db = mongoClient.getDB(SOCIAL_CHECK_DB);

    if ((user != null) && (!user.trim().isEmpty())) {
        final boolean auth = db.authenticate(user, password.toCharArray());
        if (!auth) {
            throw new IllegalArgumentException("invalid user/password");
        }//from  ww  w . j  a v  a  2 s .c  o  m
    }

    final DBCollection coll = db.getCollection(HISTORY_COL);
    final String fldName = ELEM_LST_FIELD + ".0." + EXPORTED_FIELD;
    final BasicDBObject query = new BasicDBObject(fldName, false);
    final DBCursor cursor = coll.find(query);
    final Collection<Element> col = getNotExportedElements(coll, cursor);

    cursor.close();

    return col;
}

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

License:Open Source License

private DBCollection getTitle(final String mstName, final String mstEncoding, final int indexTag,
        final int returnTag) throws UnknownHostException, BrumaException {
    assert mstName != null;
    assert indexTag > 0;
    assert returnTag > 0;

    final DBCollection coll = db.getCollection(COL_NAME);
    coll.ensureIndex(new BasicDBObject(INDEX_TAG, 1));

    final Master mst = MasterFactory.getInstance(mstName).setEncoding(mstEncoding).open();
    for (Record rec : mst) {
        if (rec.isActive()) {
            final int mfn = rec.getMfn();
            final List<Field> lst1 = rec.getFieldList(indexTag);
            if (!lst1.isEmpty()) {
                final List<Field> lst2 = rec.getFieldList(returnTag);
                if (!lst2.isEmpty()) {
                    final BasicDBList dblist1 = new BasicDBList();
                    for (Field fld1 : lst1) {
                        final String str = removeAccents(fld1.getContent()).toUpperCase();
                        dblist1.add(str);
                    }/*from   w  w w.  jav  a  2  s  . c om*/
                    final BasicDBList dblist2 = new BasicDBList();
                    for (Field fld2 : lst2) {
                        dblist2.add(fld2.getContent());
                    }
                    coll.insert(new BasicDBObject(INDEX_TAG, dblist1).append(RETURN_TAG, dblist2).append("mfn",
                            mfn));
                }
            }
        }
    }
    mst.close();

    return coll;
}

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

License:Open Source License

private void joinTitle(final DBCollection title, final String inMstName, final String inMstEncoding,
        final int joinTag, final int newTag, final String outMstName)
        throws UnknownHostException, BrumaException {
    assert title != null;
    assert inMstName != null;
    assert inMstEncoding != null;
    assert joinTag > 0;
    assert newTag > 0;
    assert outMstName != null;

    final Master inMst = MasterFactory.getInstance(inMstName).setEncoding(inMstEncoding).open();
    final Master outMst = (Master) MasterFactory.getInstance(outMstName).asAnotherMaster(inMst).forceCreate();

    int cur = 0;//from  w ww  .java2s .co  m
    for (Record rec : inMst) {
        if (++cur % 10000 == 0) {
            System.out.println("++" + cur);
        }
        if (rec.isActive()) {
            //System.out.println("out encoding=" + outMstEncoding);                
            for (Field fld : rec.getFieldList(joinTag)) {
                final String str = removeAccents(fld.getContent()).toUpperCase();
                final BasicDBObject doc = (BasicDBObject) title.findOne(new BasicDBObject(INDEX_TAG, str));
                if (doc == null) {
                    //System.out.println("rec mfn=" + rec.getMfn() + " tag=" 
                    //        + INDEX_TAG + " content=" + fld.getContent());
                } else {
                    final BasicDBList lst = (BasicDBList) doc.get(RETURN_TAG);
                    for (Object obj : lst) {
                        rec.addField(newTag, (String) obj);
                    }
                }
            }
        }
        outMst.writeRecord(rec);

    }
    inMst.close();
    outMst.close();
}

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

License:Open Source License

/**
 * Obtem uma lista com objetos IdUrl obtidos da base de dados MongoDb
 * @param coll coleo onde esto as urls/*from  w w w.j av  a 2s  .  c om*/
 * @param centerIds filtro dos centros colaboradores desejados. Nunca  nulo
 * @param filter se no nulo filtra as urls com um cc especfico
 * @param from indice inicial da lista a ser recuperado. Comea de 1.
 * @param count numero de elementos a serem devolvidos
 * @param ascendingOrder se retorna por ordem de data ascendente ou descendente
 * @return lista de objetos IdUrl
 */
private static List<IdUrl> getCenterUrls(final DBCollection coll, final Set<String> centerIds,
        final String filter, final int from, final int count, final boolean ascendingOrder) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }
    if (centerIds == null) {
        throw new NullPointerException("centerIds");
    }
    if (centerIds.isEmpty()) {
        throw new IllegalArgumentException("empty centerIds");
    }
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    //final Set<IdUrl> lst = new TreeSet<IdUrl>();
    final List<IdUrl> lst = new ArrayList<IdUrl>();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    final BasicDBObject sort = new BasicDBObject(DATE_FIELD, ascendingOrder ? 1 : -1);

    if (filter == null) {
        final BasicDBList cclst = new BasicDBList();

        for (String centerId : centerIds) {
            cclst.add(centerId);
        }
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        final BasicDBObject query = new BasicDBObject(CENTER_FIELD, in);
        //final DBCursor cursor = coll.find(query).skip(from - 1).limit(count);
        final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
        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();
    } else {
        final BasicDBObject query = new BasicDBObject(CENTER_FIELD, filter);
        final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);

        while (cursor.hasNext()) {
            final DBObject doc = cursor.next();
            final Set<String> ccs = new TreeSet<String>();
            ccs.add(filter);

            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 lst;
}

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

License:Open Source License

public static boolean updateDocument(final DBCollection coll, final DBCollection hcoll, final String docId,
        final String fixedUrl, final String user, final boolean automatic) throws IOException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//from w  w w.  ja  v a 2s . c  o  m
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    if (docId == null) {
        throw new NullPointerException("docId");
    }
    if (fixedUrl == null) {
        throw new NullPointerException("fixedUrl");
    }
    if (user == null) {
        throw new NullPointerException("user");
    }
    if (fixedUrl.length() >= 900) {
        throw new IOException("fixedUrl is too long >= 900. [" + fixedUrl + "]");
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, docId);
    final BasicDBObject doc = (BasicDBObject) coll.findOne(query);

    if (doc == null) {
        throw new IOException("document not found id[" + docId + "]");
    }

    final BasicDBList lsthdoc;
    BasicDBObject hdoc = (BasicDBObject) hcoll.findOne(query);
    if (hdoc == null) {
        hdoc = new BasicDBObject();
        hdoc.append(ID_FIELD, docId);
        hdoc.append(MST_FIELD, (String) doc.get(MST_FIELD));
        hdoc.append(DATE_FIELD, (Date) doc.get(DATE_FIELD));
        lsthdoc = new BasicDBList();
        hdoc.append(ELEM_LST_FIELD, lsthdoc);
    } else {
        lsthdoc = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
    }

    final String brokenUrl = doc.getString(BROKEN_URL_FIELD);
    final String brokenUrl_D = EncDecUrl.decodeUrl(brokenUrl);
    final String fixedUrl_E = EncDecUrl.encodeUrl(fixedUrl, CODEC, false);
    //final String fixedUrl_D = EncDecUrl.decodeUrl(fixedUrl);
    final BasicDBObject hcurdoc = new BasicDBObject();
    hcurdoc.append(BROKEN_URL_FIELD, brokenUrl).append(PRETTY_BROKEN_URL_FIELD, brokenUrl_D)
            .append(FIXED_URL_FIELD, fixedUrl_E).append(MSG_FIELD, (String) doc.get(MSG_FIELD))
            .append(CENTER_FIELD, (BasicDBList) doc.get(CENTER_FIELD)).append(AUTO_FIX_FIELD, automatic)
            .append(EXPORTED_FIELD, false).append(LAST_UPDATE_FIELD, new Date()).append(USER_FIELD, user);

    lsthdoc.add(0, hcurdoc);

    final boolean ret1 = coll.remove(doc, WriteConcern.ACKNOWLEDGED).getLastError().ok();
    final boolean ret2 = hcoll.save(hdoc).getLastError().ok();

    return ret1 && ret2;
}

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

License:Open Source License

public static boolean undoUpdateDocument(final DBCollection coll, final DBCollection hcoll, final String docId,
        final boolean updateBrokenColl) throws IOException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//from w ww  .j ava2 s . c  o  m
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    if (docId == null) {
        throw new NullPointerException("docId");
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, docId);
    final BasicDBObject hdoc = (BasicDBObject) hcoll.findOne(query);

    if (hdoc == null) {
        throw new IOException("document not found id[" + docId + "]");
    }
    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. Id[" + docId + "]");
    }
    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, docId);
    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 = updateBrokenColl ? coll.save(doc).getLastError().ok() : true;
    final boolean ret2;

    if (lst.isEmpty()) {
        ret2 = hcoll.remove(query, WriteConcern.ACKNOWLEDGED).getLastError().ok();
    } else {
        ret2 = hcoll.save(hdoc, WriteConcern.ACKNOWLEDGED).getLastError().ok();
    }

    return ret1 && ret2;
}

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  .j  a  va2  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 Set<String> filterCenterFields(final DBCollection coll, final Set<String> centerIds) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*  w w w .j  ava2 s  . com*/
    if (centerIds == null) {
        throw new NullPointerException("centerIds");
    }
    final Set<String> ret = new TreeSet<String>();

    for (String id : centerIds) {
        final BasicDBObject query = new BasicDBObject(CENTER_FIELD, id);
        final DBObject dbo = coll.findOne(query);
        if (dbo != null) {
            ret.add(id);
        }
    }

    return ret;
}

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 {// w w w  .  jav a2s  .  com
        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);/*from  w  ww  .  j  a  va  2 s. c  om*/
            }
            cursor.close();
            sendEmailToCc(ccp, eset);
        }
    }
}