11package org .opencds .cqf .tooling .common .r4 ;
22
3- import java .io .File ;
4- import java .io .FileReader ;
5- import java .io .IOException ;
6- import java .util .ArrayList ;
7- import java .util .List ;
8- import java .util .stream .Collectors ;
9-
3+ import ca .uhn .fhir .context .FhirContext ;
4+ import ca .uhn .fhir .parser .JsonParser ;
5+ import ca .uhn .fhir .parser .XmlParser ;
106import org .cqframework .cql .Main ;
11- import org .hl7 .fhir .r4 .model .CodeableConcept ;
12- import org .hl7 .fhir .r4 .model .Coding ;
13- import org .hl7 .fhir .r4 .model .Device ;
14- import org .hl7 .fhir .r4 .model .DomainResource ;
15- import org .hl7 .fhir .r4 .model .Extension ;
16- import org .hl7 .fhir .r4 .model .Meta ;
17- import org .hl7 .fhir .r4 .model .Reference ;
18- import org .hl7 .fhir .r4 .model .Resource ;
19- import org .hl7 .fhir .r4 .model .ResourceType ;
20- import org .hl7 .fhir .r4 .model .StringType ;
21- import org .opencds .cqf .tooling .common .BaseCqfmSoftwareSystemHelper ;
22- import org .opencds .cqf .tooling .common .CqfmSoftwareSystem ;
7+ import org .hl7 .fhir .r4 .model .*;
8+ import org .opencds .cqf .tooling .common .BaseSoftwareSystemHelper ;
9+ import org .opencds .cqf .tooling .common .SoftwareSystem ;
2310import org .opencds .cqf .tooling .utilities .IOUtils ;
11+ import org .opencds .cqf .tooling .utilities .constants .CqfConstants ;
12+ import org .opencds .cqf .tooling .utilities .constants .CrmiConstants ;
2413import org .slf4j .Logger ;
2514import org .slf4j .LoggerFactory ;
2615
27- import ca .uhn .fhir .context .FhirContext ;
28- import ca .uhn .fhir .parser .JsonParser ;
29- import ca .uhn .fhir .parser .XmlParser ;
16+ import java .io .FileReader ;
17+ import java .io .IOException ;
18+ import java .util .ArrayList ;
19+ import java .util .List ;
20+ import java .util .stream .Collectors ;
3021
3122
32- public class CqfmSoftwareSystemHelper extends BaseCqfmSoftwareSystemHelper {
23+ public class SoftwareSystemHelper extends BaseSoftwareSystemHelper {
3324
34- private static Logger logger = LoggerFactory .getLogger (CqfmSoftwareSystemHelper .class );
25+ private static final Logger logger = LoggerFactory .getLogger (SoftwareSystemHelper .class );
3526
36- public CqfmSoftwareSystemHelper () { }
27+ public SoftwareSystemHelper () { }
3728
38- public CqfmSoftwareSystemHelper (String rootDir ) {
29+ public SoftwareSystemHelper (String rootDir ) {
3930 super (rootDir );
4031 }
4132
42- @ SuppressWarnings ("serial" )
4333 protected <T extends DomainResource > void validateResourceForSoftwareSystemExtension (T resource ) {
4434 if (resource == null ) {
4535 throw new IllegalArgumentException ("No resource provided." );
4636 }
47-
48- List <String > eligibleResourceTypes = new ArrayList <String >() { {
49- add ("Library" );
50- add ("Measure" );
51- } };
52-
53- String eligibleResourceTypesList = String .join (", " , eligibleResourceTypes );
54- String fhirType = "Library" ;//resource.get resource.fhirType();
55- if (!eligibleResourceTypes .contains (fhirType )) {
56- throw new IllegalArgumentException (String .format ("cqfm-softwaresystem extension is only supported for the following resources: { %s }, not %s" , eligibleResourceTypesList , fhirType ));
57- }
5837 }
5938
60- public <T extends DomainResource > void ensureSoftwareSystemExtensionAndDevice (T resource , List <CqfmSoftwareSystem > softwareSystems , FhirContext fhirContext ) {
39+ public <T extends DomainResource > void ensureSoftwareSystemExtensionAndDevice (T resource , List <SoftwareSystem > softwareSystems , FhirContext fhirContext ) {
6140 validateResourceForSoftwareSystemExtension (resource );
6241
6342 if (softwareSystems != null && !softwareSystems .isEmpty ()) {
64- for (CqfmSoftwareSystem system : softwareSystems ) {
43+ for (SoftwareSystem system : softwareSystems ) {
6544 ensureSoftwareSystemExtensionAndDevice (resource , system , fhirContext );
6645 }
6746 }
6847 }
6948
70- public <T extends DomainResource > void ensureSoftwareSystemExtensionAndDevice (T resource , CqfmSoftwareSystem system , FhirContext fhirContext ) {
49+ public <T extends DomainResource > void ensureSoftwareSystemExtensionAndDevice (T resource , SoftwareSystem system , FhirContext fhirContext ) {
7150 validateResourceForSoftwareSystemExtension (resource );
7251
7352 if (this .getSystemIsValid (system )) {
@@ -85,25 +64,25 @@ public <T extends DomainResource> void ensureSoftwareSystemExtensionAndDevice(T
8564 if (path .endsWith ("xml" )) {
8665 deviceOutputEncoding = IOUtils .Encoding .XML ;
8766 XmlParser xmlParser = (XmlParser )fhirContext .newXmlParser ();
88- try (FileReader reader = new FileReader (new File ( path ) )) {
67+ try (FileReader reader = new FileReader (path )) {
8968 resourceInPath = (DomainResource ) xmlParser .parseResource (reader );
9069 } catch (IOException e ) {
91- e . printStackTrace ( );
70+ logger . warn ( "Error parsing " + e . getLocalizedMessage (), e );
9271 throw new RuntimeException ("Error parsing " + e .getLocalizedMessage ());
9372 }
9473 }
9574 else {
9675 JsonParser jsonParser = (JsonParser )fhirContext .newJsonParser ();
97- try (FileReader reader = new FileReader (new File ( path ) )) {
76+ try (FileReader reader = new FileReader (path )) {
9877 resourceInPath = (DomainResource ) jsonParser .parseResource (reader );
9978 } catch (IOException e ) {
100- e . printStackTrace ( );
79+ logger . warn ( "Error parsing " + e . getLocalizedMessage (), e );
10180 throw new RuntimeException ("Error parsing " + e .getLocalizedMessage ());
10281 }
10382 }
10483
10584 // NOTE: Takes the first device that matches on ID.
106- if (resourceInPath .getResourceType ().toString ().toLowerCase (). equals ("device" )) {
85+ if (resourceInPath .getResourceType ().toString ().equalsIgnoreCase ("device" )) {
10786 Device prospectDevice = (Device )resourceInPath ;
10887 if (prospectDevice .getIdElement ().getIdPart ().equals (systemDeviceId )) {
10988 device = (Device )resourceInPath ;
@@ -121,7 +100,7 @@ public <T extends DomainResource> void ensureSoftwareSystemExtensionAndDevice(T
121100
122101 /* Ensure that device has the current/proposed version */
123102 Device .DeviceVersionComponent proposedVersion = new Device .DeviceVersionComponent (new StringType (system .getVersion ()));
124- List <Device .DeviceVersionComponent > proposedVersionList = new ArrayList <Device . DeviceVersionComponent >();
103+ List <Device .DeviceVersionComponent > proposedVersionList = new ArrayList <>();
125104 proposedVersionList .add (proposedVersion );
126105 device .setVersion (proposedVersionList );
127106
@@ -153,22 +132,22 @@ public <T extends DomainResource> void ensureSoftwareSystemExtensionAndDevice(T
153132 }
154133 }
155134
156- if (softwareSystemExtension == null && device != null ) {
135+ if (softwareSystemExtension == null ) {
157136 softwareSystemExtension = new Extension ();
158- softwareSystemExtension .setUrl (this . getCqfmSoftwareSystemExtensionUrl () );
137+ softwareSystemExtension .setUrl (CrmiConstants . SOFTWARE_SYSTEM_EXT_URL );
159138 final Reference reference = new Reference ();
160139 reference .setReference (systemReference );
161140 softwareSystemExtension .setValue (reference );
162141
163142 resource .addExtension (softwareSystemExtension );
164143 }
165- else if (softwareSystemExtension != null && !systemReference .equals (((Reference )softwareSystemExtension .getValue ()).getReference ())) {
144+ else if (!systemReference .equals (((Reference )softwareSystemExtension .getValue ()).getReference ())) {
166145 ((Reference )softwareSystemExtension .getValue ()).setReference (systemReference );
167146 }
168- else if ( device == null ) {
169- if (resource .hasExtension (this . getCqfmSoftwareSystemExtensionUrl () )) {
147+ else {
148+ if (resource .hasExtension (CrmiConstants . SOFTWARE_SYSTEM_EXT_URL )) {
170149 resource .setExtension (extensions .stream ()
171- .filter (extension -> !extension .getUrl ().equals (this . getCqfmSoftwareSystemExtensionUrl () ))
150+ .filter (extension -> !extension .getUrl ().equals (CrmiConstants . SOFTWARE_SYSTEM_EXT_URL ))
172151 .collect (Collectors .toList ()));
173152 }
174153 }
@@ -210,7 +189,7 @@ else if (device == null) {
210189 }
211190 }
212191
213- public Device createSoftwareSystemDevice (CqfmSoftwareSystem system ) {
192+ public Device createSoftwareSystemDevice (SoftwareSystem system ) {
214193 Device device = null ;
215194
216195 if (this .getSystemIsValid (system )) {
@@ -219,15 +198,15 @@ public Device createSoftwareSystemDevice(CqfmSoftwareSystem system) {
219198
220199 /* meta.profile */
221200 Meta meta = new Meta ();
222- meta .addProfile ("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/device-softwaresystem-cqfm" );
201+ meta .addProfile (CrmiConstants . SOFTWARE_SYSTEM_DEVICE_PROFILE_URL );
223202 device .setMeta (meta );
224203
225204 device .addDeviceName ().setName (system .getName ()).setType (Device .DeviceNameType .MANUFACTURERNAME );
226205 device .setManufacturer (system .getName ());
227206
228207 /* type */
229208 Coding typeCoding = new Coding ();
230- typeCoding .setSystem ("http://hl7.org/fhir/us/cqfmeasures/CodeSystem/software-system-type" );
209+ typeCoding .setSystem (CrmiConstants . SOFTWARE_SYSTEM_DEVICE_TYPE_SYSTEM_URL );
231210 typeCoding .setCode ("tooling" );
232211
233212 List <Coding > typeCodingList = new ArrayList <>();
@@ -253,14 +232,14 @@ public Device createSoftwareSystemDevice(CqfmSoftwareSystem system) {
253232// }
254233
255234 public <T extends DomainResource > void ensureCQFToolingExtensionAndDevice (T resource , FhirContext fhirContext ) {
256- CqfmSoftwareSystem cqfToolingSoftwareSystem = new CqfmSoftwareSystem ( this . getCqfToolingDeviceName (), CqfmSoftwareSystemHelper .class .getPackage ().getImplementationVersion (), Main .class .getPackage ().getImplementationVendor ());
235+ SoftwareSystem cqfToolingSoftwareSystem = new SoftwareSystem ( CqfConstants . CQF_TOOLING_DEVICE_NAME , SoftwareSystemHelper .class .getPackage ().getImplementationVersion (), Main .class .getPackage ().getImplementationVendor ());
257236 ensureSoftwareSystemExtensionAndDevice (resource , cqfToolingSoftwareSystem , fhirContext );
258237 }
259238
260239 private void addVersion (Device device , String version ) {
261- // NOTE: The cqfm -softwaresystem extension restricts the cardinality of version to 0..1, so we overwrite any existing version entries each time
240+ // NOTE: The crmi -softwaresystem extension restricts the cardinality of version to 0..1, so we overwrite any existing version entries each time
262241 Device .DeviceVersionComponent versionComponent = new Device .DeviceVersionComponent (new StringType (version ));
263- List <Device .DeviceVersionComponent > versionList = new ArrayList <Device . DeviceVersionComponent >();
242+ List <Device .DeviceVersionComponent > versionList = new ArrayList <>();
264243 versionList .add (versionComponent );
265244
266245 device .setVersion (versionList );
0 commit comments