Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

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

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:MDBInt.DBMongo.java

License:Apache License

public String getDatacenter(String tenant, String idCloud) throws MDBIException {
    DB database = this.getDB(tenant);
    DBCollection collection = database.getCollection("datacenters");

    BasicDBObject first = new BasicDBObject();
    first.put("cloudId", idCloud);

    DBObject obj = null;//ww w  .j av a  2 s . c o m
    try {
        obj = collection.findOne(first);
    } catch (Exception e) {
        throw new MDBIException(e.getMessage());
    }
    return obj.toString();

}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * It returns OSFFM token assigned to federationUser.
 * @param tenant/*ww  w.j a  va 2  s.com*/
 * @param user, this is federation UserName
 * @return
 * @throws MDBIException
 * @author gtricomi
 */
public String getFederationToken(String tenant, String user) throws MDBIException {
    DB database = this.getDB(tenant);
    DBCollection collection = database.getCollection("credentials");

    BasicDBObject first = new BasicDBObject();
    first.put("federationUser", user);

    DBObject obj = null;
    try {
        obj = collection.findOne(first);
    } catch (Exception e) {
        throw new MDBIException(e.getMessage());
    }
    return (String) obj.get("token");

}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * Function used to retrieve cloudId from cmp_endpoint
 *
 * @param federationUser//from www.j ava  2 s .c o m
 * @param cmp_endpoint
 * @return
 * @author gtricomi
 */
public String getDatacenterIDfrom_cmpEndpoint(String federationUser, String cmp_endpoint) throws MDBIException {
    DB database = this.getDB(federationUser);
    DBCollection collection = database.getCollection("datacenters");

    BasicDBObject first = new BasicDBObject();
    first.put("idmEndpoint", cmp_endpoint);

    DBObject obj = null;
    try {
        obj = collection.findOne(first);
    } catch (Exception e) {
        throw new MDBIException(
                "An Exception is generated by OSFFM DB connector, when getDatacenterIDfrom_cmpEndpoint is launched with [federationUser:\" "
                        + federationUser + "\",cmp_endpoint: " + cmp_endpoint + "\"]\n" + e.getMessage());
    }
    return ((String) obj.get("cloudId"));

}

From source file:MDBInt.DBMongo.java

License:Apache License

public ArrayList<String> findResourceMate(String dbName, String uuid) {

    BasicDBObject first = new BasicDBObject();
    first.put("phisicalResourceId", uuid);

    DB database = this.getDB(dbName);
    DBCollection collection = database.getCollection("runTimeInfo");
    DBObject obj = null;//  w  w  w.j  a  v a  2 s  . c  o m
    BasicDBObject query = new BasicDBObject();
    DBCursor cursore = null;
    ArrayList<String> mates = new ArrayList();
    Iterator<DBObject> it;

    obj = collection.findOne(first);

    if (obj != null) {
        query.put("localResourceName", obj.get("localResourceName"));
        query.put("stackName", obj.get("stackName"));
        query.put("resourceName", obj.get("resourceName"));
        query.put("type", obj.get("type"));
        query.put("state", false);
        query.put("idCloud", new BasicDBObject("$ne", obj.get("idCloud")));

        System.out.println(query);
        cursore = collection.find(query);

    }
    if (cursore != null) {
        it = cursore.iterator();
        while (it.hasNext()) {
            mates.add(it.next().toString());

        }
    }
    return mates;

}

From source file:MDBInt.DBMongo.java

License:Apache License

public boolean testLink(String tenantName, String Cloud1, String Cloud2) {

    boolean attivo = false;
    BasicDBList or;/* www.ja  v a2  s .  co m*/
    BasicDBObject query;
    DB database = this.getDB(tenantName);
    DBCollection collection = database.getCollection("link");
    DBObject result;
    try {
        or = new BasicDBList();
        or.add(new BasicDBObject("_id", Cloud1 + "_" + Cloud2));
        or.add(new BasicDBObject("_id", Cloud2 + "_" + Cloud1));
        query = new BasicDBObject("$or", or);
        result = collection.findOne(query);
        attivo = (boolean) result.get("status");
    } catch (Exception ex) {

        return false;

    }

    return attivo;

}

From source file:MDBInt.DBMongo.java

License:Apache License

public String findResourceMate(String dbName, String uuid, String dcid) {

    BasicDBObject first = new BasicDBObject();
    first.put("phisicalResourceId", uuid);

    DB database = this.getDB(dbName);
    DBCollection collection = database.getCollection("runTimeInfo");
    DBObject obj = null;//w w  w . j a v  a2 s. c o  m
    BasicDBObject query = new BasicDBObject();
    DBCursor cursore = null;
    Iterator<DBObject> it;

    obj = collection.findOne(first);

    if (obj != null) {
        query.put("requestUID", obj.get("requestUID"));
        query.put("uuidTemplate", obj.get("uuidTemplate"));
        query.put("stackName", obj.get("stackName"));
        query.put("resourceName", obj.get("resourceName"));
        query.put("type", obj.get("type"));
        //query.put("state", false);
        query.put("idCloud", dcid);

        System.out.println("MONGO QUERY " + query + " in DB:" + dbName + " for UUID:" + uuid);
        DBCursor b = collection.find(query).sort(new BasicDBObject("insertTimestamp", -1)).limit(1);
        Iterator i = b.iterator();
        if (b != null && i.hasNext())
            return i.next().toString();
        else
            return null;
    }
    return null;
}

From source file:MDBInt.DBMongo.java

License:Apache License

public String findInfoVM(String dbName, String uuid) {

    BasicDBObject first = new BasicDBObject();
    first.put("phisicalResourceId", uuid);

    DB database = this.getDB(dbName);
    DBCollection collection = database.getCollection("runTimeInfo");
    DBObject obj = null;//from  w w  w  .  j a  v a  2  s  . c  om
    BasicDBObject query = new BasicDBObject();
    // DBCursor cursore = null;

    obj = collection.findOne(first);

    if (obj != null) {
        query.put("cloudId", obj.get("idCloud"));
        collection = database.getCollection("datacenters");
        //System.out.println("MONGO QUERY "+query+ " in DB:"+dbName+" for UUID:"+uuid );
        DBObject b = collection.findOne(query);
        if (b != null) {
            obj.put("idmEndpoint", b.get("idmEndpoint"));
            return obj.toString();
        } else
            return null;
    }
    /*if (cursore != null) {
    it = cursore.iterator();
    while (it.hasNext()) {
        mates.add(it.next().toString());
            
    }
    }
    return mates;*/
    return null;
}

From source file:MDBInt.DBMongo.java

License:Apache License

public ArrayList<String> findALLResourceMate(String dbName, String uuid) {
    ArrayList<String> finalOb = new ArrayList<String>();
    BasicDBObject first = new BasicDBObject();
    first.put("phisicalResourceId", uuid);

    DB database = this.getDB(dbName);
    DBCollection collection = database.getCollection("runTimeInfo");
    DBObject obj = null;//from   ww  w. j  a  v  a 2 s  . c om
    BasicDBObject query = new BasicDBObject();
    DBCursor cursore = null;
    Iterator<DBObject> it;

    obj = collection.findOne(first);

    if (obj != null) {
        query.put("requestUID", obj.get("requestUID"));
        query.put("uuidTemplate", obj.get("uuidTemplate"));
        query.put("localResourceName", obj.get("localResourceName"));
        query.put("stackName", obj.get("stackName"));
        query.put("resourceName", obj.get("resourceName"));
        query.put("type", obj.get("type"));
        query.put("state", false);

        System.out.println("MONGO QUERY " + query + " in DB:" + dbName + " for UUID:" + uuid);
        DBCursor b = collection.find(query).sort(new BasicDBObject("insertTimestamp", -1));
        Iterator i = b.iterator();
        if (i.hasNext())
            while (i.hasNext()) {
                DBObject tmp = (DBObject) i.next();
                tmp.removeField("_id");
                tmp.removeField("insertTimestamp");
                finalOb.add(tmp.toString());
            }
        else
            return null;
    }
    return finalOb;
}

From source file:MDBInt.DBMongo.java

License:Apache License

public boolean updateStateRunTimeInfo(String dbName, String phisicalResourceId, boolean newState) {
    boolean result = true;
    BasicDBObject first = new BasicDBObject();
    first.put("phisicalResourceId", phisicalResourceId);

    DB database = this.getDB(dbName);
    DBCollection collection = database.getCollection("runTimeInfo");
    BasicDBObject obj = null;//from   w w w  .j  a  v  a 2s .c  om

    obj = (BasicDBObject) collection.findOne(first);
    obj.append("state", newState);
    collection.save(obj);
    return result;
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * Returns a specific template istance./*from ww w .ja  v  a  2s  .  c  o m*/
 *
 * @param dbName
 * @return
 * @author gtricomi
 */
public String getTemplate(String dbName, String uuidManifest) {

    DB dataBase;
    DBCollection collezione;
    dataBase = this.getDB(dbName);
    collezione = dataBase.getCollection("templateInfo");

    BasicDBObject first = new BasicDBObject();
    first.put("id", uuidManifest);
    DBObject j = collezione.findOne(first);
    return j.toString();
}