Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:ch.agent.crnickl.mongodb.AccessMethodsForNumber.java

License:Apache License

private long extractValues(DBObject obj, Range range, TimeAddressable<Double> ts) throws Exception {
    if (range != null && range.isEmpty())
        return 0;
    long count = 0;
    @SuppressWarnings("unchecked")
    Map<String, Double> values = (Map<String, Double>) obj.get(MongoDatabase.FLD_SER_VALUES);
    for (String k : values.keySet()) {
        long t = Long.valueOf(k);
        if (range == null || range.isInRange(t)) {
            ts.put(t, values.get(k));//w w  w  .j av a 2 s .c o m
            count++;
        }
    }
    return count;
}

From source file:ch.bfh.uniboard.persistence.mongodb.PersistedPost.java

License:GNU General Public License

/**
 * Method allowing to retrieve a PersistedPost out of the DBObject returned by the database. If the passed DBObject
 * does not represent a PersistedPost, an empty PersistedPost is retuned
 *
 * @param doc the DBObject returned by the database
 * @return the corresponding persisted post
 *///w  w  w.  ja v a  2  s.  c o  m
public static PersistedPost fromDBObject(DBObject doc) {
    PersistedPost pp = new PersistedPost();

    //TODO remove try catch when DB will be cleaned
    //this is only needed since some messages in MongoDB are not byte array
    //but string (historical reasons
    try {
        pp.message = Base64.decode((String) doc.get("message"));
    } catch (ClassCastException e) {
        pp.message = JSON.serialize(doc.get("message")).getBytes();
    }

    //fill alpha attributes
    Attributes alpha = new Attributes();
    DBObject alphaList = (DBObject) doc.get("alpha");
    for (String key : alphaList.keySet()) {
        //String key = dbObj.keySet().iterator().next();
        alpha.add(key, inflateType(alphaList.get(key)));
    }
    pp.alpha = alpha;

    //fill beta attributes
    Attributes beta = new Attributes();
    DBObject betaList = (DBObject) doc.get("beta");
    for (String key : betaList.keySet()) {
        //String key = dbObj.keySet().iterator().next();
        beta.add(key, inflateType(betaList.get(key)));
    }
    pp.beta = beta;

    return pp;
}

From source file:ch.windmobile.server.social.mongodb.AuthenticationServiceImpl.java

License:Open Source License

@Override
public String authenticate(final String email, final Object password) throws AuthenticationServiceException {
    if (password == null) {
        throw new IllegalArgumentException("Password cannot be null");
    }/*from   w ww.  j ava2  s. com*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject user = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (user != null) {
        String b64 = (String) user.get(MongoDBConstants.USER_PROP_SHA1);
        try {
            boolean ok = AuthenticationServiceUtil.validateSHA1(email, password.toString(), b64);
            if (ok) {
                return (String) user.get(MongoDBConstants.USER_PROP_ROLE);
            } else {
                throw new AuthenticationService.AuthenticationServiceException("Invalid password");
            }
        } catch (NoSuchAlgorithmException e) {
            throw new AuthenticationService.AuthenticationServiceException(
                    "Unexcepted error : " + e.getMessage());
        }
    }
    throw new AuthenticationService.AuthenticationServiceException("User not found");
}

From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java

License:Open Source License

@Override
public Messages findMessages(String chatRoomId, int maxCount) {
    if (maxCount <= 0) {
        throw new IllegalArgumentException("maxCount arg[1] must be greater than 0");
    }//from   w  ww . j  a  v a2  s .c  o m
    final String collectionName = computeCollectionName(chatRoomId);

    final DBObject fields = BasicDBObjectBuilder.start("_id", 1).add(MongoDBConstants.CHAT_PROP_USER, 1)
            .add(MongoDBConstants.CHAT_PROP_TEXT, 1).add(MongoDBConstants.CHAT_PROP_TIME, 1)
            .add(MongoDBConstants.CHAT_PROP_EMAIL_HASH, 1).get();
    DBCursor result = db.getCollection(collectionName).find(null, fields)
            .sort(new BasicDBObject("$natural", -1)).limit(maxCount);
    List<DBObject> all = result.toArray();

    Messages messages = new Messages();
    for (DBObject dbObject : all) {
        Message message = new Message();
        message.setId(dbObject.get("_id").toString());
        DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime((String) dbObject.get("time"));
        message.setDate(dateTime);
        message.setPseudo((String) dbObject.get(MongoDBConstants.CHAT_PROP_USER));
        message.setText((String) dbObject.get(MongoDBConstants.CHAT_PROP_TEXT));
        message.setEmailHash((String) dbObject.get(MongoDBConstants.CHAT_PROP_EMAIL_HASH));

        messages.getMessages().add(message);
    }
    return messages;
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

private User createUser(DBObject dbObject) {
    User user = new User();
    user.setEmail((String) dbObject.get(MongoDBConstants.USER_PROP_EMAIL));
    user.setPseudo((String) dbObject.get(MongoDBConstants.USER_PROP_PSEUDO));
    user.setFullName((String) dbObject.get(MongoDBConstants.USER_PROP_FULLNAME));
    user.setRole((String) dbObject.get(MongoDBConstants.USER_PROP_ROLE));
    return user;/*  w  w  w  .j a v a 2 s . c o m*/
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> getFavorites(String email) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }/*from ww w  .  ja v a  2  s .c o m*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();

        userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
        col.save(userDb);
    }

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemsDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId(
                (Long) favoriteItemsDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> addToFavorites(String email, List<Favorite> localFavorites) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }/*  w  ww  . j a v a  2  s. c  o m*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();
    }

    if (localFavorites != null) {
        for (Favorite localFavorite : localFavorites) {
            DBObject favoriteItemsDb = (DBObject) favoritesDb.get(localFavorite.getStationId());
            long lastMessageIdDb = -1;
            if (favoriteItemsDb == null) {
                favoriteItemsDb = new BasicDBObject();
            } else {
                if (favoriteItemsDb.containsField(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID)) {
                    lastMessageIdDb = (Long) favoriteItemsDb
                            .get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID);
                }
            }

            favoriteItemsDb.put(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID,
                    Math.max(lastMessageIdDb, localFavorite.getLastMessageId()));
            favoritesDb.put(localFavorite.getStationId(), favoriteItemsDb);
        }
    }
    userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
    col.save(userDb);

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId((Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> removeFromFavorites(String email, List<Favorite> favoritesToRemove) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }//  w  w  w.  ja v  a  2 s .  c  o  m
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();
    }

    if (favoritesToRemove != null) {
        for (Favorite favoriteToRemove : favoritesToRemove) {
            DBObject favoriteItemsDb = (DBObject) favoritesDb.get(favoriteToRemove.getStationId());
            if (favoriteItemsDb != null) {
                favoritesDb.removeField(favoriteToRemove.getStationId());
            }
        }
    }
    userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
    col.save(userDb);

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId((Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}

From source file:clustering.ClusteringArtists.java

public static void main(String[] args) throws UnknownHostException {

    ArrayList<Artist> artArr = new ArrayList<Artist>();

    DBHelper dbHelper = DBHelper.getInstance();
    DBCursor artists = dbHelper.findAllArtistsWithFB();
    while (artists.hasNext()) {
        DBObject currentArtist = artists.next();
        artArr.add(/* ww w . j a  va  2  s  . com*/
                new Artist((ObjectId) currentArtist.get("_id"), (Integer) currentArtist.get("facebook_likes")));
    }

    Collections.sort(artArr);

    parse(artArr, 1);
    merge_clusters(5);
    print_clusters();

}

From source file:clustering.ClusteringArtistsTW.java

public static void main(String[] args) throws UnknownHostException {

    new Integer(5).doubleValue();

    ArrayList<ArtistTW> artArr = new ArrayList<ArtistTW>();

    DBHelper dbHelper = DBHelper.getInstance();
    DBCursor artists = dbHelper.findAllArtistsWithTW();
    while (artists.hasNext()) {
        DBObject currentArtist = artists.next();
        //System.out.println(currentArtist);            
        String twf = currentArtist.get("twitter_followers").toString();
        StringTokenizer st = new StringTokenizer(twf, ".");
        int twfint = Integer.parseInt(st.nextToken());
        System.out.println(twfint);
        ArtistTW artist = new ArtistTW((ObjectId) currentArtist.get("_id"), twfint);
        artArr.add(artist);//from   w w  w  .j a  v a 2s  .  co  m
    }

    Collections.sort(artArr);

    parse(artArr, 1);
    merge_clusters(6);
    print_clusters();

}