Index: TestHarness4LAMS2/src/org/lamsfoundation/testharness/admin/MockAdmin.java =================================================================== diff -u -ra4dea2c2d1a6d7b4b8f21c1aad27d4be330b2fb7 -r3ea6521134b1f1a4dbac58edf44a7f81e03fd699 --- TestHarness4LAMS2/src/org/lamsfoundation/testharness/admin/MockAdmin.java (.../MockAdmin.java) (revision a4dea2c2d1a6d7b4b8f21c1aad27d4be330b2fb7) +++ TestHarness4LAMS2/src/org/lamsfoundation/testharness/admin/MockAdmin.java (.../MockAdmin.java) (revision 3ea6521134b1f1a4dbac58edf44a7f81e03fd699) @@ -52,7 +52,7 @@ /** * @version * - *

+ *

* View Source *

* @@ -94,28 +94,18 @@ createCourseURL).execute(); if (!MockUser.checkPageContains(resp, MockAdmin.COURSE_FORM_FLAG)) { MockAdmin.log.debug(resp.getText()); - throw new TestHarnessException(username + " did not get course creation page with the url:" - + createCourseURL); + throw new TestHarnessException( + username + " did not get course creation page with the url:" + createCourseURL); } Map params = new HashMap(); params.put(MockAdmin.COURSE_NAME, courseName); // fill the form and submit it and return the course id - resp = (WebResponse) new Call(wc, test, username + " submit course creation form", - fillForm(resp, 0, params)).execute(); - WebTable[] tables = resp.getTables(); - if ((tables == null) || (tables.length == 0)) { - MockAdmin.log.debug(resp.getText()); - throw new TestHarnessException(username - + " failed to get an course table after submitting course creation form"); - } - WebTable table = tables[0]; - String idAsString = null; - for (int i = table.getRowCount() - 1; i >= 0; i--) { - if (table.getCellAsText(i, 1).indexOf(courseName) != -1) {// found the organisation created just now - idAsString = table.getCellAsText(i, 0); - break; - } - } + new Call(wc, test, username + " submit course creation form", fillForm(resp, 0, params)).execute(); + + resp = (WebResponse) new Call(wc, test, username + " checking course ID by name " + courseName, + "/admin/organisation.do?method=getOrganisationIdByName&name=" + courseName).execute(); + String idAsString = resp.getText(); + if (idAsString == null) { MockAdmin.log.debug(resp.getText()); throw new TestHarnessException("Failed to get the course id for " + courseName); @@ -151,18 +141,19 @@ resp = (WebResponse) new Call(wc, test, username + " creating user " + name, url).execute(); if (!MockUser.checkPageContains(resp, MockAdmin.USER_FORM_FLAG)) { MockAdmin.log.debug(resp.getText()); - throw new TestHarnessException(username + " did not get user creation page with the url " + url); + throw new TestHarnessException( + username + " did not get user creation page with the url " + url); } Map params = new HashMap(); params.put(MockAdmin.LOGIN, name); params.put(MockAdmin.PASSWORD, name); params.put(MockAdmin.PASSWORD2, name); params.put(MockAdmin.FIRST_NAME, name); params.put(MockAdmin.LAST_NAME, MockAdmin.COMMON_LAST_NAME); - params.put(MockAdmin.EMAIL, name + "@" + MockAdmin.COMMON_LAST_NAME + "." - + MockAdmin.COMMON_LAST_NAME.toLowerCase()); - resp = (WebResponse) new Call(wc, test, username + " submit user creation form", fillForm(resp, 0, - params)).execute(); + params.put(MockAdmin.EMAIL, + name + "@" + MockAdmin.COMMON_LAST_NAME + "." + MockAdmin.COMMON_LAST_NAME.toLowerCase()); + resp = (WebResponse) new Call(wc, test, username + " submit user creation form", + fillForm(resp, 0, params)).execute(); // add the roles String respText = resp.getText(); @@ -173,23 +164,23 @@ mockUser.setUserId(userId); MockAdmin.log.debug("User " + name + " already exists with ID " + userId); } else { - throw new TestHarnessException("User " + name - + " already exists, but could not retrieve his ID"); + throw new TestHarnessException( + "User " + name + " already exists, but could not retrieve his ID"); } } else { Matcher m = MockAdmin.NEW_USER_ID_PATTERN.matcher(respText); if (m.find()) { String userId = m.group(1); if (userId.equals("0")) { log.error(respText); - throw new TestHarnessException("Error while creating user " + name - + ". Server returned user ID 0"); + throw new TestHarnessException( + "Error while creating user " + name + ". Server returned user ID 0"); } mockUser.setUserId(userId); MockAdmin.log.debug("User " + name + " created with ID " + userId); } else { - throw new TestHarnessException("User " + name - + " was just created, but could not retrieve his ID"); + throw new TestHarnessException( + "User " + name + " was just created, but could not retrieve his ID"); } } } else { @@ -207,8 +198,8 @@ WebTable[] tables = resp.getTables(); if ((tables == null) || (tables.length < 2)) { MockAdmin.log.debug(resp.getText()); - throw new TestHarnessException(username - + " failed to get an user table after submitting user role form"); + throw new TestHarnessException( + username + " failed to get an user table after submitting user role form"); } WebTable table = tables[1]; Index: lams_admin/src/java/org/lamsfoundation/lams/admin/web/OrganisationAction.java =================================================================== diff -u -ref31c70885b34d2475c8b297d7f9bb0f564a829b -r3ea6521134b1f1a4dbac58edf44a7f81e03fd699 --- lams_admin/src/java/org/lamsfoundation/lams/admin/web/OrganisationAction.java (.../OrganisationAction.java) (revision ef31c70885b34d2475c8b297d7f9bb0f564a829b) +++ lams_admin/src/java/org/lamsfoundation/lams/admin/web/OrganisationAction.java (.../OrganisationAction.java) (revision 3ea6521134b1f1a4dbac58edf44a7f81e03fd699) @@ -23,6 +23,7 @@ /* $$Id$$ */ package org.lamsfoundation.lams.admin.web; +import java.io.IOException; import java.util.Collections; import java.util.List; @@ -52,109 +53,124 @@ /** * @author Fei Yang * - * @struts:action path="/organisation" - * name="OrganisationForm" - * scope="request" - * parameter="method" - * validate="false" + * @struts:action path="/organisation" name="OrganisationForm" scope="request" parameter="method" validate="false" * * @struts:action-forward name="organisation" path=".organisation" * @struts:action-forward name="organisationCourseAdmin" path=".organisationCourseAdmin" * @struts:action-forward name="orglist" path="/orgmanage.do" */ public class OrganisationAction extends LamsDispatchAction { - private static IUserManagementService service; - private static MessageService messageService; - private static List locales; - private static List status; - - public ActionForward edit(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{ - service = AdminServiceProxy.getService(getServlet().getServletContext()); - initLocalesAndStatus(); - DynaActionForm orgForm = (DynaActionForm)form; - Integer orgId = WebUtil.readIntParam(request,"orgId",true); - - HttpSession session = SessionManager.getSession(); - if (session != null) { - UserDTO userDto = (UserDTO)session.getAttribute(AttributeNames.USER); - if (userDto != null) { - Integer userId = userDto.getUserID(); - // sysadmin, global group admin, group manager, group admin can edit group - if (service.canEditGroup(userId, orgId)) { - // edit existing organisation - if (orgId != null){ - Organisation org = (Organisation)service.findById(Organisation.class,orgId); - BeanUtils.copyProperties(orgForm,org); - orgForm.set("parentId",org.getParentOrganisation().getOrganisationId()); - orgForm.set("parentName",org.getParentOrganisation().getName()); - orgForm.set("typeId",org.getOrganisationType().getOrganisationTypeId()); - orgForm.set("stateId",org.getOrganisationState().getOrganisationStateId()); - SupportedLocale locale = org.getLocale(); - orgForm.set("localeId",locale != null ? locale.getLocaleId() : null); - } - request.getSession().setAttribute("locales",locales); - request.getSession().setAttribute("status",status); - if (service.isUserSysAdmin() || service.isUserGlobalGroupAdmin()) { - return mapping.findForward("organisation"); - } else { - return mapping.findForward("organisationCourseAdmin"); - } - } - } + private static IUserManagementService service; + private static MessageService messageService; + private static List locales; + private static List status; + + public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws Exception { + OrganisationAction.service = AdminServiceProxy.getService(getServlet().getServletContext()); + initLocalesAndStatus(); + DynaActionForm orgForm = (DynaActionForm) form; + Integer orgId = WebUtil.readIntParam(request, "orgId", true); + + HttpSession session = SessionManager.getSession(); + if (session != null) { + UserDTO userDto = (UserDTO) session.getAttribute(AttributeNames.USER); + if (userDto != null) { + Integer userId = userDto.getUserID(); + // sysadmin, global group admin, group manager, group admin can edit group + if (OrganisationAction.service.canEditGroup(userId, orgId)) { + // edit existing organisation + if (orgId != null) { + Organisation org = (Organisation) OrganisationAction.service.findById(Organisation.class, + orgId); + BeanUtils.copyProperties(orgForm, org); + orgForm.set("parentId", org.getParentOrganisation().getOrganisationId()); + orgForm.set("parentName", org.getParentOrganisation().getName()); + orgForm.set("typeId", org.getOrganisationType().getOrganisationTypeId()); + orgForm.set("stateId", org.getOrganisationState().getOrganisationStateId()); + SupportedLocale locale = org.getLocale(); + orgForm.set("localeId", locale != null ? locale.getLocaleId() : null); + } + request.getSession().setAttribute("locales", OrganisationAction.locales); + request.getSession().setAttribute("status", OrganisationAction.status); + if (OrganisationAction.service.isUserSysAdmin() + || OrganisationAction.service.isUserGlobalGroupAdmin()) { + return mapping.findForward("organisation"); + } else { + return mapping.findForward("organisationCourseAdmin"); + } } - + } + } + + return error(mapping, request); + } + + public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws Exception { + OrganisationAction.service = AdminServiceProxy.getService(getServlet().getServletContext()); + initLocalesAndStatus(); + DynaActionForm orgForm = (DynaActionForm) form; + + if (!(request.isUserInRole(Role.SYSADMIN) || OrganisationAction.service.isUserGlobalGroupAdmin())) { + // only sysadmins and global group admins can create groups + if (((orgForm.get("typeId") != null) && orgForm.get("typeId").equals(OrganisationType.COURSE_TYPE)) + || (orgForm.get("typeId") == null)) { return error(mapping, request); + } } - - public ActionForward create(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{ - service = AdminServiceProxy.getService(getServlet().getServletContext()); - initLocalesAndStatus(); - DynaActionForm orgForm = (DynaActionForm)form; - - if(!(request.isUserInRole(Role.SYSADMIN) || service.isUserGlobalGroupAdmin())) { - // only sysadmins and global group admins can create groups - if (orgForm.get("typeId")!=null && orgForm.get("typeId").equals(OrganisationType.COURSE_TYPE) - || orgForm.get("typeId")==null) { - return error(mapping, request); - } - } - - // creating new organisation - orgForm.set("orgId", null); - Integer parentId = WebUtil.readIntParam(request,"parentId",true); - if (parentId!=null) { - Organisation parentOrg = (Organisation)service.findById(Organisation.class,parentId); - orgForm.set("parentName", parentOrg.getName()); - } - request.getSession().setAttribute("locales",locales); - request.getSession().setAttribute("status",status); - return mapping.findForward("organisation"); + + // creating new organisation + orgForm.set("orgId", null); + Integer parentId = WebUtil.readIntParam(request, "parentId", true); + if (parentId != null) { + Organisation parentOrg = (Organisation) OrganisationAction.service.findById(Organisation.class, parentId); + orgForm.set("parentName", parentOrg.getName()); } - - private ActionForward error(ActionMapping mapping, HttpServletRequest request) { - messageService = AdminServiceProxy.getMessageService(getServlet().getServletContext()); - request.setAttribute("errorName", "OrganisationAction"); - request.setAttribute("errorMessage", messageService.getMessage("error.authorisation")); - return mapping.findForward("error"); - } + request.getSession().setAttribute("locales", OrganisationAction.locales); + request.getSession().setAttribute("status", OrganisationAction.status); + return mapping.findForward("organisation"); + } - /*public ActionForward remove(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){ - Integer orgId = WebUtil.readIntParam(request,"orgId"); - getService().deleteById(Organisation.class,orgId); - Integer parentId = WebUtil.readIntParam(request,"parentId"); - request.setAttribute("org",parentId); - return mapping.findForward("orglist"); - }*/ - - @SuppressWarnings("unchecked") - private void initLocalesAndStatus(){ - if((locales==null)||(status==null) && service!=null){ - locales = service.findAll(SupportedLocale.class); - status = service.findAll(OrganisationState.class); - Collections.sort(locales); - } + /** + * Looks up course ID by its name. Used mainly by TestHarness. + */ + @SuppressWarnings("unchecked") + public ActionForward getOrganisationIdByName(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws IOException { + String organisationName = WebUtil.readStrParam(request, "name"); + OrganisationAction.service = AdminServiceProxy.getService(getServlet().getServletContext()); + List organisations = service.findByProperty(Organisation.class, "name", organisationName); + if (!organisations.isEmpty()) { + response.setContentType("text/plain;charset=utf-8"); + response.getWriter().print(organisations.get(0).getOrganisationId()); } + return null; + } -} // end Action + private ActionForward error(ActionMapping mapping, HttpServletRequest request) { + OrganisationAction.messageService = AdminServiceProxy.getMessageService(getServlet().getServletContext()); + request.setAttribute("errorName", "OrganisationAction"); + request.setAttribute("errorMessage", OrganisationAction.messageService.getMessage("error.authorisation")); + return mapping.findForward("error"); + } + /*public ActionForward remove(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){ + Integer orgId = WebUtil.readIntParam(request,"orgId"); + getService().deleteById(Organisation.class,orgId); + Integer parentId = WebUtil.readIntParam(request,"parentId"); + request.setAttribute("org",parentId); + return mapping.findForward("orglist"); + }*/ + + @SuppressWarnings("unchecked") + private void initLocalesAndStatus() { + if ((OrganisationAction.locales == null) + || ((OrganisationAction.status == null) && (OrganisationAction.service != null))) { + OrganisationAction.locales = OrganisationAction.service.findAll(SupportedLocale.class); + OrganisationAction.status = OrganisationAction.service.findAll(OrganisationState.class); + Collections.sort(OrganisationAction.locales); + } + } +} \ No newline at end of file