Index: lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupAction.java =================================================================== diff -u -r0b087f6386e7e8cfd72dd34e3eec37533cb97d5f -rc52aeb0714241a71f1842e1e39b1363ef274e1a7 --- lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupAction.java (.../OrganisationGroupAction.java) (revision 0b087f6386e7e8cfd72dd34e3eec37533cb97d5f) +++ lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupAction.java (.../OrganisationGroupAction.java) (revision c52aeb0714241a71f1842e1e39b1363ef274e1a7) @@ -29,7 +29,9 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; @@ -45,6 +47,7 @@ import org.apache.tomcat.util.json.JSONArray; import org.apache.tomcat.util.json.JSONException; import org.apache.tomcat.util.json.JSONObject; +import org.lamsfoundation.lams.contentrepository.InvalidParameterException; import org.lamsfoundation.lams.learningdesign.Group; import org.lamsfoundation.lams.learningdesign.Grouping; import org.lamsfoundation.lams.learningdesign.GroupingActivity; @@ -306,9 +309,11 @@ /** * Saves a course grouping. + * + * @throws InvalidParameterException */ public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) throws JSONException { + HttpServletResponse response) throws JSONException, InvalidParameterException { // check if user is allowed to edit groups Integer userId = getUserDTO().getUserID(); int organisationId = WebUtil.readIntParam(request, AttributeNames.PARAM_ORGANISATION_ID); @@ -371,8 +376,24 @@ orgGrouping = new OrganisationGrouping(); orgGrouping.setOrganisationId(organisationId); } - orgGrouping.setName(orgGroupingJSON.getString("name")); + // check if there is no grouping with the same name + String orgGroupingName = orgGroupingJSON.getString("name"); + if (!orgGroupingName.equals(orgGrouping.getName())) { + Map duplicateCheckProperties = new TreeMap(); + duplicateCheckProperties.put("organisationId", organisationId); + duplicateCheckProperties.put("name", orgGroupingName); + + OrganisationGrouping duplicateOrgGrouping = (OrganisationGrouping) getUserManagementService() + .findByProperties(OrganisationGrouping.class, duplicateCheckProperties); + if (duplicateOrgGrouping == null) { + orgGrouping.setName(orgGroupingName); + } else { + throw new InvalidParameterException("Grouping with name \"" + orgGroupingName + "\" already exists"); + } + + } + getUserManagementService().saveOrganisationGrouping(orgGrouping, orgGroups); return null; } Index: lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupServlet.java =================================================================== diff -u -rac293c56d01d08018c14f01dfa851a772dfde23a -rc52aeb0714241a71f1842e1e39b1363ef274e1a7 --- lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupServlet.java (.../OrganisationGroupServlet.java) (revision ac293c56d01d08018c14f01dfa851a772dfde23a) +++ lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupServlet.java (.../OrganisationGroupServlet.java) (revision c52aeb0714241a71f1842e1e39b1363ef274e1a7) @@ -22,7 +22,10 @@ import java.io.IOException; import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.TreeMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -63,8 +66,8 @@ private static final String METHOD_ADD_LEARNERS = "addLearners"; private static final String METHOD_REMOVE_LEARNERS = "removeLearners"; - private static final String PARAM_GROUPING_ID = "groupingId"; - private static final String PARAM_GROUP_ID = "groupId"; + private static final String PARAM_GROUPING_NAME = "groupingName"; + private static final String PARAM_GROUP_NAME = "groupName"; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -121,26 +124,19 @@ try { String method = request.getParameter(CentralConstants.PARAM_METHOD); - String responseString = null; if (OrganisationGroupServlet.METHOD_ADD_GROUPING.equals(method)) { - responseString = addGrouping(request, organisation); + addGrouping(request, organisation); } else if (OrganisationGroupServlet.METHOD_REMOVE_GROUPING.equals(method)) { - responseString = removeGrouping(request, organisation.getOrganisationId()); + removeGrouping(request, organisation.getOrganisationId()); } else if (OrganisationGroupServlet.METHOD_ADD_GROUP.equals(method)) { - responseString = addGroup(request, organisation.getOrganisationId()); + addGroup(request, organisation.getOrganisationId()); } else if (OrganisationGroupServlet.METHOD_REMOVE_GROUP.equals(method)) { - responseString = removeGroup(request, organisation.getOrganisationId()); + removeGroup(request, organisation.getOrganisationId()); } else if (OrganisationGroupServlet.METHOD_ADD_LEARNERS.equals(method)) { - responseString = addLearners(request, serverMap, organisation.getOrganisationId()); + addLearners(request, serverMap, organisation.getOrganisationId()); } else if (OrganisationGroupServlet.METHOD_REMOVE_LEARNERS.equals(method)) { - responseString = removeLearners(request, serverMap, organisation.getOrganisationId()); + removeLearners(request, serverMap, organisation.getOrganisationId()); } - - if (responseString != null) { - response.setContentType("text/plain"); - response.setCharacterEncoding("UTF-8"); - response.getWriter().write(responseString); - } } catch (ServletException e) { OrganisationGroupServlet.log.error(e); response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Course group action failed: " + e.getMessage()); @@ -158,13 +154,22 @@ doGet(request, response); } - private String addGrouping(HttpServletRequest request, Organisation organisation) throws ServletException { - String groupingName = request.getParameter(CentralConstants.ATTR_NAME); + private void addGrouping(HttpServletRequest request, Organisation organisation) throws ServletException { + String groupingName = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_NAME); if (StringUtils.isBlank(groupingName)) { throw new ServletException("Missing \"" + CentralConstants.ATTR_NAME + "\" parameter"); } - OrganisationGrouping grouping = new OrganisationGrouping(organisation.getOrganisationId(), groupingName); + + // check if grouping with such name does not exist already + OrganisationGrouping grouping = findGrouping(organisation.getOrganisationId(), groupingName); + if (grouping != null) { + throw new ServletException("Grouping with name \"" + groupingName + "\" exists in organisation with ID " + + organisation.getOrganisationId()); + } + + grouping = new OrganisationGrouping(organisation.getOrganisationId(), groupingName); getUserManagementService().saveOrganisationGrouping(grouping, null); + Long groupingId = grouping.getGroupingId(); if (groupingId == null) { throw new ServletException("Grouping could not be created"); @@ -174,52 +179,55 @@ OrganisationGroupServlet.log.debug("Created course grouping \"" + groupingName + "\" with ID " + groupingId + " in organisation with ID " + organisation.getOrganisationId()); } - return groupingId.toString(); } - private String removeGrouping(HttpServletRequest request, Integer organisationId) throws ServletException { - String groupingId = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_ID); - if (StringUtils.isBlank(groupingId)) { - throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUPING_ID + "\" parameter"); + private void removeGrouping(HttpServletRequest request, Integer organisationId) throws ServletException { + String groupingName = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_NAME); + if (StringUtils.isBlank(groupingName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUPING_NAME + "\" parameter"); } - OrganisationGrouping grouping = (OrganisationGrouping) getUserManagementService().findById( - OrganisationGrouping.class, Long.valueOf(groupingId)); + + OrganisationGrouping grouping = findGrouping(organisationId, groupingName); if (grouping == null) { - return null; + // if grouping does not exist, just ignore it + return; } - if (!grouping.getOrganisationId().equals(organisationId)) { - throw new ServletException("Grouping with ID " + groupingId + " is not a part of the given course"); - } + getUserManagementService().delete(grouping); if (OrganisationGroupServlet.log.isDebugEnabled()) { - OrganisationGroupServlet.log.debug("Deleted course grouping with ID " + groupingId + OrganisationGroupServlet.log.debug("Deleted course grouping with ID " + groupingName + " from organisation with ID " + organisationId); } - return null; } - private String addGroup(HttpServletRequest request, Integer organisationId) throws ServletException { - String groupingId = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_ID); - if (StringUtils.isBlank(groupingId)) { - throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUPING_ID + "\" parameter"); + private void addGroup(HttpServletRequest request, Integer organisationId) throws ServletException { + String groupingName = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_NAME); + if (StringUtils.isBlank(groupingName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUPING_NAME + "\" parameter"); } - String groupName = request.getParameter(CentralConstants.ATTR_NAME); + String groupName = request.getParameter(OrganisationGroupServlet.PARAM_GROUP_NAME); if (StringUtils.isBlank(groupName)) { - throw new ServletException("Missing \"" + CentralConstants.ATTR_NAME + "\" parameter"); + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUP_NAME + "\" parameter"); } - OrganisationGrouping grouping = (OrganisationGrouping) getUserManagementService().findById( - OrganisationGrouping.class, Long.valueOf(groupingId)); + + OrganisationGrouping grouping = findGrouping(organisationId, groupingName); if (grouping == null) { - throw new ServletException("Grouping with ID " + groupingId + " does not exist"); + throw new ServletException("Grouping with name \"" + groupingName + + "\" does not exist in organisation with ID " + organisationId); } - if (!grouping.getOrganisationId().equals(organisationId)) { - throw new ServletException("Grouping with ID " + groupingId + " is not a part of the given course"); + + // check group name uniqueness + Set groups = new HashSet(grouping.getGroups()); + for (OrganisationGroup group : groups) { + if (group.getName().equals(groupName)) { + throw new ServletException("Group with name \"" + groupingName + "\" exists in grouping with ID " + + grouping.getGroupingId()); + } } OrganisationGroup newGroup = new OrganisationGroup(); newGroup.setName(groupName); - Set groups = new HashSet(grouping.getGroups()); groups.add(newGroup); getUserManagementService().saveOrganisationGrouping(grouping, groups); @@ -232,60 +240,64 @@ OrganisationGroupServlet.log.debug("Created course group \"" + groupName + "\" with ID " + groupId + " in organisation with ID " + organisationId); } - return groupId.toString(); } - private String removeGroup(HttpServletRequest request, Integer organisationId) throws ServletException { - String groupId = request.getParameter(OrganisationGroupServlet.PARAM_GROUP_ID); - if (StringUtils.isBlank(groupId)) { - throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUP_ID + "\" parameter"); + private void removeGroup(HttpServletRequest request, Integer organisationId) throws ServletException { + String groupingName = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_NAME); + if (StringUtils.isBlank(groupingName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUPING_NAME + "\" parameter"); } - OrganisationGroup group = (OrganisationGroup) getUserManagementService().findById(OrganisationGroup.class, - Long.valueOf(groupId)); + String groupName = request.getParameter(OrganisationGroupServlet.PARAM_GROUP_NAME); + if (StringUtils.isBlank(groupName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUP_NAME + "\" parameter"); + } + OrganisationGrouping grouping = findGrouping(organisationId, groupingName); + if (grouping == null) { + // ignore if grouping does not exist + return; + } + OrganisationGroup group = findGroup(grouping.getGroupingId(), groupName); if (group == null) { - return null; + // ignore if group does not exist + return; } - OrganisationGrouping grouping = (OrganisationGrouping) getUserManagementService().findById( - OrganisationGrouping.class, group.getGroupingId()); - if (!grouping.getOrganisationId().equals(organisationId)) { - throw new ServletException("Group with ID " + groupId + " is not a part of the given course"); - } - Set groups = new HashSet(grouping.getGroups()); groups.remove(group); getUserManagementService().saveOrganisationGrouping(grouping, groups); if (OrganisationGroupServlet.log.isDebugEnabled()) { - OrganisationGroupServlet.log.debug("Deleted course group with ID " + groupId + OrganisationGroupServlet.log.debug("Deleted course group with ID " + group.getGroupId() + " from organisation with ID " + organisationId); } - return null; } - private String addLearners(HttpServletRequest request, ExtServerOrgMap serverMap, Integer organisationId) + private void addLearners(HttpServletRequest request, ExtServerOrgMap serverMap, Integer organisationId) throws ServletException { - String groupId = request.getParameter(OrganisationGroupServlet.PARAM_GROUP_ID); - if (StringUtils.isBlank(groupId)) { - throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUP_ID + "\" parameter"); + String groupingName = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_NAME); + if (StringUtils.isBlank(groupingName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUPING_NAME + "\" parameter"); } + String groupName = request.getParameter(OrganisationGroupServlet.PARAM_GROUP_NAME); + if (StringUtils.isBlank(groupName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUP_NAME + "\" parameter"); + } String learnerIds = request.getParameter(CentralConstants.PARAM_LEARNER_IDS); if (StringUtils.isBlank(learnerIds)) { throw new ServletException("Missing \"" + CentralConstants.PARAM_LEARNER_IDS + "\" parameter"); } - OrganisationGroup group = (OrganisationGroup) getUserManagementService().findById(OrganisationGroup.class, - Long.valueOf(groupId)); + + OrganisationGrouping grouping = findGrouping(organisationId, groupingName); + if (grouping == null) { + throw new ServletException("Grouping with name \"" + groupingName + "\" does not exist"); + } + OrganisationGroup group = findGroup(grouping.getGroupingId(), groupName); if (group == null) { - throw new ServletException("Group with ID " + groupId + " does not exist"); + throw new ServletException("Group with name \"" + groupName + "\" does not exist in grouping with ID " + + grouping.getGroupingId()); } - OrganisationGrouping grouping = (OrganisationGrouping) getUserManagementService().findById( - OrganisationGrouping.class, group.getGroupingId()); - if (!grouping.getOrganisationId().equals(organisationId)) { - throw new ServletException("Group with ID " + groupId + " is not a part of the given course"); - } String[] learnerLoginArray = learnerIds.split(","); - boolean learnersAdded = false; StringBuilder addedLearnerLogins = new StringBuilder(); for (String learnerLogin : learnerLoginArray) { @@ -310,35 +322,37 @@ if (OrganisationGroupServlet.log.isDebugEnabled()) { OrganisationGroupServlet.log.debug("Added learners " + addedLearnerLogins + " to course group with ID " - + groupId + " in organisation with ID " + organisationId); + + group.getGroupId() + " in organisation with ID " + organisationId); } } - return null; + return; } - private String removeLearners(HttpServletRequest request, ExtServerOrgMap serverMap, Integer organisationId) + private void removeLearners(HttpServletRequest request, ExtServerOrgMap serverMap, Integer organisationId) throws ServletException { - String groupId = request.getParameter(OrganisationGroupServlet.PARAM_GROUP_ID); - if (StringUtils.isBlank(groupId)) { - throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUP_ID + "\" parameter"); + String groupingName = request.getParameter(OrganisationGroupServlet.PARAM_GROUPING_NAME); + if (StringUtils.isBlank(groupingName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUPING_NAME + "\" parameter"); } + String groupName = request.getParameter(OrganisationGroupServlet.PARAM_GROUP_NAME); + if (StringUtils.isBlank(groupName)) { + throw new ServletException("Missing \"" + OrganisationGroupServlet.PARAM_GROUP_NAME + "\" parameter"); + } String learnerIds = request.getParameter(CentralConstants.PARAM_LEARNER_IDS); if (StringUtils.isBlank(learnerIds)) { throw new ServletException("Missing \"" + CentralConstants.PARAM_LEARNER_IDS + "\" parameter"); } - OrganisationGroup group = (OrganisationGroup) getUserManagementService().findById(OrganisationGroup.class, - Long.valueOf(groupId)); + OrganisationGrouping grouping = findGrouping(organisationId, groupingName); + if (grouping == null) { + throw new ServletException("Grouping with name \"" + groupingName + "\" does not exist"); + } + OrganisationGroup group = findGroup(grouping.getGroupingId(), groupName); if (group == null) { - throw new ServletException("Group with ID " + groupId + " does not exist"); + throw new ServletException("Group with name \"" + groupName + "\" does not exist in grouping with ID " + + grouping.getGroupingId()); } - OrganisationGrouping grouping = (OrganisationGrouping) getUserManagementService().findById( - OrganisationGrouping.class, group.getGroupingId()); - if (!grouping.getOrganisationId().equals(organisationId)) { - throw new ServletException("Group with ID " + groupId + " is not a part of the given course"); - } String[] learnerLoginArray = learnerIds.split(","); - boolean learnersRemoved = false; StringBuilder removedLearnerLogins = new StringBuilder(); for (String learnerLogin : learnerLoginArray) { @@ -364,12 +378,32 @@ getUserManagementService().saveOrganisationGrouping(grouping, grouping.getGroups()); if (OrganisationGroupServlet.log.isDebugEnabled()) { OrganisationGroupServlet.log.debug("Removed learners " + removedLearnerLogins + " from group with ID " - + groupId + " in organisation with ID " + organisationId); + + group.getGroupId() + " in organisation with ID " + organisationId); } } - return null; + return; } + @SuppressWarnings("unchecked") + private OrganisationGrouping findGrouping(Integer organisationId, String groupingName) { + Map queryProperties = new TreeMap(); + queryProperties.put("organisationId", organisationId); + queryProperties.put("name", groupingName); + List result = getUserManagementService().findByProperties(OrganisationGrouping.class, + queryProperties); + return (result == null) || result.isEmpty() ? null : result.get(0); + } + + @SuppressWarnings("unchecked") + private OrganisationGroup findGroup(Long groupingId, String groupName) { + Map queryProperties = new TreeMap(); + queryProperties.put("groupingId", groupingId); + queryProperties.put("name", groupName); + List result = getUserManagementService().findByProperties(OrganisationGroup.class, + queryProperties); + return (result == null) || result.isEmpty() ? null : result.get(0); + } + private IntegrationService getService() { if (OrganisationGroupServlet.integrationService == null) { OrganisationGroupServlet.integrationService = (IntegrationService) WebApplicationContextUtils