View Javadoc
1   package ca.uhn.fhir.jpa.term.snomedct;
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.*;
30  
31  public final class SctHandlerRelationship implements IRecordHandler {
32     private final Map<String, TermConcept> myCode2concept;
33     private final TermCodeSystemVersion myCodeSystemVersion;
34     private final Map<String, TermConcept> myRootConcepts;
35  
36     public SctHandlerRelationship(TermCodeSystemVersion theCodeSystemVersion, HashMap<String, TermConcept> theRootConcepts, Map<String, TermConcept> theCode2concept) {
37        myCodeSystemVersion = theCodeSystemVersion;
38        myRootConcepts = theRootConcepts;
39        myCode2concept = theCode2concept;
40     }
41  
42     @Override
43     public void accept(CSVRecord theRecord) {
44        Set<String> ignoredTypes = new HashSet<String>();
45        ignoredTypes.add("Method (attribute)");
46        ignoredTypes.add("Direct device (attribute)");
47        ignoredTypes.add("Has focus (attribute)");
48        ignoredTypes.add("Access instrument");
49        ignoredTypes.add("Procedure site (attribute)");
50        ignoredTypes.add("Causative agent (attribute)");
51        ignoredTypes.add("Course (attribute)");
52        ignoredTypes.add("Finding site (attribute)");
53        ignoredTypes.add("Has definitional manifestation (attribute)");
54  
55        String sourceId = theRecord.get("sourceId");
56        String destinationId = theRecord.get("destinationId");
57        String typeId = theRecord.get("typeId");
58        boolean active = "1".equals(theRecord.get("active"));
59  
60        TermConcept typeConcept = myCode2concept.get(typeId);
61        TermConcept sourceConcept = myCode2concept.get(sourceId);
62        TermConcept targetConcept = myCode2concept.get(destinationId);
63        if (sourceConcept != null && targetConcept != null && typeConcept != null) {
64           if (typeConcept.getDisplay().equals("Is a (attribute)")) {
65              TermConceptParentChildLink.RelationshipTypeEnum relationshipType = TermConceptParentChildLink.RelationshipTypeEnum.ISA;
66              if (!sourceId.equals(destinationId)) {
67                 if (active) {
68                    TermConceptParentChildLink link = new TermConceptParentChildLink();
69                    link.setChild(sourceConcept);
70                    link.setParent(targetConcept);
71                    link.setRelationshipType(relationshipType);
72                    link.setCodeSystem(myCodeSystemVersion);
73  
74                    targetConcept.addChild(sourceConcept, relationshipType);
75                 } else {
76                    // not active, so we're removing any existing links
77                    for (TermConceptParentChildLink next : new ArrayList<TermConceptParentChildLink>(targetConcept.getChildren())) {
78                       if (next.getRelationshipType() == relationshipType) {
79                          if (next.getChild().getCode().equals(sourceConcept.getCode())) {
80                             next.getParent().getChildren().remove(next);
81                             next.getChild().getParents().remove(next);
82                          }
83                       }
84                    }
85                 }
86              }
87           } else if (ignoredTypes.contains(typeConcept.getDisplay())) {
88              // ignore
89           } else {
90              // ourLog.warn("Unknown relationship type: {}/{}", typeId, typeConcept.getDisplay());
91           }
92        }
93     }
94  
95  }