1
1
/*
2
- * Copyright 2023 Google LLC
2
+ * Copyright 2023-2024 Google LLC
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -28,7 +28,6 @@ import ca.uhn.fhir.util.BundleBuilder
28
28
import com.google.common.collect.ImmutableMap
29
29
import java.io.File
30
30
import java.io.FileNotFoundException
31
- import java.util.Locale
32
31
import java.util.Objects
33
32
import java.util.function.Consumer
34
33
import org.hl7.fhir.instance.model.api.IBaseBundle
@@ -40,9 +39,7 @@ import org.opencds.cqf.fhir.api.Repository
40
39
import org.opencds.cqf.fhir.utility.Ids
41
40
import org.opencds.cqf.fhir.utility.dstu3.AttachmentUtil
42
41
import org.opencds.cqf.fhir.utility.matcher.ResourceMatcher
43
- import org.opencds.cqf.fhir.utility.repository.IGLayoutMode
44
42
import org.opencds.cqf.fhir.utility.repository.Repositories
45
- import org.opencds.cqf.fhir.utility.repository.ResourceCategory
46
43
47
44
/* *
48
45
* This class implements the Repository interface on onto a directory structure that matches the
@@ -51,7 +48,6 @@ import org.opencds.cqf.fhir.utility.repository.ResourceCategory
51
48
class IGInputStreamStructureRepository (
52
49
private val fhirContext : FhirContext ,
53
50
private val root : String? = null ,
54
- private val layoutMode : IGLayoutMode = IGLayoutMode .DIRECTORY ,
55
51
private val encodingEnum : EncodingEnum = EncodingEnum .JSON ,
56
52
) : Loadable(), Repository {
57
53
private val resourceCache: MutableMap <String , IBaseResource > = HashMap ()
@@ -62,42 +58,32 @@ class IGInputStreamStructureRepository(
62
58
resourceCache.clear()
63
59
}
64
60
65
- protected fun <T : IBaseResource ?, I : IIdType ?> locationForResource (
61
+ private fun <T : IBaseResource ?, I : IIdType ?> locationForResource (
66
62
resourceType : Class <T >,
67
63
id : I ,
68
64
): String {
69
65
val directory = directoryForType(resourceType)
70
66
return directory + " /" + fileNameForLayoutAndEncoding(resourceType.simpleName, id!! .idPart)
71
67
}
72
68
73
- protected fun fileNameForLayoutAndEncoding (resourceType : String , resourceId : String ): String {
69
+ private fun fileNameForLayoutAndEncoding (resourceType : String , resourceId : String ): String {
74
70
val name = resourceId + fileExtensions[encodingEnum]
75
- return if (layoutMode == = IGLayoutMode .DIRECTORY ) {
76
- // TODO: case sensitivity!!
77
- resourceType.lowercase(Locale .getDefault()) + " /" + name
78
- } else {
79
- " $resourceType -$name "
80
- }
71
+ return " $resourceType -$name "
81
72
}
82
73
83
- protected fun <T : IBaseResource ?> directoryForType (resourceType : Class <T >): String {
74
+ private fun <T : IBaseResource ?> directoryForType (resourceType : Class <T >): String {
84
75
val category = ResourceCategory .forType(resourceType.simpleName)
85
76
val directory = categoryDirectories[category]
86
77
87
78
// TODO: what the heck is the path separator?
88
79
return (if (root!! .endsWith(" /" )) root else " $root /" ) + directory
89
80
}
90
81
91
- protected fun <T : IBaseResource ?> directoryForResource (resourceType : Class <T >): String {
92
- val directory = directoryForType(resourceType)
93
- return if (layoutMode == = IGLayoutMode .DIRECTORY ) {
94
- directory + " /" + resourceType.simpleName.lowercase(Locale .getDefault())
95
- } else {
96
- directory
97
- }
82
+ private fun <T : IBaseResource ?> directoryForResource (resourceType : Class <T >): String {
83
+ return directoryForType(resourceType)
98
84
}
99
85
100
- protected fun <T : IBaseResource , I : IIdType > readLocation (
86
+ private fun <T : IBaseResource , I : IIdType > readLocation (
101
87
resourceClass : Class <T >? ,
102
88
location : String ,
103
89
): T {
@@ -112,7 +98,7 @@ class IGInputStreamStructureRepository(
112
98
} as T
113
99
}
114
100
115
- protected fun <T : IBaseResource > handleLibrary (resource : T , location : String? ): T {
101
+ private fun <T : IBaseResource > handleLibrary (resource : T , location : String? ): T {
116
102
var resourceOutput = resource
117
103
if (resourceOutput.fhirType() == " Library" ) {
118
104
val cqlLocation: String?
@@ -161,7 +147,7 @@ class IGInputStreamStructureRepository(
161
147
return resourceOutput
162
148
}
163
149
164
- protected fun getCqlContent (rootPath : String? , relativePath : String? ): String {
150
+ private fun getCqlContent (rootPath : String? , relativePath : String? ): String {
165
151
val p = File (File (rootPath).parent, relativePath).normalize().toString()
166
152
return try {
167
153
load(p)
@@ -171,18 +157,14 @@ class IGInputStreamStructureRepository(
171
157
}
172
158
}
173
159
174
- protected fun <T : IBaseResource > readLocation (resourceClass : Class <T >): Map <IIdType , T > {
160
+ private fun <T : IBaseResource > readLocation (resourceClass : Class <T >): Map <IIdType , T > {
175
161
val location = directoryForResource(resourceClass)
176
162
val resources = HashMap <IIdType , T >()
177
163
178
164
val inputFiles = listFiles(location)
179
165
180
166
for (file in inputFiles) {
181
- if (
182
- layoutMode.equals(IGLayoutMode .DIRECTORY ) ||
183
- (layoutMode.equals(IGLayoutMode .TYPE_PREFIX ) &&
184
- file.startsWith(resourceClass.simpleName + " -" ))
185
- ) {
167
+ if (file.startsWith(resourceClass.simpleName + " -" )) {
186
168
try {
187
169
val r = this .readLocation<T , IIdType >(resourceClass, " $location /$file " )
188
170
if (r.fhirType() == resourceClass.simpleName) {
@@ -282,7 +264,7 @@ class IGInputStreamStructureRepository(
282
264
): B {
283
265
val builder = BundleBuilder (fhirContext)
284
266
val resourceIdMap = readLocation(resourceType)
285
- if (searchParameters == null || searchParameters .isEmpty()) {
267
+ if (searchParameters.isEmpty()) {
286
268
resourceIdMap.values.forEach(
287
269
Consumer { theResource: T ->
288
270
builder.addCollectionEntry(
@@ -442,6 +424,34 @@ class IGInputStreamStructureRepository(
442
424
}
443
425
444
426
companion object {
427
+ enum class ResourceCategory {
428
+ DATA ,
429
+ TERMINOLOGY ,
430
+ CONTENT ,
431
+ ;
432
+
433
+ companion object {
434
+ private val TERMINOLOGY_RESOURCES : Set <String > = hashSetOf(" ValueSet" , " CodeSystem" )
435
+ private val CONTENT_RESOURCES : Set <String > =
436
+ hashSetOf(
437
+ " Library" ,
438
+ " Questionnaire" ,
439
+ " Measure" ,
440
+ " PlanDefinition" ,
441
+ " StructureDefinition" ,
442
+ " ActivityDefinition" ,
443
+ )
444
+
445
+ fun forType (resourceType : String ): ResourceCategory {
446
+ return if (TERMINOLOGY_RESOURCES .contains(resourceType)) {
447
+ TERMINOLOGY
448
+ } else {
449
+ if (CONTENT_RESOURCES .contains(resourceType)) CONTENT else DATA
450
+ }
451
+ }
452
+ }
453
+ }
454
+
445
455
private val categoryDirectories: Map <ResourceCategory , String > =
446
456
ImmutableMap .Builder <ResourceCategory , String >()
447
457
.put(ResourceCategory .CONTENT , " resources" )
0 commit comments