PageRenderTime 113ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Persistence/MongoBase.java

https://bitbucket.org/marcosflorez/mvp
Java | 2565 lines | 2128 code | 251 blank | 186 comment | 237 complexity | 85176080efdb52734f551e8bb2edf715 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. package Persistence;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.net.UnknownHostException;
  6. import java.rmi.activation.ActivationSystem;
  7. import java.util.*;
  8. import java.util.regex.Pattern;
  9. import com.fasterxml.jackson.databind.JsonNode;
  10. import com.google.code.geocoder.Geocoder;
  11. import com.google.code.geocoder.GeocoderRequestBuilder;
  12. import com.google.code.geocoder.model.GeocodeResponse;
  13. import com.google.code.geocoder.model.GeocoderRequest;
  14. import com.google.code.geocoder.model.LatLng;
  15. import com.google.gson.Gson;
  16. import com.mongodb.*;
  17. import com.mongodb.util.JSON;
  18. import com.typesafe.config.Config;
  19. import controllers.Application;
  20. import models.*;
  21. import models.Notifications.AddedToFavourites;
  22. import models.Notifications.Following;
  23. import models.Notifications.Notification;
  24. import models.Notifications.Publication;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.apache.commons.lang3.text.WordUtils;
  27. import org.apache.poi.hssf.usermodel.HSSFSheet;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.ss.usermodel.Row;
  30. import org.bson.types.ObjectId;
  31. import org.springframework.core.task.AsyncTaskExecutor;
  32. import play.Configuration;
  33. import play.Logger;
  34. import Utils.Cripto;
  35. import Utils.RandomString;
  36. import java.text.SimpleDateFormat;
  37. import java.text.DateFormat;
  38. import net.proinf.gramatica.*;
  39. import play.Play;
  40. import play.libs.WS;
  41. import play.mvc.Result;
  42. import play.libs.F;
  43. import com.typesafe.config.ConfigFactory;
  44. public class MongoBase {
  45. final static int MAXRESULTS = 100;
  46. final static int MAXTOPS = 20;
  47. final static int MAXFEEDS = 20;
  48. final static double MAXDISTANCE = 0.00078480615;//en kil�metros
  49. final static ArrayList<String> excepciones = new ArrayList<String>(Arrays.asList("wok", "alioli", "bravioli", "espagueti", "nuggets"));
  50. static DB actualMB;
  51. final static Configuration conf= Play.application().configuration();
  52. static DB connect() {
  53. if (actualMB != null) {
  54. return actualMB;
  55. }
  56. Mongo mongoClient;
  57. try {
  58. mongoClient = new Mongo(conf.getString("mongo.address"),conf.getInt("mongo.port"));
  59. // mongoClient = new Mongo("dharma.mongohq.com", 10031);
  60. //mongoClient = new Mongo("localhost" , 27017);
  61. actualMB = mongoClient.getDB("pitanzas");
  62. actualMB.authenticate("tista", "teto".toCharArray());
  63. return actualMB;
  64. } catch (UnknownHostException e) {
  65. // TODO Auto-generated catch block
  66. Logger.error(e.getMessage());
  67. }
  68. return null;
  69. }
  70. public static boolean checkValidationCode(String code) {
  71. BasicDBObject query = new BasicDBObject("Code", code);
  72. DBCollection coll = MongoBase.connect().getCollection("valCodes");
  73. DBObject retorno = coll.findOne(query);
  74. if (retorno != null) {
  75. return true;
  76. }
  77. Logger.info("Se ha introducido un código de validación INCORRECTO");
  78. return false;
  79. }
  80. public static boolean checkMail(String mail) {
  81. BasicDBObject query = new BasicDBObject("mail", mail);
  82. DBCollection coll = MongoBase.connect().getCollection("users");
  83. DBObject retorno = coll.findOne(query);
  84. if (retorno != null) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. public static User retrieveUser(String mail) {
  90. BasicDBObject query = new BasicDBObject("mail", mail);
  91. DBCollection coll = MongoBase.connect().getCollection("users");
  92. DBObject retorno = coll.findOne(query);
  93. if (retorno == null) {
  94. return null;
  95. }
  96. return new User(retorno);
  97. }
  98. public static String retrievePassword(String mail) {
  99. BasicDBObject query = new BasicDBObject("mail", mail);
  100. DBCollection coll = MongoBase.connect().getCollection("users");
  101. DBObject retorno = coll.findOne(query);
  102. if (retorno == null) {
  103. return null;
  104. }
  105. String password = null;
  106. String encPass = (String) retorno.get("password");
  107. if (encPass == null || encPass.equalsIgnoreCase("")) {
  108. return "";
  109. }
  110. try {
  111. password = Cripto.decrypt(encPass, (String) retorno.get("salt"));
  112. } catch (Exception e) {
  113. Logger.error(e.getMessage());
  114. return null;
  115. }
  116. Logger.info("Se ha recuperado el password de: " + mail);
  117. return password;
  118. }
  119. public static String retrieveClientPassword(String mail) {
  120. BasicDBObject query = new BasicDBObject("mail", mail);
  121. DBCollection coll = MongoBase.connect().getCollection("clients");
  122. DBObject retorno = coll.findOne(query);
  123. if (retorno == null) {
  124. return null;
  125. }
  126. String password = null;
  127. String encPass = (String) retorno.get("password");
  128. if (encPass == null || encPass.equalsIgnoreCase("")) {
  129. return "";
  130. }
  131. try {
  132. password = Cripto.decrypt(encPass, (String) retorno.get("salt"));
  133. } catch (Exception e) {
  134. Logger.error(e.getMessage());
  135. return null;
  136. }
  137. Logger.info("Se ha recuperado el password de: " + mail);
  138. return password;
  139. }
  140. public static String insertUser(User user) {
  141. String salt = RandomString.getRandomString(10);
  142. try {
  143. user.password = Cripto.encrypt(user.password, salt);
  144. } catch (Exception e) {
  145. Logger.error(e.getMessage());
  146. return null;
  147. }
  148. Gson gson = new Gson();
  149. DBObject doc = (DBObject) JSON.parse(gson.toJson(user));
  150. doc.put("salt", salt);
  151. doc.put("followers", new String[]{});
  152. doc.put("favUsers", new String[]{});
  153. doc.put("favVenues", new String[]{});
  154. doc.put("favDishes", new String[]{});
  155. doc.put("favPubs", new String[]{});
  156. DBCollection coll = MongoBase.connect().getCollection("users");
  157. WriteResult resultado = coll.insert(doc);
  158. if (resultado.getError() != null) {
  159. Logger.error(resultado.getError());
  160. return null;
  161. }
  162. Logger.info("Insertado el usuario " + user.user + ":" + user.mail);
  163. addNewFeed(new Feed(user));
  164. return doc.get("_id").toString();
  165. }
  166. public static String insertClient(Client client) {
  167. String salt = RandomString.getRandomString(10);
  168. try {
  169. client.setPassword(Cripto.encrypt(client.getPassword(), salt));
  170. } catch (Exception e) {
  171. Logger.error(e.getMessage());
  172. return null;
  173. }
  174. Gson gson = new Gson();
  175. DBObject doc = client.toDBO();
  176. doc.put("password",client.getPassword());
  177. doc.put("salt", salt);
  178. DBCollection coll = MongoBase.connect().getCollection("clients");
  179. WriteResult resultado = coll.insert(doc);
  180. if (resultado.getError() != null) {
  181. Logger.error(resultado.getError());
  182. return null;
  183. }
  184. Logger.info("Insertado el cliente " + client.getMail());
  185. return doc.get("_id").toString();
  186. }
  187. public static User findUserById(String id) {
  188. DBCollection coll = MongoBase.connect().getCollection("users");
  189. BasicDBObject q = new BasicDBObject();
  190. q.put("_id", new ObjectId(id));
  191. DBObject obj = coll.findOne(q);
  192. User user = new User(obj);
  193. return user;
  194. }
  195. public static String findUserJSONById(String id) {
  196. DBCollection coll = MongoBase.connect().getCollection("users");
  197. BasicDBObject q = new BasicDBObject();
  198. q.put("_id", new ObjectId(id));
  199. DBObject obj = coll.findOne(q);
  200. User user = new User(obj);
  201. return com.mongodb.util.JSON.serialize(obj);
  202. }
  203. public static String findUsersByName(String name) {
  204. DBCollection coll = MongoBase.connect().getCollection("users");
  205. BasicDBObject q = new BasicDBObject();
  206. q.put("user", java.util.regex.Pattern.compile(name,
  207. Pattern.CASE_INSENSITIVE));
  208. DBCursor cursor = coll.find(q);
  209. return "{\"users\":" + com.mongodb.util.JSON.serialize(cursor.toArray(MAXRESULTS)) + "}";
  210. }
  211. public static String findUsersById(List<String> ids) {
  212. DBCollection coll = MongoBase.connect().getCollection("users");
  213. BasicDBObject q = new BasicDBObject();
  214. List<ObjectId> oids = new ArrayList<ObjectId>(ids.size());
  215. for (String id : ids) {
  216. oids.add(new ObjectId(id));
  217. }
  218. q.put("_id", new BasicDBObject("$in", oids));
  219. DBCursor cursor = coll.find(q);
  220. return "{\"users\":" + com.mongodb.util.JSON.serialize(cursor.toArray()) + "}";
  221. }
  222. public static String findUsersFollowed(String followerId) {
  223. DBCollection coll = MongoBase.connect().getCollection("users");
  224. BasicDBObject q = new BasicDBObject();
  225. q.put("userId", followerId);
  226. DBObject user = coll.findOne(q);
  227. ArrayList<Object> al = (ArrayList<Object>) user.get("favUsers");
  228. ArrayList<String> usersId = new ArrayList<String>(al.size());
  229. if (al != null) {
  230. for (Object ob : al) {
  231. usersId.add(ob.toString());
  232. }
  233. }
  234. q = new BasicDBObject("$in", usersId);
  235. return "{\"users\":" + com.mongodb.util.JSON.serialize(coll.find(q).toArray()) + "}";
  236. }
  237. public static String insertFBUser(User user) {
  238. Gson gson = new Gson();
  239. DBObject doc = (DBObject) JSON.parse(gson.toJson(user));
  240. doc.put("followers", new String[]{});
  241. doc.put("favUsers", new String[]{});
  242. doc.put("favVenues", new String[]{});
  243. doc.put("favDishes", new String[]{});
  244. doc.put("favPubs", new String[]{});
  245. DBCollection coll = MongoBase.connect().getCollection("users");
  246. WriteResult resultado = coll.insert(doc);
  247. if (resultado.getError() != null) {
  248. Logger.error(resultado.getError());
  249. return null;
  250. }
  251. Logger.info("Insertado el usuario " + user.user + ":" + user.mail);
  252. addNewFeed(new Feed(user));
  253. return doc.get("_id").toString();
  254. }
  255. public static boolean loginAdmin(String mail, String password) {
  256. BasicDBObject query = new BasicDBObject("mail", mail);
  257. DBCollection coll = MongoBase.connect().getCollection("users");
  258. DBObject retorno = coll.findOne(query);
  259. if (retorno == null) {
  260. Logger.info("Usuario " + mail + " no encontrado");
  261. return false;
  262. }
  263. String admin = (String) retorno.get("admin");
  264. if (admin == null || admin.equalsIgnoreCase("false")) {
  265. Logger.info("El usuario " + mail + " no tiene permisos suficientes");
  266. return false;
  267. }
  268. String decPassword = null;
  269. String encPass = (String) retorno.get("password");
  270. try {
  271. decPassword = Cripto.decrypt(encPass, (String) retorno.get("salt"));
  272. } catch (Exception e) {
  273. Logger.error(e.getMessage());
  274. return false;
  275. }
  276. if (decPassword.equalsIgnoreCase(password)) {
  277. Logger.info("Administrador logueado: " + mail);
  278. return true;
  279. } else {
  280. Logger.info("Error en la password para el usuario" + mail);
  281. }
  282. Logger.info("Se ha reenviado la password a: " + mail);
  283. return false;
  284. }
  285. public static boolean loginVenue(String mail, String password) {
  286. BasicDBObject query = new BasicDBObject("mail", mail);
  287. DBCollection coll = MongoBase.connect().getCollection("clients");
  288. DBObject retorno = coll.findOne(query);
  289. if (retorno == null) {
  290. Logger.info("Usuario " + mail + " no encontrado");
  291. return false;
  292. }
  293. return true;
  294. /* String decPassword = null;
  295. String encPass = (String) retorno.get("password");
  296. try {
  297. decPassword = Cripto.decrypt(encPass, (String) retorno.get("salt"));
  298. } catch (Exception e) {
  299. Logger.error(e.getMessage());
  300. return false;
  301. }
  302. if (decPassword.equalsIgnoreCase(password)) {
  303. Logger.info("Venue logueada: " + mail);
  304. return true;
  305. } else {
  306. Logger.info("Error en la password para el usuario" + mail);
  307. }
  308. return false;*/
  309. }
  310. public static User loginUser(String mail, String password) {
  311. BasicDBObject query = new BasicDBObject("mail", mail);
  312. DBCollection coll = MongoBase.connect().getCollection("users");
  313. DBObject retorno = coll.findOne(query);
  314. if (retorno == null) {
  315. return null;
  316. }
  317. String decPassword = null;
  318. String encPass = (String) retorno.get("password");
  319. if (encPass == null || encPass.equalsIgnoreCase("")) {
  320. return null;
  321. }
  322. try {
  323. decPassword = Cripto.decrypt(encPass, (String) retorno.get("salt"));
  324. } catch (Exception e) {
  325. Logger.error(e.getMessage());
  326. e.printStackTrace();
  327. return null;
  328. }
  329. if (decPassword.equalsIgnoreCase(password)) {
  330. Logger.info("Usuario logueado: " + mail);
  331. return new User(retorno);
  332. }
  333. return null;
  334. }
  335. public static String logUser(String mail, String password) {
  336. BasicDBObject query = new BasicDBObject("mail", mail);
  337. DBCollection coll = MongoBase.connect().getCollection("users");
  338. DBObject retorno = coll.findOne(query);
  339. if (retorno == null) {
  340. return null;
  341. }
  342. String decPassword = null;
  343. String encPass = (String) retorno.get("password");
  344. if (encPass == null || encPass.equalsIgnoreCase("")) {
  345. return null;
  346. }
  347. try {
  348. decPassword = Cripto.decrypt(encPass, (String) retorno.get("salt"));
  349. } catch (Exception e) {
  350. Logger.error(e.getMessage());
  351. e.printStackTrace();
  352. return null;
  353. }
  354. if (decPassword.equalsIgnoreCase(password)) {
  355. Logger.info("Usuario logueado: " + mail);
  356. return com.mongodb.util.JSON.serialize(retorno);
  357. }
  358. return null;
  359. }
  360. public static String loginFBUser(User user, String userId) {
  361. BasicDBObject query = new BasicDBObject("mail", user.getMail());
  362. Logger.debug("loginFB: " + user);
  363. DBCollection coll = MongoBase.connect().getCollection("users");
  364. DBObject retorno = coll.findOne(query);
  365. if (retorno == null) {
  366. insertFBUser(user);
  367. retorno = coll.findOne(query);
  368. if(retorno==null){
  369. return null;
  370. }
  371. return com.mongodb.util.JSON.serialize(retorno);
  372. }
  373. if (retorno.containsField("fbid") && retorno.get("fbid").toString().equalsIgnoreCase(userId)) {
  374. return com.mongodb.util.JSON.serialize(retorno);
  375. } else {
  376. BasicDBObject set = new BasicDBObject("$set", user.toFbUpdateDBO());
  377. WriteResult resultado = coll.update(new BasicDBObject().append("mail", user.getMail()), set);
  378. Logger.info(resultado.toString());
  379. if (resultado.getError() != null) {
  380. Logger.error(resultado.getError());
  381. return null;
  382. }
  383. return com.mongodb.util.JSON.serialize(coll.findOne(query));
  384. }
  385. }
  386. public static boolean consumeValidationCode(String code) {
  387. BasicDBObject query = new BasicDBObject("Code", code);
  388. DBCollection coll = MongoBase.connect().getCollection("valCodes");
  389. WriteResult result = coll.remove(query);
  390. if (result.getError() != null) {
  391. System.out.println(result.getError());
  392. return false;
  393. }
  394. return true;
  395. }
  396. public static String findFeeds() {
  397. DBCollection coll = MongoBase.connect().getCollection("feeds");
  398. BasicDBObject s = new BasicDBObject();
  399. s.put("date", -1);
  400. DBCursor cursor = coll.find().sort(s).limit(MAXFEEDS);
  401. List<DBObject> retorno = cursor.toArray();
  402. return "{\"feeds\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  403. }
  404. public static List<Feed> findListOfFeeds() {
  405. DBCollection coll = MongoBase.connect().getCollection("feeds");
  406. BasicDBObject s = new BasicDBObject();
  407. s.put("date", -1);
  408. DBCursor cursor = coll.find().sort(s).limit(MAXFEEDS);
  409. List<Feed> retorno = new ArrayList<Feed>();
  410. while (cursor.hasNext()) {
  411. retorno.add(new Feed(cursor.next()));
  412. }
  413. return retorno;
  414. }
  415. public static String findVenues(String cadena) {
  416. /*
  417. * final DBObject textSearchCommand = new BasicDBObject();
  418. * textSearchCommand.put("text", "venues");
  419. * textSearchCommand.put("search", cadena); DB db = MongoBase.connect();
  420. * final CommandResult commandResult = db.command(textSearchCommand);
  421. *
  422. * return commandResult.toString();
  423. */
  424. DBCollection coll = MongoBase.connect().getCollection("venues");
  425. BasicDBObject q = new BasicDBObject();
  426. q.put("searchName", java.util.regex.Pattern.compile(cadena));
  427. DBCursor cursor = coll.find(q).limit(MAXTOPS);
  428. List<DBObject> retorno = cursor.toArray();
  429. return "{\"venues\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  430. }
  431. public static String findNearbyVenues(float lat, float lon, double dis) {
  432. DB db = MongoBase.connect();
  433. BasicDBObject myCmd = new BasicDBObject();
  434. myCmd.append("geoNear", "venues");
  435. float[] loc = {lat, lon};
  436. myCmd.append("near", loc);
  437. myCmd.append("limit", "10");
  438. myCmd.append("maxDistance", dis);
  439. myCmd.append("spherical", true);
  440. myCmd.append("distanceMultiplier", 6371);
  441. CommandResult myResults = db.command(myCmd);
  442. List<BasicDBObject> retorno = new ArrayList<BasicDBObject>(10);
  443. List<BasicDBObject> results = (List<BasicDBObject>) myResults.get("results");
  444. for (BasicDBObject bdbo : results) {
  445. BasicDBObject venue = (BasicDBObject) bdbo.get("obj");
  446. venue.put("dis", bdbo.get("dis"));
  447. retorno.add(venue);
  448. }
  449. return "{\"venues\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  450. }
  451. public static String findNearbyVenuesByName(float lat, float lon, double dis, String name) {
  452. DB db = MongoBase.connect();
  453. float[] loc = {lat, lon};
  454. DBObject sphere = new BasicDBObject("$centerSphere", new Object[]{loc, dis});
  455. DBObject geo = new BasicDBObject("$geoWithin", sphere);
  456. DBObject q = new BasicDBObject("coordinates", geo);
  457. q.put("searchName", java.util.regex.Pattern.compile(name));
  458. DBCursor cursor = db.getCollection("venues").find(q).limit(MAXTOPS);
  459. return "{\"venues\":" + com.mongodb.util.JSON.serialize(cursor.toArray()) + "}";
  460. }
  461. public static String findVenuesByCity(String location) {
  462. DBCollection coll = MongoBase.connect().getCollection("venues");
  463. BasicDBObject q = new BasicDBObject();
  464. q.put("address.city", location);
  465. DBCursor cursor = coll.find(q).limit(MAXTOPS);
  466. List<DBObject> retorno = cursor.toArray();
  467. return "{\"venues\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  468. }
  469. public static String findVenuesByCityAndName(String location, String name) {
  470. DBCollection coll = MongoBase.connect().getCollection("venues");
  471. BasicDBObject q = new BasicDBObject();
  472. q.put("address.city", location);
  473. q.put("searchName", java.util.regex.Pattern.compile(name));
  474. DBCursor cursor = coll.find(q).limit(MAXTOPS);
  475. List<DBObject> retorno = cursor.toArray();
  476. return "{\"venues\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  477. }
  478. public static String findVenuesByProvince(String location) {
  479. DBCollection coll = MongoBase.connect().getCollection("venues");
  480. BasicDBObject q = new BasicDBObject();
  481. q.put("address.province", location);
  482. DBCursor cursor = coll.find(q).limit(MAXTOPS);
  483. List<DBObject> retorno = cursor.toArray();
  484. return "{\"venues\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  485. }
  486. public static String findVenuesByProvinceAndName(String location, String name) {
  487. DBCollection coll = MongoBase.connect().getCollection("venues");
  488. BasicDBObject q = new BasicDBObject();
  489. q.put("address.province", location);
  490. q.put("searchName", java.util.regex.Pattern.compile(name));
  491. DBCursor cursor = coll.find(q).limit(MAXTOPS);
  492. List<DBObject> retorno = cursor.toArray();
  493. return "{\"venues\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  494. }
  495. public static String findNearbyDishesByType(float[] coord, double dis, List<String> tipos) {
  496. DB db = MongoBase.connect();
  497. System.out.println("coordenadas: " + coord);
  498. System.out.println("distancia: " + dis);
  499. System.out.println("tipos: " + tipos);
  500. DBObject sphere = new BasicDBObject("$centerSphere", new Object[]{coord, dis});
  501. DBObject geo = new BasicDBObject("$geoWithin", sphere);
  502. DBObject q = new BasicDBObject("coordinates", geo);
  503. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  504. for (String tipo : tipos) {
  505. al.add(java.util.regex.Pattern.compile(tipo,
  506. Pattern.UNICODE_CASE));
  507. }
  508. q.put("types", new BasicDBObject("$all", al));
  509. DBCursor cursor = db.getCollection("dishes").find(q).sort(new BasicDBObject("points", -1)).limit(MAXTOPS);
  510. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(cursor.toArray()) + "}";
  511. /*BasicDBObject geo= new BasicDBObject("near", coord);
  512. geo.append("distanceField","dis");
  513. geo.append("spherical","true");
  514. geo.append("maxDistance",dis);
  515. //geo.append("limit",MAXTOPS);
  516. geo.append("distanceMultiplier",6371);
  517. BasicDBObject q=new BasicDBObject();
  518. ArrayList<java.util.regex.Pattern> al=new ArrayList<java.util.regex.Pattern>();
  519. for(String tipo:tipos){
  520. al.add(java.util.regex.Pattern.compile(tipo,
  521. Pattern.UNICODE_CASE));
  522. }
  523. q.put("types",new BasicDBObject("$all", al));
  524. geo.append("query",q);
  525. DBObject near = new BasicDBObject("$geoNear", geo);
  526. DBObject sort = new BasicDBObject("$sort", new BasicDBObject("points", -1) );
  527. DBObject limit = new BasicDBObject("$limit", MAXTOPS);
  528. AggregationOutput output = db.getCollection("dishes").aggregate( near, sort,limit);
  529. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(output.results()) + "}";*/
  530. }
  531. public static String findNearbyDishes(float[] coord, double dis) {
  532. DB db = MongoBase.connect();
  533. DBObject sphere = new BasicDBObject("$centerSphere", new Object[]{coord, dis});
  534. DBObject geo = new BasicDBObject("$geoWithin", sphere);
  535. DBObject q = new BasicDBObject("coordinates", geo);
  536. DBCursor cursor = db.getCollection("dishes").find(q).sort(new BasicDBObject("points", -1)).limit(MAXTOPS);
  537. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(cursor.toArray()) + "}";
  538. }
  539. public static String findDishesByCity(String location) {
  540. DB db = MongoBase.connect();
  541. DBObject limit = new BasicDBObject("$limit", MAXTOPS);
  542. DBObject match = new BasicDBObject("$match", new BasicDBObject("city", location));
  543. DBObject sort = new BasicDBObject("$sort", new BasicDBObject("points", -1));
  544. AggregationOutput output = db.getCollection("dishes").aggregate(match, sort, limit);
  545. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(output.results()) + "}";
  546. }
  547. public static String findDishesByProvince(String location) {
  548. DB db = MongoBase.connect();
  549. BasicDBObject q = new BasicDBObject();
  550. BasicDBObject s = new BasicDBObject();
  551. q.put("province", location);
  552. s.put("points", -1);
  553. DBCursor cursor = db.getCollection("dishes").find(q).sort(s).limit(MAXTOPS);
  554. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(cursor.toArray()) + "}";
  555. }
  556. public static String findDishesByCityAndType(String location, List<String> tipos) {
  557. System.out.println("Buscando platos por ciudad y tipo: "+location+ " "+tipos.get(0)); DB db = MongoBase.connect();
  558. BasicDBObject q = new BasicDBObject();
  559. BasicDBObject s = new BasicDBObject();
  560. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  561. for (String tipo : tipos) {
  562. al.add(java.util.regex.Pattern.compile(tipo,
  563. Pattern.UNICODE_CASE));
  564. }
  565. q.put("types", new BasicDBObject("$all", al));
  566. q.append("city", location);
  567. s.put("points", -1);
  568. DBCursor cursor = db.getCollection("dishes").find(q).sort(s).limit(MAXTOPS);
  569. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(cursor.toArray()) + "}";
  570. }
  571. public static String findDishesByProvinceAndType(String location, List<String> tipos) {
  572. DB db = MongoBase.connect();
  573. BasicDBObject q = new BasicDBObject();
  574. BasicDBObject s = new BasicDBObject();
  575. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  576. for (String tipo : tipos) {
  577. al.add(java.util.regex.Pattern.compile(tipo,
  578. Pattern.UNICODE_CASE));
  579. }
  580. q.put("types", new BasicDBObject("$all", al));
  581. q.append("province", location);
  582. s.put("points", -1);
  583. DBCursor cursor = db.getCollection("dishes").find(q).sort(s).limit(MAXTOPS);
  584. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(cursor.toArray()) + "}";
  585. }
  586. public static void consolidate() {
  587. DB db = MongoBase.connect();
  588. DBCollection venues = db.getCollection("venues");
  589. DBCollection dishes = db.getCollection("dishes");
  590. DBCursor cursor = venues.find();
  591. List<DBObject> listaVenues = cursor.toArray(200);
  592. for (DBObject venue : cursor) {
  593. BasicDBObject q = new BasicDBObject();
  594. q.put("venueId", venue.get("_id"));
  595. WriteResult resultado = dishes.updateMulti(new BasicDBObject("venueId", venue.get("_id")), new BasicDBObject("$set", new BasicDBObject("venueName", venue.get("name")).append("coordinates", venue.get("coordinates"))));
  596. WriteResult resultado2 = dishes.updateMulti(new BasicDBObject("venueId", venue.get("_id")), new BasicDBObject("$set", new BasicDBObject("city", ((DBObject) venue.get("address")).get("city")).append("province", ((DBObject) venue.get("address")).get("province"))));
  597. }
  598. }
  599. public static void makeSearchNames() {
  600. DB db = MongoBase.connect();
  601. DBCollection venues = db.getCollection("venues");
  602. DBCursor cursor = venues.find();
  603. for (DBObject venue : cursor) {
  604. WriteResult resultado = venues.update(new BasicDBObject("_id", venue.get("_id")), new BasicDBObject("$set", new BasicDBObject("searchName", removeTildes(venue.get("name").toString()).toLowerCase())));
  605. }
  606. }
  607. public static void correct() {
  608. DB db = MongoBase.connect();
  609. DBCollection dishes = db.getCollection("dishes");
  610. DBCursor cursor = dishes.find();
  611. ArrayList<Object> al;
  612. ArrayList<String> types;
  613. DBObject o;
  614. String s;
  615. String tipo;
  616. while (cursor.hasNext()) {
  617. types = new ArrayList<String>();
  618. o = cursor.next();
  619. al = (ArrayList<Object>) o.get("types");
  620. if (al != null) {
  621. for (Object obj : al) {
  622. s = obj.toString();
  623. if (excepciones.contains(s)) {
  624. types.add(s);
  625. } else {
  626. tipo = sinTildes(Gramatica.plural(s).toLowerCase());
  627. types.add(tipo);
  628. Logger.info("Tipo: " + tipo);
  629. }
  630. }
  631. }
  632. dishes.update(new BasicDBObject().append("_id", new ObjectId(o.get("_id").toString())), new BasicDBObject().append("$set", new BasicDBObject("types", types)));
  633. }
  634. }
  635. public static void recalculatePoints() {
  636. DB db = MongoBase.connect();
  637. DBCollection dishes = db.getCollection("dishes");
  638. DBCursor cursor = dishes.find();
  639. DBObject o;
  640. while (cursor.hasNext()) {
  641. o = cursor.next();
  642. if (o.get("points").toString().equalsIgnoreCase("NaN") || (Float.parseFloat(o.get("points").toString())) > 0 || (o.get("reviews") != null)) {
  643. updateDishPoints(o.get("_id").toString(), dishes);
  644. }
  645. }
  646. }
  647. public static List<DBObject> findDishesForVenues(DB dataBase, List<ObjectId> venueIds, String cadena) {
  648. DBCollection coll = dataBase.getCollection("dishes");
  649. BasicDBObject q = new BasicDBObject();
  650. q.put("name", java.util.regex.Pattern.compile(cadena,
  651. Pattern.CASE_INSENSITIVE));
  652. q.put("venueId", new BasicDBObject("$in", venueIds));
  653. BasicDBObject s = new BasicDBObject();
  654. s.put("points", -1);
  655. DBCursor cursor = coll.find(q).sort(s);
  656. return cursor.toArray(MAXRESULTS);
  657. }
  658. public static List<DBObject> findDishesForVenue(DB dataBase, BasicDBObject venue, String cadena) {
  659. DBCollection coll = dataBase.getCollection("dishes");
  660. BasicDBObject q = new BasicDBObject();
  661. q.put("name", java.util.regex.Pattern.compile(cadena,
  662. Pattern.CASE_INSENSITIVE));
  663. q.put("venueId", venue.get("_id"));
  664. DBCursor cursor = coll.find(q).limit(MAXRESULTS);
  665. List<DBObject> retorno = cursor.toArray();
  666. for (DBObject dbo : retorno) {
  667. dbo.put("dis", venue.get("dis"));
  668. dbo.put("venueName", venue.get("name"));
  669. dbo.put("coordinates", venue.get("coordinates"));
  670. }
  671. return cursor.toArray(MAXRESULTS);
  672. }
  673. public static Venue findVenueById(String id) {
  674. /*
  675. * final DBObject textSearchCommand = new BasicDBObject();
  676. * textSearchCommand.put("text", "venues");
  677. * textSearchCommand.put("search", cadena); DB db = MongoBase.connect();
  678. * final CommandResult commandResult = db.command(textSearchCommand);
  679. *
  680. * return commandResult.toString();
  681. */
  682. DBCollection coll = MongoBase.connect().getCollection("venues");
  683. BasicDBObject q = new BasicDBObject();
  684. q.put("_id", new ObjectId(id));
  685. return new Venue(coll.findOne(q));
  686. }
  687. public static String findVenueIdByClientMail(String mail) {
  688. /*
  689. * final DBObject textSearchCommand = new BasicDBObject();
  690. * textSearchCommand.put("text", "venues");
  691. * textSearchCommand.put("search", cadena); DB db = MongoBase.connect();
  692. * final CommandResult commandResult = db.command(textSearchCommand);
  693. *
  694. * return commandResult.toString();
  695. */
  696. Client c=findClientByMail(mail);
  697. DBCollection coll = MongoBase.connect().getCollection("venues");
  698. BasicDBObject q = new BasicDBObject();
  699. q.put("_id", new ObjectId(c.venueId));
  700. return coll.findOne(q).get("_id").toString();
  701. }
  702. public static void deleteDish(String id) {
  703. DBCollection coll = MongoBase.connect().getCollection("dishes");
  704. BasicDBObject q = new BasicDBObject();
  705. q.put("_id", new ObjectId(id));
  706. coll.remove(q);
  707. Logger.info("Se ha borrado un plato :(");
  708. }
  709. public static void deletePub(String id) {
  710. DBCollection coll = MongoBase.connect().getCollection("publications");
  711. BasicDBObject q = new BasicDBObject();
  712. q.put("_id", new ObjectId(id));
  713. coll.remove(q);
  714. Logger.info("Se ha borrado una publicación :(");
  715. }
  716. public static void deleteVenue(String id) {
  717. DBCollection coll = MongoBase.connect().getCollection("venues");
  718. BasicDBObject q = new BasicDBObject();
  719. q.put("_id", new ObjectId(id));
  720. coll.remove(q);
  721. Logger.info("Se ha borrado una Venuelogin :(");
  722. }
  723. public static String findVenueJsonById(String id) {
  724. DBCollection coll = MongoBase.connect().getCollection("venues");
  725. BasicDBObject q = new BasicDBObject();
  726. q.put("_id", new ObjectId(id));
  727. DBObject retorno = coll.findOne(q);
  728. if (retorno == null)
  729. return null;
  730. return com.mongodb.util.JSON.serialize(retorno);
  731. }
  732. public static List<Venue> findListOfVenues(String cadena) {
  733. DBCollection coll = MongoBase.connect().getCollection("venues");
  734. BasicDBObject q = new BasicDBObject();
  735. q.put("searchName", java.util.regex.Pattern.compile(cadena)
  736. );
  737. DBCursor cursor = coll.find(q);
  738. List<Venue> retorno = new ArrayList<Venue>();
  739. while (cursor.hasNext()) {
  740. retorno.add(new Venue(cursor.next()));
  741. }
  742. return retorno;
  743. }
  744. public static List<Venue> findListOfVenuesForCity(String cadena, String location) {
  745. DBCollection coll = MongoBase.connect().getCollection("venues");
  746. BasicDBObject q = new BasicDBObject();
  747. q.put("searchName", java.util.regex.Pattern.compile(cadena));
  748. q.append("address.city", location);
  749. DBCursor cursor = coll.find(q);
  750. List<Venue> retorno = new ArrayList<Venue>();
  751. while (cursor.hasNext()) {
  752. retorno.add(new Venue(cursor.next()));
  753. }
  754. return retorno;
  755. }
  756. public static List<Venue> findListOfVenuesForProvince(String cadena, String location) {
  757. Logger.info("Search: "+cadena);
  758. DBCollection coll = MongoBase.connect().getCollection("venues");
  759. BasicDBObject q = new BasicDBObject();
  760. q.put("searchName", java.util.regex.Pattern.compile(cadena));
  761. q.append("address.province", location);
  762. DBCursor cursor = coll.find(q);
  763. List<Venue> retorno = new ArrayList<Venue>();
  764. while (cursor.hasNext()) {
  765. retorno.add(new Venue(cursor.next()));
  766. }
  767. return retorno;
  768. }
  769. public static List<Venue> findListOfVenues(String cadena, String sort, String order) {
  770. DBCollection coll = MongoBase.connect().getCollection("venues");
  771. BasicDBObject q = new BasicDBObject();
  772. q.put("searchName", java.util.regex.Pattern.compile(cadena));
  773. BasicDBObject s = new BasicDBObject();
  774. if (order.equalsIgnoreCase("-1")) {
  775. s.put(sort, -1);
  776. } else {
  777. s.put(sort, 1);
  778. }
  779. DBCursor cursor = coll.find(q).sort(s);
  780. List<Venue> retorno = new ArrayList<Venue>();
  781. while (cursor.hasNext()) {
  782. retorno.add(new Venue(cursor.next()));
  783. }
  784. return retorno;
  785. }
  786. public static List<Dish> findListOfDishes(String cadena) {
  787. DBCollection coll = MongoBase.connect().getCollection("dishes");
  788. BasicDBObject q = new BasicDBObject();
  789. q.put("name", java.util.regex.Pattern.compile(cadena,
  790. Pattern.CASE_INSENSITIVE));
  791. BasicDBObject s = new BasicDBObject();
  792. s.put("points", -1);
  793. DBCursor cursor = coll.find(q).sort(s);
  794. List<Dish> retorno = new ArrayList<Dish>();
  795. while (cursor.hasNext()) {
  796. retorno.add(new Dish(cursor.next()));
  797. }
  798. return retorno;
  799. }
  800. public static List<Dish> findListOfDishes(int max) {
  801. DBCollection coll = MongoBase.connect().getCollection("dishes");
  802. BasicDBObject s = new BasicDBObject();
  803. s.put("points", -1);
  804. DBCursor cursor = coll.find().sort(s).limit(max);
  805. List<Dish> retorno = new ArrayList<Dish>();
  806. while (cursor.hasNext()) {
  807. // System.out.println("NAME "+cursor.next().get("name"));
  808. retorno.add(new Dish(cursor.next()));
  809. }
  810. return retorno;
  811. }
  812. public static List<Dish> findListOfDishes(String cadena, String sort, String order) {
  813. DBCollection coll = MongoBase.connect().getCollection("dishes");
  814. BasicDBObject q = new BasicDBObject();
  815. q.put("name", java.util.regex.Pattern.compile(cadena,
  816. Pattern.CASE_INSENSITIVE));
  817. BasicDBObject s = new BasicDBObject();
  818. if (order.equalsIgnoreCase("-1")) {
  819. s.put(sort, -1);
  820. } else {
  821. s.put(sort, 1);
  822. }
  823. DBCursor cursor = coll.find(q).sort(s);
  824. List<Dish> retorno = new ArrayList<Dish>();
  825. while (cursor.hasNext()) {
  826. retorno.add(new Dish(cursor.next()));
  827. }
  828. return retorno;
  829. }
  830. public static String findDishesByType(List<String> tipos) {
  831. DB dataBase = MongoBase.connect();
  832. DBCollection coll = dataBase.getCollection("dishes");
  833. BasicDBObject q = new BasicDBObject();
  834. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  835. for (String tipo : tipos) {
  836. al.add(java.util.regex.Pattern.compile(tipo,
  837. Pattern.UNICODE_CASE));
  838. }
  839. q.put("types", new BasicDBObject("$all", al));
  840. BasicDBObject s = new BasicDBObject();
  841. s.put("points", -1);
  842. DBCursor cursor = coll.find(q).sort(s);
  843. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(cursor.toArray(MAXRESULTS)) + "}";
  844. }
  845. public static List<Dish> findListOfDishesByType(List<String> tipos) {
  846. DB dataBase = MongoBase.connect();
  847. DBCollection coll = dataBase.getCollection("dishes");
  848. BasicDBObject q = new BasicDBObject();
  849. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  850. for (String tipo : tipos) {
  851. al.add(java.util.regex.Pattern.compile(tipo,
  852. Pattern.UNICODE_CASE));
  853. }
  854. q.put("types", new BasicDBObject("$all", al));
  855. BasicDBObject s = new BasicDBObject();
  856. s.put("points", -1);
  857. DBCursor cursor = coll.find(q).sort(s);
  858. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  859. ArrayList<Dish> retorno = new ArrayList<Dish>();
  860. int i = 0;
  861. for (DBObject o : objetos) {
  862. retorno.add(new Dish(o));
  863. }
  864. return retorno;
  865. }
  866. public static List<Dish> findListOfDishesByProvinceAndType(List<String> tipos, String province) {
  867. DB dataBase = MongoBase.connect();
  868. DBCollection coll = dataBase.getCollection("dishes");
  869. BasicDBObject q = new BasicDBObject();
  870. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  871. for (String tipo : tipos) {
  872. al.add(java.util.regex.Pattern.compile(tipo,
  873. Pattern.UNICODE_CASE));
  874. }
  875. q.put("types", new BasicDBObject("$all", al));
  876. q.append("province", province);
  877. BasicDBObject s = new BasicDBObject();
  878. s.put("points", -1);
  879. DBCursor cursor = coll.find(q).sort(s);
  880. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  881. ArrayList<Dish> retorno = new ArrayList<Dish>();
  882. int i = 0;
  883. for (DBObject o : objetos) {
  884. retorno.add(new Dish(o));
  885. }
  886. return retorno;
  887. }
  888. public static List<Dish> findListOfDishesByCityAndType(List<String> tipos, String city) {
  889. DB dataBase = MongoBase.connect();
  890. DBCollection coll = dataBase.getCollection("dishes");
  891. BasicDBObject q = new BasicDBObject();
  892. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  893. for (String tipo : tipos) {
  894. al.add(java.util.regex.Pattern.compile(tipo,
  895. Pattern.UNICODE_CASE));
  896. }
  897. q.put("types", new BasicDBObject("$all", al));
  898. q.append("city", city);
  899. BasicDBObject s = new BasicDBObject();
  900. s.put("points", -1);
  901. DBCursor cursor = coll.find(q).sort(s);
  902. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  903. ArrayList<Dish> retorno = new ArrayList<Dish>();
  904. int i = 0;
  905. for (DBObject o : objetos) {
  906. retorno.add(new Dish(o));
  907. }
  908. return retorno;
  909. }
  910. public static List<Dish> findListOfDishesByType(List<String> tipos, String sort, String order) {
  911. DB dataBase = MongoBase.connect();
  912. DBCollection coll = dataBase.getCollection("dishes");
  913. BasicDBObject q = new BasicDBObject();
  914. ArrayList<java.util.regex.Pattern> al = new ArrayList<java.util.regex.Pattern>();
  915. for (String tipo : tipos) {
  916. al.add(java.util.regex.Pattern.compile(tipo,
  917. Pattern.UNICODE_CASE));
  918. }
  919. q.put("types", new BasicDBObject("$all", al));
  920. BasicDBObject s = new BasicDBObject();
  921. if (order.equalsIgnoreCase("-1")) {
  922. s.put(sort, -1);
  923. } else {
  924. s.put(sort, 1);
  925. }
  926. DBCursor cursor = coll.find(q).sort(s);
  927. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  928. ArrayList<Dish> retorno = new ArrayList<Dish>();
  929. int i = 0;
  930. for (DBObject o : objetos) {
  931. retorno.add(new Dish(o));
  932. }
  933. return retorno;
  934. }
  935. public static String findDishes(String cadena) {
  936. DB dataBase = MongoBase.connect();
  937. DBCollection coll = dataBase.getCollection("dishes");
  938. BasicDBObject q = new BasicDBObject();
  939. q.put("name", java.util.regex.Pattern.compile(cadena,
  940. Pattern.CASE_INSENSITIVE));
  941. BasicDBObject s = new BasicDBObject();
  942. s.put("points", -1);
  943. DBCursor cursor = coll.find(q).sort(s);
  944. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(cursor.toArray(MAXRESULTS)) + "}";
  945. }
  946. public static String findDishesByVenueId(String id) {
  947. DBCollection coll = MongoBase.connect().getCollection("dishes");
  948. BasicDBObject q = new BasicDBObject();
  949. q.put("venueId", new ObjectId(id));
  950. BasicDBObject s = new BasicDBObject();
  951. s.put("name", -1);
  952. DBCursor cursor = coll.find(q).sort(s);
  953. List<DBObject> retorno = cursor.toArray(MAXRESULTS);
  954. return "{\"dishes\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  955. }
  956. public static String findPublicationsByVenueId(String id) {
  957. DBCollection coll = MongoBase.connect().getCollection("publications");
  958. BasicDBObject q = new BasicDBObject();
  959. q.put("venueId", id);
  960. BasicDBObject s = new BasicDBObject();
  961. s.put("creationDate", -1);
  962. DBCursor cursor = coll.find(q).sort(s);
  963. List<DBObject> retorno = cursor.toArray(MAXRESULTS);
  964. return "{\"publications\":" + com.mongodb.util.JSON.serialize(retorno) + "}";
  965. }
  966. public static List<Dish> findListOfDishesByVenueId(String id) {
  967. DBCollection coll = MongoBase.connect().getCollection("dishes");
  968. BasicDBObject q = new BasicDBObject();
  969. q.put("venueId", new ObjectId(id));
  970. BasicDBObject s = new BasicDBObject();
  971. s.put("points", -1);
  972. DBCursor cursor = coll.find(q).sort(s);
  973. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  974. ArrayList<Dish> retorno = new ArrayList<Dish>();
  975. int i = 0;
  976. for (DBObject o : objetos) {
  977. retorno.add(new Dish(o));
  978. }
  979. return retorno;
  980. }
  981. public static List<Dish> findListOfDishesByVenueIdAndName(String id,String filter) {
  982. DBCollection coll = MongoBase.connect().getCollection("dishes");
  983. BasicDBObject q = new BasicDBObject();
  984. q.put("venueId", new ObjectId(id));
  985. q.put("name", java.util.regex.Pattern.compile(filter,
  986. Pattern.CASE_INSENSITIVE));
  987. BasicDBObject s = new BasicDBObject();
  988. s.put("points", -1);
  989. DBCursor cursor = coll.find(q).sort(s);
  990. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  991. ArrayList<Dish> retorno = new ArrayList<Dish>();
  992. int i = 0;
  993. for (DBObject o : objetos) {
  994. retorno.add(new Dish(o));
  995. }
  996. return retorno;
  997. }
  998. public static List<Dish> findListOfDishesByVenueId(String id,String sort, String order) {
  999. DBCollection coll = MongoBase.connect().getCollection("dishes");
  1000. BasicDBObject q = new BasicDBObject();
  1001. q.put("venueId", new ObjectId(id));
  1002. BasicDBObject s = new BasicDBObject();
  1003. if (order.equalsIgnoreCase("-1")) {
  1004. s.put("sort", -1);}
  1005. else{
  1006. s.put(sort, 1);
  1007. }
  1008. DBCursor cursor = coll.find(q).sort(s);
  1009. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  1010. ArrayList<Dish> retorno = new ArrayList<Dish>();
  1011. for (DBObject o : objetos) {
  1012. retorno.add(new Dish(o));
  1013. }
  1014. return retorno;
  1015. }
  1016. public static List<Publication> findListOfPublicationsByVenueId(String id) {
  1017. DBCollection coll = MongoBase.connect().getCollection("publications");
  1018. BasicDBObject q = new BasicDBObject();
  1019. q.put("venueId", id);
  1020. DBCursor cursor = coll.find(q);
  1021. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  1022. ArrayList<Publication> retorno = new ArrayList<Publication>();
  1023. for (DBObject o : objetos) {
  1024. retorno.add(new Publication(o));
  1025. }
  1026. return retorno;
  1027. }
  1028. public static List<Publication> findListOfPublicationsByVenueIdAndTitle(String id,String filter) {
  1029. DBCollection coll = MongoBase.connect().getCollection("publications");
  1030. BasicDBObject q = new BasicDBObject();
  1031. q.put("venueId", id);
  1032. q.put("title", java.util.regex.Pattern.compile(filter,
  1033. Pattern.CASE_INSENSITIVE));
  1034. DBCursor cursor = coll.find(q);
  1035. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  1036. ArrayList<Publication> retorno = new ArrayList<Publication>();
  1037. for (DBObject o : objetos) {
  1038. retorno.add(new Publication(o));
  1039. }
  1040. return retorno;
  1041. }
  1042. public static List<Publication> findListOfPublicationsByVenueId(String id,String sort, String order) {
  1043. DBCollection coll = MongoBase.connect().getCollection("publications");
  1044. BasicDBObject q = new BasicDBObject();
  1045. q.put("venueId", id);
  1046. BasicDBObject s = new BasicDBObject();
  1047. if (order.equalsIgnoreCase("-1")) {
  1048. s.put("sort", -1);}
  1049. else{
  1050. s.put(sort, 1);
  1051. }
  1052. DBCursor cursor = coll.find(q).sort(s);
  1053. List<DBObject> objetos = cursor.toArray(MAXRESULTS);
  1054. ArrayList<Publication> retorno = new ArrayList<Publication>();
  1055. for (DBObject o : objetos) {
  1056. retorno.add(new Publication(o));
  1057. }
  1058. return retorno;
  1059. }
  1060. public static Dish findDishById(String id) {
  1061. DBCollection coll = MongoBase.connect().getCollection("dishes");
  1062. BasicDBObject q = new BasicDBObject();
  1063. q.put("_id", new ObjectId(id));
  1064. DBObject obj = coll.findOne(q);
  1065. Dish d = new Dish(obj);
  1066. return d;
  1067. }
  1068. public static Publication findPublicationById(String id) {
  1069. DBCollection coll = MongoBase.connect().getCollection("publications");
  1070. BasicDBObject q = new BasicDBObject();
  1071. q.put("_id", new ObjectId(id));
  1072. DBObject obj = coll.findOne(q);
  1073. Publication p = new Publication(obj);
  1074. return p;
  1075. }
  1076. public static String findDishJSONById(String id) {
  1077. DBCollection coll = MongoBase.connect().getCollection("dishes");
  1078. BasicDBObject q = new BasicDBObject();
  1079. q.put("_id", new ObjectId(id));
  1080. DBObject retorno = coll.findOne(q);
  1081. if (retorno == null)
  1082. return null;
  1083. return com.mongodb.util.JSON.serialize(retorno);
  1084. }
  1085. public static boolean updateDish(Dish d) {
  1086. DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
  1087. d.modified = df.format(new Date());
  1088. BasicDBObject doc = d.toDBO();
  1089. DBCollection coll = MongoBase.connect().getCollection("dishes");
  1090. WriteResult resultado = coll.update(new BasicDBObject().append("_id", new ObjectId(d.oid.toString())), doc);
  1091. Logger.info(resultado.toString());
  1092. if (resultado.getError() != null) {
  1093. Logger.error(resultado.getError());
  1094. return false;
  1095. }
  1096. Logger.info("Editado dish desde la web: " + d.name);
  1097. return true;
  1098. }
  1099. public static boolean updatePublication(Publication p) {
  1100. DBObject doc = p.toDBO();
  1101. DBCollection coll = MongoBase.connect().getCollection("publications");
  1102. WriteResult resultado = coll.update(new BasicDBObject().append("_id", new ObjectId(p.pubId.toString())), doc);
  1103. Logger.info(resultado.toString());
  1104. if (resultado.getError() != null) {
  1105. Logger.error(resultado.getError());
  1106. return false;
  1107. }
  1108. Logger.info("Editado dish desde la web: " + p.title);
  1109. return true;
  1110. }
  1111. public static boolean updateUser(User user) {
  1112. DBObject doc;
  1113. if (user.password != null && !user.password.equalsIgnoreCase("")) {
  1114. try {
  1115. String salt = RandomString.getRandomString(10);
  1116. user.password = Cripto.encrypt(user.password, salt);
  1117. doc = user.toUpdateDBO();
  1118. doc.put("salt", salt);
  1119. Logger.info("password encriptado: " + user.password);
  1120. } catch (Exception e) {
  1121. Logger.error(e.getMessage());
  1122. return false;
  1123. }
  1124. } else {
  1125. doc = user.toUpdateDBO();
  1126. }
  1127. DBCollection coll = MongoBase.connect().getCollection("users");
  1128. BasicDBObje

Large files files are truncated, but you can click here to view the full file