PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/Application.java

https://bitbucket.org/marcosflorez/mvp
Java | 2137 lines | 1763 code | 167 blank | 207 comment | 388 complexity | 742548c6cbdc889081855c023b02ae76 MD5 | raw file

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

  1. package controllers;
  2. import Persistence.MongoBase;
  3. import com.fasterxml.jackson.databind.JsonNode;
  4. import com.google.gson.JsonArray;
  5. import com.google.gson.JsonObject;
  6. import com.google.gson.JsonParser;
  7. import com.google.gson.JsonPrimitive;
  8. import com.mongodb.Mongo;
  9. import com.typesafe.plugin.MailerAPI;
  10. import com.typesafe.plugin.MailerPlugin;
  11. import models.*;
  12. import models.Notifications.Notification;
  13. import models.Notifications.Publication;
  14. import org.apache.poi.hssf.usermodel.HSSFSheet;
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16. import org.apache.poi.ss.usermodel.Cell;
  17. import org.apache.poi.ss.usermodel.Row;
  18. import org.bson.types.ObjectId;
  19. import play.Logger;
  20. import play.data.Form;
  21. import play.libs.Akka;
  22. import play.libs.F;
  23. import play.libs.WS;
  24. import play.libs.WS.WSRequestHolder;
  25. import play.mvc.Controller;
  26. import play.mvc.Result;
  27. import play.mvc.Security;
  28. import views.html.*;
  29. import javax.mail.*;
  30. import javax.mail.internet.InternetAddress;
  31. import javax.mail.internet.MimeMessage;
  32. import java.io.File;
  33. import java.io.FileInputStream;
  34. import java.util.*;
  35. import java.util.concurrent.Callable;
  36. import java.util.regex.Pattern;
  37. import static play.data.Form.form;
  38. public class Application extends Controller {
  39. private static final String FB_APP_ID = "583844725030243";
  40. private static final String FB_APP_SECRET = "fcc5fc20bb48f1a7da3f14ea83ebdec3";
  41. @Security.Authenticated(Secured.class)
  42. public static Result index() {
  43. if(session().get("role").equalsIgnoreCase("venue")){
  44. return redirect(
  45. routes.VenuePanel.blank()
  46. );
  47. }else if(session().get("role").equalsIgnoreCase("admin")){
  48. return redirect(
  49. routes.AdminPanel.blank()
  50. );
  51. }
  52. else{
  53. return ok(index.render(MongoBase.findListOfDishes(20), MongoBase.findListOfFeeds()));
  54. }
  55. }
  56. public static Result front() {
  57. return ok(front.render());
  58. }
  59. public static Result negocios() {
  60. return ok(negocios.render(form(ClientContact.class)));
  61. }
  62. /**
  63. * Display the paginated list of computers.
  64. *
  65. * @param page Current page number (starts from 0)
  66. * @param sortBy Column to be sorted
  67. * @param order Sort order (either asc or desc)
  68. * @param filter Filter applied on computer names
  69. */
  70. @Security.Authenticated(Secured.class)
  71. public static Result list(int page, String sortBy, String order, String filter) {
  72. return ok(
  73. list.render(
  74. Venues.page(page, 10, sortBy, order, filter),
  75. sortBy, order, filter
  76. )
  77. );
  78. }
  79. /**
  80. * Display the paginated list of computers.
  81. *
  82. * @param page Current page number (starts from 0)
  83. * @param sortBy Column to be sorted
  84. * @param order Sort order (either asc or desc)
  85. * @param filter Filter applied on computer names
  86. */
  87. @Security.Authenticated(Secured.class)
  88. public static Result dishesList(int page, String sortBy, String order, String filter) {
  89. return ok(
  90. dishList.render(
  91. Dishes.page(page, 10, sortBy, order, filter),
  92. sortBy, order, filter
  93. )
  94. );
  95. }
  96. /**
  97. * Display the paginated list of computers.
  98. *
  99. * @param page Current page number (starts from 0)
  100. * @param sortBy Column to be sorted
  101. * @param order Sort order (either asc or desc)
  102. */
  103. @Security.Authenticated(Secured.class)
  104. public static Result venueDishList(int page, String sortBy, String order, String filter) {
  105. Venue v=MongoBase.findVenueById(session().get("venueId"));
  106. return ok(
  107. venuedishes.render(v,
  108. Dishes.venuePage(page, 10, sortBy, order, session().get("venueId"),filter),
  109. sortBy, order, session().get("venueId"),filter
  110. )
  111. );
  112. }
  113. /**
  114. * Display the paginated list of computers.
  115. *
  116. * @param page Current page number (starts from 0)
  117. * @param sortBy Column to be sorted
  118. * @param order Sort order (either asc or desc)
  119. */
  120. @Security.Authenticated(Secured.class)
  121. public static Result venuePubList(int page, String sortBy, String order, String filter) {
  122. // Venue v=MongoBase.findVenueById(session().get("venueId"));
  123. return ok(
  124. venuepubs.render(
  125. Publications.venuePage(page, 10, sortBy, order, session().get("venueId"),filter),
  126. sortBy, order, session().get("venueId"),filter
  127. )
  128. );
  129. }
  130. @Security.Authenticated(Secured.class)
  131. public static Result dishesShowForProvince(String search, String location) {
  132. List<Dish> dishes = MongoBase.findListOfDishesByProvinceAndType(Dishes.cleanAndSplit(search), location);
  133. ;
  134. switch (dishes.size()) {
  135. case 1:
  136. return ok(dishShow.render(dishes.get(0), MongoBase.findListOfFeeds()));
  137. default:
  138. return ok(dishesShow.render(dishes, search, MongoBase.findListOfFeeds()));
  139. }
  140. }
  141. @Security.Authenticated(Secured.class)
  142. public static Result dishesShowForCity(String search, String location) {
  143. List<Dish> dishes = MongoBase.findListOfDishesByCityAndType(Dishes.cleanAndSplit(search), location);
  144. ;
  145. switch (dishes.size()) {
  146. case 1:
  147. return ok(dishShow.render(dishes.get(0), MongoBase.findListOfFeeds()));
  148. default:
  149. return ok(dishesShow.render(dishes, search, MongoBase.findListOfFeeds()));
  150. }
  151. }
  152. @Security.Authenticated(Secured.class)
  153. public static Result venuesShowForCity(String search, String location) {
  154. List<Venue> venues = MongoBase.findListOfVenuesForCity(search, location);
  155. ;
  156. switch (venues.size()) {
  157. case 1:
  158. return ok(venueShow.render(venues.get(0), MongoBase.findListOfDishesByVenueId(venues.get(0).oid.toString()), MongoBase.findListOfFeeds()));
  159. default:
  160. return ok(venuesShow.render(venues, search, MongoBase.findListOfFeeds()));
  161. }
  162. }
  163. @Security.Authenticated(Secured.class)
  164. public static Result venuesShowForProvince(String search, String location) {
  165. List<Venue> venues = MongoBase.findListOfVenuesForProvince(search, location);
  166. ;
  167. switch (venues.size()) {
  168. case 1:
  169. return ok(venueShow.render(venues.get(0), MongoBase.findListOfDishesByVenueId(venues.get(0).oid.toString()), MongoBase.findListOfFeeds()));
  170. default:
  171. return ok(venuesShow.render(venues, search, MongoBase.findListOfFeeds()));
  172. }
  173. }
  174. public static Result checkValidationCode() {
  175. JsonNode json = request().body().asJson();
  176. String code = json.findPath("registerCode").asText();
  177. if (code == null) {
  178. return badRequest("Missing parameter [code]. Where is my motherf***ing code!!");
  179. } else if (!MongoBase.checkValidationCode(code)) {
  180. return badRequest("No code, no user ");
  181. }
  182. return ok("CODIGO CORRECTO!");
  183. }
  184. @Security.Authenticated(SecuredWS.class)
  185. public static Result findVenueNameById(String venueId) {
  186. if (venueId == null) {
  187. return badRequest("Missing parameter [venueId].");
  188. }
  189. String venueName = MongoBase.findVenueNameById(venueId);
  190. if (venueName == null) {
  191. return badRequest("Bad Request");
  192. }
  193. return ok(venueName);
  194. }
  195. @Security.Authenticated(SecuredWS.class)
  196. public static Result findVenueById(String venueId) {
  197. if (venueId == null) {
  198. return badRequest("Missing parameter [venueId].");
  199. }
  200. String venue = MongoBase.findVenueJsonById(venueId);
  201. if (venue == null) {
  202. return badRequest("Bad Request");
  203. }
  204. return ok(venue);
  205. }
  206. @Security.Authenticated(SecuredWS.class)
  207. public static Result findVenueByIdPost() {
  208. JsonNode json = request().body().asJson();
  209. if(!json.has("venueId")){
  210. return badRequest("missing parameter [venueId]");
  211. }
  212. String venue = MongoBase.findVenueJsonById(json.findPath("venueId").asText());
  213. if (venue == null) {
  214. return badRequest("Bad Request");
  215. }
  216. return ok(venue);
  217. }
  218. public static Result okTio() {
  219. return ok();
  220. }
  221. @Security.Authenticated(SecuredWS.class)
  222. public static Result findReviewsByUserId() {
  223. JsonNode json = request().body().asJson();
  224. String userId = json.findPath("userId").textValue();
  225. if (userId == null) {
  226. return badRequest("Missing parameter [userId].");
  227. }
  228. String user = MongoBase.findReviewsByUserId(new ObjectId(userId));
  229. Logger.info(user);
  230. if (user == null) {
  231. return badRequest("Bad Request");
  232. }
  233. return ok(user);
  234. }
  235. @Security.Authenticated(SecuredWS.class)
  236. public static Result findUserById() {
  237. JsonNode json = request().body().asJson();
  238. String userId = json.findPath("userId").asText();
  239. if (userId == null) {
  240. return badRequest("Missing parameter [userId].");
  241. }
  242. String user = MongoBase.findUserJSONById(userId);
  243. if (user == null) {
  244. return badRequest("Bad Request");
  245. }
  246. return ok(user);
  247. }
  248. @Security.Authenticated(SecuredWS.class)
  249. public static Result findUsersByName() {
  250. JsonNode json = request().body().asJson();
  251. String name = json.findPath("name").asText();
  252. if (name == null) {
  253. return badRequest("Missing parameter [name].");
  254. }
  255. String user = MongoBase.findUsersByName(name);
  256. if (user == null) {
  257. return badRequest("Bad Request");
  258. }
  259. return ok(user);
  260. }
  261. @Security.Authenticated(SecuredWS.class)
  262. public static Result findUsersFollowed() {
  263. JsonNode json = request().body().asJson();
  264. String userId = json.findPath("userId").asText();
  265. if (userId == null) {
  266. return badRequest("Missing parameter [useId].");
  267. }
  268. String user = MongoBase.findUsersFollowed(userId);
  269. if (user == null) {
  270. return badRequest("Bad Request");
  271. }
  272. return ok(user);
  273. }
  274. @Security.Authenticated(SecuredWS.class)
  275. public static Result findUsersByIds() {
  276. JsonNode json = request().body().asJson();
  277. Iterator<JsonNode> i = json.findPath("usersIds").elements();
  278. if (i == null) {
  279. return badRequest("Missing parameter [usersIds]");
  280. }
  281. ArrayList<String> al = new ArrayList<String>();
  282. while (i.hasNext()) {
  283. al.add(i.next().asText());
  284. }
  285. String users = MongoBase.findUsersById(al);
  286. if (users == null) {
  287. return badRequest("Bad Request");
  288. }
  289. return ok(users);
  290. }
  291. @Security.Authenticated(SecuredWS.class)
  292. public static Result updateUser() {
  293. Logger.info("UPDATING USER? ");
  294. JsonNode object = request().body().asJson();
  295. Logger.info(object.toString());
  296. User user=new User();
  297. String oldPassword="";
  298. if (object.has("user")) {
  299. user.user = object.get("user").asText();
  300. }
  301. if (object.has("mail")) {
  302. user.mail = object.get("mail").asText();
  303. }
  304. if (object.has("_id")) {
  305. user._id = new ObjectId(object.get("_id").asText());
  306. }
  307. if (object.has("fbid")) {
  308. user.fbid = object.get("fbid").asText();
  309. }
  310. if (object.has("location")) {
  311. user.location = object.get("location").asText();
  312. }
  313. if (object.has("date")) {
  314. user.date = object.get("date").asText();
  315. }
  316. if (object.has("birthday")) {
  317. user.birthday = object.get("birthday").asText();
  318. }
  319. if (object.has("gender")) {
  320. user.gender = object.get("gender").asText();
  321. }
  322. if (object.has("phone")) {
  323. user.phone = object.get("phone").asText();
  324. }
  325. if (object.has("photoURL")) {
  326. user.photoURL=object.get("photoURL").asText();
  327. }
  328. if (object.has("password")) {
  329. user.password = object.get("password").asText();
  330. }
  331. if (object.has("oldPassword")) {
  332. oldPassword=object.get("oldPassword").asText();
  333. }
  334. String pass=MongoBase.retrievePassword(user.mail);
  335. Logger.info("mail: "+object.get("mail").asText());
  336. if(pass.equalsIgnoreCase(oldPassword)){
  337. if (MongoBase.updateUser(user)) {
  338. return ok("Edición de usuario completa");
  339. } else {
  340. Logger.info("Error editando.");
  341. return badRequest("Error editando el usuario");
  342. }
  343. }return badRequest("OldPassword incorrecto");
  344. }
  345. @Security.Authenticated(VenueSecured.class)
  346. public static Result updateClient() {
  347. Logger.info("UPDATING USER? ");
  348. JsonNode object = request().body().asJson();
  349. Logger.info(object.toString());
  350. Client client=new Client();
  351. String oldPassword="";
  352. if (object.has("mail")) {
  353. client.setMail(object.get("mail").asText());
  354. }
  355. if (object.has("phone")) {
  356. client.setPhone(object.get("phone").asText());
  357. }
  358. if (object.has("password")) {
  359. client.setPassword(object.get("password").asText());
  360. }
  361. if (object.has("oldPassword")) {
  362. oldPassword=object.get("oldPassword").asText();
  363. }
  364. String pass=MongoBase.retrievePassword(client.getMail());
  365. Logger.info("mail: "+object.get("mail").asText());
  366. if(pass.equalsIgnoreCase(oldPassword)){
  367. if (MongoBase.updateClient(client)) {
  368. return ok("Edición de cliente completa");
  369. } else {
  370. Logger.info("Error editando.");
  371. return badRequest("Error editando el cliente");
  372. }
  373. }return badRequest("OldPassword incorrecto");
  374. }
  375. @Security.Authenticated(SecuredWS.class)
  376. public static Result updateUserWithoutPassword() {
  377. Logger.info("UPDATING USER? ");
  378. JsonNode object = request().body().asJson();
  379. Logger.info(object.toString());
  380. User user=new User();
  381. if (object.has("user")) {
  382. user.user = object.get("user").asText();
  383. }
  384. if (object.has("mail")) {
  385. user.mail = object.get("mail").asText();
  386. }
  387. if (object.has("_id")) {
  388. user._id = new ObjectId(object.get("_id").asText());
  389. }
  390. if (object.has("fbid")) {
  391. user.fbid = object.get("fbid").asText();
  392. }
  393. if (object.has("location")) {
  394. user.location = object.get("location").asText();
  395. }
  396. if (object.has("date")) {
  397. user.date = object.get("date").asText();
  398. }
  399. if (object.has("birthday")) {
  400. user.birthday = object.get("birthday").asText();
  401. }
  402. if (object.has("gender")) {
  403. user.gender = object.get("gender").asText();
  404. }
  405. if (object.has("phone")) {
  406. user.phone = object.get("phone").asText();
  407. }
  408. if (object.has("photoURL")) {
  409. user.photoURL=object.get("photoURL").asText();
  410. }
  411. if (MongoBase.updateUser(user)) {
  412. return ok("Edición de usuario completa");
  413. } else {
  414. Logger.info("Error editando.");
  415. return badRequest("Error editando el usuario");
  416. }
  417. }
  418. @Security.Authenticated(SecuredWS.class)
  419. public static Result createVenueByName() {
  420. JsonNode json = request().body().asJson();
  421. String name = json.findPath("name").asText();
  422. if (name == null) {
  423. return badRequest("Missing parameter [name]. Y tal...");
  424. }
  425. String result = MongoBase.insertVenueByName(name);
  426. if (result == null) {
  427. return badRequest("Error al introducir la venue. Nombre repetido?");
  428. }
  429. return ok(result);
  430. }
  431. @Security.Authenticated(SecuredWS.class)
  432. public static Result createVenueByProvinceAndName() {
  433. JsonNode json = request().body().asJson();
  434. Venue venue=new Venue();
  435. if(json.has("insertedBy")){
  436. venue.insertedBy=json.findPath("insertedBy").asText();
  437. }
  438. if(json.has("province")){
  439. venue.address.province=json.findPath("province").asText();
  440. }
  441. else{
  442. return badRequest("Missing parameter [province]. Y tal...");
  443. }
  444. if (json.has("name")) {
  445. venue.name=json.findPath("name").asText();
  446. }
  447. else{
  448. return badRequest("Missing parameter [name]. Y tal...");
  449. }
  450. String result=MongoBase.insertVenueByProvinceAndName(venue);
  451. if ( result==null) {
  452. return badRequest("Error al introducir la venue.¿¿ Tal vez nombre repetido??");
  453. }
  454. return ok(result);
  455. }
  456. @Security.Authenticated(SecuredWS.class)
  457. public static Result createVenueOnLocation() {
  458. JsonNode json = request().body().asJson();
  459. String atributo = json.findPath("name").asText();
  460. if (atributo == null) {
  461. return badRequest("Missing parameter [name]. Y tal...");
  462. }
  463. Venue v = new Venue();
  464. v.name = atributo;
  465. v.coordinates[0] = (float) json.findPath("coordinates").get(0).doubleValue();
  466. v.coordinates[1] = (float) json.findPath("coordinates").get(1).doubleValue();
  467. atributo = json.findPath("street").asText();
  468. if (atributo != null) {
  469. v.address.street = atributo;
  470. }
  471. atributo = json.findPath("date").asText();
  472. if (atributo != null) {
  473. v.date = atributo;
  474. }
  475. atributo = json.findPath("insertedBy").asText();
  476. if (atributo != null) {
  477. v.insertedBy = atributo;
  478. }
  479. //no cojemos el streetNumber, hay lios con int/string
  480. /*atributo=json.findPath("streetNumber").asText();
  481. if(atributo!=null){
  482. v.address.streetNumber=atributo;
  483. }*/
  484. atributo = json.findPath("insertedBy").asText();
  485. if (atributo != null) {
  486. v.insertedBy = atributo;
  487. }
  488. atributo = json.findPath("city").asText();
  489. if (atributo != null) {
  490. v.address.city = atributo;
  491. }
  492. atributo = json.findPath("province").asText();
  493. if (atributo != null) {
  494. v.address.province = atributo;
  495. }
  496. atributo = json.findPath("postalCode").asText();
  497. if (atributo != null) {
  498. v.address.postalCode = atributo;
  499. }
  500. String retorno = MongoBase.insertVenueOnLocation(v);
  501. if (retorno == null) {
  502. return badRequest("Error al introducir la venue. Nombre repetido?");
  503. }
  504. return ok(retorno);
  505. }
  506. @Security.Authenticated(SecuredWS.class)
  507. public static Result createDishByNameAndVenueId() {
  508. Dish d = new Dish();
  509. JsonNode json = request().body().asJson();
  510. String name = json.findPath("name").asText();
  511. String venueId = json.findPath("venueId").asText();
  512. if (name == null || venueId == null) {
  513. return badRequest("Missing parameter [name]. Y tal...");
  514. }
  515. d.name = name;
  516. d.venueId = new ObjectId(venueId);
  517. String atributo = json.findPath("date").asText();
  518. if (atributo != null) {
  519. d.date = atributo;
  520. }
  521. atributo = json.findPath("insertedBy").asText();
  522. if (atributo != null) {
  523. d.insertedBy = atributo;
  524. }
  525. d.photoURL = "http://toptasting.s3.amazonaws.com/sinImagenDish.png";
  526. String dishId = MongoBase.insertDish(d);
  527. if (dishId == null) {
  528. return badRequest("Error al introducir el dish.");
  529. }
  530. return ok(dishId);
  531. }
  532. @Security.Authenticated(SecuredWS.class)
  533. public static Result createDish() {
  534. Dish d = new Dish();
  535. JsonNode json = request().body().asJson();
  536. String name = json.findPath("name").asText();
  537. String venueId = json.findPath("venueId").asText();
  538. if (name == null || venueId == null) {
  539. return badRequest("Missing parameter [name]. Y tal...");
  540. }
  541. if(json.hasNonNull("venueName")){
  542. d.venueName=json.findPath("venueName").asText();
  543. }
  544. if(json.hasNonNull("city")){
  545. d.city=json.findPath("city").asText();
  546. }
  547. if(json.hasNonNull("province")){
  548. d.province=json.findPath("province").asText();
  549. }
  550. if(json.hasNonNull("coordinates")){
  551. double[] coor = new double[2];
  552. if(json.findPath("coordinates").hasNonNull(0)){
  553. coor[0] = (float) json.findPath("coordinates").get(0).doubleValue();
  554. }
  555. if(json.findPath("coordinates").hasNonNull(1)){
  556. coor[1] = (float)json.findPath("coordinates").get(1).doubleValue();
  557. }
  558. d.coordinates=coor;
  559. }
  560. d.name = name;
  561. d.venueId = new ObjectId(venueId);
  562. String atributo = json.findPath("date").asText();
  563. if (atributo != null) {
  564. d.date = atributo;
  565. }
  566. atributo = json.findPath("insertedBy").asText();
  567. if (atributo != null) {
  568. d.insertedBy = atributo;
  569. }
  570. d.photoURL = "http://toptasting.s3.amazonaws.com/sinImagenDish.png";
  571. String dishId = MongoBase.insertDish(d);
  572. if (dishId == null) {
  573. return badRequest("Error al introducir el dish.");
  574. }
  575. return ok(dishId);
  576. }
  577. @Security.Authenticated(SecuredWS.class)
  578. public static Result findDishes() {
  579. JsonNode json = request().body().asJson();
  580. String searchText = json.findPath("search").asText();
  581. if (searchText == null) {
  582. return badRequest("Missing search value");
  583. }
  584. String retorno = MongoBase.findDishes(searchText);
  585. if (retorno == null) {
  586. return badRequest("Error en la busqueda");
  587. }
  588. return ok(retorno);
  589. }
  590. @Security.Authenticated(SecuredWS.class)
  591. public static Result findDishesByType() {
  592. JsonNode json = request().body().asJson();
  593. Iterator<JsonNode> i = json.findPath("search").elements();
  594. if (i == null) {
  595. return badRequest("Missing search value");
  596. }
  597. ArrayList<String> al = new ArrayList<String>();
  598. while (i.hasNext()) {
  599. al.add(i.next().asText());
  600. }
  601. String retorno = MongoBase.findDishesByType(al);
  602. if (retorno == null) {
  603. return badRequest("Error en la busqueda");
  604. }
  605. return ok(retorno);
  606. }
  607. @Security.Authenticated(SecuredWS.class)
  608. public static Result findDishesByVenueId() {
  609. JsonNode json = request().body().asJson();
  610. String venueId = json.findPath("venueId").asText();
  611. if (venueId == null) {
  612. return badRequest("Missing parameter [venueId]");
  613. }
  614. String retorno = MongoBase.findDishesByVenueId(venueId);
  615. if (retorno == null) {
  616. return badRequest("Error en la busqueda");
  617. }
  618. Logger.info("Solicitado FindDishesByVenueId");
  619. return ok(retorno);
  620. }
  621. @Security.Authenticated(SecuredWS.class)
  622. public static Result findPublicationsByVenueId() {
  623. JsonNode json = request().body().asJson();
  624. String venueId = json.findPath("venueId").asText();
  625. if (venueId == null) {
  626. return badRequest("Missing parameter [venueId]");
  627. }
  628. String retorno = MongoBase.findPublicationsByVenueId(venueId);
  629. if (retorno == null) {
  630. return badRequest("Error en la busqueda");
  631. }
  632. Logger.info("Solicitado FindPublicationsByVenueId");
  633. return ok(retorno);
  634. }
  635. @Security.Authenticated(SecuredWS.class)
  636. public static Result findDrafts() {
  637. JsonNode json = request().body().asJson();
  638. String userId = json.findPath("userId").asText();
  639. Logger.info("Buscando drafts para el userId "+userId);
  640. if (userId == null) {
  641. Logger.info("Se trató de buscar drafts sin user ID");
  642. return badRequest("Missing parameter [userId].");
  643. }
  644. String drafts = MongoBase.findDrafts(new ObjectId(userId));
  645. if (drafts == null) {
  646. Logger.info("Ha habido un error buscando drafts");
  647. return badRequest("Bad Request");
  648. }
  649. Logger.info(drafts);
  650. return ok(drafts);
  651. }
  652. @Security.Authenticated(SecuredWS.class)
  653. public static Result findFavVenuesByUserId() {
  654. JsonNode json = request().body().asJson();
  655. String userId = json.findPath("userId").asText();
  656. if (userId == null) {
  657. Logger.info("Se trató de buscar drafts sin user ID");
  658. return badRequest("Missing parameter [userId].");
  659. }
  660. String venues = MongoBase.findFavVenuesByUserId(userId);
  661. if (venues == null) {
  662. Logger.info("Ha habido un error buscando venues favoritas");
  663. return badRequest("Bad Request");
  664. }
  665. return ok(venues);
  666. }
  667. @Security.Authenticated(SecuredWS.class)
  668. public static Result findFavDishesByUserId() {
  669. JsonNode json = request().body().asJson();
  670. String userId = json.findPath("userId").asText();
  671. if (userId == null) {
  672. Logger.info("Se trató de buscar platos sin user ID");
  673. return badRequest("Missing parameter [userId].");
  674. }
  675. String venues = MongoBase.findFavDishesByUserId(userId);
  676. if (venues == null) {
  677. Logger.info("Ha habido un error buscando platos favoritas");
  678. return badRequest("Bad Request");
  679. }
  680. return ok(venues);
  681. }
  682. @Security.Authenticated(SecuredWS.class)
  683. public static Result findFavPublicationsByUserId() {
  684. JsonNode json = request().body().asJson();
  685. String userId = json.findPath("userId").asText();
  686. if (userId == null) {
  687. Logger.info("Se trató de buscar publicaciones sin user ID");
  688. return badRequest("Missing parameter [userId].");
  689. }
  690. String venues = MongoBase.findFavPublicationsByUserId(userId);
  691. if (venues == null) {
  692. Logger.info("Ha habido un error buscando publicaciones favoritas");
  693. return badRequest("Bad Request");
  694. }
  695. Logger.info("VENUE PUBLICATIONS: "+venues);
  696. return ok(venues);
  697. }
  698. @Security.Authenticated(SecuredWS.class)
  699. public static Result findDishWithId(String dishId) {
  700. if (dishId == null) {
  701. return badRequest("Missing parameter [dishId].");
  702. }
  703. String dish = MongoBase.findDishJSONById(dishId);
  704. if (dish == null) {
  705. return badRequest("Bad Request");
  706. }
  707. return ok(dish);
  708. }
  709. @Security.Authenticated(SecuredWS.class)
  710. public static Result findDishById() {
  711. JsonNode json = request().body().asJson();
  712. String dishId;
  713. if(json.has("dishId")){
  714. dishId = json.findPath("dishId").asText();
  715. }else{
  716. return badRequest("Faltan parámetros [dishId]");
  717. }
  718. String dish = MongoBase.findDishJSONById(dishId);
  719. if (dish == null) {
  720. return badRequest("Bad Request");
  721. }
  722. return ok(dish);
  723. }
  724. @Security.Authenticated(SecuredWS.class)
  725. public static Result findNearbyVenues() {
  726. JsonNode json = request().body().asJson();
  727. float lat = (float) json.findPath("coordinates").get(0).doubleValue();
  728. float lon = (float) json.findPath("coordinates").get(1).doubleValue();
  729. double dis = json.findPath("dis").doubleValue();
  730. String retorno = MongoBase.findNearbyVenues(lat, lon, dis);
  731. Logger.info("dis: " + dis + " lat:" + lat + " lon:" + lon);
  732. if (retorno == null) {
  733. return badRequest("Error en la busqueda");
  734. }
  735. return ok(retorno);
  736. }
  737. @Security.Authenticated(SecuredWS.class)
  738. public static Result findNearbyVenuesByName() {
  739. JsonNode json = request().body().asJson();
  740. float lat = (float) json.findPath("coordinates").get(0).doubleValue();
  741. float lon = (float) json.findPath("coordinates").get(1).doubleValue();
  742. double dis = json.findPath("dis").doubleValue();
  743. String search = json.findPath("search").asText();
  744. String retorno = MongoBase.findNearbyVenuesByName(lat, lon, dis, search);
  745. if (retorno == null) {
  746. return badRequest("Error en la busqueda");
  747. }
  748. return ok(retorno);
  749. }
  750. @Security.Authenticated(SecuredWS.class)
  751. public static Result findVenuesByProvince() {
  752. JsonNode json = request().body().asJson();
  753. String location = json.findPath("location").asText();
  754. String retorno = MongoBase.findVenuesByProvince(location);
  755. if (retorno == null) {
  756. return badRequest("Error en la busqueda");
  757. }
  758. return ok(retorno);
  759. }
  760. @Security.Authenticated(SecuredWS.class)
  761. public static Result findVenuesByProvinceAndName() {
  762. JsonNode json = request().body().asJson();
  763. String location = json.findPath("location").asText();
  764. String search = json.findPath("search").asText();
  765. String retorno = MongoBase.findVenuesByProvinceAndName(location, search);
  766. if (retorno == null) {
  767. return badRequest("Error en la busqueda");
  768. }
  769. return ok(retorno);
  770. }
  771. public static Result findVenuesByCity() {
  772. JsonNode json = request().body().asJson();
  773. System.out.println("buscando venues por ciudad: "+json);
  774. String location = json.findPath("location").asText();
  775. String retorno = MongoBase.findVenuesByCity(location);
  776. if (retorno == null) {
  777. return badRequest("Error en la busqueda");
  778. }
  779. return ok(retorno);
  780. }
  781. public static Result findVenuesByCityAndName() {
  782. JsonNode json = request().body().asJson();
  783. String location = json.findPath("location").asText();
  784. String search = json.findPath("search").asText();
  785. String retorno = MongoBase.findVenuesByCityAndName(location, search);
  786. if (retorno == null) {
  787. return badRequest("Error en la busqueda");
  788. }
  789. return ok(retorno);
  790. }
  791. @Security.Authenticated(SecuredWS.class)
  792. public static Result findVenuesById() {
  793. JsonNode json = request().body().asJson();
  794. List<String> venueIds = json.findValuesAsText("venueIds");
  795. String retorno = MongoBase.findVenuesById(venueIds);
  796. if (retorno == null) {
  797. return badRequest("Error en la busqueda");
  798. }
  799. return ok(retorno);
  800. }
  801. @Security.Authenticated(SecuredWS.class)
  802. public static Result findNearbyDishesByType() {
  803. JsonNode json = request().body().asJson();
  804. float[] coor = new float[2];
  805. System.out.println("json recibido "+json);
  806. coor[0] = (float) json.findPath("coordinates").get(0).doubleValue();
  807. coor[1] = (float) json.findPath("coordinates").get(1).doubleValue();
  808. double dis = json.findPath("dis").doubleValue();
  809. Iterator<JsonNode> i = json.findPath("search").elements();
  810. Logger.info("dis: " + dis + " lat:" + coor[0] + " lon:" + coor[1]);
  811. if (i == null) {
  812. return badRequest("Missing search value");
  813. }
  814. ArrayList<String> al = new ArrayList<String>();
  815. while (i.hasNext()) {
  816. al.add(i.next().asText());
  817. }
  818. String retorno = MongoBase.findNearbyDishesByType(coor, dis, al);
  819. if (retorno == null) {
  820. return badRequest("Error en la busqueda");
  821. }
  822. return ok(retorno);
  823. }
  824. @Security.Authenticated(SecuredWS.class)
  825. public static Result findDishesByCityAndType() {
  826. JsonNode json = request().body().asJson();
  827. String location = json.findPath("location").asText();
  828. Iterator<JsonNode> i = json.findPath("search").elements();
  829. if (i == null) {
  830. return badRequest("Missing search value");
  831. }
  832. ArrayList<String> al = new ArrayList<String>();
  833. while (i.hasNext()) {
  834. al.add(i.next().asText());
  835. }
  836. String retorno = MongoBase.findDishesByCityAndType(location, al);
  837. if (retorno == null) {
  838. return badRequest("Error en la busqueda");
  839. }
  840. return ok(retorno);
  841. }
  842. @Security.Authenticated(SecuredWS.class)
  843. public static Result findDishesByProvinceAndType() {
  844. JsonNode json = request().body().asJson();
  845. String location = json.findPath("location").asText();
  846. Iterator<JsonNode> i = json.findPath("search").elements();
  847. if (i == null) {
  848. return badRequest("Missing search value");
  849. }
  850. ArrayList<String> al = new ArrayList<String>();
  851. while (i.hasNext()) {
  852. al.add(i.next().asText());
  853. }
  854. String retorno = MongoBase.findDishesByProvinceAndType(location, al);
  855. if (retorno == null) {
  856. return badRequest("Error en la busqueda");
  857. }
  858. return ok(retorno);
  859. }
  860. public static Result findNearbyDishes() {
  861. JsonNode json = request().body().asJson();
  862. Logger.info(json.toString());
  863. float[] coor = new float[2];
  864. coor[0] = (float) json.findPath("coordinates").get(0).doubleValue();
  865. coor[1] = (float) json.findPath("coordinates").get(1).doubleValue();
  866. double dis = json.findPath("dis").doubleValue();
  867. Logger.info("dis: " + dis + " lat:" + coor[0] + "lon:" + coor[1]);
  868. String retorno = MongoBase.findNearbyDishes(coor, dis);
  869. if (retorno == null) {
  870. return badRequest("Error en la busqueda");
  871. }
  872. return ok(retorno);
  873. }
  874. @Security.Authenticated(SecuredWS.class)
  875. public static Result findDishesByCity() {
  876. JsonNode json = request().body().asJson();
  877. String location = json.findPath("location").asText();
  878. String retorno = MongoBase.findDishesByCity(location);
  879. if (retorno == null) {
  880. return badRequest("Error en la busqueda");
  881. }
  882. return ok(retorno);
  883. }
  884. @Security.Authenticated(SecuredWS.class)
  885. public static Result findDishesByProvince() {
  886. JsonNode json = request().body().asJson();
  887. String location = json.findPath("location").asText();
  888. String retorno = MongoBase.findDishesByProvince(location);
  889. if (retorno == null) {
  890. return badRequest("Error en la busqueda");
  891. }
  892. return ok(retorno);
  893. }
  894. @Security.Authenticated(AdminSecured.class)
  895. public static Result consolidate() {
  896. MongoBase.consolidate();
  897. flash("success", "Consolidado");
  898. return redirect(
  899. routes.AdminPanel.blank()
  900. );
  901. }
  902. public static Result makeSearchNames() {
  903. MongoBase.makeSearchNames();
  904. flash("success", "Consolidado");
  905. return redirect(
  906. routes.AdminPanel.blank()
  907. );
  908. }
  909. @Security.Authenticated(AdminSecured.class)
  910. public static Result sendGCMMessage() {
  911. Publication pub=new Publication();
  912. pub.setLongDescription(" Lorem ipsum dolor sit amet");
  913. pub.setInterests(Arrays.asList(new String[]{"jornadas", "gochu"}));
  914. pub.setVenueId("522ecb6003647294e95cfe46");
  915. pub.setTitle("Pelazo abre en la pola");
  916. pub.setVenueLocation("pola de lena");
  917. MongoBase.createPublication(pub);
  918. return ok();
  919. }
  920. @Security.Authenticated(AdminSecured.class)
  921. public static Result readXML() {
  922. /* final int NAME=0;
  923. final int ADDRESS=1;
  924. final int CITY=2;
  925. final int POSTAL_CODE=4;
  926. final int BAJA=9;
  927. final int SUB_ACTIVITY=12;
  928. try
  929. {
  930. FileInputStream file = new FileInputStream(new File("/Users/marcosflorezsierra/Downloads/Establecimientos.xls"));
  931. //Create Workbook instance holding reference to .xlsx file
  932. HSSFWorkbook workbook = new HSSFWorkbook(file);
  933. //Get first/desired sheet from the workbook
  934. HSSFSheet sheet = workbook.getSheetAt(0);
  935. //Iterate through each rows one by one
  936. Iterator<Row> rowIterator = sheet.iterator();
  937. Venue venue;
  938. while (rowIterator.hasNext())
  939. {
  940. Row row = rowIterator.next();
  941. if(row.getCell(BAJA).getStringCellValue()!=null&&row.getCell(BAJA).getStringCellValue().length()>0){
  942. venue=new Venue();
  943. if(row.getCell(CITY).getStringCellValue().length()<1){
  944. venue.address.city=row.getCell(CITY+1).getStringCellValue();
  945. }else{
  946. venue.address.city=row.getCell(CITY).getStringCellValue();
  947. }
  948. venue.address.street=row.getCell(ADDRESS).getStringCellValue().toLowerCase();
  949. venue.address.postalCode=row.getCell(POSTAL_CODE).getStringCellValue();
  950. venue.address.province="Asturias";
  951. venue.name=row.getCell(NAME).getStringCellValue();
  952. if(venue.name.contains(", ")){
  953. String partes[]=venue.name.split(", ");
  954. venue.name=partes[1]+" "+partes[0];
  955. }else if(venue.name.contains(" ,")){
  956. String partes[]=venue.name.split(" ,");
  957. venue.name=partes[1]+" "+partes[0];
  958. }
  959. venue.name=venue.name.toLowerCase();
  960. venue.types.add("restaurante");
  961. String subActivity=row.getCell(SUB_ACTIVITY).getStringCellValue();
  962. if(subActivity!=null&&subActivity.length()>0){
  963. venue.types.add(subActivity.toLowerCase());
  964. }
  965. System.out.println(venue);
  966. }
  967. }
  968. file.close();
  969. }
  970. catch (Exception e)
  971. {
  972. e.printStackTrace();
  973. }
  974. */
  975. MongoBase.insertVenuesXML();
  976. return ok();
  977. }
  978. /*
  979. public static F.Promise<Result> sendGCMMessage() {
  980. JsonNode responseJson;
  981. final JsonNode json = request().body().asJson();
  982. String mail = json.findPath("mail").asText();
  983. JsonObject parameters=new JsonObject();
  984. JsonArray direcciones=new JsonArray();
  985. JsonObject data=new JsonObject();
  986. data.add("mensaje",new JsonPrimitive("yes muy malu al DOTA2"));
  987. direcciones.add(new JsonPrimitive("pepito"));
  988. parameters.add("registration_ids",direcciones);
  989. String token = json.findPath("userToken").asText();
  990. String auth = "AIzaSyD_v66gszhbLwY0iBGCi37Tsb467ruzs0o";
  991. WSRequestHolder messageRequest = WS.url("https://android.googleapis.com/gcm/send").setAuth("key",auth).setContentType("application/json");
  992. F.Promise<Result> resultado = messageRequest.post(parameters.getAsString()).map(
  993. new F.Function<WS.Response, Result>() {
  994. @Override
  995. public Result apply(WS.Response response) throws Throwable {
  996. switch(response.getStatus()){
  997. case 200: Logger.info("GCM messages correctos");
  998. flash("success", "Envíado!");
  999. return redirect(
  1000. routes.AdminPanel.blank()
  1001. );
  1002. case 400:
  1003. flash("error", "Mal JSON");
  1004. return redirect(
  1005. routes.AdminPanel.blank()
  1006. );
  1007. case 401:
  1008. flash("error", "Mala Autenticación!");
  1009. return redirect(
  1010. routes.AdminPanel.blank()
  1011. );
  1012. default: flash("error", "ROMPISTE GOOGLE!!!! BOOOM!");
  1013. return redirect(
  1014. routes.AdminPanel.blank()
  1015. );
  1016. }
  1017. }
  1018. });
  1019. return resultado;
  1020. }
  1021. */
  1022. @Security.Authenticated(AdminSecured.class)
  1023. public static Result corregirTipos() {
  1024. MongoBase.correct();
  1025. flash("success", "Corregidos");
  1026. return redirect(
  1027. routes.AdminPanel.blank()
  1028. );
  1029. }
  1030. @Security.Authenticated(AdminSecured.class)
  1031. public static Result recalculatePoints() {
  1032. MongoBase.recalculatePoints();
  1033. flash("success", "Recalculado");
  1034. return redirect(
  1035. routes.AdminPanel.blank()
  1036. );
  1037. }
  1038. @Security.Authenticated(SecuredWS.class)
  1039. public static Result findVenues() {
  1040. JsonNode json = request().body().asJson();
  1041. String searchText = json.findPath("search").asText();
  1042. if (searchText == null) {
  1043. return badRequest("Missing search value");
  1044. }
  1045. String retorno = MongoBase.findVenues(searchText);
  1046. if (retorno == null) {
  1047. return badRequest("Error en la busqueda");
  1048. }
  1049. return ok(retorno);
  1050. }
  1051. @Security.Authenticated(Secured.class)
  1052. public static Result findMoreVenues() {
  1053. JsonNode json = request().body().asJson();
  1054. String searchText = json.findPath("search").asText();
  1055. String attempt = json.findPath("attempt").asText();
  1056. if (searchText == null) {
  1057. return badRequest("Missing search value");
  1058. }
  1059. String retorno = MongoBase.findVenues(searchText);
  1060. if (retorno == null) {
  1061. return badRequest("Error en la busqueda");
  1062. }
  1063. return ok(retorno);
  1064. }
  1065. @Security.Authenticated(Secured.class)
  1066. public static Result addGcmId() {
  1067. JsonNode json = request().body().asJson();
  1068. String gcmId=null;
  1069. String userId=null;
  1070. if(json.has("gcmId")){
  1071. gcmId = json.findPath("gcmId").asText();
  1072. }else{
  1073. return badRequest("Faltan parámetros [gcmId]");
  1074. }
  1075. if(json.has("userId")){
  1076. userId = json.findPath("userId").asText();
  1077. }else{
  1078. return badRequest("Faltan parámetros [userId]");
  1079. }
  1080. String retorno = MongoBase.addGcmId(userId, gcmId);
  1081. if (retorno != null) {
  1082. return badRequest("Error añadiendo el gcmId");
  1083. }
  1084. return ok();
  1085. }
  1086. @Security.Authenticated(Secured.class)
  1087. public static Result addUserFollower() {
  1088. JsonNode json = request().body().asJson();
  1089. String followerId=null;
  1090. String followedId=null;
  1091. String followerName=null;
  1092. String followedName=null;
  1093. if(json.has("followerId")){
  1094. followerId = json.findPath("followerId").asText();
  1095. }else{
  1096. return badRequest("Faltan parámetros [followerId]");
  1097. }
  1098. if(json.has("followerName")){
  1099. followerName = json.findPath("followerName").asText();
  1100. }else{
  1101. return badRequest("Faltan parámetros [followerName]");
  1102. }
  1103. if(json.has("followedId")){
  1104. followedId = json.findPath("followedId").asText();
  1105. }else{
  1106. return badRequest("Faltan parámetros [followedId]");
  1107. }
  1108. if(json.has("followedName")){
  1109. followedName = json.findPath("followedName").asText();
  1110. }else{
  1111. return badRequest("Faltan parámetros [followedName]");
  1112. }
  1113. String retorno = MongoBase.addUserFollower(followerId, followerName,followedId,followedName);
  1114. if (retorno != null) {
  1115. return badRequest("Error añadiendo follower");
  1116. }
  1117. return ok();
  1118. }
  1119. @Security.Authenticated(Secured.class)
  1120. public static Result removeUserFollower() {
  1121. JsonNode json = request().body().asJson();
  1122. String followerId=null;
  1123. String followedId=null;
  1124. if(json.has("followerId")){
  1125. followerId = json.findPath("followerId").asText();
  1126. }else{
  1127. return badRequest("Faltan parámetros [followerId]");
  1128. }
  1129. if(json.has("followedId")){
  1130. followedId = json.findPath("followedId").asText();
  1131. }else{
  1132. return badRequest("Faltan parámetros [followedId]");
  1133. }
  1134. String retorno = MongoBase.removeUserFollower(followerId, followedId);
  1135. if (retorno != null) {
  1136. return badRequest("Error añadiendo follower");
  1137. }
  1138. return ok();
  1139. }
  1140. @Security.Authenticated(Secured.class)
  1141. public static Result addVenueFollower() {
  1142. JsonNode json = request().body().asJson();
  1143. String followerName=null;
  1144. String venueId=null;
  1145. String venueName=null;
  1146. String followerId=null;
  1147. if(json.has("followerName")){
  1148. followerName = json.findPath("followerName").asText();
  1149. }else{
  1150. return badRequest("Faltan parámetros [followerName]");
  1151. }
  1152. if(json.has("followerId")){
  1153. followerId = json.findPath("followerId").asText();
  1154. }
  1155. else{
  1156. return badRequest("Faltan parámetros [followerId]");
  1157. }
  1158. if(json.has("venueId")){
  1159. venueId = json.findPath("venueId").asText();
  1160. }else{
  1161. return badRequest("Faltan parámetros [venueId]");
  1162. }
  1163. if(json.has("venueName")){
  1164. venueName = json.findPath("venueName").asText();
  1165. }else{
  1166. return badRequest("Faltan parámetros [venueName]");
  1167. }
  1168. String retorno = MongoBase.addVenueFollower(followerId,followerName,venueId, venueName);
  1169. if (retorno != null) {
  1170. return badRequest("Error añadiendo follower");
  1171. }
  1172. return ok();
  1173. }
  1174. @Security.Authenticated(Secured.class)
  1175. public static Result removeVenueFollower() {
  1176. JsonNode json = request().body().asJson();
  1177. String venueId=null;
  1178. String followerId=null;
  1179. if(json.has("followerId")){
  1180. followerId = json.findPath("followerId").asText();
  1181. }
  1182. else{
  1183. return badRequest("Faltan parámetros [followerId]");
  1184. }
  1185. if(json.has("venueId")){
  1186. venueId = json.findPath("venueId").asText();
  1187. }else{
  1188. return badRequest("Faltan parámetros [venueId]");
  1189. }
  1190. String retorno = MongoBase.removeVenueFollower(followerId, venueId);
  1191. if (retorno != null) {
  1192. return badRequest("Error eliminando follower");
  1193. }
  1194. return ok();
  1195. }
  1196. @Security.Authenticated(Secured.class)
  1197. public static Result addInterestFollower() {
  1198. JsonNode json = request().body().asJson();
  1199. String followerId=null;
  1200. String followerName=null;
  1201. String interestName=null;
  1202. if(json.has("followerId")){
  1203. followerId = json.findPath("followerId").asText();
  1204. }else{
  1205. return badRequest("Faltan parámetros [followerId]");
  1206. }
  1207. if(json.has("followerName")){
  1208. followerName = json.findPath("followerName").asText();
  1209. }else{
  1210. return badRequest("Faltan parámetros [followerName]");
  1211. }
  1212. if(json.has("interestName")){
  1213. interestName = json.findPath("interestName").asText();
  1214. }else{
  1215. return badRequest("Faltan parámetros [interestName]");
  1216. }
  1217. String retorno = MongoBase.addInterest(followerId, followerName,interestName);
  1218. if (retorno != null) {
  1219. return badRequest("Error añadiendo follower");
  1220. }
  1221. return ok();
  1222. }
  1223. @Security.Authenticated(Secured.class)
  1224. public static Result removeInterestFollower() {
  1225. JsonNode json = request().body().asJson();
  1226. String followerId=null;
  1227. String followerName=null;
  1228. String interestName=null;
  1229. if(json.has("followerId")){
  1230. followerId = json.findPath("followerId").asText();
  1231. }else{
  1232. return badRequest("Faltan parámetros [followerId]");
  1233. }
  1234. if(json.has("interestName")){
  1235. interestName = json.findPath("interestName").asText();
  1236. }else{
  1237. return badRequest("Faltan parámetros [interestName]");
  1238. }
  1239. String retorno = MongoBase.removeInterest(followerId, interestName);
  1240. if (retorno != null) {
  1241. return badRequest("Error haciendo unfollow");
  1242. }
  1243. return ok();
  1244. }
  1245. @Security.Authenticated(Secured.class)
  1246. public static Result addDishToFavourites() {
  1247. JsonNode json = request().body().asJson();
  1248. String userId=null;
  1249. String userName=null;
  1250. String venueName=null;
  1251. String dishId=null;
  1252. if(json.has("userId")){
  1253. userId = json.findPath("userId").asText();
  1254. Logger.error(userId);
  1255. }else{
  1256. return badRequest("Faltan parámetros [userId]");
  1257. }
  1258. if(json.has("userName")){
  1259. userName = json.findPath("userName").asText();
  1260. }else{
  1261. return badRequest("Faltan parámetros [userName]");
  1262. }
  1263. if(json.has("venueName")){
  1264. venueName = json.findPath("venueName").asText();
  1265. }else{

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