forked from openmrs/openmrs-module-fhir
/
DiagnosticReportServiceImpl.java
187 lines (159 loc) · 6.2 KB
/
DiagnosticReportServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* The contents of this file are subject to the OpenMRS Public License
* Version 1.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://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.fhir.api.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.DiagnosticReport;
import org.openmrs.Encounter;
import org.openmrs.api.APIException;
import org.openmrs.api.EncounterService;
import org.openmrs.api.context.Context;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.fhir.api.DiagnosticReportService;
import org.openmrs.module.fhir.api.db.FHIRDAO;
import org.openmrs.module.fhir.api.diagnosticreport.DiagnosticReportHandler;
import org.openmrs.module.fhir.api.util.FHIRConstants;
import org.openmrs.module.fhir.api.util.FHIRDiagnosticReportUtil;
import org.openmrs.util.OpenmrsClassLoader;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* It is a default implementation of {@link org.openmrs.module.fhir.api.DiagnosticReportService}.
*/
public class DiagnosticReportServiceImpl extends BaseOpenmrsService implements DiagnosticReportService {
protected final Log log = LogFactory.getLog(this.getClass());
private FHIRDAO dao;
private static Map<String, DiagnosticReportHandler> handlers = null;
/**
* @return the dao
*/
public FHIRDAO getDao() {
return dao;
}
/**
* @param dao the dao to set
*/
public void setDao(FHIRDAO dao) {
this.dao = dao;
}
@Override
public DiagnosticReport getDiagnosticReport(String id) {
// Find Diagnostic Report (Encounter) in OpenMRS database
EncounterService encounterService = Context.getEncounterService();
Encounter omrsDiagnosticReport = encounterService.getEncounterByUuid(id);
// Get corresponding Handler
String handlerName = omrsDiagnosticReport.getEncounterType().getName();
return FHIRDiagnosticReportUtil.getFHIRDiagnosticReport(id, getHandler(handlerName));
}
@Override
public DiagnosticReport createFHIRDiagnosticReport(DiagnosticReport diagnosticReport) {
log.debug("DiagnosticReportServiceImpl : create FHIRDiagnostic Report");
List<Coding> codingList = diagnosticReport.getCategory().getCoding();
// If serviceCategory is not present in the DiagnosticReport, then use "DEFAULT"
String handlerName = FHIRConstants.DEFAULT;
if (!codingList.isEmpty()) {
handlerName = codingList.get(0).getCode();
}
return FHIRDiagnosticReportUtil.saveDiagnosticReport(diagnosticReport, getHandler(handlerName));
}
@Override
public DiagnosticReport updateFHIRDiagnosticReport(DiagnosticReport diagnosticReport, String theId) {
log.debug("DiagnosticReportServiceImpl : updateFHIRDiagnosticReport with ID " + theId);
// Find Diagnostic Report (Encounter) in OpenMRS database
EncounterService encounterService = Context.getEncounterService();
Encounter omrsDiagnosticReport = encounterService.getEncounterByUuid(theId);
// Get corresponding Handler
String handlerName = omrsDiagnosticReport.getEncounterType().getName();
return FHIRDiagnosticReportUtil.updateDiagnosticReport(diagnosticReport, theId, getHandler(
handlerName));
}
/**
* @see org.openmrs.module.fhir.api.DiagnosticReportService#retireDiagnosticReport(String)
*/
@Override
public void retireDiagnosticReport(String id) {
// Find Diagnostic Report (Encounter) in OpenMRS database
EncounterService encounterService = Context.getEncounterService();
Encounter omrsDiagnosticReport = encounterService.getEncounterByUuid(id);
// Get corresponding Handler
String handlerName = omrsDiagnosticReport.getEncounterType().getName();
FHIRDiagnosticReportUtil.retireDiagnosticReport(id, getHandler(handlerName));
}
/**
* @see org.openmrs.module.fhir.api.DiagnosticReportService#getDiagnosticReportByPatientNameAndServiceCategory(String,
* String)
*/
@Override
public List<DiagnosticReport> getDiagnosticReportByPatientNameAndServiceCategory(String patientName, String
serviceCode) {
if (serviceCode == null) {
// Get DEFAULT Handler
serviceCode = "DEFAULT";
}
return FHIRDiagnosticReportUtil.getFHIRDiagnosticReportBySubjectName(patientName, getHandler(serviceCode));
}
/****************************************************************
* Handler Implementation
***************************************************************/
@Override
public DiagnosticReportHandler getHandler(String key) {
return handlers.get(key);
}
@Override
public void setHandlers(Map<String, DiagnosticReportHandler> newHandlers) throws APIException {
if (newHandlers == null) {
DiagnosticReportServiceImpl.setStaticHandlers(null);
return;
}
for (Map.Entry<String, DiagnosticReportHandler> entry : newHandlers.entrySet()) {
registerHandler(entry.getValue().getServiceCategory(), entry.getValue());
}
}
/**
* Sets handlers using static method
*
* @param currentHandlers
*/
private static void setStaticHandlers(Map<String, DiagnosticReportHandler> currentHandlers) {
DiagnosticReportServiceImpl.handlers = currentHandlers;
}
@Override
public Map<String, DiagnosticReportHandler> getHandlers() throws APIException {
if (handlers == null) {
handlers = new LinkedHashMap<String, DiagnosticReportHandler>();
}
return handlers;
}
@Override
public void registerHandler(String key, DiagnosticReportHandler handler) throws APIException {
getHandlers().put(key, handler);
}
@SuppressWarnings("unchecked")
@Override
public void registerHandler(String key, String handlerClass) throws APIException {
try {
Class loadedClass = OpenmrsClassLoader.getInstance().loadClass(handlerClass);
registerHandler(key, (DiagnosticReportHandler) loadedClass.newInstance());
}
catch (Exception e) {
throw new APIException("Unable.load.and.instantiate.handler", e);
}
}
@Override
public void removeHandler(String key) {
handlers.remove(key);
}
}