Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Test
- public void testSHRProfiles() throws Exception {
- FhirContext ctx = FhirContext.forDstu3();
- IParser jsonParser = ctx.newJsonParser();
- final Map<String, StructureDefinition> profiles = new HashMap<>();
- for (String folder : new String[]{ "fhir/shr_profiles", "fhir/us_core_profiles"} ) {
- URL profilesFolder = ClassLoader.getSystemClassLoader().getResource(folder);
- Path path = Paths.get(profilesFolder.toURI());
- Files.walk(path)
- .filter(Files::isReadable)
- .filter(Files::isRegularFile)
- .filter(p -> {
- String filename = p.getFileName().toString();
- return filename.startsWith("StructureDefinition") && filename.endsWith(".json");
- })
- .forEach(p -> {
- try {
- StructureDefinition profile = (StructureDefinition)jsonParser.parseResource( new FileReader(p.toFile()) );
- profiles.put(profile.getUrl(), profile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- }
- String extensions = Utilities.readResource("fhir/extension-definitions.json");
- Bundle extensionsBundle = (Bundle)jsonParser.parseResource(extensions);
- for (BundleEntryComponent bec : extensionsBundle.getEntry()) {
- StructureDefinition profile = (StructureDefinition) bec.getResource();
- profiles.put(profile.getUrl(), profile);
- }
- // Create a FhirInstanceValidator and register it to a validator
- FhirValidator validator = ctx.newValidator();
- FhirInstanceValidator instanceValidator = new FhirInstanceValidator();
- validator.registerValidatorModule(instanceValidator);
- IValidationSupport valSupport = new IValidationSupport() {
- @Override
- public org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionComponent expandValueSet(FhirContext theContext, org.hl7.fhir.dstu3.model.ValueSet.ConceptSetComponent theInclude) {
- // TODO: implement
- return null;
- }
- @Override
- public List<IBaseResource> fetchAllConformanceResources(FhirContext theContext) {
- // TODO: implement
- return null;
- }
- @Override
- public List<StructureDefinition> fetchAllStructureDefinitions(FhirContext theContext) {
- return new ArrayList<>(profiles.values());
- }
- @Override
- public CodeSystem fetchCodeSystem(FhirContext theContext, String theSystem) {
- // TODO: implement
- return null;
- }
- @Override
- public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) {
- if (theClass.equals(StructureDefinition.class)) {
- return (T) fetchStructureDefinition(theContext, theUri);
- }
- return null;
- }
- @Override
- public StructureDefinition fetchStructureDefinition(FhirContext theCtx, String theUrl) {
- // TODO: implement
- return profiles.get(theUrl);
- }
- @Override
- public boolean isCodeSystemSupported(FhirContext theContext, String theSystem) {
- // TODO: implement
- return false;
- }
- @Override
- public CodeValidationResult validateCode(FhirContext theContext, String theCodeSystem, String theCode, String theDisplay) {
- // TODO: implement
- return null;
- }
- };
- ValidationSupportChain support = new ValidationSupportChain(new DefaultProfileValidationSupport(), valSupport);
- instanceValidator.setValidationSupport(support);
- Generator generator = new Generator();
- Person person = generator.generatePerson(1);
- String fhirJson = FhirStu3.convertToFHIR(person, System.currentTimeMillis());
- IBaseResource resource = ctx.newJsonParser().parseResource(fhirJson);
- ValidationResult result = validator.validateWithResult(resource);
- System.out.println(result);
- }
Add Comment
Please, Sign In to add comment