Example usage for com.mongodb BasicDBObject getString

List of usage examples for com.mongodb BasicDBObject getString

Introduction

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

Prototype

public String getString(final String key) 

Source Link

Document

Returns the value of a field as a string

Usage

From source file:bd.DVenta.java

@Override
public String insertar(Object o) {
    CVenta x = (CVenta) o;/*from ww w .j  a v a  2s . c o  m*/

    String ver = verificar(x);
    if (ver.compareTo(MSG_ACEPTADO) != 0)
        return ver;

    conecion con = new conecion(table);
    BasicDBObject datos = new BasicDBObject(x.get_datos());
    con.get_colletion().insert(datos);
    con.end();
    return datos.getString("_id");
}

From source file:bd.DVenta.java

@Override
public String eliminar(Object o) {
    CVenta x = (CVenta) o;//from ww w  .java2s . c  o  m
    conecion con = new conecion(table);
    BasicDBObject datos = new BasicDBObject("_id", x.getId());
    con.get_colletion().remove(datos);
    con.end();
    return datos.getString("_id");
}

From source file:bd.DVenta.java

@Override
public ArrayList listar() {
    ArrayList datos = new ArrayList();
    CVenta x = new CVenta();
    conecion con = new conecion(table);
    DBCursor cursor = con.get_colletion().find();
    try {//  w w w  .ja va  2  s.c  o m
        while (cursor.hasNext()) {
            x = new CVenta();
            BasicDBObject agg = (BasicDBObject) cursor.next();
            x.set_datos((HashMap) agg.toMap());
            x.setId((String) agg.getString("_id"));
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    return datos;
}

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  ww  w  .ja v  a 2 s  .c om*/
        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 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  .  ja  v  a2  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.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  ww w  .j  a v a2 s.com
    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.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);// www .  j  a  va2  s  . c om
            }
            cursor.close();
            sendEmailToCc(ccp, eset);
        }
    }
}

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  w w.  j a va2 s .  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");
    }*/// w  w  w  .j  av 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.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 {/*ww w .j a  va2 s . co  m*/
        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);
}