Guest User

Untitled

a guest
Jan 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. @Test
  2. public void testSHRProfiles() throws Exception {
  3. FhirContext ctx = FhirContext.forDstu3();
  4.  
  5. IParser jsonParser = ctx.newJsonParser();
  6.  
  7. final Map<String, StructureDefinition> profiles = new HashMap<>();
  8.  
  9. for (String folder : new String[]{ "fhir/shr_profiles", "fhir/us_core_profiles"} ) {
  10. URL profilesFolder = ClassLoader.getSystemClassLoader().getResource(folder);
  11. Path path = Paths.get(profilesFolder.toURI());
  12. Files.walk(path)
  13. .filter(Files::isReadable)
  14. .filter(Files::isRegularFile)
  15. .filter(p -> {
  16. String filename = p.getFileName().toString();
  17. return filename.startsWith("StructureDefinition") && filename.endsWith(".json");
  18. })
  19. .forEach(p -> {
  20. try {
  21. StructureDefinition profile = (StructureDefinition)jsonParser.parseResource( new FileReader(p.toFile()) );
  22. profiles.put(profile.getUrl(), profile);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. });
  27. }
  28.  
  29.  
  30. String extensions = Utilities.readResource("fhir/extension-definitions.json");
  31. Bundle extensionsBundle = (Bundle)jsonParser.parseResource(extensions);
  32.  
  33. for (BundleEntryComponent bec : extensionsBundle.getEntry()) {
  34. StructureDefinition profile = (StructureDefinition) bec.getResource();
  35. profiles.put(profile.getUrl(), profile);
  36. }
  37.  
  38.  
  39. // Create a FhirInstanceValidator and register it to a validator
  40. FhirValidator validator = ctx.newValidator();
  41. FhirInstanceValidator instanceValidator = new FhirInstanceValidator();
  42. validator.registerValidatorModule(instanceValidator);
  43.  
  44. IValidationSupport valSupport = new IValidationSupport() {
  45. @Override
  46. public org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionComponent expandValueSet(FhirContext theContext, org.hl7.fhir.dstu3.model.ValueSet.ConceptSetComponent theInclude) {
  47. // TODO: implement
  48. return null;
  49. }
  50.  
  51. @Override
  52. public List<IBaseResource> fetchAllConformanceResources(FhirContext theContext) {
  53. // TODO: implement
  54. return null;
  55. }
  56.  
  57. @Override
  58. public List<StructureDefinition> fetchAllStructureDefinitions(FhirContext theContext) {
  59. return new ArrayList<>(profiles.values());
  60. }
  61.  
  62. @Override
  63. public CodeSystem fetchCodeSystem(FhirContext theContext, String theSystem) {
  64. // TODO: implement
  65. return null;
  66. }
  67.  
  68. @Override
  69. public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) {
  70. if (theClass.equals(StructureDefinition.class)) {
  71. return (T) fetchStructureDefinition(theContext, theUri);
  72. }
  73. return null;
  74. }
  75.  
  76. @Override
  77. public StructureDefinition fetchStructureDefinition(FhirContext theCtx, String theUrl) {
  78. // TODO: implement
  79. return profiles.get(theUrl);
  80. }
  81.  
  82. @Override
  83. public boolean isCodeSystemSupported(FhirContext theContext, String theSystem) {
  84. // TODO: implement
  85. return false;
  86. }
  87.  
  88. @Override
  89. public CodeValidationResult validateCode(FhirContext theContext, String theCodeSystem, String theCode, String theDisplay) {
  90. // TODO: implement
  91. return null;
  92. }
  93. };
  94.  
  95. ValidationSupportChain support = new ValidationSupportChain(new DefaultProfileValidationSupport(), valSupport);
  96. instanceValidator.setValidationSupport(support);
  97.  
  98. Generator generator = new Generator();
  99.  
  100. Person person = generator.generatePerson(1);
  101.  
  102. String fhirJson = FhirStu3.convertToFHIR(person, System.currentTimeMillis());
  103. IBaseResource resource = ctx.newJsonParser().parseResource(fhirJson);
  104. ValidationResult result = validator.validateWithResult(resource);
  105.  
  106. System.out.println(result);
  107. }
Add Comment
Please, Sign In to add comment