Advertisement
eldieck

EventCreation

Feb 23rd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. /**
  2.      * Form processing the event creation page.
  3.      *
  4.      * @param model
  5.      *            is being passed in as well as the form information
  6.      * @return the index page.
  7.      */
  8.     @RequestMapping(value = "/createEvent", method = RequestMethod.POST)
  9.     public String createEventPost(Model model, @RequestParam("name") String name, @RequestParam("date") String dateStr,
  10.             @RequestParam("description") String description) {
  11.  
  12.         if (name != null && name.matches(".{2,}") && description != null && description.matches(".{2,}")
  13.                 && dateStr != null && dateStr.matches("[0-9]{2}/[0-9]{2}/[0-9]{4}")) {
  14.  
  15.             String[] datePart = dateStr.split("/");
  16.  
  17.             // TODO Add 1900 as a year offset constant for the deprecated date
  18.             // constructor
  19.             Date eventDate = new Date(Integer.parseInt(datePart[2]) - 1900, Integer.parseInt(datePart[0]) - 1,
  20.                     Integer.parseInt(datePart[1]));
  21.             Date currentDate = new Date(System.currentTimeMillis());
  22.  
  23.             if (eventDate.compareTo(currentDate) < 0) {
  24.  
  25.                 HashMap<String, String> errors = new HashMap<String, String>();
  26.  
  27.                 errors.put("date", "Error. The event's date must be after the current date.");
  28.  
  29.                 model.addAttribute("errors", errors);
  30.  
  31.                 return "createEvent";
  32.             }
  33.  
  34.             // TODO Add the event creator (User)
  35.             Event event = new Event(name, eventDate, description);
  36.  
  37.             try {
  38.                 getEventDao().addEvent(event);
  39.             } catch (Exception e) {
  40.                 e.printStackTrace();
  41.             }
  42.  
  43.             return "index";
  44.  
  45.         } else {
  46.  
  47.             HashMap<String, String> errors = new HashMap<String, String>();
  48.  
  49.             if (name == null || !name.matches(".{2,}")) {
  50.                 errors.put("name", "Error in the input for the event name.");
  51.             }
  52.  
  53.             if (description == null || !description.matches(".{2,}")) {
  54.                 errors.put("description", "Error in the input for the event description.");
  55.             }
  56.  
  57.             if (dateStr == null || !dateStr.matches("[0-9]{2}/[0-9]{2}/[0-9]{4}")) {
  58.                 errors.put("date", "Error in the input for the event's date.");
  59.             }
  60.  
  61.             model.addAttribute("errors", errors);
  62.  
  63.             return "createEvent";
  64.         }
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement