Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

In this page you can find the example usage for com.mongodb DBCollection insert.

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

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

License:Open Source License

public String getTeamRoom(String team, String user) {
    String room = ChatUtils.getRoomId(team, user);
    DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION);

    BasicDBObject basicDBObject = new BasicDBObject();
    basicDBObject.put("_id", room);

    DBCursor cursor = coll.find(basicDBObject);
    if (!cursor.hasNext()) {
        try {//  ww  w  . j  av  a2s.  c o m
            basicDBObject.put("team", team);
            basicDBObject.put("user", user);
            basicDBObject.put("type", TYPE_ROOM_TEAM);
            coll.insert(basicDBObject);
            ensureIndexInRoom(room);
        } catch (MongoException me) {
            log.warning(me.getCode() + " : " + room + " : " + me.getMessage());
        }
    }

    return room;
}

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

License:Open Source License

public String getExternalRoom(String identifier) {
    String room = ChatUtils.getExternalRoomId(identifier);
    DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION);

    BasicDBObject basicDBObject = new BasicDBObject();
    basicDBObject.put("_id", room);

    DBCursor cursor = coll.find(basicDBObject);
    if (!cursor.hasNext()) {
        try {/* ww  w . j a v  a 2  s . co  m*/
            basicDBObject.put("identifier", identifier);
            basicDBObject.put("type", TYPE_ROOM_EXTERNAL);
            coll.insert(basicDBObject);
            ensureIndexInRoom(room);
        } catch (MongoException me) {
            log.warning(me.getCode() + " : " + room + " : " + me.getMessage());
        }
    }

    return room;
}

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

License:Open Source License

public String getRoom(List<String> users) {
    Collections.sort(users);/*  www  . ja v a 2  s .  c  o m*/
    String room = ChatUtils.getRoomId(users);
    DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION);

    BasicDBObject basicDBObject = new BasicDBObject();
    basicDBObject.put("_id", room);

    DBCursor cursor = coll.find(basicDBObject);
    if (!cursor.hasNext()) {
        try {
            basicDBObject.put("users", users);
            basicDBObject.put("type", TYPE_ROOM_USER);
            coll.insert(basicDBObject);
            ensureIndexInRoom(room);
        } catch (MongoException me) {
            log.warning(me.getCode() + " : " + room + " : " + me.getMessage());
        }
    }

    return room;
}

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

License:Open Source License

public void addNotification(String user, String from, String type, String category, String categoryId,
        String content, String link) {
    DBCollection coll = db().getCollection(M_NOTIFICATIONS);
    BasicDBObject doc = new BasicDBObject();
    doc.put("timestamp", System.currentTimeMillis());
    doc.put("user", user);
    doc.put("from", from);
    doc.put("type", type);
    doc.put("category", category);
    doc.put("categoryId", categoryId);
    doc.put("content", content);
    doc.put("link", link);
    doc.put("isRead", false);

    coll.insert(doc);
}

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

License:Open Source License

public void addUser(String user, String token) {
    if (!hasUserWithToken(user, token)) {
        //System.out.println("TOKEN SERVICE :: ADDING :: " + user + " : " + token);
        //      removeUser(user);
        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();
            doc.put("token", token);
            doc.put("validity", System.currentTimeMillis());
            doc.put("isDemoUser", user.startsWith(ANONIM_USER));
            coll.save(doc, WriteConcern.SAFE);
        } else {/*from   w w  w .j  ava 2s  .c  o m*/
            BasicDBObject doc = new BasicDBObject();
            doc.put("_id", user);
            doc.put("user", user);
            doc.put("token", token);
            doc.put("validity", System.currentTimeMillis());
            doc.put("isDemoUser", user.startsWith(ANONIM_USER));
            coll.insert(doc);
        }
    }
}

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

License:Open Source License

public void addUserFullName(String user, String fullname) {
    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 va2 s  .  c  o  m
    if (!cursor.hasNext()) {
        BasicDBObject doc = new BasicDBObject();
        doc.put("_id", user);
        doc.put("user", user);
        doc.put("fullname", fullname);
        coll.insert(doc);
    } else {
        DBObject doc = cursor.next();
        doc.put("fullname", fullname);
        coll.save(doc);

    }
}

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

License:Open Source License

public void addUserEmail(String user, String email) {
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    DBCursor cursor = coll.find(query);/*from w  ww .j  av  a2s . c  om*/
    if (!cursor.hasNext()) {
        BasicDBObject doc = new BasicDBObject();
        doc.put("_id", user);
        doc.put("user", user);
        doc.put("email", email);
        coll.insert(doc);
    } else {
        DBObject doc = cursor.next();
        doc.put("email", email);
        coll.save(doc);

    }
}

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

License:Open Source License

public void setSpaces(String user, List<SpaceBean> spaces) {
    List<String> spaceIds = new ArrayList<String>();
    DBCollection coll = db().getCollection(M_ROOMS_COLLECTION);
    for (SpaceBean bean : spaces) {
        String room = ChatUtils.getRoomId(bean.getId());
        spaceIds.add(room);//from  w  ww  .  j a  v  a  2 s .com

        BasicDBObject query = new BasicDBObject();
        query.put("_id", room);
        DBCursor cursor = coll.find(query);
        if (!cursor.hasNext()) {
            BasicDBObject doc = new BasicDBObject();
            doc.put("_id", room);
            doc.put("space_id", bean.getId());
            doc.put("displayName", bean.getDisplayName());
            doc.put("groupId", bean.getGroupId());
            doc.put("shortName", bean.getShortName());
            doc.put("type", ChatService.TYPE_ROOM_SPACE);
            coll.insert(doc);
        } else {
            DBObject doc = cursor.next();
            String displayName = doc.get("displayName").toString();
            if (!bean.getDisplayName().equals(displayName)) {
                doc.put("_id", room);
                doc.put("displayName", bean.getDisplayName());
                doc.put("groupId", bean.getGroupId());
                doc.put("shortName", bean.getShortName());
                coll.save(doc);
            }
        }

    }
    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();
        doc.put("spaces", spaceIds);
        coll.save(doc, WriteConcern.SAFE);
    } else {
        BasicDBObject doc = new BasicDBObject();
        doc.put("_id", user);
        doc.put("user", user);
        doc.put("spaces", spaceIds);
        coll.insert(doc);
    }
}

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  .  ja  va  2 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 String setStatus(String user, String status) {
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    DBCursor cursor = coll.find(query);//w ww  .j av a  2  s  .c  o m
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        doc.put("status", status);
        coll.save(doc, WriteConcern.SAFE);
    } else {
        BasicDBObject doc = new BasicDBObject();
        doc.put("_id", user);
        doc.put("user", user);
        doc.put("status", status);
        coll.insert(doc);
    }
    return status;
}