Example usage for com.mongodb BasicDBObject getDate

List of usage examples for com.mongodb BasicDBObject getDate

Introduction

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

Prototype

public Date getDate(final String field) 

Source Link

Document

Returns the date or null if not set.

Usage

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

License:Open Source License

private static boolean saveRecord(final String mstName, final int id, final String url, final String err,
        final int urlTag, final List<Integer> ccsFlds, final Master mst, final DBCollection coll,
        final DBCollection hcoll, final Map<Integer, Map<String, Integer>> occMap)
        throws BrumaException, IOException {
    assert mstName != null;
    assert id > 0;
    assert url != null;
    assert urlTag > 0;
    assert err != null;
    assert ccsFlds != null;
    assert mst != null;
    assert coll != null;
    assert hcoll != null;
    assert occMap != null;

    final Record rec = mst.getRecord(id);
    if (!rec.isActive()) {
        //throw new BrumaException("not active record mfn=" + id);
        System.err.println("WARNING: record[" + id + "] is not active. " + "Ignoring it!");
        return false;
    }/* w  ww .  j  a  va2 s  .  c  o m*/

    final List<Field> urls = rec.getFieldList(urlTag);
    final Date now = new Date();
    Date date;

    Map<String, Integer> fldMap = occMap.get(id);
    if (fldMap == null) {
        fldMap = new HashMap<String, Integer>();
        occMap.put(id, fldMap);
    }

    final int occ = nextOcc(url, urls, fldMap);
    if (occ == -1) {
        System.err.println("url[" + url + "] not found. mfn=" + id);
        //throw new IOException("url[" + url + "] not found. mfn=" + id);                
        return false;
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, id + "_" + occ);
    final boolean ret;
    if (fixedRecently(hcoll, query, now, 60)) {
        ret = false;
    } else {
        final BasicDBObject obj = (BasicDBObject) coll.findOne(query);
        if (obj == null) {
            date = now;
        } else {
            date = obj.getDate(LAST_UPDATE_FIELD);
            if (date == null) {
                date = obj.getDate(DATE_FIELD);
            } else {
                final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED);
                if (!wr.getCachedLastError().ok()) {
                    //TODO
                }
                date = obj.getDate(DATE_FIELD);
            }
        }
        final String url_d = EncDecUrl.decodeUrl(url);
        final String url_d_l = (url_d.length() >= 900) ? url_d.substring(0, 900) + "..." : url_d;
        final String url_l = (url.length() > 900) ? url.substring(0, 900) + "..." : url;
        final BasicDBObject doc = new BasicDBObject();
        doc.put(DATE_FIELD, date);
        doc.put(LAST_UPDATE_FIELD, now);
        doc.put(MST_FIELD, mstName);
        doc.put(ID_FIELD, id + "_" + occ);
        doc.put(BROKEN_URL_FIELD, url_l);
        doc.put(PRETTY_BROKEN_URL_FIELD, url_d_l);
        doc.put(MSG_FIELD, err);
        doc.put(CENTER_FIELD, getCCS(rec, ccsFlds));

        final WriteResult wres = coll.save(doc, WriteConcern.ACKNOWLEDGED);
        ret = wres.getCachedLastError().ok();
    }
    return ret;
}

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

License:Open Source License

private static boolean fixedRecently(final DBCollection hcoll, final BasicDBObject query, final Date now,
        final int days) {
    assert hcoll != null;
    assert query != null;
    assert now != null;
    assert days >= 0;

    final boolean ret;
    final BasicDBObject obj = (BasicDBObject) hcoll.findOne(query);
    if (obj == null) {
        ret = false;/*from w ww. j  a v a2 s .c  om*/
    } else {
        final Date date = obj.getDate(LAST_UPDATE_FIELD);
        if (date == null) {
            ret = false;
        } else {
            ret = (now.getTime() - date.getTime() <= (days * MILISECONDS_IN_A_DAY));
        }
    }
    return ret;
}

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

License:Open Source License

private static boolean removeOldDocs(final DBCollection coll) {
    assert coll != null;

    final Date now = new Date();
    final DBCursor cursor = coll.find();
    boolean ret = true;

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final Date auxDate = obj.getDate(LAST_UPDATE_FIELD);
        if ((auxDate == null) || (now.getTime() - auxDate.getTime()) > 60 * 60 * 1000) {
            final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED);
            ret = ret && wr.getCachedLastError().ok();
        }//from w  ww .j  a v a 2  s  .  co m
    }
    return ret;
}

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  .j  a v  a  2 s.c o 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.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 w  ww  .  j ava 2s . 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 {//from w w w.ja  v  a 2s.  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);
}

From source file:com.emuneee.camerasyncmanager.util.DatabaseUtil.java

License:Apache License

/**
 * Converts BasicDBObject to camera//from   ww w . j av a  2 s  . c o  m
 * @param dbObj
 * @return
 */
public Camera dbObjectToCamera(BasicDBObject dbObj) {
    Camera camera = new Camera();
    camera.setId(dbObj.getString("_id"));
    camera.setTitle(dbObj.getString("title"));
    camera.setLatitude(dbObj.getDouble("latitude"));
    camera.setLongitude(dbObj.getDouble("longitude"));
    camera.setCity(dbObj.getString("city"));
    camera.setArea(dbObj.getString("area"));
    camera.setState(dbObj.getString("state"));
    camera.setZipCode(dbObj.getString("zipcode"));
    camera.setCountry(dbObj.getString("country"));
    camera.setUrl(dbObj.getString("url"));
    camera.setCreated(dbObj.getDate("created"));
    camera.setUpdated(dbObj.getDate("updated"));
    return camera;
}

From source file:com.epam.dlab.auth.dao.UserInfoDAOMongoImpl.java

License:Apache License

@Override
public UserInfo getUserInfoByAccessToken(String accessToken) {
    BasicDBObject uiSearchDoc = new BasicDBObject();
    uiSearchDoc.put("_id", accessToken);
    MongoCollection<BasicDBObject> mc = ms.getCollection("security", BasicDBObject.class);
    FindIterable<BasicDBObject> res = mc.find(uiSearchDoc);
    BasicDBObject uiDoc = res.first();
    if (uiDoc == null) {
        log.warn("UI not found {}", accessToken);
        return null;
    }/*ww  w .ja v  a 2 s .  c  o m*/
    Date lastAccess = uiDoc.getDate("expireAt");
    if (inactiveUserTimeoutMsec < Math.abs(new Date().getTime() - lastAccess.getTime())) {
        log.warn("UI for {} expired but were not evicted from DB. Contact MongoDB admin to create expireable "
                + "index" + " on 'expireAt' key.", accessToken);
        this.deleteUserInfo(accessToken);
        return null;
    }
    String name = uiDoc.get("name").toString();
    String firstName = uiDoc.getString("firstName", "");
    String lastName = uiDoc.getString("lastName", "");
    String remoteIp = uiDoc.getString("remoteIp", "");
    BasicDBList roles = (BasicDBList) uiDoc.get("roles");
    Boolean awsUser = uiDoc.getBoolean("awsUser", false);
    UserInfo ui = new UserInfo(name, accessToken);
    ui.setFirstName(firstName);
    ui.setLastName(lastName);
    ui.setRemoteIp(remoteIp);
    ui.setAwsUser(awsUser);
    Object awsKeys = uiDoc.get("awsKeys");
    if (awsKeys != null) {
        ((BasicDBObject) awsKeys).forEach((key, val) -> ui.addKey(key, val.toString()));
    }
    roles.forEach(o -> ui.addRole("" + o));
    log.debug("Found persistent {}", ui);
    return ui;
}

From source file:com.ikanow.infinit.e.api.social.sharing.ShareHandler.java

License:Open Source License

private String getReferenceString(SharePojo share) {
    // FILE://from  w  ww  .  ja va  2  s.c om
    if (null == share.getDocumentLocation().get_id()) { // local file based reference
        FileInputStream fin = null;
        Scanner s = null;
        try {
            File f = new File(share.getDocumentLocation().getCollection());
            fin = new FileInputStream(f);
            s = new Scanner(fin, "UTF-8");
            return (s.useDelimiter("\n").next());
        } catch (Exception e) {
            return null;
        } finally {
            try {
                if (null != fin)
                    fin.close();
                if (null != s)
                    s.close();
            } catch (Exception e) {
            } // (probably just never opened)               
        }
    }
    // DB:
    // Carry on, this is a database object
    HashSet<String> shareIdStrs = new HashSet<String>();
    for (ShareCommunityPojo commIds : share.getCommunities()) {
        shareIdStrs.add(commIds.get_id().toString());
    }
    String retVal = null;
    BasicDBObject query = new BasicDBObject(DocumentPojo._id_, share.getDocumentLocation().get_id()); // (same for all artifacts)
    String dbName = share.getDocumentLocation().getDatabase();
    String collectionName = share.getDocumentLocation().getCollection();
    BasicDBObject returnVal = (BasicDBObject) MongoDbManager.getCollection(dbName, collectionName)
            .findOne(query);
    try {
        BasicDBList communities = null;
        boolean bCustomJob = dbName.equals("custommr"); // (a bit different)
        boolean bFoundOverlap = false;
        if (!bCustomJob) {
            ObjectId communityId = (ObjectId) returnVal.get(DocumentPojo.communityId_); // (same for other artifacts)
            bFoundOverlap = shareIdStrs.contains(communityId.toString());
        } else {
            communities = (BasicDBList) returnVal.get("communityIds"); // (shared across multiple json types)
            for (Object commIdObj : communities) {
                ObjectId commId = (ObjectId) commIdObj;
                if (shareIdStrs.contains(commId.toString())) {
                    bFoundOverlap = true;
                    break;
                }
            }
        }
        if (!bFoundOverlap) {
            throw new RuntimeException(""); // (turned into the common message below)
        }
        if (!bCustomJob) { // everything but custom jobs
            Date modifiedTime = returnVal.getDate(DocumentPojo.modified_); // (same for other artifacts)
            if (null != modifiedTime) {
                share.setModified(modifiedTime);
            }
            retVal = returnVal.toString();
        } else { // custom jobs
            String database = returnVal.getString(CustomMapReduceJobPojo.outputDatabase_);
            if (null == database) {
                database = dbName;
            }
            Date modifiedTime = returnVal.getDate(CustomMapReduceJobPojo.lastCompletionTime_);
            if (null != modifiedTime) {
                share.setModified(modifiedTime);
            }
            String collection = returnVal.getString(CustomMapReduceJobPojo.outputCollection_);
            BasicDBObject returnVal2 = (BasicDBObject) MongoDbManager.getCollection(database, collection)
                    .findOne();
            retVal = returnVal2.toString();
        }
    } catch (Exception e) {
        throw new RuntimeException("Document not found or permission issue (no overlapping communities)");
    }
    return retVal;
}

From source file:com.tml.pathummoto.Dao.BillDao.java

public ArrayList<Bill> searchBill(String no) {

    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("Bill");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("vehicleNo", no);
    DBCursor cursor = coll.find(whereQuery);

    ArrayList<Bill> bills = new ArrayList<Bill>();

    while (cursor.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cursor.next();

        if (doc != null) {
            Bill bill = new Bill();
            bill.setDate(doc.getDate("Date"));
            bill.setKm(doc.getInt("km"));
            bill.setPayment(doc.getInt("payment"));
            bill.setService(doc.getInt("service"));
            bill.setTotalCost(doc.getInt("total"));
            String array = "";
            int partSize = doc.getInt("partsSize");
            for (int i = 0; i < partSize; i++) {
                String parts = "part" + i;
                String quentity = "quant" + i;
                array = array + doc.getString(parts) + " : " + doc.getInt(quentity) + "\n";

            }/*from   ww  w .  j av a  2 s .  c om*/
            bill.setParts(array);
            bills.add(bill);
        }
    }
    return bills;
}