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:eu.eubrazilcc.lvl.storage.dao.SandflyDAO.java

License:EUPL

private BasicDBObject parseFilter(final String parameter, final String expression, final BasicDBObject query)
        throws InvalidFilterParseException {
    BasicDBObject query2 = query;//  www  .  j  a v  a  2  s  .  c o m
    if (isNotBlank(parameter) && isNotBlank(expression)) {
        String field = null;
        // keyword matching search
        if ("source".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "dataSource";
        } else if ("definition".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "definition";
        } else if ("accession".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "accession";
        } else if ("length".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "length";
        } else if ("gene".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "gene";
        } else if ("organism".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "organism";
        } else if ("country".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "countryFeature";
        } else if ("locale".equalsIgnoreCase(parameter)) {
            field = DB_PREFIX + "locale";
        }
        if (isNotBlank(field)) {
            if ("accession".equalsIgnoreCase(parameter)) {
                // convert the expression to upper case and compare for exact matching
                query2 = (query2 != null ? query2 : new BasicDBObject()).append(field,
                        expression.toUpperCase()); // TODO
            } else if ("locale".equalsIgnoreCase(parameter)) {
                // regular expression to match the language part of the locale
                final Pattern regex = compile("(" + expression.toLowerCase() + ")([_]{1}[A-Z]{2}){0,1}");
                query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, regex); // TODO
            } else if ("length".equalsIgnoreCase(parameter)) {
                // comparison operator
                query2 = mongoNumeriComparison(field, expression); // TODO
            } else {
                // regular expression to match all entries that contains the keyword
                final Pattern regex = compile(".*" + expression + ".*", CASE_INSENSITIVE);
                query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, regex); // TODO
            }
        } else {
            // full-text search
            if ("text".equalsIgnoreCase(parameter)) {
                field = "$text";
            }
            if (isNotBlank(field)) {
                if (query2 != null) {
                    final BasicDBObject textSearch = (BasicDBObject) query2.get("$text");
                    final BasicDBObject search = new BasicDBObject("$search",
                            textSearch != null ? textSearch.getString("$search") + " " + expression
                                    : expression);
                    query2 = query2.append("$text", search.append("$language", "english"));
                } else {
                    final BasicDBObject search = new BasicDBObject("$search", expression);
                    query2 = new BasicDBObject().append("$text", search.append("$language", "english"));
                }
            } else {
                throw new InvalidFilterParseException(parameter);
            }
        }
    }
    return query2;
}

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

License:Apache License

/** 
 * Push a new item// w  w w.j  a v a 2  s  .c o 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.crawler.queue.mongodb.MongoDBSourceItemsQueue.java

License:Apache License

/** 
 * Check if item is in queue/* w ww.j ava  2  s.  co  m*/
 * 
 * @return requested field value or null
 */
//private String contains(String keyValue, String returnedField) {   
//   BasicDBObject doc = getInternal(keyValue, false);
//   if (doc==null) return null;
//   return doc.getString(returnedField);         
//}

private BasicDBObject getInternal(String keyValue, boolean done) {

    String queryTimeStamp;
    if (done) {
        if (startDepth == 0)
            queryTimeStamp = "{\"" + timestampFieldName + "\":" + String.valueOf(startTime) + "}";
        else {
            queryTimeStamp = "{\"" + timestampFieldName + "\": {\"$lte\": " + String.valueOf(startTime) + "}}";
        }
    } else
        queryTimeStamp = "{\"" + timestampFieldName + "\": {\"$gt\": " + String.valueOf(startTime) + "}}";

    String queryHash = "{\"" + hashFieldName + "\":" + keyValue.hashCode() + "}";
    String query = "{\"$and\": [" + queryTimeStamp + ", " + queryHash + "]}";

    BasicDBObject docsearch = MongoDBHelper.JSON2BasicDBObject(query);

    //synchronized (collMonitor) {
    DBCursor cur = coll.getColl().find(docsearch);
    if (cur.count() == 0)
        return null;
    while (cur.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cur.next();
        if (keyValue.equals(doc.getString(uniqueKeyFieldName))) {
            return doc;
        }
    }
    //}
    return null;
}

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

License:Apache License

public boolean updateDone(Map<String, Object> item) {
    // TODO: optimize 1 search + 1 update !!! may be the MongoDB _id is in the json
    BasicDBObject doc = new BasicDBObject(item);
    String keyValue = doc.getString(uniqueKeyFieldName);

    synchronized (collMonitor) {
        BasicDBObject curDoc = getInternal(keyValue, true);
        if (curDoc == null)
            return false;
        coll.update(curDoc, doc);/*from   w w  w.ja va  2  s.co m*/
    }
    return true;
}

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

License:Apache License

public ArrayList<String> getValues(BasicDBObject docsearch, String field) {
    DBCursor cur = null;// w w w .  ja  v a2 s .co m
    if (docsearch != null)
        cur = coll.find(docsearch);
    else
        cur = coll.find();
    if (cur.count() == 0)
        return null;
    ArrayList<String> values = new ArrayList<String>();
    while (cur.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cur.next();
        values.add((String) doc.getString(field));
    }
    return values;
}

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

License:Apache License

public String getValue(BasicDBObject docsearch, String field) {
    DBCursor cur = coll.find(docsearch);
    if (cur.count() != 1)
        return null;
    BasicDBObject doc = (BasicDBObject) cur.next();
    return (String) doc.getString(field);
}

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

License:Apache License

public String getFirstValue(BasicDBObject docsearch, String field) {
    DBCursor cur = coll.find(docsearch);
    if (cur.count() == 0)
        return null;
    BasicDBObject doc = (BasicDBObject) cur.next();
    return (String) doc.getString(field);
}

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

License:Open Source License

/**
 * /*from   w ww  . j a va  2  s . c  o m*/
 * @param dbvitam
 * @param model
 * @param bson
 * @return True if inserted in ES
 */
public static final boolean addEsIndex(final CassandraAccess dbvitam, final String model,
        final BSONObject bson) {
    BasicDBObject maip = getFiltered(bson);
    final String id = maip.getString(VitamType.ID);
    maip.removeField(VitamType.ID);
    return dbvitam.addEsEntryIndex(model, id, maip.toString());
}

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

License:Open Source License

/**
 * Should be called only once saved (last time), but for the moment let the object as it is, next should remove not indexable
 * entries/* w  w  w .j a v a 2s  . co  m*/
 *
 * @param dbvitam
 * @param model
 * @param indexes
 * @param bson
 * @return the number of DAip incorporated (0 if none)
 */
public static final int addEsIndex(final CassandraAccess dbvitam, final String model,
        final Map<String, String> indexes, final BSONObject bson) {
    BasicDBObject maip = getFiltered(bson);
    final String id = maip.getString(VitamType.ID);
    maip.removeField(VitamType.ID);
    // System.err.println(maip);
    // System.err.println(this);
    indexes.put(id, maip.toString());
    int nb = 0;
    if (indexes.size() > GlobalDatas.LIMIT_ES_NEW_INDEX) {
        nb = indexes.size();
        dbvitam.addEsEntryIndex(indexes, model);
        // dbvitam.flushOnDisk();
        indexes.clear();
        System.out.print("o");
    }
    maip.clear();
    maip = null;
    return nb;
}

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

License:Open Source License

/**
 * /* ww w  . j  a  v  a2  s .com*/
 * @param dbvitam
 * @param model
 * @param bson
 * @return True if inserted in ES
 */
public static final boolean addEsIndex(final MongoDbAccess dbvitam, final String model, final BSONObject bson) {
    BasicDBObject maip = getFiltered(bson);
    final String id = maip.getString(VitamType.ID);
    maip.removeField(VitamType.ID);
    return dbvitam.addEsEntryIndex(model, id, maip.toString());
}