-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from MeasureAuthoringTool/feature/mat-7156-71…
…62-shift-qicore-test-case-dates [MAT-7156, MAT-7162] Shift QICore Test Case Dates
- Loading branch information
Showing
14 changed files
with
389 additions
and
24 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/gov/cms/madie/madiefhirservice/resources/TestCaseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package gov.cms.madie.madiefhirservice.resources; | ||
|
||
import gov.cms.madie.madiefhirservice.services.TestCaseDateShifterService; | ||
import gov.cms.madie.models.measure.TestCase; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.security.Principal; | ||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping(path = "/fhir/") | ||
@Tag(name = "Test-Case-Controller", description = "API for manipulating FHIR Test Cases") | ||
@RequiredArgsConstructor | ||
public class TestCaseController { | ||
private final TestCaseDateShifterService testCaseDateShifterService; | ||
|
||
@PutMapping("/test-cases/shift-dates") | ||
public ResponseEntity<List<TestCase>> shiftTestCasesDates( | ||
Principal principal, | ||
@RequestBody List<TestCase> testCases, | ||
@RequestParam(name = "shifted", defaultValue = "0") int shifted) { | ||
log.info( | ||
"User [{}] requested date shift for test cases [{}] of [{}] years", | ||
principal.getName(), | ||
testCases.stream().map(TestCase::getId).collect(joining(", ")), | ||
shifted); | ||
return ResponseEntity.ok(testCaseDateShifterService.shiftDates(testCases, shifted)); | ||
} | ||
|
||
@PutMapping("/test-case/shift-dates") | ||
public ResponseEntity<TestCase> shiftTestCaseDates( | ||
Principal principal, | ||
@RequestBody TestCase testCase, | ||
@RequestParam(name = "shifted", defaultValue = "0") int shifted) { | ||
log.info( | ||
"User [{}] requested date shift for test case [{}] of [{}] years", | ||
principal.getName(), | ||
testCase.getId(), | ||
shifted); | ||
return ResponseEntity.ok(testCaseDateShifterService.shiftDates(testCase, shifted)); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/gov/cms/madie/madiefhirservice/services/TestCaseDateShifterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package gov.cms.madie.madiefhirservice.services; | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import ca.uhn.fhir.parser.DataFormatException; | ||
import ca.uhn.fhir.parser.IParser; | ||
import ca.uhn.fhir.parser.StrictErrorHandler; | ||
import gov.cms.madie.models.measure.TestCase; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.hl7.fhir.r4.model.Base; | ||
import org.hl7.fhir.r4.model.BaseDateTimeType; | ||
import org.hl7.fhir.r4.model.Bundle; | ||
import org.hl7.fhir.r4.model.Property; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.*; | ||
|
||
@Slf4j | ||
@Service | ||
@AllArgsConstructor | ||
public class TestCaseDateShifterService { | ||
|
||
private FhirContext fhirContext; | ||
|
||
public TestCase shiftDates(TestCase testCase, int shifted) { | ||
if (testCase == null) { | ||
return null; | ||
} | ||
List<TestCase> shiftedTestCases = shiftDates(List.of(testCase), shifted); | ||
if (CollectionUtils.isNotEmpty(shiftedTestCases) && shiftedTestCases.size() == 1) { | ||
return shiftedTestCases.get(0); | ||
} | ||
return null; | ||
} | ||
|
||
public List<TestCase> shiftDates(List<TestCase> testCases, int shifted) { | ||
if (CollectionUtils.isEmpty(testCases)) { | ||
return Collections.emptyList(); | ||
} | ||
List<TestCase> shiftedTestCases = new ArrayList<>(); | ||
|
||
IParser parser = getIParser(); | ||
for (TestCase testCase : new ArrayList<>(testCases)) { | ||
try { | ||
if (StringUtils.isBlank(testCase.getJson())) { | ||
throw new DataFormatException("Empty test case"); | ||
} | ||
// convert test case json to bundle | ||
Bundle bundle = parser.parseResource(Bundle.class, testCase.getJson()); | ||
// update the test case dates | ||
bundle.getEntry().forEach(entry -> shiftDates(entry.getResource(), shifted)); | ||
// convert the updated bundle to string and assign back to test case. | ||
String json = parser.encodeResourceToString(bundle); | ||
testCase.setJson(json); | ||
shiftedTestCases.add(testCase); | ||
} catch (DataFormatException dfe) { | ||
log.info("skipping the test case with id [{}] as it is empty or invalid", testCase.getId()); | ||
} | ||
} | ||
return shiftedTestCases; | ||
} | ||
|
||
void shiftDates(Base baseResource, int shifted) { | ||
List<Field> fields = new LinkedList<>(); | ||
getAllFields(fields, baseResource.getClass()); | ||
for (Field field : fields) { | ||
Property property = baseResource.getNamedProperty(field.getName()); | ||
if (property != null) { | ||
List<Base> values = property.getValues(); | ||
for (Base value : values) { | ||
if (value.isPrimitive()) { | ||
if (value.isDateTime()) { | ||
BaseDateTimeType dateType = (BaseDateTimeType) value; | ||
dateType.add(1, shifted); | ||
} | ||
} else { | ||
shiftDates(value, shifted); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void getAllFields(List<Field> fields, Class<?> type) { | ||
fields.addAll(List.of(type.getDeclaredFields())); | ||
if (type.getSuperclass() != null) { | ||
getAllFields(fields, type.getSuperclass()); | ||
} | ||
} | ||
|
||
IParser getIParser() { | ||
return fhirContext | ||
.newJsonParser() | ||
.setParserErrorHandler(new StrictErrorHandler()) | ||
.setPrettyPrint(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.