View Javadoc
1   package ca.uhn.fhir.jpa.term.loinc;
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.entity.TermCodeSystemVersion;
24  import ca.uhn.fhir.jpa.entity.TermConcept;
25  import ca.uhn.fhir.jpa.term.IHapiTerminologyLoaderSvc;
26  import org.apache.commons.csv.CSVRecord;
27  import org.hl7.fhir.r4.model.ConceptMap;
28  import org.hl7.fhir.r4.model.ValueSet;
29  
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Properties;
33  
34  import static org.apache.commons.lang3.StringUtils.*;
35  
36  public class LoincAnswerListHandler extends BaseLoincHandler {
37  
38  	private final Map<String, TermConcept> myCode2Concept;
39  	private final TermCodeSystemVersion myCodeSystemVersion;
40  
41  	public LoincAnswerListHandler(TermCodeSystemVersion theCodeSystemVersion, Map<String, TermConcept> theCode2concept, List<ValueSet> theValueSets, List<ConceptMap> theConceptMaps, Properties theUploadProperties) {
42  		super(theCode2concept, theValueSets, theConceptMaps, theUploadProperties);
43  		myCodeSystemVersion = theCodeSystemVersion;
44  		myCode2Concept = theCode2concept;
45  	}
46  
47  	@Override
48  	public void accept(CSVRecord theRecord) {
49  
50  		// this is the code for the list (will repeat)
51  		String answerListId = trim(theRecord.get("AnswerListId"));
52  		String answerListName = trim(theRecord.get("AnswerListName"));
53  		String answerListOid = trim(theRecord.get("AnswerListOID"));
54  		String externallyDefined = trim(theRecord.get("ExtDefinedYN"));
55  		String extenrallyDefinedCs = trim(theRecord.get("ExtDefinedAnswerListCodeSystem"));
56  		String externallyDefinedLink = trim(theRecord.get("ExtDefinedAnswerListLink"));
57  		// this is the code for the actual answer (will not repeat)
58  		String answerString = trim(theRecord.get("AnswerStringId"));
59  		String sequenceNumber = trim(theRecord.get("SequenceNumber"));
60  		String displayText = trim(theRecord.get("DisplayText"));
61  		String extCodeId = trim(theRecord.get("ExtCodeId"));
62  		String extCodeDisplayName = trim(theRecord.get("ExtCodeDisplayName"));
63  		String extCodeSystem = trim(theRecord.get("ExtCodeSystem"));
64  		String extCodeSystemVersion = trim(theRecord.get("ExtCodeSystemVersion"));
65  
66  
67  		// Answer list code
68  		if (!myCode2Concept.containsKey(answerListId)) {
69  			TermConcept concept = new TermConcept(myCodeSystemVersion, answerListId);
70  			concept.setDisplay(answerListName);
71  			myCode2Concept.put(answerListId, concept);
72  		}
73  
74  		// Answer list ValueSet
75  		ValueSet vs = getValueSet(answerListId, "http://loinc.org/vs/" + answerListId, answerListName, "answerlist.version");
76  		if (vs.getIdentifier().isEmpty()) {
77  			vs.addIdentifier()
78  				.setSystem("urn:ietf:rfc:3986")
79  				.setValue("urn:oid:" + answerListOid);
80  		}
81  
82  		if (isNotBlank(answerString)) {
83  
84  			// Answer code
85  			if (!myCode2Concept.containsKey(answerString)) {
86  				TermConcept concept = new TermConcept(myCodeSystemVersion, answerString);
87  				concept.setDisplay(displayText);
88  				if (isNotBlank(sequenceNumber) && sequenceNumber.matches("^[0-9]$")) {
89  					concept.setSequence(Integer.parseInt(sequenceNumber));
90  				}
91  				myCode2Concept.put(answerString, concept);
92  			}
93  
94  			vs
95  				.getCompose()
96  				.getIncludeFirstRep()
97  				.setSystem(IHapiTerminologyLoaderSvc.LOINC_URI)
98  				.addConcept()
99  				.setCode(answerString)
100 				.setDisplay(displayText);
101 
102 		}
103 
104 	}
105 
106 }