Example usage for com.mongodb BasicDBList get

List of usage examples for com.mongodb BasicDBList get

Introduction

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

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value at an index.

Usage

From source file:net.luminis.useradmin.mongodb.MongoSerializerHelper.java

License:Apache License

/**
 * Returns all roles mentioned in the given list.
 * //from  www  .j  av a2  s . c  o m
 * @param list the list with role names to convert.
 * @return a list with {@link Role}s, never <code>null</code>.
 */
private List<Role> getRoles(BasicDBList list) {
    List<Role> result = new ArrayList<Role>();
    for (int i = 0, size = list.size(); i < size; i++) {
        final String memberName = (String) list.get(i);
        result.add(findExistingMember(memberName));
    }
    return result;
}

From source file:net.tbnr.commerce.items.CommerceItemManager.java

License:Open Source License

@Override
public void reloadPlayer(TBNRPlayer player, Class<? extends CommerceItem>... recentlyPurchased) {
    Gearz.getInstance().debug("Loading player " + player.getUsername() + "...");
    BasicDBList commerce_purchases = getPurchaseList(player);
    if (this.playerCommerceData.containsKey(player)) {
        for (CommerceItem commerceItem : this.playerCommerceData.get(player).getItems()) {
            HandlerList.unregisterAll(commerceItem);
            commerceItem.onUnregister();
        }//  w  w w  . j a  v a 2  s.  c o m
    }
    List<CommerceItem> items = new ArrayList<>();
    for (int x = commerce_purchases.size() - 1; x > -1; x--) { //Load in reverse
        Object commerce_purchase = commerce_purchases.get(x);
        if (!(commerce_purchase instanceof DBObject))
            continue;
        DBObject object = (DBObject) commerce_purchase;
        String key;
        try {
            key = (String) object.get("key");
        } catch (ClassCastException ex) {
            continue;
        }
        CommerceItem commerceItem = constructCommerceItem(key, player);
        if (commerceItem == null)
            return;
        if (contains(recentlyPurchased, commerceItem.getClass()))
            commerceItem.onPurchase();
        try {
            commerceItem.register();
        } catch (Exception ex) {
            ex.printStackTrace();
            continue;
        }
        items.add(commerceItem);
    }
    this.playerCommerceData.put(player, new PlayerCommerceItems(player, items));
}

From source file:nosqltools.DBConnection.java

/**
 * Saves a collection to the DB/*w w w.j  av a2s .c  o m*/
 * @param JSON string 
 */
public boolean saveColl(String json) {
    BasicDBList objList = null;
    List<Object> documents = new ArrayList<>();
    boolean parse_error = false;

    //Get the string from text area and typecast to basic db list and dbobjects respectively

    try {
        objList = (BasicDBList) JSON.parse(json);
        parse_error = true;

        //Add objects to array list
        for (int i = 0; i < objList.size(); i++) {
            documents.add(objList.get(i));
        }

        //Save each document to collection
        for (int i = 0; i < documents.size(); i++) {
            DBObject object = (DBObject) documents.get(i);
            collection.save(object);
        }
    } catch (Exception e) {
        parse_error = false;
    }

    return parse_error;
}

From source file:org.apache.calcite.adapter.mongodb.MongoTable.java

License:Apache License

/** Executes an "aggregate" operation on the underlying collection.
 *
 * <p>For example://w  w w .  j  a  va  2  s.co  m
 * <code>zipsTable.aggregate(
 * "{$filter: {state: 'OR'}",
 * "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: '$pop'}}}")
 * </code></p>
 *
 * @param mongoDb MongoDB connection
 * @param fields List of fields to project; or null to return map
 * @param operations One or more JSON strings
 * @return Enumerator of results
 */
public Enumerable<Object> aggregate(final DB mongoDb, final List<Map.Entry<String, Class>> fields,
        final List<String> operations) {
    final List<DBObject> list = new ArrayList<DBObject>();
    final BasicDBList versionArray = (BasicDBList) mongoDb.command("buildInfo").get("versionArray");
    final Integer versionMajor = parseIntString(versionArray.get(0).toString());
    final Integer versionMinor = parseIntString(versionArray.get(1).toString());
    //    final Integer versionMaintenance = parseIntString(versionArray
    //      .get(2).toString());
    //    final Integer versionBuild = parseIntString(versionArray
    //      .get(3).toString());

    for (String operation : operations) {
        list.add((DBObject) JSON.parse(operation));
    }
    final DBObject first = list.get(0);
    final List<DBObject> rest = Util.skip(list);
    final Function1<DBObject, Object> getter = MongoEnumerator.getter(fields);
    return new AbstractEnumerable<Object>() {
        public Enumerator<Object> enumerator() {
            final Iterator<DBObject> resultIterator;
            try {
                // Changed in version 2.6: The db.collection.aggregate() method
                // returns a cursor
                // and can return result sets of any size.
                // See: http://docs.mongodb.org/manual/core/aggregation-pipeline
                if (versionMajor > 1) {
                    // MongoDB version 2.6+
                    if (versionMinor > 5) {
                        AggregationOptions options = AggregationOptions.builder()
                                .outputMode(AggregationOptions.OutputMode.CURSOR).build();
                        // Warning - this can result in a very large ArrayList!
                        // but you should know your data and aggregate accordingly
                        ArrayList<DBObject> resultAsArrayList = new ArrayList<DBObject>(
                                Util.toList(mongoDb.getCollection(collectionName).aggregate(list, options)));
                        resultIterator = resultAsArrayList.iterator();
                    } else { // Pre MongoDB version 2.6
                        AggregationOutput result = aggregateOldWay(mongoDb.getCollection(collectionName), first,
                                rest);
                        resultIterator = result.results().iterator();
                    }
                } else { // Pre MongoDB version 2
                    AggregationOutput result = aggregateOldWay(mongoDb.getCollection(collectionName), first,
                            rest);
                    resultIterator = result.results().iterator();
                }
            } catch (Exception e) {
                throw new RuntimeException(
                        "While running MongoDB query " + Util.toString(operations, "[", ",\n", "]"), e);
            }
            return new MongoEnumerator(resultIterator, getter);
        }
    };
}

From source file:org.apache.camel.component.mongodb.MongoDbProducer.java

License:Apache License

/**
* All headers except collection and database are non available for this
* operation./*from  w ww .  ja va  2s  . com*/
* 
* @param exchange
* @throws Exception
*/
protected void doAggregate(Exchange exchange) throws Exception {
    DBCollection dbCol = calculateCollection(exchange);
    DBObject query = exchange.getIn().getMandatoryBody(DBObject.class);

    // Impossible with java driver to get the batch size and number to skip
    Iterable<DBObject> dbIterator = null;
    try {
        AggregationOutput aggregationResult = null;

        // Allow body to be a pipeline
        // @see http://docs.mongodb.org/manual/core/aggregation/
        if (query instanceof BasicDBList) {
            BasicDBList queryList = (BasicDBList) query;
            aggregationResult = dbCol.aggregate((DBObject) queryList.get(0),
                    queryList.subList(1, queryList.size()).toArray(new BasicDBObject[queryList.size() - 1]));
        } else {
            aggregationResult = dbCol.aggregate(query);
        }

        dbIterator = aggregationResult.results();
        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.aggregate);
        resultMessage.setBody(dbIterator);

        // Mongo Driver does not allow to read size and to paginate aggregate result
    } catch (Exception e) {
        // rethrow the exception
        throw e;
    }
}

From source file:org.apache.chemistry.opencmis.mongodb.MongodbUtils.java

License:Apache License

public String getPathToNode(BasicDBObject node, DBCollection collection) {
    StringBuilder path = new StringBuilder();
    BasicDBList ancestorsList = this.getNodeAncestors(node, collection);
    for (int i = 0; i < ancestorsList.size(); i++) {

    }/*from   w w w. j  a va 2s.  c  o m*/
    int i = 0;
    while (ancestorsList.get(i) != null) {
        DBObject ancestor = (DBObject) ancestorsList.get(i);
        path.append(PATH_SEPARATOR).append(ancestor.get("title").toString());
    }
    DBCursor ancestors = collection
            .find(new BasicDBObject().append("left", new BasicDBObject().append("$lt", node.getLong("left"))))
            .sort(new BasicDBObject().append("left", 1));
    while (ancestors.hasNext()) {
        DBObject ancestor = ancestors.next();
        path.append(PATH_SEPARATOR).append(ancestor.get("name").toString());
    }
    return path.toString();
}

From source file:org.apache.felix.useradmin.mongodb.MongoSerializerHelper.java

License:Apache License

/**
 * Returns all roles mentioned in the given list.
 * //from   w ww .j  av a2 s.com
 * @param list the list with role names to convert, can be <code>null</code>.
 * @return a list with {@link Role}s, never <code>null</code>.
 */
private List<Role> getRoles(BasicDBList list) {
    List<Role> result = new ArrayList<Role>();
    // FELIX-4399: MongoDB does return null for empty properties...
    int size = (list == null) ? 0 : list.size();
    for (int i = 0; i < size; i++) {
        final String memberName = (String) list.get(i);
        result.add(findExistingMember(memberName));
    }
    return result;
}

From source file:org.apache.isis.objectstore.nosql.db.mongo.MongoStateReader.java

License:Apache License

@Override
public List<StateReader> readCollection(final String id) {
    BasicDBList array = (BasicDBList) instance.get(id);
    final List<StateReader> readers = new ArrayList<StateReader>();
    if (array != null) {
        final int size = array.size();
        for (int i = 0; i < size; i++) {
            readers.add(new MongoStateReader((DBObject) array.get(i)));
        }/* w w w  . j av a 2  s  .  com*/
    }
    return readers;
}

From source file:org.apache.rya.mongodb.instance.MongoDetailsAdapter.java

License:Apache License

private static PCJIndexDetails.Builder getPCJIndexDetails(final BasicDBObject basicObj) {
    final BasicDBObject pcjIndexDBO = (BasicDBObject) basicObj.get(PCJ_DETAILS_KEY);

    final PCJIndexDetails.Builder pcjBuilder = PCJIndexDetails.builder()
            .setEnabled(pcjIndexDBO.getBoolean(PCJ_ENABLED_KEY))
            .setFluoDetails(new FluoDetails(pcjIndexDBO.getString(PCJ_FLUO_KEY)));

    final BasicDBList pcjs = (BasicDBList) pcjIndexDBO.get(PCJ_PCJS_KEY);
    if (pcjs != null) {
        for (int ii = 0; ii < pcjs.size(); ii++) {
            final BasicDBObject pcj = (BasicDBObject) pcjs.get(ii);
            pcjBuilder.addPCJDetails(toPCJDetails(pcj));
        }//from   www. j a  va2s  . c  om
    }
    return pcjBuilder;
}

From source file:org.aw20.mongoworkbench.command.MongoCommand.java

License:Open Source License

protected BasicDBList fixNumbers(BasicDBList dbo) {
    for (int x = 0; x < dbo.size(); x++) {
        Object o = dbo.get(x);

        if (o instanceof Double) {
            dbo.set(x, NumberUtil.fixDouble((Double) o));
        } else if (o instanceof BasicDBObject) {
            dbo.set(x, fixNumbers((BasicDBObject) o));
        } else if (o instanceof BasicDBList) {
            dbo.set(x, fixNumbers((BasicDBList) o));
        }/*from   w w  w  .j  a va2  s  . c om*/
    }

    return dbo;
}