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  		if (isBlank(answerString)) {
67  			return;
68  		}
69  
70  		// Answer list code
71  		if (!myCode2Concept.containsKey(answerListId)) {
72  			TermConcept concept = new TermConcept(myCodeSystemVersion, answerListId);
73  			concept.setDisplay(answerListName);
74  			myCode2Concept.put(answerListId, concept);
75  		}
76  
77  		// Answer code
78  		if (!myCode2Concept.containsKey(answerString)) {
79  			TermConcept concept = new TermConcept(myCodeSystemVersion, answerString);
80  			concept.setDisplay(displayText);
81  			if (isNotBlank(sequenceNumber) && sequenceNumber.matches("^[0-9]$")) {
82  				concept.setSequence(Integer.parseInt(sequenceNumber));
83  			}
84  			myCode2Concept.put(answerString, concept);
85  		}
86  
87  		// Answer list ValueSet
88  		ValueSet vs = getValueSet(answerListId, "http://loinc.org/vs/" + answerListId, answerListName, "answerlist.version");
89  		if (vs.getIdentifier().isEmpty()) {
90  			vs.addIdentifier()
91  				.setSystem("urn:ietf:rfc:3986")
92  				.setValue("urn:oid:" + answerListOid);
93  		}
94  
95  		vs
96  			.getCompose()
97  			.getIncludeFirstRep()
98  			.setSystem(IHapiTerminologyLoaderSvc.LOINC_URI)
99  			.addConcept()
100 			.setCode(answerString)
101 			.setDisplay(displayText);
102 	}
103 
104 }