Skip to content

Commit

Permalink
FLAG-81: Fix the module warning (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManojLL authored Aug 18, 2024
1 parent 296e263 commit 03d2542
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public abstract class PatientFlagMetadataBundle extends AbstractMetadataBundle {

@SuppressWarnings("unchecked")
protected void install(TagDescriptor tagDescriptor) {
Tag tag = new Tag(tagDescriptor.name());
if (StringUtils.isNotBlank(tagDescriptor.uuid())) {
Expand Down Expand Up @@ -48,6 +49,7 @@ protected void install(PriorityDescriptor priorityDescriptor) {
this.install((OpenmrsObject) priority);
}

@SuppressWarnings("unchecked")
protected void install(FlagDescriptor flagDescriptor) {
Flag flag = new Flag(flagDescriptor.name(), flagDescriptor.criteria(), flagDescriptor.message());
if (StringUtils.isNotBlank(flagDescriptor.priority())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public Flag getFlagByName(String name) throws DAOException {
criteria.add(Restrictions.eq("name", name));
}

@SuppressWarnings("unchecked")
List<Flag> list = criteria.list();

if (list.size() == 1) {
Expand Down Expand Up @@ -185,6 +186,7 @@ public Tag getTag(String name) {
criteria.add(Restrictions.eq("name", name));
}

@SuppressWarnings("unchecked")
List<Tag> list = criteria.list();

if (list.size() == 1) {
Expand Down Expand Up @@ -269,6 +271,7 @@ public Priority getPriorityByName(String name) throws DAOException {
criteria.add(Restrictions.eq("name", name));
}

@SuppressWarnings("unchecked")
List<Priority> list = criteria.list();

if (list.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package org.openmrs.module.patientflags.evaluator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.openmrs.Cohort;
Expand All @@ -30,7 +32,7 @@ public class CQLFlagEvaluator implements FlagEvaluator {

@Override
public Boolean eval(Flag flag, Patient patient, Map<Object, Object> context) {
if(patient.isVoided())
if(patient.getVoided())
throw new APIException("Unable to evaluate CQL flag " + flag.getName() + " against voided patient");

// create a Cohort that contains just the patient, and then evaluate that Cohort
Expand Down Expand Up @@ -65,12 +67,14 @@ public Cohort evalCohort(Flag flag, Cohort cohort, Map<Object, Object> context)
}

private List<Patient> getPatients(Cohort cohort) {

Set<Integer> memberIds = new HashSet<>();
cohort.getMemberships().forEach(member -> memberIds.add(member.getPatientId()));

PatientService patientService = Context.getPatientService();

List<Patient> patients = new ArrayList<Patient>();
if (cohort != null) {
for (Integer patientId : cohort.getMemberIds()) {
for (Integer patientId : memberIds) {
patients.add(patientService.getPatient(patientId));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class GroovyFlagEvaluator implements FlagEvaluator {
*/
public Boolean eval(Flag flag, Patient patient, Map<Object, Object> context) {

if(patient.isVoided())
if(patient.getVoided())
throw new APIException("Unable to evaluate Groovy flag " + flag.getName() + " against voided patient");

// create a Cohort that contains just the patient to test, and then evaluate that Cohort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class SQLFlagEvaluator implements FlagEvaluator {
*/
public Boolean eval(Flag flag, Patient patient, Map<Object, Object> context) {

if(patient.isVoided())
if(patient.getVoided())
throw new APIException("Unable to evaluate SQL flag " + flag.getName() + " against voided patient");

String criteria = flag.getCriteria();
Expand Down Expand Up @@ -98,7 +98,7 @@ public Cohort evalCohort(Flag flag, Cohort cohort, Map<Object, Object> context)
Integer patient_id = (Integer) row.get(0);

// only add patients that haven't been voided
if(!Context.getPatientService().getPatient(patient_id).isVoided())
if(!Context.getPatientService().getPatient(patient_id).getVoided())
resultCohort.addMember((Integer) row.get(0));
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public String evalMessage(Flag flag, int patientId) {
log.info("Replacing values in "+message);

Patient p = Context.getPatientService().getPatient(patientId);
if(p.isVoided())
if(p.getVoided())
throw new APIException("VOIDED PATIENT");

String criteria = flag.getCriteria();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.openmrs.CohortMembership;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.api.context.Daemon;
Expand Down Expand Up @@ -78,7 +81,7 @@ private static void generatePatientFlags(Flag flag, FlagService service) {
}

private static void generatePatientFlagsForFlagAndPatient(Flag flag, FlagService service){
if (!flag.getEnabled() || flag.isRetired()) {
if (!flag.getEnabled() || flag.getRetired()) {
return;
}

Expand All @@ -87,9 +90,15 @@ private static void generatePatientFlagsForFlagAndPatient(Flag flag, FlagService
if (cohort == null) {
return;
}

java.util.Set<Integer> members = cohort.getMemberIds();

Set<Integer> members = cohort.getMemberships()
.stream()
.map(CohortMembership::getPatientId)
.collect(Collectors.toSet());

for (Integer patientId : members) {

@SuppressWarnings("unchecked")
List<String> flgs = (List<String>)context.get(patientId);
if (flgs != null) {
for (String flg : flgs) {
Expand All @@ -108,6 +117,8 @@ private void generatePatientFlags(Patient patient, FlagService service) {
HashMap<Object, Object> context = new HashMap<Object, Object>();
List<Flag> flags = service.generateFlagsForPatient(patient, context);
for (Flag flag : flags) {

@SuppressWarnings("unchecked")
List<String> flgs = (List<String>)context.get(patient.getPatientId());
if (flgs != null) {
for (String flg : flgs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package org.openmrs.module.patientflags;

import junit.framework.Assert;
import org.junit.Assert;

import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.servlet.http.HttpServletRequest;

import org.openmrs.Cohort;
import org.openmrs.CohortMembership;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.patientflags.Flag;
Expand Down Expand Up @@ -116,7 +117,12 @@ public ModelAndView processSubmit(@ModelAttribute("flag") Flag flag, BindingResu
List<Map<String, Object>> fpl = new ArrayList<Map<String, Object>>();

if(flaggedPatients != null){
Set<Integer> idsFp = flaggedPatients.getMemberIds();

Set<Integer> idsFp = flaggedPatients.getMemberships()
.stream()
.map(CohortMembership::getPatientId)
.collect(Collectors.toSet());

for (Integer patientId : idsFp) {
Map<String, Object> mapFp = new HashMap<String, Object>();

Expand All @@ -140,31 +146,34 @@ public ModelAndView processSubmit(@ModelAttribute("flag") Flag flag, BindingResu

@RequestMapping(method = RequestMethod.GET, params = "tags")
public ModelAndView processSubmit(@ModelAttribute("filter") Filter filter, BindingResult result, SessionStatus status) {

if (result.hasErrors()) {
return new ModelAndView("/module/patientflags/findFlaggedPatients");
}

FlagService flagService = Context.getService(FlagService.class);

// get the flags to test on
List<Flag> flags = flagService.getFlagsByFilter(filter);

// returns a map of flagged Patients and the respective flags
Cohort flaggedPatients = flagService.getFlaggedPatients(flags, null);
Set<Integer> flaggedPatients = flagService.getFlaggedPatients(flags, null).getMemberships()
.stream()
.map(CohortMembership::getPatientId)
.collect(Collectors.toSet());

Cohort allPatients = new Cohort();

Context.getPatientService().getAllPatients().forEach( patient ->
allPatients.addMember(patient.getPatientId())
);

// create the model map
ModelMap model = new ModelMap();
model.addAttribute("allPatients", allPatients);

Set<Integer> flaggedPatientIds = flaggedPatients.getMemberIds();
model.addAttribute("allPatients", allPatients);

List<Map<String, Integer>> flaggedPatientList = flaggedPatientIds.stream()
List<Map<String, Integer>> flaggedPatientList = flaggedPatients.stream()
.map(patientId -> {
Map<String, Integer> mapFlaggerPatient = new HashMap<>();
mapFlaggerPatient.put("patientId", patientId);
Expand All @@ -177,7 +186,7 @@ public ModelAndView processSubmit(@ModelAttribute("filter") Filter filter, Bindi

// clears the command object from the session
status.setComplete();

// displays the query results
return new ModelAndView("/module/patientflags/findFlaggedPatientsResults", model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.openmrs.module.patientflags.web;

import org.openmrs.Cohort;
import org.openmrs.CohortMembership;
import org.openmrs.Patient;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
Expand All @@ -29,6 +30,8 @@
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Controller that handles retrieving Patients that match a Flag criteria for email output
Expand Down Expand Up @@ -58,6 +61,12 @@ public ModelAndView processRequest(@ModelAttribute("flag") Flag flag, BindingRes
FlagService flagService = Context.getService(FlagService.class);
flag = flagService.getFlag(flag.getFlagId());
Cohort flaggedPatients = flagService.getFlaggedPatients(flag, null);

Set<Integer> patientIds = flaggedPatients.getMemberships()
.stream()
.map(CohortMembership::getPatientId)
.collect(Collectors.toSet());

Cohort allPatients = new Cohort();
List<Patient> patients = Context.getPatientService().getAllPatients();
for (Patient i : patients) {
Expand All @@ -69,7 +78,7 @@ public ModelAndView processRequest(@ModelAttribute("flag") Flag flag, BindingRes
model.addAttribute("flag", flag);
model.addAttribute("allPatients", allPatients);
if(flaggedPatients != null){
model.addAttribute("flaggedPatients", flaggedPatients.getMemberIds());
model.addAttribute("flaggedPatients", patientIds);
}
else{
model.addAttribute("flaggedPatients", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public PatientFlag save(PatientFlag delegate) {
*/
@Override
protected void delete(PatientFlag delegate, String reason, RequestContext context) throws ResponseException {
if (delegate.isVoided()) {
if (delegate.getVoided()) {
// DELETE is idempotent, so we return success here
return;
}
Expand All @@ -98,7 +98,7 @@ protected void delete(PatientFlag delegate, String reason, RequestContext contex
*/
@Override
protected PatientFlag undelete(PatientFlag delegate, RequestContext context) throws ResponseException {
if (delegate.isVoided()) {
if (delegate.getVoided()) {
Context.getService(FlagService.class).unvoidPatientFlag(delegate);
}
return delegate;
Expand Down

0 comments on commit 03d2542

Please sign in to comment.