From ea2bacc66403a7643fbfef82f7ce1b53f4b6cfe1 Mon Sep 17 00:00:00 2001 From: Mutesasira Moses Date: Mon, 24 Jul 2023 01:00:42 +0200 Subject: [PATCH] process the calculated values --- .../service/LogbookPersistServiceImpl.java | 3 + .../action/util/TestCalculatedUtil.java | 182 ++++++++++++++++++ .../rest/CalculatedValueRestController.java | 18 -- .../dao/ResultCalculationDAO.java | 13 +- .../daoimpl/ResultCalculationDAOImpl.java | 73 ++++++- .../service/ResultCalculationService.java | 11 ++ .../service/ResultCalculationServiceImpl.java | 24 ++- .../valueholder/ResultCalculation.java | 32 +-- 8 files changed, 316 insertions(+), 40 deletions(-) create mode 100644 src/main/java/org/openelisglobal/testcalculated/action/util/TestCalculatedUtil.java diff --git a/src/main/java/org/openelisglobal/result/service/LogbookPersistServiceImpl.java b/src/main/java/org/openelisglobal/result/service/LogbookPersistServiceImpl.java index 48eaa8e22a..7863902c7e 100644 --- a/src/main/java/org/openelisglobal/result/service/LogbookPersistServiceImpl.java +++ b/src/main/java/org/openelisglobal/result/service/LogbookPersistServiceImpl.java @@ -28,6 +28,7 @@ import org.openelisglobal.sample.valueholder.Sample; import org.openelisglobal.sampleitem.valueholder.SampleItem; import org.openelisglobal.spring.util.SpringContext; +import org.openelisglobal.testcalculated.action.util.TestCalculatedUtil; import org.openelisglobal.testreflex.action.util.TestReflexBean; import org.openelisglobal.testreflex.action.util.TestReflexUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -167,12 +168,14 @@ private void saveReferralsWithRequiredObjects(ReferralSet referralSet, String sy protected List setTestReflexes(ResultsUpdateDataSet actionDataSet, String sysUserId) { TestReflexUtil testReflexUtil = new TestReflexUtil(); + TestCalculatedUtil testCalulatedUtil = new TestCalculatedUtil(); List allResults = actionDataSet.getNewResults(); allResults.addAll(actionDataSet.getModifiedResults()); List reflexAnalysises = testReflexUtil .addNewTestsToDBForReflexTests(convertToTestReflexBeanList(allResults), sysUserId); testReflexUtil.updateModifiedReflexes(convertToTestReflexBeanList(actionDataSet.getModifiedResults()), sysUserId); + testCalulatedUtil.addNewTestsToDBForCalculatedTests(allResults, sysUserId) ; return reflexAnalysises; } diff --git a/src/main/java/org/openelisglobal/testcalculated/action/util/TestCalculatedUtil.java b/src/main/java/org/openelisglobal/testcalculated/action/util/TestCalculatedUtil.java new file mode 100644 index 0000000000..c3622b20ac --- /dev/null +++ b/src/main/java/org/openelisglobal/testcalculated/action/util/TestCalculatedUtil.java @@ -0,0 +1,182 @@ +package org.openelisglobal.testcalculated.action.util; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.openelisglobal.analysis.valueholder.Analysis; +import org.openelisglobal.common.util.DateUtil; +import org.openelisglobal.result.action.util.ResultSet; +import org.openelisglobal.result.service.ResultService; +import org.openelisglobal.result.valueholder.Result; +import org.openelisglobal.spring.util.SpringContext; +import org.openelisglobal.test.service.TestService; +import org.openelisglobal.test.valueholder.Test; +import org.openelisglobal.testcalculated.service.ResultCalculationService; +import org.openelisglobal.testcalculated.service.TestCalculationService; +import org.openelisglobal.testcalculated.valueholder.Calculation; +import org.openelisglobal.testcalculated.valueholder.Operation; +import org.openelisglobal.testcalculated.valueholder.ResultCalculation; +import org.openelisglobal.testresult.service.TestResultService; +import org.springframework.context.annotation.DependsOn; +import org.springframework.stereotype.Service; + +@Service +@DependsOn({ "springContext" }) +public class TestCalculatedUtil { + + private TestResultService testResultService = SpringContext.getBean(TestResultService.class); + + private ResultCalculationService resultcalculationService = SpringContext.getBean(ResultCalculationService.class); + + private TestCalculationService calculationService = SpringContext.getBean(TestCalculationService.class); + + private TestService testService = SpringContext.getBean(TestService.class); + + private ResultService resultService = SpringContext.getBean(ResultService.class); + + public List addNewTestsToDBForCalculatedTests(List resultSetList, String sysUserId) + throws IllegalStateException { + + for (ResultSet resultSet : resultSetList) { + List calculations = calculationService.getAll(); + for (Calculation calculation : calculations) { + if (!calculation.getActive()) { + break; + } + List resultCalculations = resultcalculationService + .getResultCalculationByPatientAndCalculation(resultSet.patient, calculation); + if (resultCalculations.isEmpty()) { + Boolean createResultCalculation = false; + for (Operation oper : calculation.getOperations()) { + if (oper.getType().equals(Operation.OperationType.TEST_RESULT)) { + if (Integer.valueOf(oper.getValue()) + .equals(Integer.valueOf(resultSet.result.getTestResult().getTest().getId()))) { + createResultCalculation = true; + break; + } + } + } + if (createResultCalculation) { + ResultCalculation calc = new ResultCalculation(); + calc.setCalculation(calculation); + calc.setPatient(resultSet.patient); + Set tests = new HashSet<>(); + calculation.getOperations().forEach(oper -> { + if (oper.getType().equals(Operation.OperationType.TEST_RESULT)) { + Test test = testService.getActiveTestById(Integer.valueOf(oper.getValue())); + tests.add(test); + } + }); + calc.setTest(tests); + Map map = new HashMap<>(); + tests.forEach(test -> { + map.put(Integer.valueOf(test.getId()), null); + }); + // insert innitail result value + map.put(Integer.valueOf(resultSet.result.getTestResult().getTest().getId()), + Integer.valueOf(resultSet.result.getId())); + calc.setTestResultMap(map); + System.out.println(calc.getTestResultMap()); + resultcalculationService.insert(calc); + } + + } else { + for (ResultCalculation resultCalculation : resultCalculations) { + resultCalculation.getTestResultMap().put( + Integer.valueOf(resultSet.result.getTestResult().getTest().getId()), + Integer.valueOf(resultSet.result.getId())); + resultcalculationService.update(resultCalculation); + } + } + } + List resultCalculations = resultcalculationService + .getResultCalculationByPatientAndTest(resultSet.patient, resultSet.result.getTestResult().getTest()); + + if (!resultCalculations.isEmpty()) { + for (ResultCalculation resultCalculation : resultCalculations) { + Boolean missingParams = false; + for (Map.Entry entry : resultCalculation.getTestResultMap().entrySet()) { + Integer key = entry.getKey(); + Integer value = entry.getValue(); + if (entry.getValue() == null) { + missingParams = true; + } + } + if (!missingParams) { + Calculation calculation = resultCalculation.getCalculation(); + + StringBuffer function = new StringBuffer(); + calculation.getOperations().forEach(operation -> { + switch (operation.getType()) { + case TEST_RESULT: + addNumericOperation(operation ,resultCalculation ,function ,Operation.OperationType.TEST_RESULT.toString()); + break; + case INTEGER: + function.append(Integer.valueOf(operation.getValue())).append(" "); + break; + case MATH_FUNCTION: + if (operation.getValue().equals(Operation.IN_NORMAL_RANGE.toLowerCase())) { + int order = operation.getOrder(); + Operation prevOperation = calculation.getOperations().get(order - 1); + addNumericOperation(prevOperation ,resultCalculation ,function ,"IN_NORMAL_RANGE"); + + } else if (operation.getValue().equals(Operation.OUTSIDE_NORMAL_RANGE.toString())) { + int order = operation.getOrder(); + Operation prevOperation = calculation.getOperations().get(order - 1); + addNumericOperation(prevOperation ,resultCalculation ,function , "OUTSIDE_NORMAL_RANGE"); + } else { + function.append(operation.getValue()).append(" "); + } + break; + case PATIENT_ATTRIBUTE: + if (operation.getValue().equals(Operation.PatientAttribute.AGE.toString())) { + int age = DateUtil.getAgeInYears( + new Date(resultSet.patient.getBirthDate().getTime()), new Date()); + function.append(age); + } + break; + + } + }); + System.out.println(function); + } else { + } + } + } + } + + return null; + } + + private void addNumericOperation(Operation operation, ResultCalculation resultCalculation, StringBuffer function, + String inputType) { + Test test = testService.getActiveTestById(Integer.valueOf(operation.getValue())); + if (test != null) { + Integer resultId = resultCalculation.getTestResultMap().get(Integer.valueOf(test.getId())); + Result result = resultService.get(resultId.toString()); + if (result != null) { + if (result.getResultType().equals("N")) { + switch (inputType) { + case "TEST_RESULT": + function.append(result.getValue()).append(" "); + break; + case "IN_NORMAL_RANGE": + function.append(" >= ").append(result.getMinNormal()).append(" && ").append(result.getValue()) + .append(" <= ").append(result.getMaxNormal()).append(" "); + break; + case "OUTSIDE_NORMAL_RANGE": + function.append(" <= ").append(result.getMinNormal()).append(" || ").append(result.getValue()) + .append(" >= ").append(result.getMaxNormal()).append(" "); + break; + } + } + } + } + + } + +} diff --git a/src/main/java/org/openelisglobal/testcalculated/controller/rest/CalculatedValueRestController.java b/src/main/java/org/openelisglobal/testcalculated/controller/rest/CalculatedValueRestController.java index 0327ed7536..02b9ee75e6 100644 --- a/src/main/java/org/openelisglobal/testcalculated/controller/rest/CalculatedValueRestController.java +++ b/src/main/java/org/openelisglobal/testcalculated/controller/rest/CalculatedValueRestController.java @@ -80,24 +80,6 @@ public void saveReflexRule(HttpServletRequest request, @RequestBody Calculation } else { testCalculationService.save(calculation); } - // Patient pat = patientService.getAll().get(0); - // Result result = resultService.getAll().get(0); - - // ResultCalculation calc = new ResultCalculation(); - // calc.setCalculation(calculation); - // calc.setPatient(pat); - // List tests = new ArrayList<>(); - // calculation.getOperations().forEach(oper -> { - // if (oper.getType().equals(Operation.OperationType.TEST_RESULT)) { - // Test test = testService.getActiveTestById(Integer.valueOf(oper.getValue())); - // tests.add(test); - // } - // }); - // calc.setTest(tests); - // Map map = new HashMap<>(); - // map.put(Integer.valueOf(result.getAnalysis().getTest().getId()), Integer.valueOf(result.getId())); - // calc.setTestRsultMap(map); - // resultCalculationService.insert(calc); } diff --git a/src/main/java/org/openelisglobal/testcalculated/dao/ResultCalculationDAO.java b/src/main/java/org/openelisglobal/testcalculated/dao/ResultCalculationDAO.java index 16b6aff913..8923f35444 100644 --- a/src/main/java/org/openelisglobal/testcalculated/dao/ResultCalculationDAO.java +++ b/src/main/java/org/openelisglobal/testcalculated/dao/ResultCalculationDAO.java @@ -1,8 +1,19 @@ package org.openelisglobal.testcalculated.dao; +import java.util.List; + import org.openelisglobal.common.dao.BaseDAO; +import org.openelisglobal.common.exception.LIMSRuntimeException; +import org.openelisglobal.patient.valueholder.Patient; +import org.openelisglobal.test.valueholder.Test; +import org.openelisglobal.testcalculated.valueholder.Calculation; import org.openelisglobal.testcalculated.valueholder.ResultCalculation; public interface ResultCalculationDAO extends BaseDAO{ - + + List getResultCalculationByPatientAndTest(Patient patient , Test test) throws LIMSRuntimeException; + + List getResultCalculationByTest(Test test) throws LIMSRuntimeException; + + List getResultCalculationByPatientAndCalculation(Patient patient , Calculation calculation) throws LIMSRuntimeException; } diff --git a/src/main/java/org/openelisglobal/testcalculated/daoimpl/ResultCalculationDAOImpl.java b/src/main/java/org/openelisglobal/testcalculated/daoimpl/ResultCalculationDAOImpl.java index 4fc085ba9d..ab3a6fdf16 100644 --- a/src/main/java/org/openelisglobal/testcalculated/daoimpl/ResultCalculationDAOImpl.java +++ b/src/main/java/org/openelisglobal/testcalculated/daoimpl/ResultCalculationDAOImpl.java @@ -1,18 +1,85 @@ package org.openelisglobal.testcalculated.daoimpl; +import java.util.Collections; +import java.util.List; + import javax.transaction.Transactional; +import org.hibernate.Session; +import org.hibernate.query.Query; import org.openelisglobal.common.daoimpl.BaseDAOImpl; +import org.openelisglobal.patient.valueholder.Patient; +import org.openelisglobal.test.valueholder.Test; import org.openelisglobal.testcalculated.dao.ResultCalculationDAO; +import org.openelisglobal.testcalculated.valueholder.Calculation; import org.openelisglobal.testcalculated.valueholder.ResultCalculation; import org.springframework.stereotype.Component; @Component @Transactional -public class ResultCalculationDAOImpl extends BaseDAOImpl implements ResultCalculationDAO{ - +public class ResultCalculationDAOImpl extends BaseDAOImpl implements ResultCalculationDAO { + public ResultCalculationDAOImpl() { - super(ResultCalculation.class); + super(ResultCalculation.class); + } + + @Override + public List getResultCalculationByPatientAndTest(Patient patient, Test test) { + try { + + String sql = "from ResultCalculation r WHERE r.patient.id = :patientId AND :test IN ELEMENTS(r.test)"; + Query query = entityManager.unwrap(Session.class).createQuery(sql, ResultCalculation.class); + query.setParameter("patientId", Integer.parseInt(patient.getId())); + query.setParameter("test", test); + + List results = query.list(); + if (results.size() > 0) { + return results; + } + } + catch (RuntimeException e) { + handleException(e, "getResultCalculationByPatientAndTest"); + } + return Collections.emptyList(); + } + + @Override + public List getResultCalculationByTest(Test test) { + try { + + String sql = "from ResultCalculation r JOIN r.test t WHERE t.id = :testId"; + Query query = entityManager.unwrap(Session.class).createQuery(sql, ResultCalculation.class); + query.setParameter("testId", Integer.parseInt(test.getId())); + + List results = query.list(); + if (results.size() > 0) { + return results; + } + } + catch (RuntimeException e) { + handleException(e, "getResultCalculationByPatientAndTest"); + } + return Collections.emptyList(); + } + + @Override + public List getResultCalculationByPatientAndCalculation(Patient patient, Calculation calculation) { + try { + + String sql = "from ResultCalculation r WHERE r.patient.id = :patientId AND r.calculation.id = :calculationId"; + Query query = entityManager.unwrap(Session.class).createQuery(sql, ResultCalculation.class); + query.setParameter("patientId", Integer.parseInt(patient.getId())); + query.setParameter("calculationId", calculation.getId()); + + List results = query.list(); + if (results.size() > 0) { + return results; + } + } + catch (RuntimeException e) { + handleException(e, "getResultCalculationByPatientAndCalculation"); + } + return Collections.emptyList(); } } diff --git a/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationService.java b/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationService.java index ee56955d86..7dcfa42758 100644 --- a/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationService.java +++ b/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationService.java @@ -1,8 +1,19 @@ package org.openelisglobal.testcalculated.service; +import java.util.List; + import org.openelisglobal.common.service.BaseObjectService; +import org.openelisglobal.patient.valueholder.Patient; +import org.openelisglobal.test.valueholder.Test; +import org.openelisglobal.testcalculated.valueholder.Calculation; import org.openelisglobal.testcalculated.valueholder.ResultCalculation; public interface ResultCalculationService extends BaseObjectService{ + + List getResultCalculationByPatientAndTest(Patient patient , Test test); + + List getResultCalculationByTest(Test test); + + List getResultCalculationByPatientAndCalculation(Patient patient , Calculation calculation); } diff --git a/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationServiceImpl.java b/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationServiceImpl.java index 906a3338ab..7eaeb4f45e 100644 --- a/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationServiceImpl.java +++ b/src/main/java/org/openelisglobal/testcalculated/service/ResultCalculationServiceImpl.java @@ -1,8 +1,13 @@ package org.openelisglobal.testcalculated.service; +import java.util.List; + import org.openelisglobal.common.dao.BaseDAO; import org.openelisglobal.common.service.BaseObjectServiceImpl; +import org.openelisglobal.patient.valueholder.Patient; +import org.openelisglobal.test.valueholder.Test; import org.openelisglobal.testcalculated.dao.ResultCalculationDAO; +import org.openelisglobal.testcalculated.valueholder.Calculation; import org.openelisglobal.testcalculated.valueholder.ResultCalculation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -10,7 +15,7 @@ @Service public class ResultCalculationServiceImpl extends BaseObjectServiceImpl implements ResultCalculationService{ @Autowired - ResultCalculationDAO resultCalculationDAOdao; + ResultCalculationDAO resultCalculationDAO; public ResultCalculationServiceImpl() { super(ResultCalculation.class); @@ -18,7 +23,22 @@ public ResultCalculationServiceImpl() { @Override protected BaseDAO getBaseObjectDAO() { - return resultCalculationDAOdao; + return resultCalculationDAO; + } + + @Override + public List getResultCalculationByPatientAndTest(Patient patient, Test test) { + return resultCalculationDAO.getResultCalculationByPatientAndTest(patient, test); + } + + @Override + public List getResultCalculationByTest(Test test) { + return resultCalculationDAO.getResultCalculationByTest(test); + } + + @Override + public List getResultCalculationByPatientAndCalculation(Patient patient, Calculation calculation) { + return resultCalculationDAO.getResultCalculationByPatientAndCalculation(patient, calculation); } } diff --git a/src/main/java/org/openelisglobal/testcalculated/valueholder/ResultCalculation.java b/src/main/java/org/openelisglobal/testcalculated/valueholder/ResultCalculation.java index abb2902d05..618dddd88b 100644 --- a/src/main/java/org/openelisglobal/testcalculated/valueholder/ResultCalculation.java +++ b/src/main/java/org/openelisglobal/testcalculated/valueholder/ResultCalculation.java @@ -1,7 +1,7 @@ package org.openelisglobal.testcalculated.valueholder; -import java.util.List; import java.util.Map; +import java.util.Set; import javax.persistence.CollectionTable; import javax.persistence.Column; @@ -41,13 +41,13 @@ public class ResultCalculation extends BaseObject { @ElementCollection @CollectionTable(name = "test_operations", joinColumns = @JoinColumn(name = "result_calculation_id", referencedColumnName = "id")) @Column(name = "test_id") - private List test; + private Set test; @ElementCollection @CollectionTable(name = "test_result_map", joinColumns = @JoinColumn(name = "result_calculation_id", referencedColumnName = "id")) @MapKeyColumn(name = "test_id") @Column(name = "result_id") - private Map testRsultMap; + private Map testResultMap; @Override public Integer getId() { @@ -58,11 +58,7 @@ public Integer getId() { public void setId(Integer id) { this.id = id; } - - public Map getTestRsultMap() { - return testRsultMap; - } - + public Calculation getCalculation() { return calculation; } @@ -78,16 +74,20 @@ public Patient getPatient() { public void setPatient(Patient patient) { this.patient = patient; } - - public void setTestRsultMap(Map testRsultMap) { - this.testRsultMap = testRsultMap; - } - - public List getTest() { + + public Set getTest() { return test; } - - public void setTest(List test) { + + public void setTest(Set test) { this.test = test; } + + public Map getTestResultMap() { + return testResultMap; + } + + public void setTestResultMap(Map testResultMap) { + this.testResultMap = testResultMap; + } }