View Javadoc
1   package ca.uhn.fhir.jpa.provider;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.jpa.term.IHapiTerminologyLoaderSvc;
24  import ca.uhn.fhir.jpa.term.IHapiTerminologyLoaderSvc.UploadStatistics;
25  import ca.uhn.fhir.rest.api.server.RequestDetails;
26  import ca.uhn.fhir.rest.param.StringParam;
27  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
28  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
29  import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
30  import org.hl7.fhir.r4.model.*;
31  import org.springframework.beans.factory.annotation.Autowired;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import java.io.*;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import static org.apache.commons.lang3.StringUtils.*;
39  
40  public abstract class BaseTerminologyUploaderProvider extends BaseJpaProvider {
41  	public static final String UPLOAD_EXTERNAL_CODE_SYSTEM = "$upload-external-code-system";
42  
43  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseTerminologyUploaderProvider.class);
44  	public static final String CONCEPT_COUNT = "conceptCount";
45  	public static final String TARGET = "target";
46  
47  	@Autowired
48  	private IHapiTerminologyLoaderSvc myTerminologyLoaderSvc;
49  
50  	protected Parameters handleUploadExternalCodeSystem(
51  		HttpServletRequest theServletRequest,
52  		StringParam theCodeSystemUrl,
53  		List<StringType> theLocalFile,
54  		List<Attachment> thePackage, RequestDetails theRequestDetails
55  	) {
56  
57  		startRequest(theServletRequest);
58  
59  		if (theLocalFile == null || theLocalFile.size() == 0) {
60  			if (thePackage == null || thePackage.size() == 0) {
61  				throw new InvalidRequestException("No 'localfile' or 'package' parameter, or package had no data");
62  			}
63  		}
64  
65  		try {
66  			List<IHapiTerminologyLoaderSvc.FileDescriptor> localFiles = new ArrayList<>();
67  			if (theLocalFile != null && theLocalFile.size() > 0) {
68  				for (StringType nextLocalFile : theLocalFile) {
69  					if (isNotBlank(nextLocalFile.getValue())) {
70  						ourLog.info("Reading in local file: {}", nextLocalFile.getValue());
71  						File nextFile = new File(nextLocalFile.getValue());
72  						if (!nextFile.exists() || !nextFile.isFile()) {
73  							throw new InvalidRequestException("Unknown file: " + nextFile.getName());
74  						}
75  						localFiles.add(new IHapiTerminologyLoaderSvc.FileDescriptor() {
76  							@Override
77  							public String getFilename() {
78  								return nextFile.getAbsolutePath();
79  							}
80  
81  							@Override
82  							public InputStream getInputStream() {
83  								try {
84  									return new FileInputStream(nextFile);
85  								} catch (FileNotFoundException theE) {
86  									throw new InternalErrorException(theE);
87  								}
88  							}
89  						});
90  					}
91  				}
92  			}
93  
94  			if (thePackage != null) {
95  				for (Attachment nextPackage : thePackage) {
96  					if (isBlank(nextPackage.getUrl())) {
97  						throw new UnprocessableEntityException("Package is missing mandatory url element");
98  					}
99  
100 					localFiles.add(new IHapiTerminologyLoaderSvc.FileDescriptor() {
101 						@Override
102 						public String getFilename() {
103 							return nextPackage.getUrl();
104 						}
105 
106 						@Override
107 						public InputStream getInputStream() {
108 							return new ByteArrayInputStream(nextPackage.getData());
109 						}
110 					});
111 				}
112 			}
113 
114 			String url = theCodeSystemUrl != null ? theCodeSystemUrl.getValue() : null;
115 			url = defaultString(url);
116 
117 			UploadStatistics stats;
118 			if (IHapiTerminologyLoaderSvc.SCT_URI.equals(url)) {
119 				stats = myTerminologyLoaderSvc.loadSnomedCt(localFiles, theRequestDetails);
120 			} else if (IHapiTerminologyLoaderSvc.LOINC_URI.equals(url)) {
121 				stats = myTerminologyLoaderSvc.loadLoinc(localFiles, theRequestDetails);
122 			} else {
123 				throw new InvalidRequestException("Unknown URL: " + url);
124 			}
125 
126 			Parameters retVal = new Parameters();
127 			retVal.addParameter().setName(CONCEPT_COUNT).setValue(new IntegerType(stats.getConceptCount()));
128 			retVal.addParameter().setName(TARGET).setValue(new Reference(stats.getTarget().getValue()));
129 			return retVal;
130 		} finally {
131 			endRequest(theServletRequest);
132 		}
133 	}
134 
135 
136 }