Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject() 

Source Link

Document

Creates an empty object.

Usage

From source file:backend.facades.UserController.java

public Long getUserIdByEmail(String email) {
    Long id = 0L;/*from  www. j av a2 s.  com*/
    BasicDBObject query = new BasicDBObject();
    query.put("email", email);
    DBCursor cursor = userCollection.find(query);
    try {
        if (cursor.count() > 0) {
            DBObject document = cursor.next();
            id = (Long) document.get("_id");
        }
    } finally {
        cursor.close();
    }
    return id;
}

From source file:backend.facades.UserController.java

public boolean checkEmail(String email) {
    boolean retValue = false;
    BasicDBObject query = new BasicDBObject();
    query.put("email", email);
    DBCursor cursor = userCollection.find(query);
    try {/*  w  w  w. j a v  a2s.co  m*/
        if (cursor == null) {
            return false;
        }
        if (cursor != null && cursor.count() > 0) {
            retValue = true;
        } else {
            return false;
        }
    } finally {
        cursor.close();
    }
    return retValue;
}

From source file:backend.facades.UserController.java

public boolean checkUsername(String username) {
    boolean retValue = false;
    BasicDBObject query = new BasicDBObject();
    query.put("username", username);
    DBCursor cursor = userCollection.find(query);

    try {/* w  w w  . j  a  va  2s  .com*/
        if (cursor != null && cursor.count() > 0) {
            retValue = true;
        }
    } finally {
        cursor.close();
    }
    return retValue;
}

From source file:backend.facades.UserController.java

protected static String getNextId(DB db, String seq_name) {
    String sequence_collection = "seq"; // the name of the sequence collection
    String sequence_field = "seq"; // the name of the field which holds the sequence

    DBCollection seq = db.getCollection(sequence_collection); // get the collection (this will create it if needed)               

    if (seq == null) {
        seq = db.createCollection(sequence_collection, null);
    }/* w  ww .j  a  v  a  2  s.  co  m*/

    // this object represents your "query", its analogous to a WHERE clause in SQL
    DBObject query = new BasicDBObject();
    query.put("_id", seq_name); // where _id = the input sequence name

    // this object represents the "update" or the SET blah=blah in SQL
    DBObject change = new BasicDBObject(sequence_field, 1);
    DBObject update = new BasicDBObject("$inc", change); // the $inc here is a mongodb command for increment

    // Atomically updates the sequence field and returns the value for you
    DBObject res = seq.findAndModify(query, new BasicDBObject(), new BasicDBObject(), false, update, true,
            true);
    return res.get(sequence_field).toString();
}

From source file:bank_server.Task2.java

@Override
public void run() {
    try {//from  ww  w .j  av a 2  s  .com
        SecretKey KS = new SecretKeySpec("cisco789".getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, KS);
        System.out.println("KS " + KS);

        oos = new ObjectOutputStream(cSock.getOutputStream());
        System.out.println(oos);
        ois = new ObjectInputStream(cSock.getInputStream());
        System.out.println(ois);
        if (ois == null || oos == null)
            System.exit(1);
        else
            System.out.println("Flux crs");

        Boolean fin = false;
        Pull pull = null;
        Push push = null;
        while (fin != true) {
            pull = (Pull) ois.readObject();
            cipher.init(Cipher.DECRYPT_MODE, KS);
            SealedObject so = pull.getTicket().getKCS();
            KCS unsealed = null;
            unsealed = (KCS) so.getObject(cipher);
            SecretKey KCS = unsealed.getKcs();
            // Verification timestamp
            long validity = unsealed.getValidity().getTime();
            long now = new Date().getTime();
            if ((validity - now) < 0)
                throw new BadTimestampException();
            else
                System.out.println("Timestamp valide");

            switch (pull.getName()) {
            case "Hello": {
                push = new Push();
                push.setName("Hello");
            }
                break;
            case "END": {
                System.out.println("Closing Connection");
                fin = true;
            }
                break;
            case "recherche": {
                push = new Push();
                push.setName("recherche");
                DBObject query = new BasicDBObject();
                if (pull.getValide().equalsIgnoreCase("true") || pull.getValide().equalsIgnoreCase("false"))
                    query.put("valide", pull.getValide());
                if (!pull.getBanque().equals(""))
                    query.put("banque.name", pull.getBanque());

                DBCursor cursor = collection.find(query);
                iterateOverCursor(cursor, push);
                cursor.close();
            }
                break;
            case "validation": {
                for (int i = 0; i < pull.getIds().size(); i++) {
                    //System.out.println(pull.getIds().get(i));
                    DBObject query = new BasicDBObject("_id", Integer.parseInt(pull.getIds().get(i)));
                    DBObject update = new BasicDBObject();
                    update.put("$set", new BasicDBObject("valide", "true"));
                    collection.update(query, update);
                }

                push = new Push();
                push.setName("validOK");

            }
                break;
            }
            cipher.init(Cipher.DECRYPT_MODE, KCS);
            AuthenticatorTGS auth = (AuthenticatorTGS) pull.getAuthenticator().getObject(cipher);
            cipher.init(Cipher.ENCRYPT_MODE, KCS);
            push.setTimestamp(new SealedObject(auth.getCurrentTime(), cipher));

            oos.writeObject(push);
        }
        oos.close();
        ois.close();
        cSock.close();
    } catch (BadTimestampException ex) {
        System.out.println("BadTimeStampExc");
    } catch (Exception ex) {
        Logger.getLogger(Task.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:bd.DArticulo.java

public ArrayList getAllOrganitation() {
    ArrayList datos = new ArrayList();
    CArticulo x = new CArticulo();
    conecion con = new conecion(table);
    BasicDBObject id = new BasicDBObject();
    return (ArrayList) con.get_colletion().distinct("organizacion");

}

From source file:be.datablend.blueprints.impls.mongodb.MongoDBQuery.java

private void addToQuery(String key, Object value) {
    String mongoKey = MongoDBUtil.createPropertyKey(key);
    if (this.query.containsField(mongoKey)) {
        List<DBObject> andValues = null;

        if (query.containsField("$and")) {
            andValues = (List<DBObject>) query.get("$and");
        } else {/*  w w  w  .  j av  a2s .c  om*/
            andValues = new ArrayList<DBObject>();

        }
        //remove the value that was stored in the key and add it to the and.
        Object removedObject = query.remove(mongoKey);
        BasicDBObject removedObjectQuery = new BasicDBObject().append(mongoKey, removedObject);
        andValues.add(removedObjectQuery);
        //add new value eto and list
        andValues.add(new BasicDBObject().append(mongoKey, value));
        this.query.put("$and", andValues);
    } else {
        query.append(mongoKey, value);
    }
    //System.out.println("Current mongo query: " + query.toString());
}

From source file:be.datablend.blueprints.impls.mongodb.MongoDBQuery.java

@Override
public <T extends Comparable<T>> GraphQuery has(String key, T value, Compare compare) {
    super.has(key, value, compare);

    String operator = null;//w w w  .jav  a  2 s  .c o  m
    if (compare == Compare.GREATER_THAN) {
        operator = "$gt";
    } else if (compare == Compare.GREATER_THAN_EQUAL) {
        operator = "$gte";
    } else if (compare == Compare.LESS_THAN) {
        operator = "$lt";
    } else if (compare == Compare.LESS_THAN_EQUAL) {
        operator = "$lte";
    } else if (compare == Compare.NOT_EQUAL) {
        operator = "$neq";
    }

    if (operator != null) {

        BasicDBObject queryObj = new BasicDBObject().append(operator, value);
        addToQuery(key, queryObj);
    } else {
        addToQuery(key, value);
    }
    System.out.println("Adding compare query to query object: " + query.toString());
    return this;
}

From source file:be.datablend.blueprints.impls.mongodb.MongoDBQuery.java

@Override
public GraphQuery has(String key, Predicate predicate, Object value) {
    String operator = null;// w  w  w.j av  a 2  s  .c o m
    if (predicate == com.tinkerpop.blueprints.Compare.GREATER_THAN) {
        operator = "$gt";
    } else if (predicate == com.tinkerpop.blueprints.Compare.GREATER_THAN_EQUAL) {
        operator = "$gte";
    } else if (predicate == com.tinkerpop.blueprints.Compare.LESS_THAN) {
        operator = "$lt";
    } else if (predicate == com.tinkerpop.blueprints.Compare.LESS_THAN_EQUAL) {
        operator = "$lte";
    } else if (predicate == com.tinkerpop.blueprints.Compare.NOT_EQUAL) {
        operator = "$ne";
    }

    if (operator != null) {
        BasicDBObject query = new BasicDBObject().append(operator, value);
        addToQuery(key, query);

        return this;
    }

    if (predicate == com.tinkerpop.blueprints.Compare.EQUAL) {
        addToQuery(key, value);
        return this;
    }

    if (predicate instanceof ArrayRangeSearchPredicate) {
        Object min = null;
        Object max = null;
        if (value instanceof Object[]) {
            Object[] valueArray = (Object[]) value;
            min = valueArray[0];
            max = valueArray[1];
            return this;
        } else if (value instanceof List) {
            List valueList = (List) value;
            min = valueList.get(0);
            max = valueList.get(1);
        } else {
            String errorMsg = "Do not know how to deal with value of type: " + value.getClass().getName()
                    + " for ArrayRangeSearchPredicate";
            System.out.println(errorMsg);
            throw new RuntimeException(errorMsg);
        }
        BasicDBObject query = new BasicDBObject().append("$gte", min).append("$lte", max);
        addToQuery(key, query);
        return this;
    } else if (predicate instanceof ArraySearchPredicate) {
        if (value instanceof Map) {
            Map<String, Object> valueMap = (Map<String, Object>) value;
            BasicDBObject query = new BasicDBObject();
            for (String mapKey : valueMap.keySet()) {
                query.put(mapKey, valueMap.get(mapKey));
            }
            addToQuery(key, query);
        } else if (value instanceof DBObject) {
            DBObject query = (DBObject) value;
            addToQuery(key, query);
        } else {
            String errorMsg = "Do not know how to deal with value of type " + value.getClass().getName()
                    + " for ArraySearchPredicate";
            System.out.println(errorMsg);
            throw new RuntimeException(errorMsg);
        }
        return this;
    }

    super.has(key, predicate, value);
    this.predicates.add(new PredicateContainer(key, predicate, value));
    System.out.println("MongoDbQuery has Called with predicate, not sure what to do here");
    return this;
}

From source file:be.datablend.blueprints.impls.mongodb.MongoDBQuery.java

@Override
public GraphQuery hasNot(String key, Object value) {
    super.hasNot(key, value);
    String operator = "$neq";
    BasicDBObject queryObj = new BasicDBObject().append(operator, value);
    query.append(MongoDBUtil.createPropertyKey(key), queryObj);
    return this;
}