/calendar/app/controllers/Locations.java

https://github.com/ese-unibe-ch/ese2011-team4 · Java · 117 lines · 99 code · 18 blank · 0 comment · 13 complexity · cbf403b8acf4e57cfa9c6ca06aa5a4e2 MD5 · raw file

  1. package controllers;
  2. import java.util.List;
  3. import org.joda.time.DateTime;
  4. import org.joda.time.format.DateTimeFormat;
  5. import org.joda.time.format.DateTimeFormatter;
  6. import play.Logger;
  7. import play.data.validation.Check;
  8. import play.data.validation.Match;
  9. import play.data.validation.Required;
  10. import play.data.validation.Validation;
  11. import play.mvc.Controller;
  12. import play.mvc.With;
  13. import models.*;
  14. @With(Secure.class)
  15. public class Locations extends Controller {
  16. public static void index() {
  17. List<Location> locations = Location.all().fetch();
  18. render(locations);
  19. }
  20. public static void add() {
  21. render();
  22. }
  23. public static void show(Long id) {
  24. Location location = Location.findById(id);
  25. assert location != null;
  26. User connectedUser = User.find("byEmail", Security.connected()).first();
  27. List<Event> events = location.getVisibleUpcomingEvents(connectedUser);
  28. long numberOfEvents = location.numberOfAllUpcomingEvents();
  29. render(location, events, numberOfEvents);
  30. }
  31. public static void edit(Long locationId) {
  32. Location location = Location.findById(locationId);
  33. render(location);
  34. }
  35. public static void delete(Long locationId) {
  36. Location location = Location.findById(locationId);
  37. List<Event> events = location.getAllEvents();
  38. List<User> users = User.find("byAddress", location).fetch();
  39. for(Event event: events) {
  40. event.location = null;
  41. event.validateAndSave();
  42. }
  43. for(User user: users) {
  44. user.address = null;
  45. user.validateAndSave();
  46. }
  47. location.delete();
  48. index();
  49. }
  50. public static void update( Long locationId,
  51. @Required String street,
  52. @Required String num,
  53. @Required String city,
  54. @Required String country,
  55. @Required String pincode) {
  56. Location location = Location.findById(locationId);
  57. assert location != null;
  58. location.street = street;
  59. location.num = num;
  60. location.city = city;
  61. location.country = country;
  62. location.pincode = pincode;
  63. if(location.validateAndSave()) {
  64. show(locationId);
  65. } else {
  66. for(play.data.validation.Error e : Validation.errors())
  67. Logger.error(e.message());
  68. params.flash();
  69. validation.keep();
  70. edit(locationId);
  71. }
  72. }
  73. public static void addLocation( String street,
  74. String num,
  75. String city,
  76. String country,
  77. String pincode) {
  78. Location existingLocation = Location.find(street, num, city, country, pincode);
  79. if(existingLocation == null) {
  80. Location location = new Location();
  81. location.street = street;
  82. location.num = num;
  83. location.city = city;
  84. location.country = country;
  85. location.pincode = pincode;
  86. if (location.validateAndSave())
  87. show(location.id);
  88. else {
  89. for(play.data.validation.Error e : Validation.errors())
  90. Logger.error(e.message());
  91. params.flash();
  92. validation.keep();
  93. add();
  94. }
  95. } else {
  96. show(existingLocation.id);
  97. }
  98. }
  99. }