Skip to content

Commit

Permalink
MAT-6487 admin api to adjust measure version values
Browse files Browse the repository at this point in the history
  • Loading branch information
sb-prateekkeerthi committed Sep 17, 2024
1 parent 2fcee87 commit f008401
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 12 deletions.
111 changes: 103 additions & 8 deletions src/main/java/cms/gov/madie/measure/resources/AdminController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package cms.gov.madie.measure.resources;

import java.security.Principal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import cms.gov.madie.measure.exceptions.InvalidRequestException;
import cms.gov.madie.measure.exceptions.InvalidResourceStateException;
import cms.gov.madie.measure.exceptions.MeasureNotDraftableException;
import cms.gov.madie.measure.services.*;
import gov.cms.madie.models.common.Version;
import org.apache.commons.collections4.CollectionUtils;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
Expand All @@ -31,10 +34,6 @@
import cms.gov.madie.measure.dto.TestCaseValidationReport;
import cms.gov.madie.measure.exceptions.ResourceNotFoundException;
import cms.gov.madie.measure.repositories.MeasureRepository;
import cms.gov.madie.measure.services.ActionLogService;
import cms.gov.madie.measure.services.MeasureService;
import cms.gov.madie.measure.services.MeasureSetService;
import cms.gov.madie.measure.services.TestCaseService;
import gov.cms.madie.models.common.ActionType;
import gov.cms.madie.models.measure.Measure;
import gov.cms.madie.models.measure.MeasureSet;
Expand All @@ -51,6 +50,7 @@ public class AdminController {
private final TestCaseService testCaseService;
private final MeasureSetService measureSetService;
private final ActionLogService actionLogService;
private final VersionService versionService;

private final MeasureRepository measureRepository;

Expand Down Expand Up @@ -171,6 +171,101 @@ public ResponseEntity<List<Map<String, Object>>> getMeasureSharedWith(
return ResponseEntity.ok(results);
}

@PutMapping("/measures/{id}")
@PreAuthorize("#request.getHeader('api-key') == #apiKey")
public ResponseEntity<Measure> correctMeasureVersion(
HttpServletRequest request,
@Value("${admin-api-key}") String apiKey,
Principal principal,
@PathVariable String id,
@RequestParam String correctVersion,
@RequestParam String draftVersion) {

Measure measureToCorrectVersion = measureService.findMeasureById(id);
if (measureToCorrectVersion == null) {
throw new ResourceNotFoundException("Measure", id);
}

// check if the associated measure set already has a draft
List<Measure> relatedMeasures =
measureRepository.findAllByMeasureSetIdInAndActiveAndMeasureMetaDataDraft(
List.of(measureToCorrectVersion.getMeasureSetId()), true, true);

if (!CollectionUtils.isEmpty(relatedMeasures)
&& !relatedMeasures.get(0).getId().equals(measureToCorrectVersion.getId())) {
throw new MeasureNotDraftableException(measureToCorrectVersion.getId());
}

// check if the draftVersion is less that correctVersion
if (!isLessThan(correctVersion, draftVersion)) {
throw new InvalidRequestException("Draft version cannot be less than correct version");
}

// check if the given version is already associated
if (!checkIfVersionIsAlreadyAssociated(
measureToCorrectVersion.getMeasureSetId(), correctVersion, draftVersion)) {
throw new InvalidResourceStateException(
"Version number cannot be corrected. "
+ "The given draft or correct version number is already associated");
}

Version newDraftVersion = Version.parse(draftVersion);
String newCql =
measureToCorrectVersion
.getCql()
.replace(
versionService.generateLibraryContentLine(
measureToCorrectVersion.getCqlLibraryName(),
measureToCorrectVersion.getVersion()),
versionService.generateLibraryContentLine(
measureToCorrectVersion.getCqlLibraryName(), newDraftVersion));
measureToCorrectVersion.setCql(newCql);
measureToCorrectVersion.setVersion(newDraftVersion);
measureToCorrectVersion.getMeasureMetaData().setDraft(true);

Measure correctedVersionMeasure = measureRepository.save(measureToCorrectVersion);
actionLogService.logAction(id, Measure.class, ActionType.UPDATED, principal.getName());
return ResponseEntity.ok(correctedVersionMeasure);
}

private boolean checkIfVersionIsAlreadyAssociated(
String measureSetId, String correctVersion, String draftVersion) {
List<Measure> allByMeasureSetIdAndActive =
measureRepository.findAllByMeasureSetIdAndActive(measureSetId, true);
List<Measure> measureStream =
allByMeasureSetIdAndActive.stream()
.filter(
measure ->
measure.getVersion().toString().equals(correctVersion)
|| measure.getVersion().toString().equals(draftVersion))
.toList();
return CollectionUtils.isEmpty(measureStream);
}

private boolean isLessThan(String correctVersion, String draftVersion) {
String[] correctVersionParts = correctVersion.split("\\.");
String[] draftVersionParts = draftVersion.split("\\.");

int length = Math.max(correctVersionParts.length, draftVersionParts.length);

for (int i = 0; i < length; i++) {
// parse the parts as integers for comparison. If a part is missing, treat it as zero.
int correctVersionPart =
i < correctVersionParts.length ? Integer.parseInt(correctVersionParts[i]) : 0;
int draftVersionPart =
i < draftVersionParts.length ? Integer.parseInt(draftVersionParts[i]) : 0;

// compare corresponding parts of the version strings
if (draftVersionPart < correctVersionPart) {
return true;
} else if (draftVersionPart > correctVersionPart) {
return false;
}
}
// if all parts are equal, draftVersion is not less than correctVersion
return false;
}

private Callable<MeasureTestCaseValidationReport> buildCallableForMeasureId(
final String measureId, final String accessToken) {
return () -> testCaseService.updateTestCaseValidResourcesWithReport(measureId, accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import cms.gov.madie.measure.dto.MeasureTestCaseValidationReport;
import cms.gov.madie.measure.dto.TestCaseValidationReport;
import cms.gov.madie.measure.repositories.MeasureRepository;
import cms.gov.madie.measure.services.ActionLogService;
import cms.gov.madie.measure.services.MeasureService;
import cms.gov.madie.measure.services.MeasureSetService;
import cms.gov.madie.measure.services.TestCaseService;
import cms.gov.madie.measure.services.*;
import gov.cms.madie.models.access.AclSpecification;
import gov.cms.madie.models.access.RoleEnum;
import gov.cms.madie.models.measure.Measure;
Expand Down Expand Up @@ -51,6 +48,7 @@ public class AdminControllerMvcTest {
@MockBean private MeasureSetService measureSetService;
@MockBean private TestCaseService testCaseService;
@MockBean private ActionLogService actionLogService;
@MockBean private VersionService versionService;

@MockBean private MeasureRepository measureRepository;

Expand Down

0 comments on commit f008401

Please sign in to comment.