Пример #1
0
  public void testCollectionFilter() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Group hb = new Group("hibernate");
    User gavin = new User("gavin");
    User max = new User("max");
    hb.getUsers().put("gavin", gavin);
    hb.getUsers().put("max", max);
    gavin.getGroups().put("hibernate", hb);
    max.getGroups().put("hibernate", hb);
    s.persist(hb);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    hb =
        (Group)
            s.createCriteria(Group.class)
                .setFetchMode("users", FetchMode.SELECT)
                .add(Restrictions.idEq("hibernate"))
                .uniqueResult();
    assertFalse(Hibernate.isInitialized(hb.getUsers()));
    // gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult();
    Long size = (Long) s.createFilter(hb.getUsers(), "select count(*)").uniqueResult();
    assertEquals(new Long(2), size);
    assertFalse(Hibernate.isInitialized(hb.getUsers()));
    s.delete(hb);
    t.commit();
    s.close();
  }
Пример #2
0
  public static void checkUser(User user) {
    // Check optional fields
    // NOTE fullName cannot be checked
    // NOTE isEnabled cannot be checked
    // NOTE isLocked cannot be checked
    // NOTE im cannot be checked
    // NOTE nameInSource cannot be checked
    // NOTE isAlertEnabled cannot be checked
    // NOTE alterEmailPrefix cannot be checked
    // NOTE isExternal cannot be checked
    // NOTE isDefaultCached cannot be checked
    // NOTE isGroupRole cannot be checked
    // NOTE password cannot be checked

    if (user.getEmailAddress() != null) {
      checkEmailAddress(user.getEmailAddress());
    }
    if (user.getTelephone() != null) {
      checkTelephone(user.getTelephone());
    }
    if (user.getAlertEmail() != null) {
      checkEmailAddress(user.getAlertEmail());
    }
    if (user.getStoredVmQuota() != null) {
      assertTrue(
          user.getStoredVmQuota() >= 0,
          String.format(OBJ_FIELD_GTE_0, "User", "storedVmQuota", user.getStoredVmQuota()));
    }
    if (user.getDeployedVmQuota() != null) {
      assertTrue(
          user.getDeployedVmQuota() >= 0,
          String.format(OBJ_FIELD_GTE_0, "User", "deployedVmQuota", user.getDeployedVmQuota()));
    }
    if (user.getRole() != null) {
      checkReferenceType(user.getRole());
    }
    if (user.getGroups() != null) {
      for (Reference group : user.getGroups()) {
        checkReferenceType(group);
      }
    }

    // parent type
    checkEntityType(user);
  }
  @Override
  public void deleteUser(User user) {
    Iterator<UserGroup> iterator = user.getGroups().iterator();

    while (iterator.hasNext()) {
      UserGroup group = iterator.next();
      group.getMembers().remove(user);
      userGroupService.updateUserGroup(group);
    }
  }
 private boolean isAdminUser(User user) {
   if (user == null) {
     return false;
   }
   for (Group group : user.getGroups()) {
     if (ADMIN_GROUP.equals(group.getName())) {
       return true;
     }
   }
   return false;
 }
Пример #5
0
 public Object getPropertyValue(Object target, String property) throws Exception {
   User user = (User) target;
   if ("id".equals(property)) {
     return new Long(121212);
   }
   if ("groups".equals(property)) {
     return getPropertyIn(user.getGroups(), "name");
   } else if ("mngGroups".equals(property)) {
     return getPropertyIn(user.getMngGroups(), "name");
   } else if ("mngUsers".equals(property)) {
     return getUserNames(user.getMngUsers());
   } else {
     return super.getPropertyValue(target, property);
   }
 }
Пример #6
0
  public void testJoinFetchManyToMany() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Group hb = new Group("hibernate");
    User gavin = new User("gavin");
    User max = new User("max");
    hb.getUsers().put("gavin", gavin);
    hb.getUsers().put("max", max);
    gavin.getGroups().put("hibernate", hb);
    max.getGroups().put("hibernate", hb);
    s.persist(hb);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    hb = (Group) s.get(Group.class, "hibernate");
    assertTrue(Hibernate.isInitialized(hb.getUsers()));
    gavin = (User) hb.getUsers().get("gavin");
    assertFalse(Hibernate.isInitialized(gavin.getGroups()));
    max = (User) s.get(User.class, "max");
    assertFalse(Hibernate.isInitialized(max.getGroups()));
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    hb =
        (Group)
            s.createCriteria(Group.class)
                .setFetchMode("users", FetchMode.JOIN)
                .setFetchMode("users.groups", FetchMode.JOIN)
                .uniqueResult();
    assertTrue(Hibernate.isInitialized(hb.getUsers()));
    gavin = (User) hb.getUsers().get("gavin");
    assertTrue(Hibernate.isInitialized(gavin.getGroups()));
    max = (User) s.get(User.class, "max");
    assertTrue(Hibernate.isInitialized(max.getGroups()));
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    s.delete(hb);
    t.commit();
    s.close();
  }
  /**
   * 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);
    }
  }
Пример #8
0
 public void addUser(User user) {
   if (user.getGroups().add(this)) {
     getUsers().add(user);
   }
 }