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:br.ufabc.impress.mongo.manager.DBHelper.java

@Override
public String getAll(String tableName) {
    if (tableName == null || tableName.equals("")) {
        return "501";
    }/*from   www .  ja v  a  2  s  .c om*/
    String row = "";
    DBCollection table = db.getCollection(tableName);
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("status", "active");
    DBCursor cursor = table.find(searchQuery);
    if (cursor.size() > 0) {
        JSON json = new JSON();
        row = json.serialize(cursor);
        cursor.close();
        return row;
    } else {
        cursor.close();
        return null;
    }
}

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

@Override
public String getByKey(String tableName, String _id) {
    if (tableName == null || tableName.equals("") || _id == null || _id.equals("")) {
        return "501";
    }//ww  w .  jav  a  2s.  c  o  m
    String row = null;
    DBCursor cursor = null;
    DBCollection table = db.getCollection(tableName);
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(_id));
    cursor = table.find(searchQuery);
    if (cursor.size() > 0) {
        JSON json = new JSON();
        row = json.serialize(cursor);
        cursor.close();
        return row;
    } else {
        cursor.close();
        return null;
    }
}

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

@Override
public boolean modify(String tableName, String json, String _id) {
    if (tableName == null || tableName.equals("") || json == null || json.equals("") || _id == null
            || _id.equals("")) {
        return false;
    }/*  w  w  w .  j av  a 2 s.com*/
    DBCollection table = db.getCollection(tableName);
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(_id));
    DBObject dbObject = (DBObject) JSON.parse(json);
    dbObject.put("update", System.currentTimeMillis() + "");
    WriteResult result = table.update(searchQuery, dbObject);
    return result.isUpdateOfExisting();
}

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

private static BasicDBObject createDocument(final Record rec, final int idTag,
        final Map<Integer, ZeFDT.ZeFDTField> mfdt, final boolean useOnlyFDT) throws BrumaException {
    assert rec != null;
    assert idTag > 0;

    final BasicDBObject doc = new BasicDBObject();
    final Map<Integer, List<Field>> flds = new TreeMap<Integer, List<Field>>();
    if (idTag <= 0) {
        doc.put("_id", rec.getMfn());
    } else {// w w  w .j  av a 2  s  .  co  m
        final ZeFDT.ZeFDTField fdt = (mfdt == null) ? null : mfdt.get(idTag);
        final boolean isNumeric = (fdt == null) ? false : fdt.isNumeric();
        final String id = rec.getField(idTag, 1).getContent();

        if (isNumeric) {
            doc.put("_id", Integer.valueOf(id));
        } else {
            doc.put("_id", id);
        }
    }

    doc.put("nvf", rec.getNvf());

    for (Field fld : rec) {
        List<Field> fldLst = flds.get(fld.getId());
        if (fldLst == null) {
            fldLst = new ArrayList<Field>();
            flds.put(fld.getId(), fldLst);
        }
        fldLst.add(fld);
    }
    for (Map.Entry<Integer, List<Field>> entry : flds.entrySet()) {
        final int tag = entry.getKey();
        final ZeFDT.ZeFDTField fdt = (mfdt == null) ? null : mfdt.get(tag);
        final String auxId = (fdt == null) ? null : fdt.getDescription();
        final boolean isNumeric = (fdt == null) ? false : fdt.isNumeric();
        final String fid = (auxId == null) ? (useOnlyFDT ? null : "v" + tag) : auxId;

        if (fid != null) {
            final List<Field> fields = entry.getValue();
            final int size = fields.size();

            if (size == 1) {
                if (isNumeric) {
                    doc.put(fid, getNumSubfields(fields.get(0)));
                } else {
                    doc.put(fid, getStrSubfields(fields.get(0)));
                }
            } else if (size > 1) {
                final BasicDBList objL = new BasicDBList();
                for (Field fld : fields) {
                    if (isNumeric) {
                        objL.add(getNumSubfields(fld));
                    } else {
                        objL.add(getStrSubfields(fld));
                    }
                }
                doc.put(fid, objL);
            }
        }
    }

    return doc;
}

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

private static BasicDBObject getStrSubfields(final Field fld) {
    final BasicDBObject obj = new BasicDBObject();
    final Map<Character, List<String>> map = new HashMap<Character, List<String>>();

    for (Subfield sub : fld) {
        final char id = sub.getId();
        final List<String> lst;

        if (map.containsKey(id)) {
            lst = map.get(id);//from  ww w.  j  av  a  2  s . co m
        } else {
            lst = new ArrayList<String>();
            map.put(id, lst);
        }
        lst.add(sub.getContent());
    }
    for (Map.Entry<Character, List<String>> entry : map.entrySet()) {
        final String id = entry.getKey().toString();
        final List<String> lst = entry.getValue();

        if (lst.size() == 1) {
            obj.put(id, lst.get(0));
        } else {
            final BasicDBList bobj = new BasicDBList();
            for (String elem : lst) {
                bobj.add(elem);
            }
            obj.put(id, bobj);
        }
    }

    return obj;
}

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

private static BasicDBObject getNumSubfields(final Field fld) {
    final BasicDBObject obj = new BasicDBObject();
    final Map<Character, List<Integer>> map = new HashMap<Character, List<Integer>>();

    for (Subfield sub : fld) {
        final char id = sub.getId();
        final List<Integer> lst;

        if (map.containsKey(id)) {
            lst = map.get(id);//from  w w w  .j  ava  2s.  com
        } else {
            lst = new ArrayList<Integer>();
            map.put(id, lst);
        }
        lst.add(Integer.valueOf(sub.getContent()));
    }
    for (Map.Entry<Character, List<Integer>> entry : map.entrySet()) {
        final String id = entry.getKey().toString();
        final List<Integer> lst = entry.getValue();

        if (lst.size() == 1) {
            obj.put(id, lst.get(0));
        } else {
            final BasicDBList bobj = new BasicDBList();
            for (Integer elem : lst) {
                bobj.add(elem);
            }
            obj.put(id, bobj);
        }
    }

    return obj;
}

From source file:BusinessLogic.Service.RestaurantService.java

public Restaurant getBy(String name) {
    ArrayList restaurantConsult = new ArrayList();
    MongoConnection dbSingleton = MongoConnection.getInstance();

    DB db = dbSingleton.getTestdb();//from w w  w  .j  a  va2  s  . c o m
    DBCollection coll = db.getCollection(collName);
    BasicDBObject whereQuery = new BasicDBObject();
    // Sentence to search one account number
    whereQuery.put("name", name);

    DBCursor cursor = coll.find(whereQuery);
    while (cursor.hasNext()) {
        DBObject consultDocument = cursor.next();
        restaurantConsult.add(consultDocument.get("name"));
        restaurantConsult.add(consultDocument.get("direccion"));
        restaurantConsult.add(consultDocument.get("phone"));

    }
    Restaurant rs = new Restaurant(whereQuery);
    String response = "";
    response = "Name:" + restaurantConsult.get(0) + " ,Address: " + restaurantConsult.get(1) + " ,Phone: "
            + restaurantConsult.get(2);
    System.out.println(rs);
    return rs;

}

From source file:BusinessLogic.Service.RestaurantService.java

public String update(
        //String name, String direccion, String phone) {
        Restaurant rs) {/*from  w  w  w  .  j a va2  s .co m*/
    String name = rs.getName();
    String direccion = rs.getAddress();
    String phone = rs.getPhone();
    String reservationUpdate = null;
    try {
        // To connect to mongo dbserver
        MongoConnection dbSingleton = MongoConnection.getInstance();
        DB db = dbSingleton.getTestdb();
        DBCollection coll = db.getCollection(collName);

        System.out.println("Collection restaurants selected successfully");

        BasicDBObject whereQuery = new BasicDBObject();
        // Sentence to search one account number
        whereQuery.put("name", name);

        DBCursor cursor = coll.find(whereQuery);

        while (cursor.hasNext()) {

            DBObject updateDocument = cursor.next();
            updateDocument.put("name", name);
            updateDocument.put("direccion", direccion);
            updateDocument.put("phone", phone);

            coll.update(whereQuery, updateDocument);

        }
        System.out.println("Document updated successfully");
        reservationUpdate = "Success";

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
    if (reservationUpdate != null) {
        return "The restaurant has been updated successfully!";
    } else {
        return "The restaurant has not been updated!";
    }
}

From source file:BusinessLogic.Service.RestaurantService.java

public String delete(String name) {
    String deleteReservation = null;
    try {/*w  w  w.  j a  v  a2 s.  c o m*/
        // To connect to mongo dbserver
        MongoConnection dbSingleton = MongoConnection.getInstance();
        DB db = dbSingleton.getTestdb();
        DBCollection coll = db.getCollection(collName);

        System.out.println("Collection restaurants selected successfully");

        BasicDBObject whereQuery = new BasicDBObject();
        // Sentence to search one account number
        whereQuery.put("name", name);

        DBCursor cursor = coll.find(whereQuery);

        coll.remove(whereQuery);
        System.out.println("Document deleted successfully");
        deleteReservation = "Success";

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
    if (deleteReservation != null) {
        return "The restaurant has been deleted successfully!";
    } else {
        return "The restaurant has not been deleted!";
    }

}

From source file:buysell.login.java

private void loginSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginSActionPerformed
    // TODO add your handling code here:
    shopping sh1 = new shopping();
    try {/*from   www  .jav  a2  s .  com*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("snehal");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("user");
        System.out.println("Collection created successfully");
        String name = jTextField1.getText().toString();
        String passwd = jPasswordField1.getText().toString();
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", name);
        searchQuery.put("passwd", passwd);

        DBCursor cursor = coll.find(searchQuery);
        int i = 0;
        while (cursor.hasNext()) {
            i = 1;
            System.out.println(cursor.next());
        }
        if (i == 0) {
            JOptionPane.showMessageDialog(null, "user name or passwod is wrong");

        } else {
            System.out.println("user get");
            sh1.setVisible(true);
            dispose();
        }
        System.out.println("Document inserted successfully");
    } catch (NumberFormatException e) {
        System.out.println();
    }

}