Skip to content

Commit

Permalink
Merge pull request #33 from OpenSRP/31-naming-conventions
Browse files Browse the repository at this point in the history
Standardize Naming conventions, Map missing properties, Pass user on plan evaluator
  • Loading branch information
githengi committed Jun 23, 2020
2 parents b722558 + 7231e28 commit 44f4483
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 216 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>opensrp-plan-evaluator</artifactId>
<packaging>jar</packaging>
<version>0.0.8-SNAPSHOT</version>
<version>0.0.9-SNAPSHOT</version>
<name>OpenSRP Plan Evaluator</name>
<description>OpenSRP Plan Evaluator Library</description>
<url>https://github.com/OpenSRP/opensrp-plan-evaluator</url>
Expand Down
52 changes: 34 additions & 18 deletions src/main/java/org/smartregister/converters/LocationConverter.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
package org.smartregister.converters;

import com.ibm.fhir.model.resource.Location;
import com.ibm.fhir.model.type.*;
import com.ibm.fhir.model.type.String;
import com.ibm.fhir.model.type.code.LocationMode;
import com.ibm.fhir.model.type.code.LocationStatus;
import org.apache.commons.lang3.StringUtils;
import org.smartregister.domain.PhysicalLocation;

import java.lang.Boolean;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.smartregister.domain.LocationProperty.PropertyStatus;
import org.smartregister.domain.PhysicalLocation;

import com.ibm.fhir.model.resource.Location;
import com.ibm.fhir.model.type.Code;
import com.ibm.fhir.model.type.CodeableConcept;
import com.ibm.fhir.model.type.Coding;
import com.ibm.fhir.model.type.Id;
import com.ibm.fhir.model.type.Identifier;
import com.ibm.fhir.model.type.Instant;
import com.ibm.fhir.model.type.Meta;
import com.ibm.fhir.model.type.Reference;
import com.ibm.fhir.model.type.String;
import com.ibm.fhir.model.type.Uri;
import com.ibm.fhir.model.type.code.LocationMode;
import com.ibm.fhir.model.type.code.LocationStatus;

public class LocationConverter {

public static Location convertPhysicalLocationToLocationResource(PhysicalLocation physicalLocation) {
Location.Builder builder = Location.builder().id(physicalLocation.getId());
if(physicalLocation.getProperties().getStatus().equals(PropertyStatus.PENDING_REVIEW)) {
physicalLocation.getProperties().setStatus(PropertyStatus.ACTIVE);
}
LocationStatus locationStatus = LocationStatus.builder()
.value(StringUtils.toRootLowerCase(physicalLocation.getProperties().getStatus().name()))
.build();

Reference partOf = Reference.builder()
.reference(String.builder().value(physicalLocation.getProperties().getParentId()).build()).build();
String name = String.builder().value(physicalLocation.getProperties().getName()).build();
if(StringUtils.isNotBlank(physicalLocation.getProperties().getName())) {
builder.name(String.of(physicalLocation.getProperties().getName()));
}

if(StringUtils.isNotBlank(physicalLocation.getProperties().getType())) {
builder.type(CodeableConcept.builder().id("locationType").text(String.of(physicalLocation.getProperties().getType())).build());
}

java.lang.String version = java.lang.String.valueOf(physicalLocation.getProperties().getVersion());
Id versionId = Id.builder().value(version).build();
java.time.ZonedDateTime zdt = java.time.ZonedDateTime
.ofInstant(java.time.Instant.ofEpochMilli(physicalLocation.getServerVersion()), ZoneId.systemDefault());
Instant lastUpdated = Instant.builder().value(zdt).build();
Meta meta = Meta.builder().versionId(versionId).lastUpdated(lastUpdated).build();
String name_en = null;
if (physicalLocation.getProperties().getCustomProperties().get("name_en") != null) {
name_en = String.builder().value(physicalLocation.getProperties().getCustomProperties().get("name_en"))
.build();
builder.alias(String.of(physicalLocation.getProperties().getCustomProperties().get("name_en")));
}

Identifier identifier;
Expand All @@ -55,11 +74,8 @@ public static Location convertPhysicalLocationToLocationResource(PhysicalLocatio
CodeableConcept.builder().coding(buCoding).build();

LocationMode mode = LocationMode.builder().id("mode").value("instance").build();
Location.Builder builder = Location.builder().status(locationStatus).partOf(partOf).name(name)
.identifier(identifiers).meta(meta).mode(mode).physicalType(physicalType);;
if (name_en != null) {
builder = builder.alias(name_en);
}
builder.status(locationStatus).partOf(partOf)
.identifier(identifiers).meta(meta).mode(mode).physicalType(physicalType);

return builder.build();
}
Expand Down
111 changes: 5 additions & 106 deletions src/main/java/org/smartregister/domain/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
/**
* Created by samuelgithengi on 4/29/19.
*/
@NoArgsConstructor
@Setter
@Getter
public class Action {

private String identifier;
Expand All @@ -32,116 +35,12 @@ public class Action {

private String taskTemplate;

private Set<Trigger> triggers;
private Set<Trigger> trigger;

private Set<Condition> conditions;
private Set<Condition> condition;

private String definitionUri;

public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public int getPrefix() {
return prefix;
}

public void setPrefix(int prefix) {
this.prefix = prefix;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public ExecutionPeriod getTimingPeriod() {
return timingPeriod;
}

public void setTimingPeriod(ExecutionPeriod timingPeriod) {
this.timingPeriod = timingPeriod;
}

public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

public String getGoalId() {
return goalId;
}

public void setGoalId(String goalId) {
this.goalId = goalId;
}

public SubjectConcept getSubjectCodableConcept() {
return subjectCodableConcept;
}

public void setSubjectCodableConcept(SubjectConcept subjectCodableConcept) {
this.subjectCodableConcept = subjectCodableConcept;
}

public String getTaskTemplate() {
return taskTemplate;
}

public void setTaskTemplate(String taskTemplate) {
this.taskTemplate = taskTemplate;
}

public Set<Trigger> getTriggers() {
return triggers;
}

public void setTriggers(Set<Trigger> triggers) {
this.triggers = triggers;
}

public Set<Condition> getConditions() {
return conditions;
}

public void setConditions(Set<Condition> conditions) {
this.conditions = conditions;
}

public String getDefinitionUri() {
return definitionUri;
}

public void setDefinitionUri(String definitionUri) {
this.definitionUri = definitionUri;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package org.smartregister.pathevaluator;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.smartregister.pathevaluator.dao.ClientDao;
import org.smartregister.pathevaluator.dao.ClientProvider;
import org.smartregister.pathevaluator.dao.EventDao;
Expand All @@ -29,6 +32,8 @@
@Getter
public class PathEvaluatorLibrary {

private static Logger logger = Logger.getLogger(PathEvaluatorLibrary.class.getSimpleName());

private static PathEvaluatorLibrary instance;

private FHIRPathEvaluator fhirPathEvaluator;
Expand All @@ -38,22 +43,19 @@ public class PathEvaluatorLibrary {
private ClientProvider clientProvider;

private TaskProvider taskProvider;

private String userName;


private EventProvider eventProvider;

private PathEvaluatorLibrary(LocationDao locationDao, ClientDao clientDao, TaskDao taskDao, EventDao eventDao, String userName) {
private PathEvaluatorLibrary(LocationDao locationDao, ClientDao clientDao, TaskDao taskDao, EventDao eventDao) {
fhirPathEvaluator = FHIRPathEvaluator.evaluator();
locationProvider = new LocationProvider(locationDao);
clientProvider = new ClientProvider(clientDao);
taskProvider = new TaskProvider(taskDao);
eventProvider = new EventProvider(eventDao);
this.userName = userName;
}

public static void init(LocationDao locationDao, ClientDao clientDao, TaskDao taskDao, EventDao eventDao, String userName) {
instance = new PathEvaluatorLibrary(locationDao, clientDao, taskDao, eventDao,userName);
public static void init(LocationDao locationDao, ClientDao clientDao, TaskDao taskDao, EventDao eventDao) {
instance = new PathEvaluatorLibrary(locationDao, clientDao, taskDao, eventDao);
}

/**
Expand All @@ -67,6 +69,7 @@ public static PathEvaluatorLibrary getInstance() {

/**
* Evaluates a boolean FHIR Path expression on a resource
*
* @param resource the resource expression is being evaluated on
* @param expression the expression to evaluate
* @return results of expression or false if the expression is not valid
Expand All @@ -75,28 +78,33 @@ public boolean evaluateBooleanExpression(Resource resource, String expression) {

try {
Collection<FHIRPathNode> nodes = fhirPathEvaluator.evaluate(resource, expression);
return nodes != null ? nodes.iterator().next().as(FHIRPathBooleanValue.class)._boolean() : false;
return nodes != null && nodes.iterator().hasNext()
? nodes.iterator().next().as(FHIRPathBooleanValue.class)._boolean()
: false;
}
catch (FHIRPathException e) {
logger.log(Level.SEVERE,
"Error execuring expression " + expression + "resource " + ReflectionToStringBuilder.toString(resource), e);
return false;
}
}

/**
* Evaluates a FHIR Path expression on a resource
*
* @param resource the resource expression is being evaluated on
* @param expression the expression to evaluate
* @return results of expression
* @return results of expression
*/
public FHIRPathElementNode evaluateElementExpression(Resource resource, String expression) {
public FHIRPathElementNode evaluateElementExpression(Resource resource, String expression) {

try {
return fhirPathEvaluator.evaluate(resource, expression).iterator().next().asElementNode();
}
catch (FHIRPathException e) {
e.printStackTrace();
return null;
}
try {
return fhirPathEvaluator.evaluate(resource, expression).iterator().next().asElementNode();
}
catch (FHIRPathException e) {
logger.log(Level.SEVERE, "Error execuring expression "+expression, e);
return null;
}
}

}
12 changes: 6 additions & 6 deletions src/main/java/org/smartregister/pathevaluator/ResourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
*/
public enum ResourceType {

JURISDICTION("jurisdiction"),
LOCATION("location"),
FAMILY("family"),
FAMILY_MEMBER("family_ember"),
TASK("task"),
QUESTIONAIRRE_RESPONSE("questionnaire_response");
JURISDICTION("Jurisdiction"),
LOCATION("Location"),
FAMILY("Family"),
FAMILY_MEMBER("FamilyMmber"),
TASK("Task"),
QUESTIONAIRRE_RESPONSE("QuestionnaireResponse");

private String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/
public enum TriggerType {

PLAN_ACTIVATION("plan_activation"),
PLAN_JURISDICTION_MODIFICATION("plan_jurisdiction_modification"),
EVENT_SUBMISSION("event_submission");
PLAN_ACTIVATION("PlanActivation"),
PLAN_JURISDICTION_MODIFICATION("PlanJurisdictionModification"),
EVENT_SUBMISSION("EventSubmission");

private String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ConditionHelper {
*/
public boolean evaluateActionConditions(Resource resource, Action action, String planIdentifier) {
boolean isValid = true;
for (Condition condition : action.getConditions()) {
for (Condition condition : action.getCondition()) {
SubjectConcept concept = condition.getExpression().getSubjectConcept();
if (concept != null) {
List<? extends Resource> resources = actionHelper.getConditionSubjectResources(condition, action, resource,planIdentifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public class PlanEvaluator {
private TaskHelper taskHelper;

private TriggerHelper triggerHelper;

private String username;

public PlanEvaluator() {
public PlanEvaluator(String username) {
actionHelper = new ActionHelper();
conditionHelper = new ConditionHelper(actionHelper);
taskHelper = new TaskHelper();
triggerHelper = new TriggerHelper(actionHelper);
this.username=username;
}

private FHIRPathEvaluator fhirPathEvaluator = FHIRPathEvaluator.evaluator();
Expand Down Expand Up @@ -98,11 +101,11 @@ private void evaluatePlan(PlanDefinition planDefinition, TriggerType triggerEven
QuestionnaireResponse questionnaireResponse) {

planDefinition.getActions().forEach(action -> {
if (triggerHelper.evaluateTrigger(action.getTriggers(), triggerEvent, planDefinition.getIdentifier(),
if (triggerHelper.evaluateTrigger(action.getTrigger(), triggerEvent, planDefinition.getIdentifier(),
questionnaireResponse)) {
actionHelper.getSubjectResources(action, jurisdiction).forEach(resource -> {
if (conditionHelper.evaluateActionConditions(resource, action, planDefinition.getIdentifier())) {
taskHelper.generateTask(resource, action,planDefinition.getIdentifier(),jurisdiction.getCode());
taskHelper.generateTask(resource, action,planDefinition.getIdentifier(),jurisdiction.getCode(),username);
}
});
}
Expand Down
Loading

0 comments on commit 44f4483

Please sign in to comment.