Skip to content

Commit

Permalink
Merge branch 'story/341/validation-rules-engine' of https://github.co…
Browse files Browse the repository at this point in the history
…m/CDCgov/trusted-intermediary into story/341/validation-rules-engine
  • Loading branch information
luis-pabon-tf committed Mar 14, 2024
2 parents 8e12166 + fd407b8 commit 4c58894
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static OrderController getInstance() {
public Order<?> parseOrders(DomainRequest request) throws FhirParseException {
logger.logInfo("Parsing orders");
var fhirBundle = fhir.parseResource(request.getBody(), Bundle.class);
ruleEngine.loadRules();
ruleEngine.validate(fhirBundle);
metadata.put(fhirBundle.getId(), EtorMetadataStep.RECEIVED_FROM_REPORT_STREAM);
return new HapiOrder(fhirBundle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package gov.hhs.cdc.trustedintermediary.etor.ruleengine;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.hl7.fhir.instance.model.api.IBaseResource;

public class RuleEngine {

private final String RULES_CONFIG_FILE_NAME = "rule_definitions.json";
private static final RuleEngine INSTANCE = new RuleEngine();
private final List<Rule> rules = new ArrayList<>();

private RuleEngine() {
private RuleEngine() {}

public static RuleEngine getInstance() {
return INSTANCE;
}

public void loadRules() {
var fileUrl = getClass().getClassLoader().getResource(RULES_CONFIG_FILE_NAME);
if (fileUrl == null) {
throw new IllegalArgumentException("File not found: " + RULES_CONFIG_FILE_NAME);
}
try {
var loadedRules = RuleLoader.getInstance().loadRules();
var loadedRules = RuleLoader.getInstance().loadRules(Paths.get(fileUrl.getPath()));
rules.addAll(loadedRules);
} catch (RuleLoaderException e) {
throw new RuntimeException(e);
}
}

public static RuleEngine getInstance() {
return INSTANCE;
}

public void addRule(Rule rule) {
this.rules.add(rule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;

public class RuleLoader {

private final String RULES_CONFIG_FILE_NAME = "rule_definitions.json";
private static final RuleLoader INSTANCE = new RuleLoader();
@Inject Formatter formatter;
@Inject Logger logger;
Expand All @@ -26,14 +23,6 @@ public static RuleLoader getInstance() {
return INSTANCE;
}

List<ValidationRule> loadRules() throws RuleLoaderException {
var fileUrl = getClass().getClassLoader().getResource(RULES_CONFIG_FILE_NAME);
if (fileUrl == null) {
throw new IllegalArgumentException("File not found: " + RULES_CONFIG_FILE_NAME);
}
return loadRules(Paths.get(fileUrl.getPath()));
}

List<ValidationRule> loadRules(Path configPath) throws RuleLoaderException {
try {
String fileContent = Files.readString(configPath);
Expand Down

0 comments on commit 4c58894

Please sign in to comment.