Example usage for com.mongodb BasicDBObject get

List of usage examples for com.mongodb BasicDBObject get

Introduction

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

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value from this object

Usage

From source file:eu.eubrazilcc.lvl.storage.oauth2.dao.PendingUserDAO.java

License:EUPL

@SuppressWarnings("unused")
private PendingUser parseObject(final Object obj) {
    final BasicDBObject obj2 = (BasicDBObject) obj;
    return map((BasicDBObject) obj2.get("obj")).getPendingUser();
}

From source file:eu.eubrazilcc.lvl.storage.oauth2.dao.ResourceOwnerDAO.java

License:EUPL

@SuppressWarnings("unused")
private ResourceOwner parseObject(final Object obj, final boolean excludeHistory) {
    final BasicDBObject obj2 = (BasicDBObject) obj;
    final ResourceOwner owner = map((BasicDBObject) obj2.get("obj")).getResourceOwner();
    addGravatar(owner);/*  w ww.  java2 s.c o  m*/
    excludeHistory(owner, excludeHistory);
    return owner;
}

From source file:example.springdata.mongodb.util.MongosSystemForTestFactory.java

License:Apache License

private boolean isReplicaSetStarted(BasicDBObject setting) {
    if (setting.get("members") == null) {
        return false;
    }//from w w  w  .  ja  va2  s. co m

    BasicDBList members = (BasicDBList) setting.get("members");
    for (Object m : members.toArray()) {
        BasicDBObject member = (BasicDBObject) m;
        logger.info(member.toString());
        int state = member.getInt("state");
        logger.info("state: {}", state);
        // 1 - PRIMARY, 2 - SECONDARY, 7 - ARBITER
        if (state != 1 && state != 2 && state != 7) {
            return false;
        }
    }
    return true;
}

From source file:fr.cnes.sitools.dataset.database.mongodb.RequestMongoDB.java

License:Open Source License

@Override
public String getFilterClause(List<Predicat> predicats, List<Column> columns) {
    BasicDBObject whereClause = new BasicDBObject();
    // loop over the predicats
    // boolean first = true;
    // String result = "{";
    ////from  ww  w .j  a  v  a  2 s  .c  om
    // Map<String, List<Predicat>> orderedPredicats = orderPredicat(predicats);
    // for (Predicat predicat : predicats) {
    //
    // String filter = getFilter(predicat);
    // if (filter != null && !"".equals(filter)) {
    // // DBObject objPredicat = (DBObject) JSON.parse(filter);
    // // if (objPredicat != null) {
    // if (first) {
    // whereClause.append("$and", new ArrayList<DBObject>());
    // }
    // else {
    // result += ",";
    // }
    // first = false;
    //
    // result += filter;
    //
    // // ((List<DBObject>) whereClause.get("$and")).add(objPredicat);
    //
    // // if (whereClause.containsField(key)) {
    // // // If the key already exists append the value to the existing key
    // // // DBObject obj = new BasicDBObject();
    // // // obj.put("$and", objPredicat.get(key));
    // //
    // // if (!whereClause.containsField("$and")) {
    // // whereClause.append("$and", new ArrayList<DBObject>());
    // // DBObject pred = (DBObject) whereClause.get(key);
    // // whereClause.remove(key);
    // // ((List<DBObject>) whereClause.get("$and")).add(pred);
    // //
    // // }
    // // ((List<DBObject>) whereClause.get("$and")).add(objPredicat);
    // //
    // // // ((DBObject) whereClause.get(key)).putAll(obj);
    // // }
    // // else {
    // // // if the key doesn't exists just append the predicat to the whereClause
    // // whereClause.append(key, objPredicat.get(key));
    // // }
    //
    // }
    // // }
    // }

    for (Predicat predicat : predicats) {
        String filter = getFilter(predicat);
        if (filter != null && !"".equals(filter)) {
            DBObject objPredicat = (DBObject) JSON.parse(filter);
            if (objPredicat != null) {
                Set<String> keys = objPredicat.keySet();
                for (String key : keys) {
                    if (whereClause.containsField(key)) {
                        ((DBObject) whereClause.get(key)).putAll((DBObject) objPredicat.get(key));
                    } else {
                        whereClause.append(key, objPredicat.get(key));
                    }

                }
            }
        }
    }

    return whereClause.toString();
}

From source file:fr.cnes.sitools.datasource.mongodb.dbexplorer.MongoDBExplorerResource.java

License:Open Source License

/**
 * Create a List of {@link MongoDBAttributeValue} from a {@link BasicDBObject} Each {@link MongoDBAttributeValue}
 * contains the key and its children. If withValue is true, it also contains the value
 * //from www.  j ava  2 s . c  om
 * @param dbObject
 *          the {@link BasicDBObject}
 * @param withValue
 *          true to set the value as well
 * @return the List of {@link MongoDBAttributeValue}
 */
private List<MongoDBAttributeValue> getAttributeValue(BasicDBObject dbObject, boolean withValue) {
    List<MongoDBAttributeValue> children = new ArrayList<MongoDBAttributeValue>();
    for (String key : dbObject.keySet()) {
        Object value = dbObject.get(key);
        MongoDBAttributeValue attr = new MongoDBAttributeValue();
        attr.setName(key.toString());
        // TODO check null values
        attr.setType(dbObject.get(key).getClass().getSimpleName());
        if (isObject(value)) {
            BasicDBObject dbObjectValue = (BasicDBObject) value;
            attr.setChildren(getAttributeValue(dbObjectValue, withValue));
        } else if (withValue) {
            attr.setValue(dbObject.get(key));
        }

        children.add(attr);
    }
    return children;
}

From source file:fr.eolya.crawler.queue.mongodb.MongoDBSourceItemsQueue.java

License:Apache License

/** 
 * Push a new item//from w  w w .ja v  a 2 s  .  co  m
 * 
 * @return success or not
 */
public boolean push(Map<String, Object> item) throws QueueIncoherenceException, QueueInvalidDataException {

    boolean ret = true;

    BasicDBObject doc = new BasicDBObject(item);

    String keyValue = doc.getString(uniqueKeyFieldName);
    String depth = doc.getString(depthFieldName);
    String sourceId = doc.getString(sourceIdFieldName);

    if (sourceId == null || keyValue == null || depth == null)
        throw new QueueInvalidDataException("Missing fields in json");
    if (Integer.parseInt(sourceId) != this.sourceId)
        throw new QueueInvalidDataException("Invalid source id in json");

    String referer = doc.getString(refererFieldName);

    // Get existing item in queue
    String currentDepth = null;
    String currentReferers = null;
    long currentTimestamp = 0;
    BasicDBObject docsearch = new BasicDBObject();
    docsearch.put(sourceIdFieldName, Integer.parseInt(sourceId));
    docsearch.put(hashFieldName, keyValue.hashCode());

    synchronized (collMonitor) {
        BasicDBObject curDoc = null;
        DBCursor cur = coll.getColl().find(docsearch);
        if (cur.count() > 0) {
            while (cur.hasNext() && curDoc == null) {
                curDoc = (BasicDBObject) cur.next();
                if (!keyValue.equals(doc.getString(uniqueKeyFieldName))) {
                    curDoc = null;
                }
            }
            if (curDoc != null) {
                currentDepth = curDoc.getString(depthFieldName);
                currentReferers = curDoc.getString(referersFieldName);
                currentTimestamp = curDoc.getLong(timestampFieldName);

                /*
                 * Remember : for an item of the collection :
                 *       timestamp < starttime   => not in queue
                 *       timestamp > starttime   => in queue
                 *       timestamp = starttime   => done
                 */
                if ((Long.parseLong(depth) >= Long.parseLong(currentDepth)) && (currentTimestamp >= startTime))
                    return false;
            }
        }

        // build new doc
        doc.put(hashFieldName, keyValue.hashCode());
        doc.put(timestampFieldName, new Date().getTime());

        if (referer != null) {
            if (currentReferers == null) {
                currentReferers = referer;
            } else {
                currentReferers += "/n" + referer;
            }
        }
        if (currentReferers != null) {
            doc.put(referersFieldName, currentReferers);
        }
        if (curDoc != null) {

            doc.put("content_type", curDoc.get("content_type"));
            doc.put("crawl_last_time", curDoc.get("crawl_last_time"));
            doc.put("condget_last_modified", curDoc.get("condget_last_modified"));
            doc.put("condget_etag", curDoc.get("condget_etag"));

            coll.update(curDoc, doc);
            // TODO : decrease done size in some case ???
        } else {
            doc.put(createdFieldName, new Date().getTime());
            coll.add(doc);
        }
        size++;
        return ret;
    }
}

From source file:fr.eolya.utils.nosql.mongodb.MongoDBCollection.java

License:Apache License

public String add(BasicDBObject doc) {
    try {//  w  w  w .ja  v  a2 s  .  c o m
        coll.insert(doc);
        ObjectId id = (ObjectId) doc.get("_id");
        return id.toString();
    } catch (Exception e) {
        //e.printStackTrace();
        return null;
    }
}

From source file:fr.eolya.utils.nosql.mongodb.MongoDBHelper.java

License:Apache License

public static Map<String, Object> BasicDBObject2Map(BasicDBObject doc) {
    Map<String, Object> ret = new HashMap<String, Object>();
    Iterator<Entry<String, Object>> it = doc.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
        Object o = doc.get(entry.getKey());
        ret.put(entry.getKey(), o);//from  w w w  .  jav a2s. c om
        if (o != null) {
            if (o instanceof Double) {
                try {
                    Double d = (Double) o;
                    ret.put(entry.getKey(), new Integer(d.intValue()));
                } catch (Exception e) {
                }
            }
        }
    }
    return ret;
}

From source file:fr.gouv.vitam.cases.ElasticSearchAccess.java

License:Open Source License

private static final BasicDBObject getFiltered(final BSONObject bson) {
    BasicDBObject maip = new BasicDBObject();
    maip.putAll(bson);/*w ww .  j a  v  a 2s . com*/
    maip.removeField(VitamLinks.DAip2DAip.field1to2);
    // Keep it maip.removeField(VitamLinks.DAip2DAip.field2to1);
    //maip.removeField(VitamLinks.Domain2DAip.field2to1);
    //maip.removeField(VitamLinks.DAip2Dua.field1to2);
    //maip.removeField(VitamLinks.DAip2PAip.field1to2);
    // maip.removeField(ParserIngest.REFID);
    // DOMDEPTH already ok but duplicate it
    @SuppressWarnings("unchecked")
    final HashMap<String, Integer> map = (HashMap<String, Integer>) maip.get(DAip.DAIPDEPTHS);
    //final List<String> list = new ArrayList<>(map.keySet());
    maip.append(DAip.DAIPPARENTS, map.keySet());// was list);
    return maip;
}

From source file:GeoHazardServices.Inst.java

License:Apache License

private String _computeById(User user, String evtid, Integer dur, Integer accel, Integer gridres, String algo) {
    DBObject eq = db.getCollection("eqs").findOne(new BasicDBObject("_id", evtid));
    if (eq == null)
        return null;

    BasicDBObject process = new BasicDBObject("process", new BasicDBList());
    BasicDBObject set = new BasicDBObject("$set", process);
    db.getCollection("eqs").update(eq, set);

    /* extract properties to pass them to the request method */
    BasicDBObject prop = (BasicDBObject) eq.get("prop");
    double lat = prop.getDouble("latitude");
    double lon = prop.getDouble("longitude");
    double dip = prop.getDouble("dip");
    double strike = prop.getDouble("strike");
    double rake = prop.getDouble("rake");
    double depth = prop.getDouble("depth");
    Date date = prop.getDate("date");

    EQParameter eqp;/*from   ww  w. j  a  va2  s. c  o  m*/
    double mag = 0.0;
    double slip = 0.0;
    double length = 0.0;
    double width = 0.0;
    if (prop.get("magnitude") == null) {
        slip = prop.getDouble("slip");
        length = prop.getDouble("length");
        width = prop.getDouble("width");
        eqp = new EQParameter(lon, lat, slip, length, width, depth, dip, strike, rake, date);
    } else {
        mag = prop.getDouble("magnitude");
        eqp = new EQParameter(lon, lat, mag, depth, dip, strike, rake, date);
    }

    if (accel == null)
        accel = 1;

    /* start request */
    EQTask task = new EQTask(eqp, evtid, user, dur, accel, gridres);
    task.algo = algo;
    task.setSlots(IScheduler.SLOT_NORMAL, IScheduler.SLOT_EXCLUSIVE);
    return request(evtid, task);
}