Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #303 from MeasureAuthoringTool/feature/MAT-2572-uc…
Browse files Browse the repository at this point in the history
…um-units

Feature/mat 2572 ucum units
  • Loading branch information
greenemcg authored Apr 23, 2021
2 parents 83f3a4c + afc0abd commit d15e49a
Show file tree
Hide file tree
Showing 5 changed files with 2,199 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gov.cms.mat.fhir.services.rest;

import gov.cms.mat.fhir.services.service.UCUMValidationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping(path = "/ucum")
@Tag(name = "UCUM-Controller",
description = "API for validating ucum codes")
@Slf4j
@RequiredArgsConstructor
public class UCUMController {
private final UCUMValidationService ucumValidationService;

@Operation(summary = "Validate",
description = "Validate the ucum unit")
@GetMapping("/{unit}")
public Boolean validateCode(@PathVariable("unit") String unit) {
return ucumValidationService.validate(URLDecoder.decode(unit, StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gov.cms.mat.fhir.services.service;

import org.fhir.ucum.UcumEssenceService;
import org.fhir.ucum.UcumException;
import org.fhir.ucum.UcumService;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;

@Service
public class UCUMValidationService {
@Value("classpath:ucum/ucum-essence.xml")
private Resource resource;

private UcumService ucumService;

@PostConstruct
void postConstruct() {
try {
ucumService = new UcumEssenceService(resource.getInputStream());
} catch (UcumException | IOException e) {
throw new InvalidPropertyException(this.getClass(), "resource", e.getMessage());
}
}

public Boolean validate(String unit) {
String result = ucumService.validate(unit);

//Returns:null if valid
return result == null;
}
}
Loading

0 comments on commit d15e49a

Please sign in to comment.