List of usage examples for com.mongodb DBCursor next
@Override
public DBObject next()
From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java
License:Apache License
/** * <p>/* w ww .ja va 2 s .c o m*/ * Triggers "refresh" -- whatever that means -- of the implementation. * The general contract is that any should always leave itself in a * consistent, operational state, and that the refresh atomically updates * internal state from old to new. * </p> * * @param alreadyRefreshed s that are known to have already been refreshed as * a result of an initial call to a method on some object. This ensures * that objects in a refresh dependency graph aren't refreshed twice * needlessly. * @see #refreshData(String, Iterable, boolean) */ @Override public void refresh(Collection<Refreshable> alreadyRefreshed) { BasicDBObject query = new BasicDBObject(); query.put("deleted_at", new BasicDBObject("$gt", mongoTimestamp)); DBCursor cursor = collection.find(query); Date ts = new Date(0); while (cursor.hasNext()) { Map<String, Object> user = (Map<String, Object>) cursor.next().toMap(); String userID = getID(user.get(mongoUserID), true); Collection<List<String>> items = Lists.newArrayList(); List<String> item = Lists.newArrayList(); item.add(getID(user.get(mongoItemID), false)); item.add(Float.toString(getPreference(user.get(mongoPreference)))); items.add(item); try { refreshData(userID, items, false); } catch (NoSuchUserException e) { log.warn("No such user ID: {}", userID); } catch (NoSuchItemException e) { log.warn("No such items: {}", items); } if (ts.compareTo(getDate(user.get("created_at"))) < 0) { ts = getDate(user.get("created_at")); } } query = new BasicDBObject(); query.put("created_at", new BasicDBObject("$gt", mongoTimestamp)); cursor = collection.find(query); while (cursor.hasNext()) { Map<String, Object> user = (Map<String, Object>) cursor.next().toMap(); if (!user.containsKey("deleted_at")) { String userID = getID(user.get(mongoUserID), true); Collection<List<String>> items = Lists.newArrayList(); List<String> item = Lists.newArrayList(); item.add(getID(user.get(mongoItemID), false)); item.add(Float.toString(getPreference(user.get(mongoPreference)))); items.add(item); try { refreshData(userID, items, true); } catch (NoSuchUserException e) { log.warn("No such user ID: {}", userID); } catch (NoSuchItemException e) { log.warn("No such items: {}", items); } if (ts.compareTo(getDate(user.get("created_at"))) < 0) { ts = getDate(user.get("created_at")); } } } if (mongoTimestamp.compareTo(ts) < 0) { mongoTimestamp = ts; } }
From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java
License:Apache License
private void buildModel() throws UnknownHostException { userIsObject = false;// w w w . j a va 2 s . c o m itemIsObject = false; idCounter = 0; preferenceIsString = true; Mongo mongoDDBB = new Mongo(mongoHost, mongoPort); DB db = mongoDDBB.getDB(mongoDB); mongoTimestamp = new Date(0); FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<Collection<Preference>>(); if (!mongoAuth || db.authenticate(mongoUsername, mongoPassword.toCharArray())) { collection = db.getCollection(mongoCollection); collectionMap = db.getCollection(mongoMapCollection); DBObject indexObj = new BasicDBObject(); indexObj.put("element_id", 1); collectionMap.ensureIndex(indexObj); indexObj = new BasicDBObject(); indexObj.put("long_value", 1); collectionMap.ensureIndex(indexObj); collectionMap.remove(new BasicDBObject()); DBCursor cursor = collection.find(); while (cursor.hasNext()) { Map<String, Object> user = (Map<String, Object>) cursor.next().toMap(); if (!user.containsKey("deleted_at")) { long userID = Long.parseLong(fromIdToLong(getID(user.get(mongoUserID), true), true)); long itemID = Long.parseLong(fromIdToLong(getID(user.get(mongoItemID), false), false)); float ratingValue = getPreference(user.get(mongoPreference)); Collection<Preference> userPrefs = userIDPrefMap.get(userID); if (userPrefs == null) { userPrefs = Lists.newArrayListWithCapacity(2); userIDPrefMap.put(userID, userPrefs); } userPrefs.add(new GenericPreference(userID, itemID, ratingValue)); if (user.containsKey("created_at") && mongoTimestamp.compareTo(getDate(user.get("created_at"))) < 0) { mongoTimestamp = getDate(user.get("created_at")); } } } } delegate = new GenericDataModel(GenericDataModel.toDataMap(userIDPrefMap, true)); }
From source file:amazonwebcrawler.Mongo.java
public void removeData(String name) { try {//from w w w . j av a 2 s. co m BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("name", name); DBCursor cursor = myTable.find(searchQuery); while (cursor.hasNext()) { myTable.remove(cursor.next()); } } catch (MongoException e) { e.printStackTrace(); } }
From source file:amazonwebcrawler.Mongo.java
public void searchData(String name) { try {//from w ww .ja va 2 s. c o m BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("name", name); DBCursor cursor = myTable.find(searchQuery); while (cursor.hasNext()) { System.out.println(cursor.next()); } } catch (MongoException e) { e.printStackTrace(); } }
From source file:app.model.Model.java
public void setTable(JTable table, String collection) { DBCollection col = this.getCollection(collection); DBCursor cur = col.find(); String[] columnNames = { "id", "CIDADE", "UF" }; DefaultTableModel model = new DefaultTableModel(columnNames, 0); while (cur.hasNext()) { DBObject obj = cur.next(); String cidade = (String) obj.get("CIDADE"); String uf = (String) obj.get("UF"); ObjectId id = (ObjectId) obj.get("_id"); model.addRow(new Object[] { id, cidade, uf }); }/*from ww w . j a v a 2s. co m*/ cur.close(); table.setModel(model); }
From source file:App.modules.main_menu.model.dao.Sign_in_DAO.java
public static int Select_Admin_dni_mongo() { DBCursor cursor = null; int ok = 0;/*from w w w.j a v a 2 s . c o m*/ Singleton_App.c = new Client(); try { BasicDBObject query = new BasicDBObject(); query.put("user", Sign_in.User_txt.getText()); query.put("pass", Sign_in.Pass_txt.getText()); // BasicDBObject searchById = new BasicDBObject(); // searchById.append("dni", 1).append("_id",0); cursor = Singleton_App.collection.find(query); if (cursor.count() != 0) { while (cursor.hasNext()) { BasicDBObject document = (BasicDBObject) cursor.next(); Singleton_App.c = Singleton_App.c.Client_to_DB(document); ok = 1; } } else { ok = 0; // JOptionPane.showMessageDialog(null, "Cliente no encontrado "+ ok); // System.out.println("NOT DATA"); } } finally { if (cursor != null) { cursor.close(); } } // JOptionPane.showMessageDialog(null, Singleton_App.c); return ok; }
From source file:app.modules.SignIn.model.DAO_Login.java
/** * Compara el nombre de usuario con el de todos los clientes de la base de datos, cuando encuentra una coincidencia * compara la contrasea introducida con la de la base de datos, cuando ambos datos coinciden devuelve true y * rellena el perfil del usuario/*from w w w . j a va 2 s . com*/ * @return login True si se ha logrado iniciar sesin */ public static boolean SignInClient() { boolean login = false; DBCursor cursor = null; client c = new client(); try { cursor = singleton_global.collection.find(new BasicDBObject().append("user", addusername.getText())); if (cursor.count() != 0) { for (int i = 0; i < cursor.size(); i++) { BasicDBObject document = (BasicDBObject) cursor.next(); c = c.BBDD_to_client(document); fecha aux = new fecha(); if (c.getPass().equals(addpass.getText())) { new client_controller(new clientnew_view(), 2).Iniciar(2); adddnic.setText(c.getDni()); caddname.setText(c.getName()); caddsurname.setText(c.getSubname()); caddmobile.setText(c.getMobile()); caddemail.setText(c.getEmail()); cadddatebirthday.setCalendar(aux.stringtocalendar(c.getDate_birthday())); caddnameuser.setText(c.getUser()); caddpassword.setText(c.getPass()); caddavatar.setText(c.getAvatar()); cadd_status.setSelectedItem(c.getState()); caddreg.setCalendar(aux.stringtocalendar(c.getUp_date())); caddshopping.setText(Float.toString(c.getShopping())); caddpremium.setSelectedItem(c.getPremium()); caddtype.setText(c.getClient_type()); login = true; if ("Premium".equals(c.getPremium())) { cadddesc.setText("10%"); } else { cadddesc.setText("0%"); } caddyearsservice.setText(Integer.toString( aux.restafechas(aux.stringtocalendar(c.getUp_date()), aux.fechasystem(), "years"))); } } } else { System.out.println("NOT DATA"); } } catch (Exception e) { if (cursor != null) { cursor.close(); } } return login; }
From source file:App.modules.users.Client.Model.dao.Client_DB_DAO.java
/** * // ww w. jav a2 s. c o m */ public static void print_table() { DBCursor cursor = null; // String rdo = ""; Client c = new Client(); Singleton_cli.cli.clear(); try { cursor = Singleton_App.collection.find(); if (cursor.count() != 0) { while (cursor.hasNext()) { BasicDBObject document = (BasicDBObject) cursor.next(); // JOptionPane.showMessageDialog(null, c.toString()); Singleton_cli.cli.add(c.Client_to_DB(document)); // JOptionPane.showMessageDialog(null, Singleton_cli.cli.toString()); // rdo = rdo + (c.getNombre() + " - " + w.getApellidos() + " - " +w.getEdad() + "\n"); } } else { System.out.println("NOT DATA"); } } finally { if (cursor != null) { cursor.close(); } } //return rdo; }
From source file:app.modules.users.client.model.DAO.DAO_client.java
/** * Comprueba que todos los datos sean correctos y los guarda sobre los antiguos * @return True si se ha modificado correctamente *//*from www .ja v a 2 s.co m*/ public static boolean saveeditclient() { boolean val = false; if (pidedni() && pidenombre() && pideapellidos() && pidefechanacimiento() && pidetelefono() && pideemail() && pideusuario() && pidecontrasenya() && pidefecharegistro() && pidecompras() && pidetipo()) { if ("admin".equals(singleton_global.type)) { String dni = clientnew_view.adddnic.getText(); String name = clientnew_view.caddname.getText(); String surname = clientnew_view.caddsurname.getText(); String mobile = clientnew_view.caddmobile.getText(); String email = clientnew_view.caddemail.getText(); fecha aux = new fecha(); String datebirthday = aux.calendartostring(clientnew_view.cadddatebirthday.getCalendar(), 0); String nameuser = clientnew_view.caddnameuser.getText(); String passwd = clientnew_view.caddpassword.getText(); String avatar = clientnew_view.caddavatar.getText(); String status = clientnew_view.cadd_status.getSelectedItem().toString(); String update = aux.calendartostring(clientnew_view.caddreg.getCalendar(), 0); float shopping = Float.parseFloat(clientnew_view.caddshopping.getText()); String premium = clientnew_view.caddpremium.getSelectedItem().toString(); String type = clientnew_view.caddtype.getText(); int inicio = (pagina.currentPageIndex - 1) * pagina.itemsPerPage; selectedcli = TABLA.getSelectedRow(); int selected1 = inicio + selectedcli; BLL_client.UpdateClientMongo(singleton_client.client.getClient(selected1).getDni()); singleton_client.client.getClient(selected1).setDni(dni); singleton_client.client.getClient(selected1).setName(name); singleton_client.client.getClient(selected1).setSubname(surname); singleton_client.client.getClient(selected1).setMobile(mobile); singleton_client.client.getClient(selected1).setEmail(email); singleton_client.client.getClient(selected1).setDate_birthday(datebirthday); singleton_client.client.getClient(selected1).setUser(nameuser); singleton_client.client.getClient(selected1).setPass(passwd); singleton_client.client.getClient(selected1).setAvatar(avatar); singleton_client.client.getClient(selected1).setState(status); singleton_client.client.getClient(selected1).setUp_date(update); singleton_client.client.getClient(selected1).setShopping(shopping); singleton_client.client.getClient(selected1).setPremium(premium); singleton_client.client.getClient(selected1).setClient_type(type); val = true; } else { client c = new client(); DBCursor cursor = singleton_global.collection .find(new BasicDBObject().append("dni", adddnic.getText())); for (int i = 0; i < cursor.size(); i++) { BasicDBObject document = (BasicDBObject) cursor.next(); c = c.BBDD_to_client(document); if (c.getDni().equals(adddnic.getText())) { c.setDni(clientnew_view.adddnic.getText()); c.setName(clientnew_view.caddname.getText()); c.setSubname(clientnew_view.caddsurname.getText()); c.setMobile(clientnew_view.caddmobile.getText()); c.setEmail(clientnew_view.caddemail.getText()); fecha aux = new fecha(); c.setDate_birthday(aux.calendartostring(clientnew_view.cadddatebirthday.getCalendar(), 0)); c.setUser(clientnew_view.caddnameuser.getText()); c.setPass(clientnew_view.caddpassword.getText()); c.setAvatar(clientnew_view.caddavatar.getText()); c.setState(clientnew_view.cadd_status.getSelectedItem().toString()); c.setUp_date(aux.calendartostring(clientnew_view.caddreg.getCalendar(), 0)); c.setShopping(Float.parseFloat(clientnew_view.caddshopping.getText())); c.setPremium(clientnew_view.caddpremium.getSelectedItem().toString()); c.setClient_type(clientnew_view.caddtype.getText()); BLL_client.UpdateClientMongo(c.getDni()); } } val = true; } } return val; }
From source file:app.modules.users.client.model.DAO.DAO_Mongo.java
/** * Lee todos los clientes de la bbdd para mostrarlos en nuestra app *//*w w w . java 2s . c o m*/ public static void read_client() { DBCursor cursor = null; client c = new client(); try { cursor = singleton_global.collection.find(); if (cursor.count() != 0) { for (int i = 0; i < cursor.size(); i++) { BasicDBObject document = (BasicDBObject) cursor.next(); c = c.BBDD_to_client(document); client j = new client(c.getDni(), c.getName(), c.getSubname(), c.getMobile(), c.getEmail(), c.getDate_birthday(), c.getUser(), c.getPass(), c.getAvatar(), c.getState(), c.getUp_date(), c.getShopping(), c.getPremium(), c.getClient_type()); singleton_client.client.AddClient(j); } } else { System.out.println("NOT DATA"); } } catch (Exception e) { if (cursor != null) { cursor.close(); } } }