Example usage for com.mongodb DBObject toMap

List of usage examples for com.mongodb DBObject toMap

Introduction

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

Prototype

Map toMap();

Source Link

Document

Returns a map representing this BSONObject.

Usage

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

/**
 * <p>/*from w ww .jav  a2s. co  m*/
 * Translates the MongoDB identifier to Mahout/MongoDBDataModel's internal
 * identifier, if required.
 * </p>
 * <p>
 * If MongoDB identifiers are long datatypes, it returns the id.
 * </p>
 * <p>
 * This conversion is needed since Mahout uses the long datatype to feed the
 * recommender, and MongoDB uses 12 bytes to create its identifiers.
 * </p>
 *
 * @param id     MongoDB identifier
 * @param isUser
 * @return String containing the translation of the external MongoDB ID to
 *         internal long ID (mapping).
 * @see #fromLongToId(long)
 * @see <a href="http://www.mongodb.org/display/DOCS/Object%20IDs">
 *      Mongo Object IDs</a>
 */
public String fromIdToLong(String id, boolean isUser) {
    DBObject objectIdLong = collectionMap.findOne(new BasicDBObject("element_id", id));
    if (objectIdLong != null) {
        Map<String, Object> idLong = (Map<String, Object>) objectIdLong.toMap();
        Object value = idLong.get("long_value");
        return value == null ? null : value.toString();
    } else {
        objectIdLong = new BasicDBObject();
        String longValue = Long.toString(idCounter++);
        objectIdLong.put("element_id", id);
        objectIdLong.put("long_value", longValue);
        collectionMap.insert(objectIdLong);
        log.info("Adding Translation {}: {} long_value: {}", isUser ? "User ID" : "Item ID", id, longValue);
        return longValue;
    }
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

/**
 * <p>/*w  w w. j av  a2s  .c om*/
 * Translates the Mahout/MongoDBDataModel's internal identifier to MongoDB
 * identifier, if required.
 * </p>
 * <p>
 * If MongoDB identifiers are long datatypes, it returns the id in String
 * format.
 * </p>
 * <p>
 * This conversion is needed since Mahout uses the long datatype to feed the
 * recommender, and MongoDB uses 12 bytes to create its identifiers.
 * </p>
 *
 * @param id Mahout's internal identifier
 * @return String containing the translation of the internal long ID to
 *         external MongoDB ID (mapping).
 * @see #fromIdToLong(String, boolean)
 * @see <a href="http://www.mongodb.org/display/DOCS/Object%20IDs">
 *      Mongo Object IDs</a>
 */
public String fromLongToId(long id) {
    DBObject objectIdLong = collectionMap.findOne(new BasicDBObject("long_value", Long.toString(id)));
    Map<String, Object> idLong = (Map<String, Object>) objectIdLong.toMap();
    Object value = idLong.get("element_id");
    return value == null ? null : value.toString();
}

From source file:bank_server.Task2.java

public void iterateOverCursor(DBCursor dbCursor, Push push) {
    for (DBObject o : dbCursor) {
        //System.out.println(o);
        HashMap map = (HashMap) o.toMap();
        push.getIds().add(map.get("_id").toString().replace(".0", ""));
        push.getMontants().add(map.get("montant").toString().replace(".0", ""));
        push.getTypes().add(map.get("type").toString());
        String str = map.get("valide").toString();
        if (str.equals("true"))
            push.getValides().add(Boolean.TRUE);
        else//from  ww  w .  java  2 s.  c  o  m
            push.getValides().add(Boolean.FALSE);
        String tab1[] = map.get("banque").toString().split("\"name\" : \"");
        String tab2[] = tab1[1].split("\"");
        push.getBanques().add(tab2[0]);
    }
}

From source file:br.rdu.ifpb.mongo.repositorio.PessoaRepositoriMongo.java

@Override
public Pessoa find(String key, String value) {
    Pessoa pessoa = new Pessoa();
    DB db = getConnection();//from   www  .j  a v a 2s  .c om
    DBCollection table = db.getCollection("pessoa");
    BasicDBObject query = new BasicDBObject().append(key, value);

    DBCursor result = table.find(query);

    result.hasNext();

    DBObject DBO = result.next();
    pessoa.setId(DBO.toMap().get("_id").toString());
    pessoa.setNome(DBO.toMap().get("nome").toString());
    pessoa.setCpf(DBO.toMap().get("cpf").toString());
    int idade = Integer.parseInt(DBO.toMap().get("idade").toString());
    pessoa.setIdade(idade);

    return pessoa;
}

From source file:br.ufabc.impress.mongo.manager.DBHelper.java

@Override
public List<Boolean> modifyNotOveride(String tableName, String json, String _id) {
    if (tableName == null || tableName.equals("") || json == null || json.equals("") || _id == null
            || _id.equals("")) {
        return null;
    }//ww w. j ava2s. c  om
    List<Boolean> resList = new ArrayList();
    DBCollection table = db.getCollection(tableName);
    DBCursor cursor = table.find();
    while (cursor.hasNext()) {
        DBObject updateDocument = cursor.next();
        DBObject searchQuery = cursor.next();
        DBObject dbObject = (DBObject) JSON.parse(json);
        updateDocument.putAll(dbObject.toMap());
        updateDocument.removeField("_id");
        WriteResult result = table.update(searchQuery, updateDocument);
        resList.add(result.isUpdateOfExisting());
    }
    return resList;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from  w w  w .j  av  a  2 s . c o m*/
public AuthCode findAuthCode(String authCode, String redirectUri) {
    BasicDBObject keys = new BasicDBObject();
    keys.put(AUTH_CODE_ID_NAME, authCode);
    keys.put(REDIRECT_URI_NAME, redirectUri);
    keys.put(VALID_NAME, true);
    DBCursor list = db.getCollection(AUTH_CODE_COLLECTION_NAME).find(new BasicDBObject(keys));
    while (list.hasNext()) {
        DBObject result = list.next();
        Map<String, Object> mapLoaded = result.toMap();
        AuthCode loadedAuthCode = AuthCode.loadFromMap(mapLoaded);
        log.debug(loadedAuthCode.getClientId());
        list.close();
        return loadedAuthCode;
    }
    list.close();
    return null;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<Scope> getAllScopes() {
    List<Scope> list = new ArrayList<Scope>();
    DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
    List<DBObject> result = coll.find().toArray();
    for (DBObject obj : result) {
        Map<String, Object> mapLoaded = obj.toMap();
        Scope scope = Scope.loadFromMap(mapLoaded);
        list.add(scope);//www  .  ja  v  a 2  s .com
    }
    return list;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<AccessToken> getAccessTokenByUserIdAndClientApp(String userId, String clientId) {
    List<AccessToken> accessTokens = new ArrayList<AccessToken>();
    BasicDBObject dbObject = new BasicDBObject();
    // TODO: add indexes
    dbObject.put(USER_ID, userId);//from   w  ww  .ja  va  2s.  c  om
    dbObject.put(CLIENTS_ID_NAME, clientId);
    dbObject.put(VALID_NAME, true);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    for (DBObject object : list) {
        Map<String, Object> mapLoaded = object.toMap();
        // convert details list to String
        if (mapLoaded.get("details") instanceof BasicDBObject) {
            BasicDBObject details = (BasicDBObject) mapLoaded.get("details");
            mapLoaded.put("details", details.toString());
        }
        AccessToken loadedAccessToken = AccessToken.loadFromMap(mapLoaded);
        accessTokens.add(loadedAccessToken);
    }
    return accessTokens;
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*  w w  w. j av  a  2  s  .c om*/
public AuthCode findAuthCode(String authCode, String redirectUri) {
    BasicDBObject keys = new BasicDBObject();
    keys.put(AUTH_CODE, authCode);
    keys.put(ACCESS_TOKEN_REDIRECT_URI, redirectUri);
    keys.put(ACCESS_TOKEN_VALID, true);
    DBCursor list = db.getCollection(AUTH_CODE_COLLECTION_NAME).find(new BasicDBObject(keys));
    if (list.hasNext()) {
        DBObject result = list.next();
        Map<String, Object> mapLoaded = result.toMap();
        AuthCode loadedAuthCode = AuthCode.loadFromMap(mapLoaded);
        log.debug(loadedAuthCode.getClientId());
        list.close();
        return loadedAuthCode;
    }
    list.close();
    return null;
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<ClientCredentials> getAllApplications() {
    List<ClientCredentials> list = new ArrayList<ClientCredentials>();
    DBCollection coll = db.getCollection(CLIENTS_COLLECTION_NAME);
    List<DBObject> result = coll.find().toArray();
    for (DBObject obj : result) {
        Map<String, Object> mapLoaded = obj.toMap();
        ClientCredentials loadedCreds = ClientCredentials.loadFromMap(mapLoaded);
        list.add(loadedCreds);//w ww. j  a  v a2 s .  c o m
    }
    return list;
}