Example usage for com.mongodb DBObject containsField

List of usage examples for com.mongodb DBObject containsField

Introduction

In this page you can find the example usage for com.mongodb DBObject containsField.

Prototype

boolean containsField(String s);

Source Link

Document

Checks if this object contains a field with the given name.

Usage

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

public boolean isFavorite(String user, String targetUser) {
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    DBCursor cursor = coll.find(query);//from w w  w.  ja v  a  2  s.c  o m
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        if (doc.containsField("favorites")) {
            List<String> favorites = (List<String>) doc.get("favorites");
            if (favorites.contains(targetUser))
                return true;
        }
    }
    return false;
}

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

public void addTeamRoom(String user, String teamRoomId) {
    List<String> teamIds = new ArrayList<String>();
    teamIds.add(teamRoomId);/*  w w  w .  j a v  a2 s  . co m*/
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    DBCursor cursor = coll.find(query);
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        if (doc.containsField("teams")) {
            List<String> existingTeams = ((List<String>) doc.get("teams"));
            if (!existingTeams.contains(teamRoomId))
                existingTeams.add(teamRoomId);
            doc.put("teams", existingTeams);
        } else {
            doc.put("teams", teamIds);
        }
        coll.save(doc, WriteConcern.SAFE);
    } else {
        BasicDBObject doc = new BasicDBObject();
        doc.put("_id", user);
        doc.put("user", user);
        doc.put("teams", teamIds);
        coll.insert(doc);
    }
}

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

public void removeTeamUsers(String teamRoomId, List<String> users) {
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    for (String user : users) {
        log.info("Team Remove : " + user);
        BasicDBObject query = new BasicDBObject();
        query.put("user", user);
        DBCursor cursor = coll.find(query);
        if (cursor.hasNext()) {
            DBObject doc = cursor.next();
            if (doc.containsField("teams")) {
                List<String> teams = (List<String>) doc.get("teams");
                if (teams.contains(teamRoomId)) {
                    teams.remove(teamRoomId);
                    doc.put("teams", teams);
                    coll.save(doc, WriteConcern.SAFE);
                }/*from w w  w.j a  va  2  s  . c  o m*/
            }
        }

    }
}

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

private RoomBean getTeam(String teamId) {
    RoomBean roomBean = null;/*from  ww  w  .j  av a2 s .  co  m*/
    DBCollection coll = db().getCollection(M_ROOMS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("_id", teamId);
    DBCursor cursor = coll.find(query);
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        roomBean = new RoomBean();
        roomBean.setRoom(teamId);
        roomBean.setUser(doc.get("user").toString());
        roomBean.setFullname(doc.get("team").toString());
        if (doc.containsField("timestamp")) {
            roomBean.setTimestamp(((Long) doc.get("timestamp")).longValue());
        }
    }

    return roomBean;
}

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

public RoomBean getRoom(String user, String roomId) {
    RoomBean roomBean = new RoomBean();
    roomBean.setRoom(roomId);/*w  w w  . j  ava2s  .c om*/
    DBCollection coll = db().getCollection(M_ROOMS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("_id", roomId);
    DBCursor cursor = coll.find(query);
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        if (doc.containsField("timestamp")) {
            roomBean.setTimestamp(((Long) doc.get("timestamp")).longValue());
        }
        String type = doc.get("type").toString();
        if ("s".equals(type)) {
            roomBean.setUser(ChatService.SPACE_PREFIX + roomId);
            roomBean.setFullname(doc.get("displayName").toString());
            roomBean.setSpace(true);
        } else if ("t".equals(type)) {
            roomBean.setUser(ChatService.TEAM_PREFIX + roomId);
            roomBean.setFullname(doc.get("team").toString());
            roomBean.setTeam(true);
        } else if ("u".equals(type)) {
            List<String> users = ((List<String>) doc.get("users"));
            users.remove(user);
            String targetUser = users.get(0);
            roomBean.setUser(targetUser);
            roomBean.setFullname(this.getUserFullName(targetUser));
        } else if ("e".equals(type)) {
            roomBean.setUser(ChatService.EXTERNAL_PREFIX + roomId);
            roomBean.setFullname(doc.get("identifier").toString());
        }
    }

    return roomBean;
}

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

private SpaceBean getSpace(String roomId) {
    SpaceBean spaceBean = null;//  www  . j  a va2s .c  o  m
    DBCollection coll = db().getCollection(M_ROOMS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("_id", roomId);
    DBCursor cursor = coll.find(query);
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        spaceBean = new SpaceBean();
        spaceBean.setRoom(roomId);
        spaceBean.setId(doc.get("space_id").toString());
        spaceBean.setDisplayName(doc.get("displayName").toString());
        spaceBean.setGroupId(doc.get("groupId").toString());
        spaceBean.setShortName(doc.get("shortName").toString());
        if (doc.containsField("timestamp")) {
            spaceBean.setTimestamp(((Long) doc.get("timestamp")).longValue());
        }
    }

    return spaceBean;
}

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

public String getStatus(String user) {
    String status = STATUS_NONE;/*from  w ww . ja va 2 s. c  o  m*/
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    DBCursor cursor = coll.find(query);
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        if (doc.containsField("status"))
            status = doc.get("status").toString();
        else
            status = setStatus(user, STATUS_AVAILABLE);
    } else {
        status = setStatus(user, STATUS_AVAILABLE);
    }

    return status;
}

From source file:org.benjp.services.mongodb.UserServiceImpl.java

License:Open Source License

public UserBean getUser(String user, boolean withFavorites) {
    UserBean userBean = new UserBean();
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    DBCursor cursor = coll.find(query);/* w w  w  .jav  a 2s .c om*/
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        userBean.setName(user);
        if (doc.get("fullname") != null)
            userBean.setFullname(doc.get("fullname").toString());
        if (doc.get("email") != null)
            userBean.setEmail(doc.get("email").toString());
        if (doc.get("status") != null)
            userBean.setStatus(doc.get("status").toString());
        if (withFavorites) {
            if (doc.containsField("favorites")) {
                userBean.setFavorites((List<String>) doc.get("favorites"));
            }
        }
    }

    return userBean;
}

From source file:org.breizhbeans.thrift.tools.thriftmongobridge.protocol.TBSONUnstackedProtocol.java

License:Apache License

private Stack<ThriftFieldMetadata> getFieldsStack(Class<? extends TBase> tbase, DBObject dbObject)
        throws TException {
    // extract the fields (key from MongoDB)

    Stack<ThriftFieldMetadata> writeStack = new Stack<>();

    for (Map.Entry<String, Object> pair : ((BasicDBObject) dbObject).entrySet()) {
        //System.out.println("push field " + mongoKey + " of " + tbase.getSimpleName());
        TFieldIdEnum tfieldIdEnum = getFieldId(tbase, pair.getKey());
        if (tfieldIdEnum != null) {
            ThriftFieldMetadata thriftFieldMetadata = getTBaseFields(tbase)
                    .get(tfieldIdEnum.getThriftFieldId());
            writeStack.push(thriftFieldMetadata);
        }//ww w .j  av  a 2s.  co  m
    }

    // extract secure fields stack
    if (dbObject.containsField("securedwrap")) {
        for (String id : ((DBObject) dbObject.get("securedwrap")).keySet()) {
            ThriftFieldMetadata thriftFieldMetadata = getTBaseFields(tbase).get(Short.parseShort(id));
            if (!thriftFieldMetadata.securedFieldMetaData.isHash()) {
                writeStack.push(thriftFieldMetadata);
            }
        }
    }

    return writeStack;
}

From source file:org.broad.igv.plugin.mongocollab.MongoFeatureSource.java

License:Open Source License

/**
 * Check to see if we have an index useful for queries
 * @param buildIndex Whether to build locus index if not found
 *///from  w  w  w .ja  v a2s.  com
private void checkForLocusIndex(boolean buildIndex) {
    if (buildIndex) {
        ensureLocusIndex(collection);
    }
    //Check to see if we have the index we want
    List<DBObject> indexes = collection.getIndexInfo();
    DBObject neededFields = getLocusIndexKeys();
    for (DBObject index : indexes) {

        boolean isMatchingIndex = true;
        DBObject indexKey = (DBObject) index.get("key");
        for (String key : neededFields.keySet()) {
            boolean hasKey = indexKey.containsField(key);
            if (!hasKey) {
                isMatchingIndex = false;
                break;
            }
            Object value = indexKey.get(key);
            boolean equals = neededFields.get(key).equals(value);
            isMatchingIndex &= equals;
        }

        if (isMatchingIndex) {
            this.hasLocusIndex = true;
            break;
        }
    }
}