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.entity.TermConceptParentChildLink;
26  import ca.uhn.fhir.jpa.term.IRecordHandler;
27  import org.apache.commons.csv.CSVRecord;
28  
29  import java.util.Map;
30  
31  import static org.apache.commons.lang3.StringUtils.isNotBlank;
32  import static org.apache.commons.lang3.StringUtils.trim;
33  
34  public class LoincHierarchyHandler implements IRecordHandler {
35  
36     private Map<String, TermConcept> myCode2Concept;
37     private TermCodeSystemVersion myCodeSystemVersion;
38  
39     public LoincHierarchyHandler(TermCodeSystemVersion theCodeSystemVersion, Map<String, TermConcept> theCode2concept) {
40        myCodeSystemVersion = theCodeSystemVersion;
41        myCode2Concept = theCode2concept;
42     }
43  
44     @Override
45     public void accept(CSVRecord theRecord) {
46        String parentCode = trim(theRecord.get("IMMEDIATE_PARENT"));
47        String childCode = trim(theRecord.get("CODE"));
48        String childCodeText = trim(theRecord.get("CODE_TEXT"));
49  
50        if (isNotBlank(parentCode) && isNotBlank(childCode)) {
51           TermConcept parent = getOrCreate(parentCode, "(unknown)");
52           TermConcept child = getOrCreate(childCode, childCodeText);
53  
54           parent.addChild(child, TermConceptParentChildLink.RelationshipTypeEnum.ISA);
55        }
56     }
57  
58     private TermConcept getOrCreate(String theCode, String theDisplay) {
59        TermConcept retVal = myCode2Concept.get(theCode);
60        if (retVal == null) {
61           retVal = new TermConcept();
62           retVal.setCodeSystemVersion(myCodeSystemVersion);
63           retVal.setCode(theCode);
64           retVal.setDisplay(theDisplay);
65           myCode2Concept.put(theCode, retVal);
66        }
67        return retVal;
68     }
69  
70  }