Example #1
0
  /**
   * Validates the login. Writes the isValid flag into the session along with the current user.
   *
   * @return true if OK, false if there's a problem
   */
  private boolean validateLogin(
      HttpSession session, HttpServletRequest req, HttpServletResponse res) throws Exception {

    // Creates a user database access bean.
    UserManager userManager = new UserManager();
    // (no setSession() here, since user may not exist yet)

    // Validates the login
    String username = req.getParameter("Username");
    String password = req.getParameter("Password");
    boolean isValid = userManager.isValidUser(username, password);
    boolean isAdmin = userManager.isAdmin(username);

    // To allow bootstrapping the system, if there are no users
    // yet, set this session valid, and grant admin privileges.
    if (userManager.getRecords().isEmpty()) {
      isValid = true;
      isAdmin = true;
    }

    if (isValid) {
      // Writes User object and validity flag to the session
      session.setAttribute("user", new User(username, password, isAdmin));
      session.setAttribute("isValid", new Boolean(isValid));
    } else {
      Util.putMessagePage(res, "Invalid user or password");
      return false;
    }
    return isValid;
  }
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {

    log.info("########## START EDIT EVENT POST ###########");

    // Check for valid user session
    lpo.User user = UserManager.GetUser();

    if (user == null) resp.sendRedirect("WelcomePage.jsp");

    // get event from datastore
    String eventKey = req.getParameter("k");

    // pull the event object out
    lpo.Event event = EventManager.GetEvent(eventKey);

    String eventName = req.getParameter("eventName").trim();
    String description = req.getParameter("description").trim();

    boolean formIsComplete = true;

    int minParticipants = 0;
    try {
      minParticipants = Integer.parseInt(req.getParameter("minParticipants"));
    } catch (Exception e) {
      log.info("ERROR PARSING MIN PARTICIPANTS: " + e.toString());
      formIsComplete = false;
    }

    log.info("FORM VARS : " + eventName + " " + description + " " + minParticipants);

    if (eventName == null
        || eventName.isEmpty()
        || description == null
        || description.isEmpty()
        || minParticipants < 1) {

      formIsComplete = false;
    }

    if (formIsComplete) {

      // create event and populate available attributes
      event.setName(eventName);
      event.setDescription(description);
      event.setMinParticipants(minParticipants);

      // persist to database
      DataAccessManager.UpdateEvent(event);

      resp.sendRedirect("/Menu");
    } else {
      // reshow the same jsp page with error message :
      req.getRequestDispatcher("/WEB-INF/EditEvent.jsp").forward(req, resp);
    }

    return;
  }
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    log.info("########## START EDIT EVENT GET ###########");

    // Check for valid user session
    lpo.User user = UserManager.GetUser();

    if (user == null) resp.sendRedirect("WelcomePage.jsp");

    // get event from datastore
    String eventKey = req.getParameter("k");

    // pull the event object out
    lpo.Event event = EventManager.GetEvent(eventKey);

    req.setAttribute("event", event);

    // build display page
    req.getRequestDispatcher("/WEB-INF/EditEvent.jsp").forward(req, resp);
  }
  /**
   * Displays a given Meeting page for a HTTP Get, or creates a new Meeting for a HTTP Post
   *
   * <p>- Requires a cookie for the session user - Requires a meetingId request parameter for a GET
   * - Requires description, createdByUserId, datepicker, meetingTime, groupId request parameters
   * for a POST
   *
   * @param req The HTTP Request
   * @param res The HTTP Response
   */
  public void meetingAction(HttpServletRequest req, HttpServletResponse res) {
    // Ensure there is a cookie for the session user
    if (AccountController.redirectIfNoCookie(req, res)) return;

    Map<String, Object> viewData = new HashMap<String, Object>();
    viewData.put("title", "Meeting");

    // Initialise Manager connections
    MeetingManager meetingMan = new MeetingManager();
    GroupManager groupMan = new GroupManager();

    if (req.getMethod() == HttpMethod.Get) {
      // Get request parameter
      int meetingId = Integer.parseInt(req.getParameter("meetingId"));
      Meeting meeting = meetingMan.get(meetingId);

      if (meeting != null) {

        List<User> meetingUsers = groupMan.getGroupUsers(meeting.getGroupId());
        viewData.put("meetingUsers", meetingUsers);
        viewData.put("meeting", meeting);
        view(req, res, "/views/group/Meeting.jsp", viewData);

      } else {
        httpNotFound(req, res);
      }
    } else if (req.getMethod() == HttpMethod.Post) {

      // Get details from request
      String description = req.getParameter("description");
      int createdByUserId = Integer.parseInt(req.getParameter("createdByUserId"));
      Date dateCreated = new Date();

      String meetingDate = req.getParameter("datepicker");
      String meetingTime = req.getParameter("meetingTime");

      // Parse meeting date time details
      DateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm");
      Date dateDue = new Date();
      try {
        dateDue = format.parse(meetingDate + " " + meetingTime);
      } catch (ParseException e) {
        // Unable to parse date. This shouldn't happen since we are
        // performing javascript validation.
      }

      int groupId = Integer.parseInt(req.getParameter("groupId"));

      // Create a Meeting
      Meeting meeting = new Meeting();
      meeting.setDescription(description);
      meeting.setCreatedByUserId(createdByUserId);
      meeting.setDateCreated(dateCreated);
      meeting.setDateDue(dateDue);
      meeting.setGroupId(groupId);

      meetingMan.createMeeting(meeting);
      int meetingId = meetingMan.getIdFor(meeting);
      meeting.setId(meetingId);

      UserManager userMan = new UserManager();
      User createdByUser = userMan.get(createdByUserId);

      // Create a notification for all users in group
      NotificationManager notificationMan = new NotificationManager();
      List<User> users = groupMan.getGroupUsers(groupId);

      for (User u : users) {
        Notification notification =
            new Notification(
                u.getId(),
                u,
                groupId,
                null,
                "Meeting " + description + " was created by " + createdByUser.getFullName(),
                "/group/meeting?meetingId=" + meetingId);
        notificationMan.createNotification(notification);
      }

      // Update the User Session to show new meeting
      HttpSession session = req.getSession();
      Session userSession = (Session) session.getAttribute("userSession");
      User admin = userSession.getUser();
      admin.getMeetings().add(meeting);

      // Show meeting page
      viewData.put("meetingUsers", users);
      viewData.put("meeting", meeting);
      view(req, res, "/views/group/Meeting.jsp", viewData);
    }
  }