From c9ccacb4fc51e254c3f4c5e7472bc052148c09c1 Mon Sep 17 00:00:00 2001 From: rehammuzzamil Date: Fri, 10 Jul 2020 14:05:51 +0500 Subject: [PATCH 1/3] 57 : Add functionality to Populate a Task from action Dynamic variables --- .../smartregister/pathevaluator/dao/TaskDao.java | 4 ++++ .../pathevaluator/plan/PlanEvaluator.java | 10 ++++++++-- .../pathevaluator/task/TaskHelper.java | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java b/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java index ef35263d..58710a04 100644 --- a/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java +++ b/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java @@ -31,5 +31,9 @@ public interface TaskDao { void saveTask(org.smartregister.domain.Task task, QuestionnaireResponse questionnaireResponse); boolean checkIfTaskExists(String baseEntityId, String planIdentifier, String code); + + org.smartregister.domain.Task getTaskById(String id); + + org.smartregister.domain.Task updateTask(org.smartregister.domain.Task task); } diff --git a/src/main/java/org/smartregister/pathevaluator/plan/PlanEvaluator.java b/src/main/java/org/smartregister/pathevaluator/plan/PlanEvaluator.java index e67d00ae..70602b36 100644 --- a/src/main/java/org/smartregister/pathevaluator/plan/PlanEvaluator.java +++ b/src/main/java/org/smartregister/pathevaluator/plan/PlanEvaluator.java @@ -3,6 +3,7 @@ import java.util.Collections; import java.util.List; +import org.smartregister.domain.Action; import org.smartregister.domain.Jurisdiction; import org.smartregister.domain.PlanDefinition; import org.smartregister.pathevaluator.PathEvaluatorLibrary; @@ -108,8 +109,13 @@ private void evaluatePlan(PlanDefinition planDefinition, TriggerType triggerEven questionnaireResponse == null ? resource : questionnaireResponse.toBuilder().contained(Collections.singleton(resource)).build(), action, planDefinition.getIdentifier(), triggerEvent)) { - taskHelper.generateTask(resource, action, planDefinition.getIdentifier(), jurisdiction.getCode(), - username,questionnaireResponse); + if (action.getType().equals(Action.ActionType.UPDATE)) { + taskHelper.updateTask(resource, action); + } else { + taskHelper.generateTask(resource, action, planDefinition.getIdentifier(), jurisdiction.getCode(), + username, questionnaireResponse); + } + } }); } diff --git a/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java b/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java index 6636fbbb..30baab90 100644 --- a/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java +++ b/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java @@ -70,6 +70,20 @@ public void generateTask(DomainResource resource, Action action, String planIden logger.info("Created task " + task.toString()); } } + + public void updateTask(DomainResource resource, Action action) { + TaskDao taskDao = PathEvaluatorLibrary.getInstance().getTaskProvider().getTaskDao(); + Task task = taskDao.getTaskById(resource.getId()); + for (DynamicValue dynamicValue : action.getDynamicValue()) { + if (dynamicValue.getPath().equals("status")) { + task.setStatus(Task.TaskStatus.valueOf(dynamicValue.getExpression().getExpression())); + } + if (dynamicValue.getPath().equals("businessStatus")) { + task.setBusinessStatus(dynamicValue.getExpression().getExpression()); + } + } + taskDao.updateTask(task); + } private DateTime getDateTime(ExecutionPeriod executionPeriod, boolean start) { if (executionPeriod != null) { From e502502fc4fcd555d05c2202913e7cb3007f7d28 Mon Sep 17 00:00:00 2001 From: rehammuzzamil Date: Mon, 13 Jul 2020 14:29:11 +0500 Subject: [PATCH 2/3] 57 : Add functionality to Populate a Task from action Dynamic variables --- .../java/org/smartregister/domain/Task.java | 20 +++++++++- .../pathevaluator/dao/TaskDao.java | 4 +- .../pathevaluator/task/TaskHelper.java | 25 ++++++++---- .../smartregister/pathevaluator/TestData.java | 40 +++++++++++++++++++ .../pathevaluator/task/TaskHelperTest.java | 15 +++++++ 5 files changed, 94 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/smartregister/domain/Task.java b/src/main/java/org/smartregister/domain/Task.java index a355f7eb..dfe2e43b 100644 --- a/src/main/java/org/smartregister/domain/Task.java +++ b/src/main/java/org/smartregister/domain/Task.java @@ -7,7 +7,9 @@ import org.joda.time.DateTime; import java.io.Serializable; +import java.util.HashMap; import java.util.List; +import java.util.Map; @ToString public class Task implements Serializable{ @@ -22,7 +24,23 @@ public enum TaskStatus { IN_PROGRESS, @SerializedName("Completed") COMPLETED, @SerializedName("Failed") FAILED, @SerializedName("Archived") - ARCHIVED + ARCHIVED; + + private static final Map lookup = new HashMap(); + + static { + lookup.put("Draft", TaskStatus.DRAFT); + lookup.put("Ready", TaskStatus.READY); + lookup.put("Cancelled", TaskStatus.CANCELLED); + lookup.put("In Progress", TaskStatus.IN_PROGRESS); + lookup.put("Completed", TaskStatus.COMPLETED); + lookup.put("Failed", TaskStatus.FAILED); + lookup.put("Archived", TaskStatus.ARCHIVED); + } + + public static TaskStatus get(String algorithm) { + return lookup.get(algorithm); + } } public static final String[] INACTIVE_TASK_STATUS = new String[]{TaskStatus.CANCELLED.name(), TaskStatus.ARCHIVED.name()}; diff --git a/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java b/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java index 58d7d114..53240257 100644 --- a/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java +++ b/src/main/java/org/smartregister/pathevaluator/dao/TaskDao.java @@ -34,8 +34,8 @@ public interface TaskDao { List findAllTasksForEntity(String id); - org.smartregister.domain.Task getTaskById(String id); + org.smartregister.domain.Task getTaskByEntityId(String id); - org.smartregister.domain.Task updateTask(org.smartregister.domain.Task task); + void updateTask(org.smartregister.domain.Task task); } diff --git a/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java b/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java index 30baab90..66109943 100644 --- a/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java +++ b/src/main/java/org/smartregister/pathevaluator/task/TaskHelper.java @@ -3,7 +3,9 @@ */ package org.smartregister.pathevaluator.task; +import java.lang.reflect.Field; import java.util.UUID; +import java.util.logging.Level; import java.util.logging.Logger; import org.joda.time.DateTime; @@ -73,15 +75,24 @@ public void generateTask(DomainResource resource, Action action, String planIden public void updateTask(DomainResource resource, Action action) { TaskDao taskDao = PathEvaluatorLibrary.getInstance().getTaskProvider().getTaskDao(); - Task task = taskDao.getTaskById(resource.getId()); - for (DynamicValue dynamicValue : action.getDynamicValue()) { - if (dynamicValue.getPath().equals("status")) { - task.setStatus(Task.TaskStatus.valueOf(dynamicValue.getExpression().getExpression())); - } - if (dynamicValue.getPath().equals("businessStatus")) { - task.setBusinessStatus(dynamicValue.getExpression().getExpression()); + Task task = taskDao.getTaskByEntityId(resource.getId()); + try { + for (DynamicValue dynamicValue : action.getDynamicValue()) { + Field aField = task.getClass().getDeclaredField(dynamicValue.getPath()); + aField.setAccessible(true); + if (aField.getType().isAssignableFrom(Task.TaskStatus.class)) { + aField.set(task, Task.TaskStatus.get(dynamicValue.getExpression().getExpression())); + } else if (aField.getType().isAssignableFrom(String.class)) { + aField.set(task, dynamicValue.getExpression().getExpression()); + } else { + throw new IllegalArgumentException(); + } } } + catch (Exception e) { + logger.log(Level.SEVERE, "Exception occurred while updating properties using Reflection" + e); + } + taskDao.updateTask(task); } diff --git a/src/test/java/org/smartregister/pathevaluator/TestData.java b/src/test/java/org/smartregister/pathevaluator/TestData.java index da3d4195..3cb481e9 100644 --- a/src/test/java/org/smartregister/pathevaluator/TestData.java +++ b/src/test/java/org/smartregister/pathevaluator/TestData.java @@ -7,11 +7,16 @@ import java.util.ArrayList; import java.util.List; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.smartregister.domain.Jurisdiction; +import org.smartregister.domain.Action; +import org.smartregister.domain.DynamicValue; +import org.smartregister.domain.Expression; import org.smartregister.domain.PlanDefinition; import org.smartregister.utils.DateTypeConverter; import org.smartregister.utils.TaskDateTimeTypeConverter; @@ -102,6 +107,8 @@ public class TestData { + "}]\n" + "}"; + public static String TASK_JSON = "{\"identifier\":\"tsk11231jh22\",\"planIdentifier\":\"IRS_2018_S1\",\"groupIdentifier\":\"2018_IRS-3734{\",\"status\":\"Ready\",\"businessStatus\":\"Not Visited\",\"priority\":3,\"code\":\"IRS\",\"description\":\"Spray House\",\"focus\":\"IRS Visit\",\"for\":\"location.properties.uid:41587456-b7c8-4c4e-b433-23a786f742fc\",\"executionStartDate\":\"2018-11-10T2200\",\"executionEndDate\":null,\"authoredOn\":\"2018-10-31T0700\",\"lastModified\":\"2018-10-31T0700\",\"owner\":\"demouser\",\"note\":[{\"authorString\":\"demouser\",\"time\":\"2018-01-01T0800\",\"text\":\"This should be assigned to patrick.\"}],\"serverVersion\":0,\"reasonReference\":\"reasonrefuuid\",\"location\":\"catchment1\",\"requester\":\"chw1\",\"syncStatus\":null,\"structureId\":null,\"rowid\":null}"; + public static PlanDefinition createPlan() { return gson.fromJson(plan, PlanDefinition.class); } @@ -141,4 +148,37 @@ public static PlanDefinition createPlanV1() { return gson.fromJson(plan_1, PlanDefinition.class); } + public static org.smartregister.domain.Task createDomainTask() { + return gson.fromJson(TASK_JSON, org.smartregister.domain.Task.class); + } + + public static Action createAction(){ + Action action = new Action(); + Set dynamicValues = new HashSet<>(); + DynamicValue dynamicValue = new DynamicValue(); + Expression expression = new Expression(); + expression.setExpression("Cancelled"); + dynamicValue.setPath("status"); + dynamicValue.setExpression(expression); + dynamicValues.add(dynamicValue); + + dynamicValue = new DynamicValue(); + expression = new Expression(); + expression.setExpression("Family Already Registered"); + dynamicValue.setPath("businessStatus"); + dynamicValue.setExpression(expression); + dynamicValues.add(dynamicValue); + + dynamicValue = new DynamicValue(); + expression = new Expression(); + expression.setExpression("Family Already Registered"); + dynamicValue.setPath("businessStatus"); + dynamicValue.setExpression(expression); + dynamicValues.add(dynamicValue); + + action.setType(Action.ActionType.UPDATE); + action.setDynamicValue(dynamicValues); + return action; + } + } diff --git a/src/test/java/org/smartregister/pathevaluator/task/TaskHelperTest.java b/src/test/java/org/smartregister/pathevaluator/task/TaskHelperTest.java index 235667b6..2360cbed 100644 --- a/src/test/java/org/smartregister/pathevaluator/task/TaskHelperTest.java +++ b/src/test/java/org/smartregister/pathevaluator/task/TaskHelperTest.java @@ -108,4 +108,19 @@ public void testShouldNotGenerateTask() { taskHelper.generateTask(patient, planDefinition.getActions().get(0), planIdentifier, jurisdiction, "testUser", null); verify(taskDao, never()).saveTask(any(Task.class), any(QuestionnaireResponse.class)); } + + @Test + public void testUpdateTask() { + Task task = TestData.createDomainTask(); + Action action = TestData.createAction(); + + when(taskDao.getTaskByEntityId(anyString())).thenReturn(task); + Mockito.doNothing().when(taskDao).updateTask(any(Task.class)); + taskHelper.updateTask(patient, action); + verify(taskDao, times(1)).updateTask(taskCaptor.capture()); + Task updatedTask = taskCaptor.getValue(); + assertEquals("location.properties.uid:41587456-b7c8-4c4e-b433-23a786f742fc", updatedTask.getForEntity()); + assertEquals("Family Already Registered", updatedTask.getBusinessStatus()); + assertEquals("CANCELLED", updatedTask.getStatus().name()); + } } From 688733fdf04403fe648dcdce87a0003e13621786 Mon Sep 17 00:00:00 2001 From: rehammuzzamil Date: Mon, 13 Jul 2020 14:33:52 +0500 Subject: [PATCH 3/3] Bump up version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4e21d60e..a332c9e9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ opensrp-plan-evaluator jar - 0.0.16-SNAPSHOT + 0.0.17-SNAPSHOT OpenSRP Plan Evaluator OpenSRP Plan Evaluator Library https://github.com/OpenSRP/opensrp-plan-evaluator