Example usage for com.mongodb DBCollection save

List of usage examples for com.mongodb DBCollection save

Introduction

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

Prototype

public WriteResult save(final DBObject document, final WriteConcern writeConcern) 

Source Link

Document

Update an existing document or insert a document depending on the parameter.

Usage

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 a  v a2 s . c om
            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.TokenServiceImpl.java

License:Open Source License

public void updateValidity(String user, String token) {
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    query.put("token", token);
    DBCursor cursor = coll.find(query);//from w w w  .j  a  va2s  . co m
    while (cursor.hasNext()) {
        DBObject doc = cursor.next();
        doc.put("validity", System.currentTimeMillis());
        coll.save(doc, WriteConcern.SAFE);
    }
}

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

License:Open Source License

public void toggleFavorite(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  ww  .jav  a2  s .  com*/
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        List<String> favorites = new ArrayList<String>();
        if (doc.containsField("favorites")) {
            favorites = (List<String>) doc.get("favorites");
        }
        if (favorites.contains(targetUser))
            favorites.remove(targetUser);
        else
            favorites.add(targetUser);

        doc.put("favorites", favorites);
        coll.save(doc, WriteConcern.SAFE);
    }
}

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);//from  w  w  w. j  a v a2 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("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);
                }//ww w .  j a  v  a 2  s . c  o m
            }
        }

    }
}

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);//from w  ww  . j av  a  2  s . co  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;
}

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

License:Open Source License

public void setAsAdmin(String user, boolean isAdmin) {
    DBCollection coll = db().getCollection(M_USERS_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    DBCursor cursor = coll.find(query);/*w ww  .ja  v  a 2 s . com*/
    if (cursor.hasNext()) {
        DBObject doc = cursor.next();
        doc.put("isSupportAdmin", isAdmin);
        coll.save(doc, WriteConcern.SAFE);
    } else {
        BasicDBObject doc = new BasicDBObject();
        doc.put("_id", user);
        doc.put("user", user);
        doc.put("isSupportAdmin", isAdmin);
        coll.insert(doc);
    }
}

From source file:org.exoplatform.chat.services.mongodb.ChatServiceImpl.java

License:Open Source License

public void delete(String room, String user, String messageId) {
    DBCollection coll = db().getCollection(M_ROOM_PREFIX + room);
    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(messageId));
    query.put("user", user);
    DBCursor cursor = coll.find(query);// ww  w .j a  va2 s. c  o m
    if (cursor.hasNext()) {
        DBObject dbo = cursor.next();
        dbo.put("message", TYPE_DELETED);
        dbo.put("type", TYPE_DELETED);
        dbo.put("lastUpdatedTimestamp", System.currentTimeMillis());
        coll.save(dbo, WriteConcern.NONE);
    }
}

From source file:org.exoplatform.chat.services.mongodb.ChatServiceImpl.java

License:Open Source License

public void edit(String room, String user, String messageId, String message) {
    DBCollection coll = db().getCollection(M_ROOM_PREFIX + room);

    message = StringUtils.chomp(message);
    message = message.replaceAll("&", "&#38");
    message = message.replaceAll("<", "&lt;");
    message = message.replaceAll(">", "&gt;");
    message = message.replaceAll("\"", "&quot;");
    message = message.replaceAll("\n", "<br/>");
    message = message.replaceAll("\\\\", "&#92");

    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(messageId));
    query.put("user", user);
    DBCursor cursor = coll.find(query);/*from w w w. j a v a2  s .  c o m*/
    if (cursor.hasNext()) {
        DBObject dbo = cursor.next();
        dbo.put("message", message);
        dbo.put("type", TYPE_EDITED);
        dbo.put("lastUpdatedTimestamp", System.currentTimeMillis());
        coll.save(dbo, WriteConcern.NONE);
    }
}

From source file:org.exoplatform.chat.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 ww  .ja v a 2s .c om
            }
        }

    }
}