Index: lams_admin/conf/language/lams/ApplicationResources.properties =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_admin/conf/language/lams/ApplicationResources.properties (.../ApplicationResources.properties) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_admin/conf/language/lams/ApplicationResources.properties (.../ApplicationResources.properties) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -608,16 +608,14 @@ label.summary =Summary label.full.policy =Full policy label.view.previous.versions =View previous versions -label.set.status.to =Set status to "{0}" +label.activate =Activate +label.deactivate =Deactivate label.minor.change =Minor change label.create.new.draft =Create a new draft label.policy.consents =Policy consents label.consented =Consented? label.consented.on =Consented on label.user.consents =User consents -label. = -label. = -label. = -label. = +label.user.full.name =Full name #======= End labels: Exported 582 labels for en AU ===== Index: lams_admin/src/java/org/lamsfoundation/lams/admin/web/action/PolicyManagementAction.java =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_admin/src/java/org/lamsfoundation/lams/admin/web/action/PolicyManagementAction.java (.../PolicyManagementAction.java) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_admin/src/java/org/lamsfoundation/lams/admin/web/action/PolicyManagementAction.java (.../PolicyManagementAction.java) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -1,15 +1,12 @@ package org.lamsfoundation.lams.admin.web.action; +import java.io.IOException; +import java.text.DateFormat; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.Collection; import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; -import java.util.Properties; -import java.util.Set; import java.util.UUID; import javax.servlet.http.HttpServletRequest; @@ -22,19 +19,22 @@ import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; -import org.lamsfoundation.lams.admin.web.dto.UserPolicyConsentDTO; +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.policies.Policy; -import org.lamsfoundation.lams.policies.PolicyConsent; +import org.lamsfoundation.lams.policies.dto.UserPolicyConsentDTO; import org.lamsfoundation.lams.policies.service.IPolicyService; import org.lamsfoundation.lams.usermanagement.User; import org.lamsfoundation.lams.usermanagement.dto.UserDTO; import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; -import org.lamsfoundation.lams.util.FileUtil; +import org.lamsfoundation.lams.util.DateUtil; import org.lamsfoundation.lams.util.WebUtil; import org.lamsfoundation.lams.web.session.SessionManager; import org.lamsfoundation.lams.web.util.AttributeNames; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; +import org.springframework.web.util.HtmlUtils; /** * Handles Policy management requests. @@ -49,7 +49,7 @@ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { + HttpServletResponse response) throws JSONException, IOException { if (policyService == null) { WebApplicationContext wac = WebApplicationContextUtils @@ -64,66 +64,35 @@ String method = WebUtil.readStrParam(request, "method", true); if (StringUtils.equals(method, "list") || isCancelled(request)) { - return list(mapping, form, request, response); + handleListPage(request); + return mapping.findForward("policyList"); } else if (StringUtils.equals(method, "edit")) { return edit(mapping, form, request, response); } else if (StringUtils.equals(method, "save")) { return save(mapping, form, request, response); } else if (StringUtils.equals(method, "displayUserConsents")) { return displayUserConsents(mapping, form, request, response); + } else if (StringUtils.equals(method, "getConsentsGridData")) { + return getConsentsGridData(mapping, form, request, response); } else if (StringUtils.equals(method, "viewPreviousVersions")) { - return viewPreviousVersions(mapping, form, request, response); - } else if (StringUtils.equals(method, "changeStatus")) { - return changeStatus(mapping, form, request, response); - } + handleViewPreviousVersionsPage(request); + return mapping.findForward("policyList"); + } else if (StringUtils.equals(method, "togglePolicyStatus")) { + return togglePolicyStatus(mapping, form, request, response); + } return mapping.findForward("error"); } - private ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { + /** + * Prepare all data for the "list" page. + */ + private void handleListPage(HttpServletRequest request) { + Collection policies = policyService.getPoliciesOfDistinctVersions(); + request.setAttribute("policies", policies); - List policies = policyService.getAllPoliciesWithUserConsentsCount(); - - //calculate which policies have previous version instances - HashMap policyCount = new LinkedHashMap(); - for (Policy policy : policies) { - Long policyId = policy.getPolicyId(); - int previousVersionsCount = policyCount.get(policyId) == null ? 0 : policyCount.get(policyId); - policyCount.put(policy.getPolicyId(), previousVersionsCount + 1); - } - - //filter out older versioned policies - HashMap filteredPolicies = new LinkedHashMap(); - for (Policy policy : policies) { - Long policyId = policy.getPolicyId(); - boolean hasPreviousVersions = policyCount.get(policyId) != null && policyCount.get(policyId) > 1; - policy.setPreviousVersions(hasPreviousVersions); - - if (filteredPolicies.containsKey(policyId)) { - Policy alreadyAddedPolicy = filteredPolicies.get(policyId); - Integer policyStateId = policy.getPolicyStateId(); - - //active policy has priority - if (Policy.STATUS_ACTIVE.equals(policyStateId)) { - filteredPolicies.put(policyId, policy); - - //if neither are active - newer has priority - } else if (!Policy.STATUS_ACTIVE.equals(alreadyAddedPolicy.getPolicyStateId()) - && policy.getLastModified().after(alreadyAddedPolicy.getLastModified())) { - filteredPolicies.put(policyId, policy); - } - - } else { - filteredPolicies.put(policyId, policy); - } - - } - request.setAttribute("policies", filteredPolicies.values()); - int userCount = userManagementService.getCountUsers(); request.setAttribute("userCount", userCount); - return mapping.findForward("policyList"); } private ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, @@ -201,7 +170,6 @@ if (!policyFromFamily.getUid().equals(policyUid) && Policy.STATUS_ACTIVE.equals(policyFromFamily.getPolicyStateId())) { policyFromFamily.setPolicyStateId(Policy.STATUS_INACTIVE); - policyFromFamily.setLastModified(new Date()); userManagementService.save(policyFromFamily); } } @@ -210,7 +178,6 @@ Policy oldPolicy = policyService.getPolicyByUid((Long) policyUid); if (Policy.STATUS_ACTIVE.equals(oldPolicy.getPolicyStateId())) { oldPolicy.setPolicyStateId(Policy.STATUS_INACTIVE); - oldPolicy.setLastModified(new Date()); userManagementService.save(oldPolicy); } } @@ -235,59 +202,105 @@ policy.setLastModified(new Date()); userManagementService.save(policy); - return mapping.findForward("policyListMethod"); + return mapping.findForward("policyListRedirect"); } private ActionForward displayUserConsents(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { Long policyUid = WebUtil.readLongParam(request, "policyUid"); Policy policy = policyService.getPolicyByUid(policyUid); - Set consents = policy.getConsents(); request.setAttribute("policy", policy); - - List users = userManagementService.getAllUsers(); - LinkedList userPolicyConsents = new LinkedList(); - for (User user : users) { - UserPolicyConsentDTO userPolicyConsent = new UserPolicyConsentDTO(user.getUserId(), user.getFirstName(), user.getLastName(), user.getLogin()); - - for (PolicyConsent consent : consents) { - if (consent.getUser().getUserId().equals(user.getUserId())) { - userPolicyConsent.setConsentGivenByUser(true); - userPolicyConsent.setDateAgreedOn(consent.getDateAgreedOn()); - } - } - userPolicyConsents.add(userPolicyConsent); - } - request.setAttribute("userPolicyConsents", userPolicyConsents); return mapping.findForward("userConsents"); } - private ActionForward viewPreviousVersions(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { - + /** + * Prepares all data for "vewPreviousVersions" page. + */ + private void handleViewPreviousVersionsPage(HttpServletRequest request) { long policyId = WebUtil.readLongParam(request, "policyId"); - List policyFamily = policyService.getPreviousVersionsPolicies(policyId); - //remove the first one from the list - if (policyFamily.size() > 1) { + List previousVersions = policyService.getPreviousVersionsPolicies(policyId); +// remove the first one from the list +// if (previousVersion.size() > 1) { // policyFamily.remove(policyFamily.size() - 1); - } - request.setAttribute("policies", policyFamily); +// } + request.setAttribute("policies", previousVersions); int userCount = userManagementService.getCountUsers(); request.setAttribute("userCount", userCount); - request.setAttribute("viewPreviousVersions", "true"); - return mapping.findForward("policyList"); + + request.setAttribute("viewPreviousVersions", true); } - private ActionForward changeStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, + private ActionForward togglePolicyStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { long policyUid = WebUtil.readLongParam(request, "policyUid"); - int newStatus = WebUtil.readIntParam(request, "newStatus"); - policyService.changePolicyStatus(policyUid, newStatus); + policyService.togglePolicyStatus(policyUid); - return mapping.findForward("policyListMethod"); + Boolean isViewPreviousVersions = (Boolean) WebUtil.readBooleanParam(request, "viewPreviousVersions", false); + if (isViewPreviousVersions) { + handleViewPreviousVersionsPage(request); + } else { + handleListPage(request); + } + return mapping.findForward("policyTable"); } + + /** + */ + @SuppressWarnings("unchecked") + public ActionForward getConsentsGridData(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws JSONException, IOException { + Long policyUid = WebUtil.readLongParam(request, "policyUid"); + Policy policy = policyService.getPolicyByUid(policyUid); + + // Getting the params passed in from the jqGrid + int page = WebUtil.readIntParam(request, "page"); + int rowLimit = WebUtil.readIntParam(request, "rows"); + String sortOrder = WebUtil.readStrParam(request, "sord"); + String sortBy = WebUtil.readStrParam(request, "sidx", true); + if (StringUtils.isEmpty(sortBy)) { + sortBy = "fullName"; + } + String searchString = WebUtil.readStrParam(request, "fullName", true); + + List consentDtos = policyService.getConsentDtosByPolicy(policyUid, page-1, rowLimit, sortBy, sortOrder, searchString); + int countUsers = userManagementService.getCountUsers(searchString); + int totalPages = new Double( + Math.ceil(new Integer(countUsers).doubleValue() / new Integer(rowLimit).doubleValue())) + .intValue(); + + JSONObject responcedata = new JSONObject(); + responcedata.put("total", "" + totalPages); + responcedata.put("page", "" + page); + responcedata.put("records", "" + countUsers); + + JSONArray rows = new JSONArray(); + for (UserPolicyConsentDTO consentDto : consentDtos) { + JSONArray userData = new JSONArray(); + userData.put(consentDto.getUserID()); + String firstName = consentDto.getFirstName() == null ? "" : consentDto.getFirstName(); + String lastName = consentDto.getLastName() == null ? "" : consentDto.getLastName(); + String fullName = HtmlUtils.htmlEscape(lastName) + " " + HtmlUtils.htmlEscape(firstName); + userData.put(fullName); + String consentedIcon = consentDto.isConsentGivenByUser() + ? "" + : "-"; + userData.put(consentedIcon); + String dateAgreedOn = consentDto.getDateAgreedOn() == null ? "" + : DateUtil.convertToStringForJSON(consentDto.getDateAgreedOn(), request.getLocale()); + userData.put(HtmlUtils.htmlEscape(dateAgreedOn)); + + JSONObject cellobj = new JSONObject(); + cellobj.put("id", "" + consentDto.getUserID()); + cellobj.put("cell", userData); + rows.put(cellobj); + } + responcedata.put("rows", rows); + response.setContentType("application/json;charset=utf-8"); + response.getWriter().print(new String(responcedata.toString())); + return null; + } } Fisheye: Tag 86521284ae546f32e01ae0a6bc1dfb99232d19cd refers to a dead (removed) revision in file `lams_admin/src/java/org/lamsfoundation/lams/admin/web/dto/UserPolicyConsentDTO.java'. Fisheye: No comparison available. Pass `N' to diff? Index: lams_admin/web/WEB-INF/struts-config.xml =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_admin/web/WEB-INF/struts-config.xml (.../struts-config.xml) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_admin/web/WEB-INF/struts-config.xml (.../struts-config.xml) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -896,11 +896,16 @@ redirect="false" /> + - + Index: lams_admin/web/policies/policies.jsp =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_admin/web/policies/policies.jsp (.../policies.jsp) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_admin/web/policies/policies.jsp (.../policies.jsp) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -27,137 +27,51 @@ 'data' : { 'orgID' : "orgID" }, - 'height' : 600, + 'height' : 500, 'width' : 600, 'title' : "", 'open' : function() { $('iframe', this).attr('src', url); } }, true, exists); }); + + //handler for "change-status" links + $(document).on("click", ".change-status-link", function() { + var policyUid = $(this).data("policy-uid"); + var policyId = $(this).data("policy-id"); + + $("#policy-table").load( + "policyManagement.do", + { + method: "togglePolicyStatus", + policyUid: policyUid, + policyId: policyId, + viewPreviousVersions: "${viewPreviousVersions}" + }, + function() {} + ); + }); + });

- +

- - - - - +
+ <%@ include file="policyTable.jsp"%> +
- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ${fn:length(policy.consents)}/${userCount} - - - - - - - - - - - - - - - - - - 2 - - - - 1 - - - -    - - - - - - - -    - - - - - - - - -
- - + Index: lams_admin/web/policies/policyTable.jsp =================================================================== diff -u --- lams_admin/web/policies/policyTable.jsp (revision 0) +++ lams_admin/web/policies/policyTable.jsp (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -0,0 +1,100 @@ +<%@ include file="/taglibs.jsp"%> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ${fn:length(policy.consents)}/${userCount} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file Index: lams_admin/web/policies/userConsents.jsp =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_admin/web/policies/userConsents.jsp (.../userConsents.jsp) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_admin/web/policies/userConsents.jsp (.../userConsents.jsp) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -5,75 +5,107 @@ - + + + + + + + +
-

+

-
- - - - - - - - - - - - - - - -
+ + :

- - - - - - - - - - - - - -
 
- - - - - - - - - - - - - - - - - - - - -
+
+
+
+
+
Index: lams_central/web/policyConsents.jsp =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_central/web/policyConsents.jsp (.../policyConsents.jsp) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_central/web/policyConsents.jsp (.../policyConsents.jsp) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -18,9 +18,12 @@ .alert.alert-danger { margin-right: 5px; } - .full-policy, .policy-details { - padding-left: 15px; + .policy-details { + padding-left: 10px; } + h5 { + margin-bottom: 5px; + } @@ -73,50 +76,47 @@ -
+

-

+
- : - - - - - - - - - - - - - - -
- - - -
- - + : + + + + + + + + + + + + + + + +
+ +
+ + +
- -
- +
+ + +
+
-
-
- -
-
Index: lams_common/src/java/org/lamsfoundation/lams/policies/dao/IPolicyDAO.java =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_common/src/java/org/lamsfoundation/lams/policies/dao/IPolicyDAO.java (.../IPolicyDAO.java) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_common/src/java/org/lamsfoundation/lams/policies/dao/IPolicyDAO.java (.../IPolicyDAO.java) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -28,6 +28,7 @@ import org.lamsfoundation.lams.policies.Policy; import org.lamsfoundation.lams.policies.PolicyConsent; import org.lamsfoundation.lams.policies.PolicyDTO; +import org.lamsfoundation.lams.policies.dto.UserPolicyConsentDTO; public interface IPolicyDAO { @@ -54,5 +55,8 @@ * @return */ List getConsentsByUserId(Integer userId); + + List getConsentDtosByPolicy(Long policyUid, int page, int size, String sortBy, + String sortOrder, String searchString); } Index: lams_common/src/java/org/lamsfoundation/lams/policies/dao/hibernate/PolicyDAO.java =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_common/src/java/org/lamsfoundation/lams/policies/dao/hibernate/PolicyDAO.java (.../PolicyDAO.java) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_common/src/java/org/lamsfoundation/lams/policies/dao/hibernate/PolicyDAO.java (.../PolicyDAO.java) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -1,16 +1,22 @@ package org.lamsfoundation.lams.policies.dao.hibernate; +import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.StringUtils; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.lamsfoundation.lams.dao.hibernate.LAMSBaseDAO; import org.lamsfoundation.lams.policies.Policy; import org.lamsfoundation.lams.policies.PolicyConsent; import org.lamsfoundation.lams.policies.PolicyDTO; import org.lamsfoundation.lams.policies.dao.IPolicyDAO; +import org.lamsfoundation.lams.policies.dto.UserPolicyConsentDTO; +import org.lamsfoundation.lams.usermanagement.dao.hibernate.UserDAO; +import org.lamsfoundation.lams.usermanagement.dto.UserDTO; import org.springframework.stereotype.Repository; import com.sun.webkit.PolicyClient; @@ -108,4 +114,75 @@ return (List) doFind(query, userId); } + @Override + public List getConsentDtosByPolicy(Long policyUid, int page, int size, String sortBy, + String sortOrder, String searchString) { + + String queryText = "SELECT user.user_id, user.login, user.first_name, user.last_name, policyConsent.uid IS NOT NULL as consented, policyConsent.date_agreed_on " + + " FROM lams_user AS user" + + " LEFT JOIN lams_policy_consent AS policyConsent ON policyConsent.user_id = user.user_id AND policyConsent.policy_uid = :policyUid " + + " WHERE user.disabled_flag=0"; + + switch (sortBy) { + case "userId": + sortBy = "user.userId + 0 "; + break; + case "login": + sortBy = "user.login "; + break; + case "fullName": + sortBy = "(CONCAT(user.last_name, ' ', user.first_name)) "; + break; + case "consented": + sortBy = "consented "; + break; + case "consentedOn": + sortBy = "policyConsent.date_agreed_on "; + break; + } + + StringBuilder queryBuilder = new StringBuilder(queryText); + + // support for custom search from a toolbar + if (StringUtils.isNotBlank(searchString)) { + String[] tokens = searchString.trim().split("\\s+"); + for (String token : tokens) { + String escToken = StringEscapeUtils.escapeSql(token).replace("\\", "\\\\"); + queryBuilder.append(" AND (user.first_name LIKE '%").append(escToken) + .append("%' OR user.last_name LIKE '%").append(escToken).append("%' OR user.login LIKE '%") + .append(escToken).append("%')"); + } + } + + //order by + queryBuilder.append(" ORDER BY ").append(sortBy).append(sortOrder); + + Query query = getSession().createSQLQuery(queryBuilder.toString()); + query.setLong("policyUid", policyUid); + query.setMaxResults(size); + query.setFirstResult(page * size); + List list = query.list(); + + //group by userId as long as it returns all completed visitLogs for each user + List policyConsentDtos = new ArrayList(); + for (Object[] element : list) { + Integer userId = ((Number) element[0]).intValue(); + String login = (String) element[1]; + String firstName = (String) element[2]; + String lastName = (String) element[3]; + + UserPolicyConsentDTO policyConsentDto = new UserPolicyConsentDTO(userId, firstName, lastName, login); + + boolean isConsentGivenByUser = ((Number) element[4]).intValue() == 1; + policyConsentDto.setConsentGivenByUser(isConsentGivenByUser); + + Date dateAgreedOn = (Date) element[5]; + policyConsentDto.setDateAgreedOn(dateAgreedOn); + + policyConsentDtos.add(policyConsentDto); + } + + return policyConsentDtos; + } + } Index: lams_common/src/java/org/lamsfoundation/lams/policies/dto/UserPolicyConsentDTO.java =================================================================== diff -u --- lams_common/src/java/org/lamsfoundation/lams/policies/dto/UserPolicyConsentDTO.java (revision 0) +++ lams_common/src/java/org/lamsfoundation/lams/policies/dto/UserPolicyConsentDTO.java (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -0,0 +1,37 @@ +package org.lamsfoundation.lams.policies.dto; + +import java.util.Date; + +import org.lamsfoundation.lams.usermanagement.dto.UserBasicDTO; + +/** + * Used for displaying which users consented to the policy. + * + * @author Andrey Balan + */ +public class UserPolicyConsentDTO extends UserBasicDTO { + private boolean isConsentGivenByUser; + + private Date dateAgreedOn; + + public UserPolicyConsentDTO(Integer userID, String firstName, String lastName, String login) { + super(userID, firstName, lastName, login); + } + + public void setConsentGivenByUser(boolean isConsentGivenByUser) { + this.isConsentGivenByUser = isConsentGivenByUser; + } + + public boolean isConsentGivenByUser() { + return isConsentGivenByUser; + } + + public Date getDateAgreedOn() { + return dateAgreedOn; + } + + public void setDateAgreedOn(Date dateAgreedOn) { + this.dateAgreedOn = dateAgreedOn; + } + +} Index: lams_common/src/java/org/lamsfoundation/lams/policies/service/IPolicyService.java =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_common/src/java/org/lamsfoundation/lams/policies/service/IPolicyService.java (.../IPolicyService.java) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_common/src/java/org/lamsfoundation/lams/policies/service/IPolicyService.java (.../IPolicyService.java) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -23,11 +23,13 @@ package org.lamsfoundation.lams.policies.service; +import java.util.Collection; import java.util.List; import org.lamsfoundation.lams.policies.Policy; import org.lamsfoundation.lams.policies.PolicyConsent; import org.lamsfoundation.lams.policies.PolicyDTO; +import org.lamsfoundation.lams.policies.dto.UserPolicyConsentDTO; public interface IPolicyService { @@ -36,11 +38,12 @@ Policy getPolicyByUid(Long uid); /** - * Return all active policies together with how many users have consented to each of them + * Return distinct versions policies (i.e. one policy per policyId). Also return number of users that have consented + * to each policy. * * @return */ - List getAllPoliciesWithUserConsentsCount(); + Collection getPoliciesOfDistinctVersions(); List getPreviousVersionsPolicies(Long policyId); @@ -61,13 +64,14 @@ * @return */ List getConsentsByUserId(Integer userId); - + + List getConsentDtosByPolicy(Long policyUid, int page, int size, String sortBy, + String sortOrder, String searchString); + /** - * Changes policy status as set by the admin. In case of deactivating policy, it also removes all associated - * consents. + * Toggles policy status as requested by admin * * @param policyUid - * @param newPolicyStatus */ - void changePolicyStatus(Long policyUid, Integer newPolicyStatus); + void togglePolicyStatus(Long policyUid); } Index: lams_common/src/java/org/lamsfoundation/lams/policies/service/PolicyService.java =================================================================== diff -u -r4c134bb552fd30a24e610ddd532846ec38a3a51c -r86521284ae546f32e01ae0a6bc1dfb99232d19cd --- lams_common/src/java/org/lamsfoundation/lams/policies/service/PolicyService.java (.../PolicyService.java) (revision 4c134bb552fd30a24e610ddd532846ec38a3a51c) +++ lams_common/src/java/org/lamsfoundation/lams/policies/service/PolicyService.java (.../PolicyService.java) (revision 86521284ae546f32e01ae0a6bc1dfb99232d19cd) @@ -1,17 +1,16 @@ package org.lamsfoundation.lams.policies.service; -import java.util.Date; +import java.util.Collection; import java.util.HashMap; -import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import org.lamsfoundation.lams.lesson.service.ILessonService; import org.lamsfoundation.lams.policies.Policy; import org.lamsfoundation.lams.policies.PolicyConsent; import org.lamsfoundation.lams.policies.PolicyDTO; import org.lamsfoundation.lams.policies.dao.IPolicyDAO; -import org.lamsfoundation.lams.usermanagement.OrganisationState; -import org.lamsfoundation.lams.usermanagement.OrganisationType; +import org.lamsfoundation.lams.policies.dto.UserPolicyConsentDTO; import org.lamsfoundation.lams.usermanagement.User; import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; @@ -38,30 +37,65 @@ } @Override - public void changePolicyStatus(Long policyUid, Integer newPolicyStatus) { + public void togglePolicyStatus(Long policyUid) { Policy policy = policyDAO.getPolicyByUid(policyUid); + Integer newPolicyStatus = Policy.STATUS_ACTIVE.equals(policy.getPolicyStateId()) ? Policy.STATUS_INACTIVE : Policy.STATUS_ACTIVE; policy.setPolicyStateId(newPolicyStatus); - policy.setLastModified(new Date()); userManagementService.save(policy); -// //remove according user consents -// if (Policy.STATUS_INACTIVE.equals(newPolicyStatus) || Policy.STATUS_DRAFT.equals(newPolicyStatus)) { -// HashMap properties = new HashMap(); -// properties.put("policy.uid", policyUid); -// List consents = userManagementService.findByProperties(PolicyConsent.class, properties); -// -// Iterator iter = consents.iterator(); -// while (iter.hasNext()) { -// PolicyConsent consent = iter.next(); -// userManagementService.delete(consent); -// iter.remove(); -// } -// } + //in case policy gets activated, deactivate all its other versions + if (Policy.STATUS_ACTIVE.equals(newPolicyStatus)) { + Long policyId = policy.getPolicyId(); + List previousVersions = getPreviousVersionsPolicies(policyId); + for (Policy previousVersion : previousVersions) { + if (!previousVersion.getUid().equals(policyUid) + && Policy.STATUS_ACTIVE.equals(previousVersion.getPolicyStateId())) { + previousVersion.setPolicyStateId(Policy.STATUS_INACTIVE); + userManagementService.save(previousVersion); + } + } + } } @Override - public List getAllPoliciesWithUserConsentsCount() { - return policyDAO.getAllPoliciesWithUserConsentsCount(); + public Collection getPoliciesOfDistinctVersions() { + List policies = policyDAO.getAllPoliciesWithUserConsentsCount(); + + //calculate which policies have previous version instances + HashMap policyCount = new LinkedHashMap(); + for (Policy policy : policies) { + Long policyId = policy.getPolicyId(); + int previousVersionsCount = policyCount.get(policyId) == null ? 0 : policyCount.get(policyId); + policyCount.put(policy.getPolicyId(), previousVersionsCount + 1); + } + + //filter out older versioned policies + HashMap filteredPolicies = new LinkedHashMap(); + for (Policy policy : policies) { + Long policyId = policy.getPolicyId(); + boolean hasPreviousVersions = policyCount.get(policyId) != null && policyCount.get(policyId) > 1; + policy.setPreviousVersions(hasPreviousVersions); + + if (filteredPolicies.containsKey(policyId)) { + Policy alreadyAddedPolicy = filteredPolicies.get(policyId); + Integer policyStateId = policy.getPolicyStateId(); + + //active policy has priority + if (Policy.STATUS_ACTIVE.equals(policyStateId)) { + filteredPolicies.put(policyId, policy); + + //if neither are active - newer has priority + } else if (!Policy.STATUS_ACTIVE.equals(alreadyAddedPolicy.getPolicyStateId()) + && policy.getLastModified().after(alreadyAddedPolicy.getLastModified())) { + filteredPolicies.put(policyId, policy); + } + + } else { + filteredPolicies.put(policyId, policy); + } + + } + return filteredPolicies.values(); } @Override @@ -83,6 +117,12 @@ public List getConsentsByUserId(Integer userId) { return policyDAO.getConsentsByUserId(userId); } + + @Override + public List getConsentDtosByPolicy(Long policyUid, int page, int size, String sortBy, + String sortOrder, String searchString) { + return policyDAO.getConsentDtosByPolicy(policyUid, page, size, sortBy, sortOrder, searchString); + } public void setPolicyDAO(IPolicyDAO policyDAO) { this.policyDAO = policyDAO;