-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.configuration.customsearch | ||
|
||
import org.hl7.fhir.r4.model.Bundle | ||
|
||
interface ISearchParametersConfigStore { | ||
|
||
suspend fun write(bundle: Bundle) | ||
|
||
fun read(): Bundle? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.configuration.customsearch | ||
|
||
import org.hl7.fhir.r4.model.Bundle | ||
import org.hl7.fhir.r4.model.Enumerations | ||
import org.hl7.fhir.r4.model.ResourceType | ||
import org.hl7.fhir.r4.model.SearchParameter | ||
|
||
class SearchParametersConfigService( | ||
private val store: ISearchParametersConfigStore, | ||
Check warning on line 25 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
) { | ||
|
||
fun getCustomSearchParameters(): List<SearchParameter> { | ||
return predefinedCustomSearchParameters + readSavedSearchParameters() | ||
Check warning on line 29 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
} | ||
|
||
private fun readSavedSearchParameters(): List<SearchParameter> { | ||
val searchParametersBundle = store.read() ?: return emptyList() | ||
|
||
return searchParametersBundle.entry | ||
Check warning on line 35 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
.filter { it.resource.resourceType == ResourceType.SearchParameter } | ||
.mapNotNull { it.resource as? SearchParameter } | ||
} | ||
|
||
suspend fun saveBundle(bundle: Bundle) { | ||
store.write(bundle) | ||
Check warning on line 41 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
} | ||
|
||
/** List of predefined custom search parameters. */ | ||
private val predefinedCustomSearchParameters: List<SearchParameter> | ||
get() { | ||
val activeGroupSearchParameter = | ||
SearchParameter().apply { | ||
url = "http://smartregister.org/SearchParameter/group-active" | ||
addBase("Group") | ||
name = ACTIVE_SEARCH_PARAM | ||
code = ACTIVE_SEARCH_PARAM | ||
type = Enumerations.SearchParamType.TOKEN | ||
expression = "Group.active" | ||
description = "Search the active field" | ||
Check warning on line 55 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
} | ||
|
||
val flagStatusSearchParameter = | ||
SearchParameter().apply { | ||
url = "http://smartregister.org/SearchParameter/flag-status" | ||
addBase("Flag") | ||
name = STATUS_SEARCH_PARAM | ||
code = STATUS_SEARCH_PARAM | ||
type = Enumerations.SearchParamType.TOKEN | ||
expression = "Flag.status" | ||
description = "Search the status field" | ||
Check warning on line 66 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
} | ||
|
||
val medicationSortSearchParameter = | ||
SearchParameter().apply { | ||
url = MEDICATION_SORT_URL | ||
addBase("Medication") | ||
name = SORT_SEARCH_PARAM | ||
code = SORT_SEARCH_PARAM | ||
type = Enumerations.SearchParamType.NUMBER | ||
expression = "Medication.extension.where(url = '$MEDICATION_SORT_URL').value" | ||
description = "Search the sort field" | ||
Check warning on line 77 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
} | ||
|
||
val patientSearchParameter = | ||
SearchParameter().apply { | ||
url = "http://smartregister.org/SearchParameter/patient-search" | ||
addBase("Patient") | ||
name = SEARCH_PARAM | ||
code = SEARCH_PARAM | ||
type = Enumerations.SearchParamType.STRING | ||
expression = "Patient.name.text | Patient.identifier.value" | ||
description = "Search patients by name and identifier fields" | ||
Check warning on line 88 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
} | ||
|
||
return listOf( | ||
activeGroupSearchParameter, | ||
flagStatusSearchParameter, | ||
medicationSortSearchParameter, | ||
patientSearchParameter, | ||
Check warning on line 95 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/customsearch/SearchParametersConfigService.kt
|
||
) | ||
} | ||
|
||
companion object { | ||
private const val ACTIVE_SEARCH_PARAM = "active" | ||
private const val STATUS_SEARCH_PARAM = "status" | ||
private const val SORT_SEARCH_PARAM = "sort" | ||
private const val SEARCH_PARAM = "search" | ||
private const val MEDICATION_SORT_URL = | ||
"http://smartregister.org/SearchParameter/medication-sort" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.di | ||
|
||
import android.content.Context | ||
import ca.uhn.fhir.parser.IParser | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import javax.inject.Singleton | ||
import kotlinx.coroutines.withContext | ||
import org.hl7.fhir.r4.model.Bundle | ||
import org.smartregister.fhircore.engine.configuration.customsearch.ISearchParametersConfigStore | ||
import org.smartregister.fhircore.engine.configuration.customsearch.SearchParametersConfigService | ||
import org.smartregister.fhircore.engine.util.DispatcherProvider | ||
|
||
@InstallIn(SingletonComponent::class) | ||
@Module | ||
class CustomSearchModule { | ||
|
||
@Singleton | ||
@Provides | ||
@SearchParametersFileStore | ||
fun provideCustomSearchParameterConfigStore( | ||
@ApplicationContext context: Context, | ||
parser: IParser, | ||
dispatcherProvider: DispatcherProvider, | ||
): ISearchParametersConfigStore { | ||
val searchParametersFileName = "customSearchParameters.json" | ||
val searchParametersFile = File(context.filesDir, searchParametersFileName) | ||
|
||
return object : ISearchParametersConfigStore { | ||
override suspend fun write(bundle: Bundle) { | ||
withContext(dispatcherProvider.io()) { | ||
FileOutputStream(searchParametersFile).use { | ||
it.write(parser.encodeResourceToString(bundle).toByteArray()) | ||
} | ||
} | ||
} | ||
|
||
override fun read(): Bundle? { | ||
if (!searchParametersFile.exists()) return null | ||
return FileInputStream(searchParametersFile).bufferedReader().use { | ||
parser.parseResource(Bundle::class.java, it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideCustomSearchParameterService( | ||
@SearchParametersFileStore searchParametersStore: ISearchParametersConfigStore, | ||
): SearchParametersConfigService { | ||
return SearchParametersConfigService(searchParametersStore) | ||
} | ||
} |