Index: lams_central/src/java/org/lamsfoundation/lams/web/DisplayGroupAction.java =================================================================== RCS file: /usr/local/cvsroot/lams_central/src/java/org/lamsfoundation/lams/web/DisplayGroupAction.java,v diff -u -r1.15 -r1.16 --- lams_central/src/java/org/lamsfoundation/lams/web/DisplayGroupAction.java 31 Mar 2009 03:17:28 -0000 1.15 +++ lams_central/src/java/org/lamsfoundation/lams/web/DisplayGroupAction.java 7 Apr 2009 03:39:08 -0000 1.16 @@ -18,11 +18,11 @@ * * http://www.gnu.org/licenses/gpl.txt * **************************************************************** - */ - -/* $Id$ */ -package org.lamsfoundation.lams.web; + */ +/* $Id$ */ +package org.lamsfoundation.lams.web; + import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; @@ -58,7 +58,7 @@ import org.lamsfoundation.lams.util.WebUtil; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; - + /** * @author jliew * @@ -69,221 +69,248 @@ */ public class DisplayGroupAction extends Action { - private static Logger log = Logger.getLogger(DisplayGroupAction.class); - private static IUserManagementService service; - private static LessonService lessonService; - private Integer stateId = OrganisationState.ACTIVE; - - @SuppressWarnings({"unchecked"}) - public ActionForward execute(ActionMapping mapping, - ActionForm form, - HttpServletRequest request, - HttpServletResponse response) throws Exception { - - String display = WebUtil.readStrParam(request, "display", false); - stateId = WebUtil.readIntParam(request, "stateId", false); - Integer orgId = WebUtil.readIntParam(request, "orgId", false); - - Organisation org = null; - if (orgId != null) { - org = (Organisation)getService().findById(Organisation.class, orgId); + private static Logger log = Logger.getLogger(DisplayGroupAction.class); + private static IUserManagementService service; + private static LessonService lessonService; + private Integer stateId = OrganisationState.ACTIVE; + + @SuppressWarnings( { "unchecked" }) + public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws Exception { + + String display = WebUtil.readStrParam(request, "display", false); + stateId = WebUtil.readIntParam(request, "stateId", false); + Integer orgId = WebUtil.readIntParam(request, "orgId", false); + + Organisation org = null; + if (orgId != null) { + org = (Organisation) getService().findById(Organisation.class, orgId); + } + + String forwardPath = "group"; + if (org != null) { + boolean allowSorting = false; + List roles = new ArrayList(); + List userOrganisationRoles = getService().getUserOrganisationRoles(orgId, + request.getRemoteUser()); + for (UserOrganisationRole userOrganisationRole : userOrganisationRoles) { + Integer roleId = userOrganisationRole.getRole().getRoleId(); + roles.add(roleId); + if (roleId.equals(Role.ROLE_GROUP_MANAGER) || roleId.equals(Role.ROLE_MONITOR)) { + allowSorting = true; } - - String forwardPath = "group"; - if (org != null) { - boolean allowSorting = false; - List roles = new ArrayList(); - List userOrganisationRoles = getService().getUserOrganisationRoles(orgId,request.getRemoteUser()); - for(UserOrganisationRole userOrganisationRole:userOrganisationRoles){ - Integer roleId = userOrganisationRole.getRole().getRoleId(); - roles.add(roleId); - if (roleId.equals(Role.ROLE_GROUP_MANAGER) || roleId.equals(Role.ROLE_MONITOR)) { - allowSorting = true; - } - } - - IndexOrgBean iob; - if (StringUtils.equals(display, "contents")) { - iob = new IndexOrgBean(org.getOrganisationId(), org.getName(), org.getOrganisationType().getOrganisationTypeId()); - iob = populateContentsOrgBean(iob, org, roles, request.getRemoteUser(), request.isUserInRole(Role.SYSADMIN)); - forwardPath = "groupContents"; - } else if (StringUtils.equals(display, "header")) { - iob = createHeaderOrgBean(org, roles, request.getRemoteUser(), request.isUserInRole(Role.SYSADMIN), false); - forwardPath = "groupHeader"; - } else { - iob = createHeaderOrgBean(org, roles, request.getRemoteUser(), request.isUserInRole(Role.SYSADMIN), true); - } - - request.setAttribute("orgBean", iob); - request.setAttribute("allowSorting", allowSorting); - } - - return mapping.findForward(forwardPath); + } + + IndexOrgBean iob; + if (StringUtils.equals(display, "contents")) { + iob = new IndexOrgBean(org.getOrganisationId(), org.getName(), org.getOrganisationType() + .getOrganisationTypeId()); + iob = populateContentsOrgBean(iob, org, roles, request.getRemoteUser(), request + .isUserInRole(Role.SYSADMIN)); + forwardPath = "groupContents"; + } else if (StringUtils.equals(display, "header")) { + iob = createHeaderOrgBean(org, roles, request.getRemoteUser(), request.isUserInRole(Role.SYSADMIN), + false); + forwardPath = "groupHeader"; + } else { + iob = createHeaderOrgBean(org, roles, request.getRemoteUser(), request.isUserInRole(Role.SYSADMIN), + true); + } + + request.setAttribute("orgBean", iob); + request.setAttribute("allowSorting", allowSorting); } - @SuppressWarnings({"unchecked"}) - private IndexOrgBean createHeaderOrgBean(Organisation org, List roles, String username, boolean isSysAdmin, boolean includeContents) - throws SQLException, NamingException { - IndexOrgBean orgBean = new IndexOrgBean(org.getOrganisationId(), org.getName(), org.getOrganisationType().getOrganisationTypeId()); + return mapping.findForward(forwardPath); + } - // set org links - List links = new ArrayList(); - List moreLinks = new ArrayList(); - if(isSysAdmin && stateId.equals(OrganisationState.ACTIVE)){ - if (orgBean.getType().equals(OrganisationType.COURSE_TYPE)) { - moreLinks.add(new IndexLinkBean("index.classman", "javascript:openOrgManagement(" + org.getOrganisationId()+")", "manage-group-button", null)); - } - } - if ((contains(roles, Role.ROLE_GROUP_ADMIN) || contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles,Role.ROLE_MONITOR)) - && stateId.equals(OrganisationState.ACTIVE)) { - if (orgBean.getType().equals(OrganisationType.COURSE_TYPE)) { - if((!isSysAdmin)&&(contains(roles, Role.ROLE_GROUP_ADMIN) || contains(roles, Role.ROLE_GROUP_MANAGER))){ - moreLinks.add(new IndexLinkBean("index.classman", "javascript:openOrgManagement(" + org.getOrganisationId()+")", "manage-group-button", null)); - } - if(contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles,Role.ROLE_MONITOR)) - links.add(new IndexLinkBean("index.addlesson", Configuration.get(ConfigurationKeys.SERVER_URL) + "/home.do?method=addLesson&courseID="+org.getOrganisationId()+"&classID=" + "" + "&KeepThis=true&TB_iframe=true&height=480&width=610", "add-lesson-button thickbox" + org.getOrganisationId(), null)); - moreLinks.add(new IndexLinkBean("index.searchlesson", Configuration.get(ConfigurationKeys.SERVER_URL) + "/findUserLessons.do?dispatch=getResults&courseID=" + org.getOrganisationId()+"&KeepThis=true&TB_iframe=true&height=400&width=600", "search-lesson thickbox"+org.getOrganisationId(), "index.searchlesson.tooltip")); - }else{//CLASS_TYPE - if(contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles,Role.ROLE_MONITOR)) - links.add(new IndexLinkBean("index.addlesson", Configuration.get(ConfigurationKeys.SERVER_URL) + "/home.do?method=addLesson&courseID="+org.getParentOrganisation().getOrganisationId()+"&classID="+org.getOrganisationId()+"&KeepThis=true&TB_iframe=true&height=480&width=610", "add-lesson-button thickbox"+org.getOrganisationId(), null)); - } - } - orgBean.setLinks(links); - orgBean.setMoreLinks(moreLinks); - - // set archived date if archived - if (stateId.equals(OrganisationState.ARCHIVED) && org.getOrganisationState().getOrganisationStateId().equals(OrganisationState.ARCHIVED)) { - orgBean.setArchivedDate(org.getArchivedDate()); - } - - if (includeContents) { - orgBean = populateContentsOrgBean(orgBean, org, roles, username, isSysAdmin); - } - - return orgBean; + @SuppressWarnings( { "unchecked" }) + private IndexOrgBean createHeaderOrgBean(Organisation org, List roles, String username, + boolean isSysAdmin, boolean includeContents) throws SQLException, NamingException { + IndexOrgBean orgBean = new IndexOrgBean(org.getOrganisationId(), org.getName(), org.getOrganisationType() + .getOrganisationTypeId()); + + // set org links + List links = new ArrayList(); + List moreLinks = new ArrayList(); + if (isSysAdmin && stateId.equals(OrganisationState.ACTIVE)) { + if (orgBean.getType().equals(OrganisationType.COURSE_TYPE)) { + moreLinks.add(new IndexLinkBean("index.classman", "javascript:openOrgManagement(" + + org.getOrganisationId() + ")", "manage-group-button", null)); + } } - - private IndexOrgBean populateContentsOrgBean(IndexOrgBean orgBean, Organisation org, List roles, String username, boolean isSysAdmin) - throws SQLException, NamingException { - User user = (User)getService().findByProperty(User.class, "login", username).get(0); - - // set lesson beans - List lessonBeans = null; - try { - Map map = populateLessonBeans(user.getUserId(), org.getOrganisationId(), roles); - lessonBeans = IndexUtils.sortLessonBeans(org.getOrderedLessonIds(), map); - } catch (Exception e) { - log.error("Failed retrieving user's lessons from database: " + e, e); + if ((contains(roles, Role.ROLE_GROUP_ADMIN) || contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles, + Role.ROLE_MONITOR)) + && stateId.equals(OrganisationState.ACTIVE)) { + if (orgBean.getType().equals(OrganisationType.COURSE_TYPE)) { + if ((!isSysAdmin) + && (contains(roles, Role.ROLE_GROUP_ADMIN) || contains(roles, Role.ROLE_GROUP_MANAGER))) { + moreLinks.add(new IndexLinkBean("index.classman", "javascript:openOrgManagement(" + + org.getOrganisationId() + ")", "manage-group-button", null)); } - orgBean.setLessons(lessonBeans); + if (contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles, Role.ROLE_MONITOR)) + links.add(new IndexLinkBean("index.addlesson", Configuration.get(ConfigurationKeys.SERVER_URL) + + "/home.do?method=addLesson&courseID=" + org.getOrganisationId() + "&classID=" + "" + + "&KeepThis=true&TB_iframe=true&height=480&width=610", "add-lesson-button thickbox" + + org.getOrganisationId(), null)); + moreLinks.add(new IndexLinkBean("index.searchlesson", Configuration.get(ConfigurationKeys.SERVER_URL) + + "/findUserLessons.do?dispatch=getResults&courseID=" + org.getOrganisationId() + + "&KeepThis=true&TB_iframe=true&height=400&width=600", "search-lesson thickbox" + + org.getOrganisationId(), "index.searchlesson.tooltip")); + } else {//CLASS_TYPE + if (contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles, Role.ROLE_MONITOR)) + links.add(new IndexLinkBean("index.addlesson", Configuration.get(ConfigurationKeys.SERVER_URL) + + "/home.do?method=addLesson&courseID=" + org.getParentOrganisation().getOrganisationId() + + "&classID=" + org.getOrganisationId() + + "&KeepThis=true&TB_iframe=true&height=480&width=610", "add-lesson-button thickbox" + + org.getOrganisationId(), null)); + } + } + orgBean.setLinks(links); + orgBean.setMoreLinks(moreLinks); - // create subgroup beans - if(orgBean.getType().equals(OrganisationType.COURSE_TYPE)){ - Set children = org.getChildOrganisations(); + // set archived date if archived + if (stateId.equals(OrganisationState.ARCHIVED) + && org.getOrganisationState().getOrganisationStateId().equals(OrganisationState.ARCHIVED)) { + orgBean.setArchivedDate(org.getArchivedDate()); + } - List childOrgBeans = new ArrayList(); - for(Organisation organisation:children){ - if(organisation.getOrganisationState().getOrganisationStateId().equals(stateId)){ - List classRoles = new ArrayList(); - List userOrganisationRoles = getService().getUserOrganisationRoles(organisation.getOrganisationId(),username); - // don't list the subgroup if user is not a member, and not a group admin/manager - if (userOrganisationRoles==null || userOrganisationRoles.isEmpty()) { - if (!contains(roles,Role.ROLE_GROUP_ADMIN) && - !contains(roles,Role.ROLE_GROUP_MANAGER) && - !isSysAdmin) { - continue; - } - } - for(UserOrganisationRole userOrganisationRole:userOrganisationRoles){ - classRoles.add(userOrganisationRole.getRole().getRoleId()); - } - if(contains(roles,Role.ROLE_GROUP_MANAGER)) classRoles.add(Role.ROLE_GROUP_MANAGER); - childOrgBeans.add(createHeaderOrgBean(organisation,classRoles,username,isSysAdmin,true)); - } + if (includeContents) { + orgBean = populateContentsOrgBean(orgBean, org, roles, username, isSysAdmin); + } + + return orgBean; + } + + private IndexOrgBean populateContentsOrgBean(IndexOrgBean orgBean, Organisation org, List roles, + String username, boolean isSysAdmin) throws SQLException, NamingException { + User user = (User) getService().findByProperty(User.class, "login", username).get(0); + + // set lesson beans + List lessonBeans = null; + try { + Map map = populateLessonBeans(user.getUserId(), org.getOrganisationId(), roles); + lessonBeans = IndexUtils.sortLessonBeans(org.getOrderedLessonIds(), map); + } catch (Exception e) { + log.error("Failed retrieving user's lessons from database: " + e, e); + } + orgBean.setLessons(lessonBeans); + + // create subgroup beans + if (orgBean.getType().equals(OrganisationType.COURSE_TYPE)) { + Set children = org.getChildOrganisations(); + + List childOrgBeans = new ArrayList(); + for (Organisation organisation : children) { + if (organisation.getOrganisationState().getOrganisationStateId().equals(stateId)) { + List classRoles = new ArrayList(); + List userOrganisationRoles = getService().getUserOrganisationRoles( + organisation.getOrganisationId(), username); + // don't list the subgroup if user is not a member, and not a group admin/manager + if (userOrganisationRoles == null || userOrganisationRoles.isEmpty()) { + if (!contains(roles, Role.ROLE_GROUP_ADMIN) && !contains(roles, Role.ROLE_GROUP_MANAGER) + && !isSysAdmin) { + continue; } - Collections.sort(childOrgBeans); - orgBean.setChildIndexOrgBeans(childOrgBeans); + } + for (UserOrganisationRole userOrganisationRole : userOrganisationRoles) { + classRoles.add(userOrganisationRole.getRole().getRoleId()); + } + if (contains(roles, Role.ROLE_GROUP_MANAGER)) + classRoles.add(Role.ROLE_GROUP_MANAGER); + childOrgBeans.add(createHeaderOrgBean(organisation, classRoles, username, isSysAdmin, true)); } - return orgBean; + } + Collections.sort(childOrgBeans); + orgBean.setChildIndexOrgBeans(childOrgBeans); } - - // create lesson beans - private Map populateLessonBeans(Integer userId, Integer orgId, List roles) - throws SQLException, NamingException { - - // iterate through user's lessons where they are learner - Map map = getLessonService().getLessonsByOrgAndUserWithCompletedFlag(userId, orgId, false); - for (IndexLessonBean bean : map.values()) { - List lessonLinks = new ArrayList(); - String url = null; - Integer lessonStateId = bean.getState(); - if (stateId.equals(OrganisationState.ACTIVE)) { - if (contains(roles, Role.ROLE_LEARNER)) { - if (lessonStateId.equals(Lesson.STARTED_STATE) || lessonStateId.equals(Lesson.FINISHED_STATE)) { - url = "javascript:openLearner("+bean.getId()+")"; - } - } - } else if (stateId.equals(OrganisationState.ARCHIVED)) { - if (contains(roles, Role.ROLE_LEARNER)) { - if (lessonStateId.equals(Lesson.STARTED_STATE) || lessonStateId.equals(Lesson.FINISHED_STATE)) { - lessonLinks.add(new IndexLinkBean("label.export.portfolio","javascript:openExportPortfolio("+bean.getId()+")")); - } - } - } - if (lessonLinks.size()>0 || url!=null) { - bean.setUrl(url); - bean.setLinks(lessonLinks); - } + return orgBean; + } + + // create lesson beans + private Map populateLessonBeans(Integer userId, Integer orgId, List roles) + throws SQLException, NamingException { + + // iterate through user's lessons where they are learner + Map map = getLessonService().getLessonsByOrgAndUserWithCompletedFlag(userId, orgId, + false); + for (IndexLessonBean bean : map.values()) { + List lessonLinks = new ArrayList(); + String url = null; + Integer lessonStateId = bean.getState(); + if (stateId.equals(OrganisationState.ACTIVE)) { + if (contains(roles, Role.ROLE_LEARNER)) { + if (lessonStateId.equals(Lesson.STARTED_STATE) || lessonStateId.equals(Lesson.FINISHED_STATE)) { + url = "javascript:openLearner(" + bean.getId() + ")"; + } } - - // iterate through user's lessons where they are staff, and add staff links to the beans in the map. - Map staffMap = getLessonService().getLessonsByOrgAndUserWithCompletedFlag(userId, orgId, true); - for (IndexLessonBean bean : staffMap.values()) { - if (map.containsKey(bean.getId())) { - bean = map.get(bean.getId()); - } - List lessonLinks = bean.getLinks(); - if (lessonLinks == null) lessonLinks = new ArrayList(); - if (stateId.equals(OrganisationState.ACTIVE)) { - if (contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles, Role.ROLE_MONITOR)){ - lessonLinks.add(new IndexLinkBean("index.monitor", "javascript:openMonitorLesson(" + bean.getId()+")")); - } - } else if (stateId.equals(OrganisationState.ARCHIVED)) { - if (contains(roles, Role.ROLE_GROUP_MANAGER)) { - lessonLinks.add(new IndexLinkBean("index.monitor", "javascript:openMonitorLesson(" + bean.getId()+")")); - } - } - if (lessonLinks.size() > 0) { - bean.setLinks(lessonLinks); - } - map.put(bean.getId(), bean); + } else if (stateId.equals(OrganisationState.ARCHIVED)) { + if (contains(roles, Role.ROLE_LEARNER)) { + if (lessonStateId.equals(Lesson.STARTED_STATE) || lessonStateId.equals(Lesson.FINISHED_STATE)) { + lessonLinks.add(new IndexLinkBean("label.export.portfolio", "javascript:openExportPortfolio(" + + bean.getId() + ")")); + } } - - return map; + } + if (lessonLinks.size() > 0 || url != null) { + bean.setUrl(url); + bean.setLinks(lessonLinks); + } } - private boolean contains(List roles, Integer roleId) { - for (int i = 0; i < roles.size(); i++) { - if (roleId.equals(roles.get(i))) - return true; + // iterate through user's lessons where they are staff, and add staff links to the beans in the map. + Map staffMap = getLessonService().getLessonsByOrgAndUserWithCompletedFlag(userId, orgId, + true); + for (IndexLessonBean bean : staffMap.values()) { + if (map.containsKey(bean.getId())) { + bean = map.get(bean.getId()); + } + List lessonLinks = bean.getLinks(); + if (lessonLinks == null) + lessonLinks = new ArrayList(); + if (stateId.equals(OrganisationState.ACTIVE)) { + if (contains(roles, Role.ROLE_GROUP_MANAGER) || contains(roles, Role.ROLE_MONITOR)) { + lessonLinks.add(new IndexLinkBean("index.monitor", "javascript:openMonitorLesson(" + bean.getId() + + ")")); } - return false; - } - - private IUserManagementService getService(){ - if(service==null){ - WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet().getServletContext()); - service = (IUserManagementService) ctx.getBean("userManagementService"); + } else if (stateId.equals(OrganisationState.ARCHIVED)) { + if (contains(roles, Role.ROLE_GROUP_MANAGER)) { + lessonLinks.add(new IndexLinkBean("index.monitor", "javascript:openMonitorLesson(" + bean.getId() + + ")")); } - return service; + } + if (lessonLinks.size() > 0) { + bean.setLinks(lessonLinks); + } + map.put(bean.getId(), bean); } - - private LessonService getLessonService(){ - if(lessonService==null){ - WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet().getServletContext()); - lessonService = (LessonService) ctx.getBean("lessonService"); - } - return lessonService; + + return map; + } + + private boolean contains(List roles, Integer roleId) { + for (int i = 0; i < roles.size(); i++) { + if (roleId.equals(roles.get(i))) + return true; } + return false; + } + + private IUserManagementService getService() { + if (service == null) { + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet() + .getServletContext()); + service = (IUserManagementService) ctx.getBean("userManagementService"); + } + return service; + } + + private LessonService getLessonService() { + if (lessonService == null) { + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet() + .getServletContext()); + lessonService = (LessonService) ctx.getBean("lessonService"); + } + return lessonService; + } } - \ No newline at end of file