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:org.canedata.provider.mongodb.entity.MongoEntity.java

License:Apache License

private void loadForMissedCacheHits(Map<Object, MongoFields> missed, Set<String> proj) {
    Set<Object> ids = missed.keySet();
    if (logger.isDebug())
        logger.debug("Loading data for missed cache hits, _id is {0}.", Arrays.toString(ids.toArray()));

    BasicDBObject query = new BasicDBObject();
    query.append("_id", new BasicDBObject().append("$in", ids.toArray()));

    DBCursor cursor = null;/*  ww  w. j  a v a2  s. c om*/
    try {
        cursor = getCollection().find(query);
        while (cursor.hasNext()) {
            BasicDBObject dbo = (BasicDBObject) cursor.next();

            Object id = dbo.get("_id");

            if (logger.isDebug())
                logger.debug("Loaded data for missed cache hits, _id is {0}.", id.toString());

            MongoFields mf = missed.get(id).putTarget(dbo);
            getCache().cache(mf.clone());
            if (!proj.isEmpty())
                mf.project(proj);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
}

From source file:org.canedata.provider.mongodb.entity.MongoEntity.java

License:Apache License

private void prepareOptions(BasicDBObject options) {
    for (String o : options.keySet()) {
        if (Options.MONGO_OPTION.equals(o)) {
            getCollection().addOption(options.getInt(o));
            continue;
        }/*from   w ww  .  jav  a  2 s.  c  o  m*/

        if (Options.RESET_MONGO_OPTIONS.equals(o)) {
            getCollection().resetOptions();
            continue;
        }

        if (Options.READ_PREFERENCE.equals(o)) {
            if (!(options.get(o) instanceof ReadPreference))
                throw new MalformedParameterizedTypeException();

            getCollection().setReadPreference((ReadPreference) options.get(o));

            break;
        }

        if (Options.WRITE_CONCERN.equals(o)) {
            if (!(options.get(o) instanceof WriteConcern))
                throw new MalformedParameterizedTypeException();

            getCollection().setWriteConcern((WriteConcern) options.get(o));
            break;
        }
    }
}

From source file:org.codinjutsu.tools.mongo.view.model.JsonTreeModel.java

License:Apache License

public static void processDbObject(JsonTreeNode parentNode, DBObject mongoObject) {
    if (mongoObject instanceof BasicDBList) {
        BasicDBList mongoObjectList = (BasicDBList) mongoObject;
        for (int i = 0; i < mongoObjectList.size(); i++) {
            Object mongoObjectOfList = mongoObjectList.get(i);
            JsonTreeNode currentNode = new JsonTreeNode(
                    MongoValueDescriptor.createDescriptor(i, mongoObjectOfList));
            if (mongoObjectOfList instanceof DBObject) {
                processDbObject(currentNode, (DBObject) mongoObjectOfList);
            }/*from ww  w .ja va 2 s.  c  o m*/
            parentNode.add(currentNode);
        }
    } else if (mongoObject instanceof BasicDBObject) {
        BasicDBObject basicDBObject = (BasicDBObject) mongoObject;
        for (String key : basicDBObject.keySet()) {
            Object value = basicDBObject.get(key);
            JsonTreeNode currentNode = new JsonTreeNode(MongoKeyValueDescriptor.createDescriptor(key, value));
            if (value instanceof DBObject) {
                processDbObject(currentNode, (DBObject) value);
            }
            parentNode.add(currentNode);
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.view.model.JsonTreeModel.java

License:Apache License

public static void processDbObject(NoSqlTreeNode parentNode, DBObject mongoObject) {
    if (mongoObject instanceof BasicDBList) {
        BasicDBList mongoObjectList = (BasicDBList) mongoObject;
        for (int i = 0; i < mongoObjectList.size(); i++) {
            Object mongoObjectOfList = mongoObjectList.get(i);
            NoSqlTreeNode currentNode = new NoSqlTreeNode(
                    MongoValueDescriptor.createDescriptor(i, mongoObjectOfList));
            if (mongoObjectOfList instanceof DBObject) {
                processDbObject(currentNode, (DBObject) mongoObjectOfList);
            }//from   w ww  .  jav a2s .  co m
            parentNode.add(currentNode);
        }
    } else if (mongoObject instanceof BasicDBObject) {
        BasicDBObject basicDBObject = (BasicDBObject) mongoObject;
        for (String key : basicDBObject.keySet()) {
            Object value = basicDBObject.get(key);
            NoSqlTreeNode currentNode = new NoSqlTreeNode(MongoKeyValueDescriptor.createDescriptor(key, value));
            if (value instanceof DBObject) {
                processDbObject(currentNode, (DBObject) value);
            }
            parentNode.add(currentNode);
        }
    }
}

From source file:org.datanucleus.store.mongodb.valuegenerator.IncrementGenerator.java

License:Open Source License

protected ValueGenerationBlock reserveBlock(long size) {
    if (size < 1) {
        return null;
    }/*from ww w .  j  a  v  a  2 s.  co  m*/

    List oids = new ArrayList();
    ManagedConnection mconn = connectionProvider.retrieveConnection();
    try {
        DB db = (DB) mconn.getConnection();
        if (!storeMgr.isAutoCreateTables() && !db.collectionExists(collectionName)) {
            throw new NucleusUserException(LOCALISER.msg("040011", collectionName));
        }

        // Create the collection if not existing
        DBCollection dbCollection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject();
        query.put("field-name", key);
        DBCursor curs = dbCollection.find(query);
        if (curs == null || !curs.hasNext()) {
            // No current entry for this key, so add initial entry
            long initialValue = 0;
            if (properties.containsKey("key-initial-value")) {
                initialValue = Long.valueOf(properties.getProperty("key-initial-value")) - 1;
            }
            BasicDBObject dbObject = new BasicDBObject();
            dbObject.put("field-name", key);
            dbObject.put(INCREMENT_COL_NAME, new Long(initialValue));
            dbCollection.insert(dbObject);
        }

        // Create the entry for this field if not existing
        query = new BasicDBObject();
        query.put("field-name", key);
        curs = dbCollection.find(query);
        DBObject dbObject = curs.next();

        Long currentValue = (Long) dbObject.get(INCREMENT_COL_NAME);
        long number = currentValue.longValue();
        for (int i = 0; i < size; i++) {
            oids.add(number + i + 1);
        }
        dbObject.put(INCREMENT_COL_NAME, new Long(number + size));
        dbCollection.save(dbObject);
    } finally {
        connectionProvider.releaseConnection();
    }

    return new ValueGenerationBlock(oids);
}

From source file:org.datavyu.models.db.MongoVariable.java

License:Open Source License

/**
 * Deserializes a mongo object into an Argument.
 *
 * @param serial_type The serialized argument.
 *
 * @return The argument held inside the mongo object.
 *//*from w w  w  . j  av a  2 s. c o m*/
private Argument deserializeArgument(BasicDBObject serial_type) {

    String name = (String) serial_type.get("name");
    int type_ordinal = (Integer) serial_type.get("type_ordinal");
    Argument.Type type = Argument.Type.values()[type_ordinal];
    long id = (Long) serial_type.get("id");

    Argument arg = new Argument(name, type, id);

    if (type == Argument.Type.MATRIX) {
        List<BasicDBObject> DBchildArguments = (ArrayList<BasicDBObject>) serial_type.get("child_arguments");
        List<Argument> childArguments = new ArrayList<Argument>();

        for (BasicDBObject child : DBchildArguments) {
            childArguments.add(deserializeArgument(child));
        }
        arg.childArguments = childArguments;
    }

    return arg;
}

From source file:org.eclipse.birt.data.oda.mongodb.internal.impl.QueryModel.java

License:Open Source License

private static BasicDBObject findOperation(BasicDBObject opObj, String operator) {
    if (opObj == null)
        return null;
    Object op = opObj.get(operator);
    return op instanceof BasicDBObject ? (BasicDBObject) op : null;
}

From source file:org.ecloudmanager.repository.deployment.StackTraceElementEntityConverter.java

License:Open Source License

@Override
public Object decode(final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo)
        throws MappingException {
    StackTraceElementEntity entity = new StackTraceElementEntity();
    if (fromDBObject instanceof BasicDBObject) {
        BasicDBObject basicDBObject = (BasicDBObject) fromDBObject;
        entity.setClassName((String) basicDBObject.get("className"));
        entity.setFileName((String) basicDBObject.get("fileName"));
        entity.setMethodName((String) basicDBObject.get("methodName"));
        entity.setLineNumber((Integer) basicDBObject.get("lineNumber"));
    }//from   w w  w  . ja va2 s  .  com

    return entity;
}

From source file:org.elasticsearch.river.mongodb.RiverMongoDBTestAbstract.java

License:Apache License

private boolean isReplicaSetStarted(BasicDBObject setting) {
    if (setting.get("members") == null) {
        return false;
    }//from  w  w  w.  ja  v a 2  s  . c om

    BasicDBList members = (BasicDBList) setting.get("members");
    int numPrimaries = 0;
    for (Object m : members.toArray()) {
        BasicDBObject member = (BasicDBObject) m;
        logger.trace("Member: {}", member);
        int state = member.getInt("state");
        logger.info("Member state: " + state);
        // 1 - PRIMARY, 2 - SECONDARY, 7 - ARBITER
        if (state != 1 && state != 2 && state != 7) {
            return false;
        }
        if (state == 1) {
            ++numPrimaries;
        }
    }
    if (numPrimaries != 1) {
        logger.warn("Expected 1 primary, instead found " + numPrimaries);
        return false;
    }
    return true;
}

From source file:org.exoplatform.chat.server.ChatServer.java

License:Open Source License

@Resource
@Route("/sendMeetingNotes")
public Response.Content sendMeetingNotes(String user, String token, String room, String fromTimestamp,
        String toTimestamp) throws IOException {
    if (!tokenService.hasUserWithToken(user, token)) {
        return Response.notFound("Petit malin !");
    }/* w w  w. java2 s.c  o m*/

    Long from = null;
    Long to = null;
    String html = "";
    try {
        if (fromTimestamp != null && !"".equals(fromTimestamp))
            from = Long.parseLong(fromTimestamp);
    } catch (NumberFormatException nfe) {
        LOG.info("fromTimestamp is not a valid Long number");
    }
    try {
        if (toTimestamp != null && !"".equals(toTimestamp))
            to = Long.parseLong(toTimestamp);
    } catch (NumberFormatException nfe) {
        LOG.info("fromTimestamp is not a valid Long number");
    }
    String data = chatService.read(room, userService, false, from, to);
    BasicDBObject datao = (BasicDBObject) JSON.parse(data);
    if (datao.containsField("messages")) {
        List<UserBean> users = userService.getUsers(room);
        ReportBean reportBean = new ReportBean();
        reportBean.fill((BasicDBList) datao.get("messages"), users);

        ArrayList<String> tos = new ArrayList<String>();
        String senderFullname = user;
        for (UserBean userBean : users) {
            if (!"".equals(userBean.getEmail())) {
                tos.add(userBean.getEmail());
            }
            if (user.equals(userBean.getName())) {
                senderFullname = userBean.getFullname();
            }
        }

        String roomName = "";
        List<SpaceBean> spaces = userService.getSpaces(user);
        for (SpaceBean spaceBean : spaces) {
            if (room.equals(spaceBean.getRoom())) {
                roomName = spaceBean.getDisplayName();
            }
        }
        List<RoomBean> roomBeans = userService.getTeams(user);
        for (RoomBean roomBean : roomBeans) {
            if (room.equals(roomBean.getRoom())) {
                roomName = roomBean.getFullname();
            }
        }
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String date = formatter.format(new GregorianCalendar().getTime());
        String title = roomName + " : Meeting Notes [" + date + "]";
        html = reportBean.getAsHtml(title);

        try {
            sendMailWithAuth(senderFullname, tos, html.toString(), title);
        } catch (Exception e) {
            LOG.info(e.getMessage());
        }

    }

    return Response.ok("sent").withMimeType("text/event-stream; charset=UTF-8").withHeader("Cache-Control",
            "no-cache");
}