// new version without group ds combunation
  private void printUserGroupList(PrintWriter out, User user) {
    Group group = null;
    String label = null;
    String groupName = null;
    String groupType = null;
    String userName = user.getUserName();
    if (user.getGroupList().size() == 0) {

      out.println("<p>No group is selected.</p>");
    } else {
      out.println("<table >");
      out.println("<tr><th> group name </th>");
      out.println("<th> group structure</th>");
      out.println("<th> user choice</th>");
      out.println("<th> admin decision</th>");
      out.println("<th> admin decision</th></tr>");
      for (int i = 0; i < user.getGroupList().size(); i++) {

        group = (Group) (user.getGroupList().get(i));
        groupName = group.getGroupName();
        groupType = group.getGroupType();

        if (groupType.equals("5")) {
          label = " user wants to add this group ";
        } else if (groupType.equals("0")) {
          label = " user wants to delete this group ";
        }
        out.println("<tr><td>" + groupName + "</td>");
        this.printGroupStructureLink(out, group.getGroupName());
        out.println("<td>" + label + " </td>");

        out.println("<FORM ACTION=/gsn/MyUpdateUserGroupServlet METHOD=POST>");
        out.println("<INPUT  TYPE=HIDDEN NAME=groupname VALUE=" + groupName + ">");
        out.println("<INPUT  TYPE=HIDDEN NAME=grouptype VALUE=" + groupType + ">");
        out.println("<INPUT TYPE=HIDDEN NAME=username VALUE= " + userName + ">");
        out.println("<INPUT TYPE=HIDDEN NAME=update VALUE= yes>");
        out.println(
            "<td><INPUT TYPE=SUBMIT class=creategroupbuttonstyle VALUE=\"agree to update\"></td>");
        out.println("</FORM>");

        out.println("<FORM ACTION=/gsn/MyUpdateUserGroupServlet METHOD=POST>");
        out.println("<INPUT  TYPE=HIDDEN NAME=groupname VALUE=" + groupName + ">");
        out.println("<INPUT  TYPE=HIDDEN NAME=grouptype VALUE=" + groupType + ">");
        out.println("<INPUT TYPE=HIDDEN NAME=username VALUE= " + userName + ">");
        out.println("<INPUT TYPE=HIDDEN NAME=update VALUE= no>");
        out.println(
            "<td><INPUT TYPE=SUBMIT class=creategroupbuttonstyle VALUE=\"refuse to update\"></td></tr>");
        out.println("</FORM>");
      }
      out.println("</table>");
    }
  }
  private void printGroupDataSourceList(PrintWriter out, Group group) {
    DataSource ds = null;
    String dsname = null;
    String dstype = null;
    String label = null;
    out.println("<table >");
    out.println(" <caption>" + group.getGroupName() + " combination</caption>");
    out.println("<tr><th> virtual sensor name </th>");
    out.println("<th> access right</th></tr>");

    for (int j = 0; j < group.getDataSourceList().size(); j++) {
      ds = (DataSource) group.getDataSourceList().get(j);
      dsname = ds.getDataSourceName();
      dstype = ds.getDataSourceType();

      if (dstype.charAt(0) == '1') {
        label = "read";
      } else if (dstype.charAt(0) == '2') {
        label = "write";
      } else if (dstype.charAt(0) == '3') {
        label = "read/write";
      }
      out.println("<tr><td>" + dsname + "</td>");
      out.println("<td>" + label + "</td></tr>");
    }
    out.println("</table>");
  }
  private void printRemainingGroupsList(PrintWriter out, User user, ConnectToDB ctdb)
      throws SQLException {
    Vector remainingGroupList =
        ctdb.getGroupListsDifference(ctdb.getGroupList(), user.getGroupList());
    Group group = null;
    String groupName = null;
    String userName = user.getUserName();
    if (remainingGroupList.size() == 0) {
      out.println("<table class =transparenttable>");
      out.println("<tr><td><FONT COLOR=#000000>No group is available.</td></tr>");
      out.println("</table>");
    } else {
      out.println("<table>");
      out.println("<tr>");
      out.println("<th>group name</th>");
      out.println("<th>group structure</th>");
      out.println("<th>updates</th>");
      out.println("</tr>");

      for (int i = 0; i < remainingGroupList.size(); i++) {
        group = (Group) (remainingGroupList.get(i));
        groupName = group.getGroupName();
        out.println("<tr>");
        out.println("<td>" + groupName + "</td>");
        this.printGroupStructureLink(out, groupName);
        if (ctdb.valueExistsForThisColumnUnderTwoConditions(
                new Column("ISUSERWAITING", "yes"),
                new Column("GROUPNAME", groupName),
                new Column("USERNAME", userName),
                "ACUSER_ACGROUP")
            == false) {
          out.println("<FORM ACTION=/gsn/MyUpdateUserWaitingForGroupServlet METHOD=POST>");
          out.println("<INPUT  TYPE=HIDDEN NAME=groupname VALUE=" + groupName + ">");
          out.println("<INPUT  TYPE=HIDDEN NAME=addgroup VALUE=Yes>");
          out.println(
              "<td style=text-align:center><INPUT TYPE=SUBMIT class= buttonstyle VALUE=\"add\"></td>");
          out.println("</FORM>");
        } else {
          out.println("<td>" + "<FONT COLOR=#0000FF>in updates waiting list!</td>");
        }
        out.println("</tr>");
      }
      out.println("</table>");
    }
  }
  private void printGroupListModulo(PrintWriter out, User user, int index) {
    Group group = null;
    String label = null;
    String groupName = null;
    String groupType = null;
    String userName = user.getUserName();
    group = (Group) (user.getGroupList().get(index));
    groupName = group.getGroupName();
    groupType = group.getGroupType();
    if (groupType.equals("5")) {
      label = " user wants to add this group ";
    } else if (groupType.equals("0")) {
      label = " user wants to delete this group ";
    }

    out.println("<table class=\"transparenttable\">");
    out.println("<tr><td><B>groupname: </B>" + groupName + "</td></tr>");
    out.println("<tr><td><B>user choice: </B>" + label + " </td></tr>");
    out.println("</table>");
    out.println("<BR>");
    this.printGroupDataSourceList(out, group);
    out.println("<BR>");

    out.println("<FORM ACTION=/gsn/MyUpdateUserGroupServlet METHOD=POST>");
    out.println("<INPUT  TYPE=HIDDEN NAME=groupname VALUE=" + groupName + ">");
    out.println("<INPUT  TYPE=HIDDEN NAME=grouptype VALUE=" + groupType + ">");
    out.println("<INPUT TYPE=HIDDEN NAME=username VALUE= " + userName + ">");
    out.println("<INPUT TYPE=HIDDEN NAME=update VALUE= yes>");
    out.println("<table class=\"transparenttable\">");
    out.println(
        "<td><INPUT TYPE=SUBMIT class=creategroupbuttonstyle VALUE=\"agree to update\"></td>");
    out.println("</FORM>");

    out.println("<FORM ACTION=/gsn/MyUpdateUserGroupServlet METHOD=POST>");
    out.println("<INPUT  TYPE=HIDDEN NAME=groupname VALUE=" + groupName + ">");
    out.println("<INPUT  TYPE=HIDDEN NAME=grouptype VALUE=" + groupType + ">");
    out.println("<INPUT TYPE=HIDDEN NAME=username VALUE= " + userName + ">");
    out.println("<INPUT TYPE=HIDDEN NAME=update VALUE= no>");
    out.println(
        "<td><INPUT TYPE=SUBMIT class=creategroupbuttonstyle VALUE=\"refuse to update\"></td></tr>");
    out.println("</FORM>");

    out.println("</table>");
  }
  /**
   * Creates a notification to a Group coordinator signaling that a user wants to join their group
   *
   * <p>- Requires a groupId request parameter for the GET
   *
   * @param req The HTTP Request
   * @param res The HTTP Response
   */
  public void inviteAction(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>();

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

    try {

      // Get the session user
      HttpSession session = req.getSession();
      Session userSession = (Session) session.getAttribute("userSession");
      User user = userSession.getUser();

      // Get the coordinator for the group
      GroupManager groupMan = new GroupManager();
      Group group = groupMan.get(groupId);
      User coordinator = groupMan.getCoordinator(groupId);

      // Send a notification to the coordinator for them to permit access to the group
      NotificationManager notificationMan = new NotificationManager();
      Notification notification =
          new Notification(
              coordinator.getId(),
              coordinator,
              groupId,
              group,
              user.getFullName() + " wants to join your group " + group.getGroupName(),
              "/home/notifications?addUserId=" + user.getId() + "&groupId=" + group.getId());
      notificationMan.createNotification(notification);

      redirectToLocal(req, res, "/home/dashboard");
      return;

    } catch (Exception e) {
      redirectToLocal(req, res, "/home/dashboard");
    }
  }
  /**
   * Displays a given Research Group page for a HTTP Get, or creates a new Group for a HTTP Post
   *
   * <p>- Requires a cookie for the session user - Requires a groupId request parameter for a GET -
   * Requires a groupName, description, createdByUserId request parameters for a POST
   *
   * @param req The HTTP Request
   * @param res The HTTP Response
   */
  public void researchgroupAction(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", "Research Group");

    if (req.getMethod() == HttpMethod.Get) {
      // Load group data into Map
      GroupManager gm = new GroupManager();
      int groupId = Integer.parseInt(req.getParameter("groupId"));
      Group group = gm.get(groupId);

      if (group != null) {
        // Load Group into map
        viewData.put("group", group);

        // Load group members into Map
        List<String> groupMembers = gm.getGroupMembers(groupId);
        viewData.put("groupMembers", groupMembers);

        // Load meetings into map
        MeetingManager meetMan = new MeetingManager();
        List<Meeting> groupMeetings = meetMan.getGroupMeetings(groupId);
        viewData.put("groupMeetings", groupMeetings);

        // Load Document Data into Map
        DocumentManager docMan = new DocumentManager();
        List<Document> groupDocuments = docMan.getGroupDocuments(groupId);
        viewData.put("groupDocuments", groupDocuments);

        // Load discussion threads
        DiscussionManager dm = new DiscussionManager();
        viewData.put("groupDiscussions", dm.getThreads(groupId));

        // Check if the user is a member
        boolean isMember = false;
        HttpSession session = req.getSession();
        Session userSession = (Session) session.getAttribute("userSession");
        User user = userSession.getUser();

        for (Group g : gm.getAllGroups(user.getId())) {
          if (g.getId() == group.getId()) {
            isMember = true;
            break;
          }
        }

        viewData.put("notMember", !isMember);

        // View group page.
        view(req, res, "/views/group/ResearchGroup.jsp", viewData);

      } else {
        httpNotFound(req, res);
      }

    } else if (req.getMethod() == HttpMethod.Post) {
      // Create Group

      // Get data from parameters
      String groupName = req.getParameter("groupName");
      String description = req.getParameter("description");
      int adminId = Integer.parseInt(req.getParameter("createdByUserId"));

      // Create the Group
      GroupManager groupMan = new GroupManager();
      Group group = new Group();
      group.setGroupName(groupName);
      group.setDescription(description);
      group.setCoordinatorId(adminId);
      // Create the mapping
      groupMan.createGroup(group);
      int groupId = groupMan.getIdFor(group);
      groupMan.createMapping(groupId, adminId);

      group.setId(groupId);

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

      // Show the Group Page
      viewData.put("groupName", group.getGroupName());
      List<String> groupMembers = groupMan.getGroupMembers(groupId);
      viewData.put("groupMembers", groupMembers);

      view(req, res, "/views/group/ResearchGroup.jsp", viewData);
    }
  }