Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Massive refactoring

  • Loading branch information...
commit d6505a74605eada925a6a771a6f06e3079931576 1 parent 1928fd8
Martin Westergaard Lassen mwl authored
4 sc-core/src/main/java/dk/sst/snomedcave/model/Concept.java
@@ -123,4 +123,8 @@ public String toString() {
123 123 append("childs", childs).
124 124 toString();
125 125 }
  126 +
  127 + public void setChilds(Set<ConceptRelation> childs) {
  128 + this.childs = childs;
  129 + }
126 130 }
128 sc-web/src/main/java/dk/sst/snomedcave/controllers/ConceptController.java
@@ -6,7 +6,6 @@
6 6 import dk.sst.snomedcave.dao.ConceptRepository;
7 7 import dk.sst.snomedcave.model.Concept;
8 8 import dk.sst.snomedcave.model.ConceptRelation;
9   -import org.apache.commons.collections15.CollectionUtils;
10 9 import org.apache.commons.collections15.Predicate;
11 10 import org.apache.commons.collections15.Transformer;
12 11 import org.apache.log4j.Logger;
@@ -27,10 +26,10 @@
27 26 import javax.inject.Inject;
28 27 import java.util.*;
29 28
  29 +import static java.util.Arrays.asList;
30 30 import static java.util.Collections.reverse;
31 31 import static java.util.Collections.sort;
32   -import static org.apache.commons.collections15.CollectionUtils.collect;
33   -import static org.apache.commons.collections15.CollectionUtils.filter;
  32 +import static org.apache.commons.collections15.CollectionUtils.*;
34 33 import static org.neo4j.graphdb.DynamicRelationshipType.withName;
35 34 import static org.neo4j.graphdb.traversal.Evaluators.returnWhereEndNodeIs;
36 35
@@ -58,6 +57,8 @@ public int compare(ConceptNode o1, ConceptNode o2) {
58 57
59 58 private Concept isAType;
60 59
  60 + private Gson gson = new GsonBuilder().create();
  61 +
61 62 final TraversalDescription td = Traversal.description()
62 63 .breadthFirst()
63 64 .relationships(withName("childs"), Direction.OUTGOING)
@@ -71,10 +72,46 @@ private Concept getIsA() {
71 72 }
72 73
73 74 private ConceptNode toConceptNode(Concept concept) {
  75 + get(concept);
  76 + boolean hasChilds = exists(concept.getChilds(), new Predicate<ConceptRelation>() {
  77 + @Override
  78 + public boolean evaluate(ConceptRelation relation) {
  79 + return shouldInclude(relation);
  80 + }
  81 + });
  82 + return new ConceptNode(
  83 + concept.getConceptId(),
  84 + concept.getTerm(),
  85 + hasChilds);
  86 + }
  87 +
  88 + private ConceptNode toConceptNodeWithChilds(Concept concept) {
  89 + return toConceptNodeWithChilds(concept, new ConceptNode("", null, false));
  90 + }
  91 +
  92 + private ConceptNode toConceptNodeWithChilds(Concept concept, final ConceptNode included) {
  93 + get(concept);
  94 + //TODO: filter childs
  95 + return toConceptNodeWithChilds(
  96 + concept,
  97 + collect(concept.getChilds(), new Transformer<ConceptRelation, ConceptNode>() {
  98 + @Override
  99 + public ConceptNode transform(ConceptRelation relation) {
  100 + final Concept child = get(relation).getChild();
  101 + return get(child).getConceptId().equals(included.conceptId) ? included : toConceptNode(child);
  102 + }
  103 + })
  104 + );
  105 + }
  106 +
  107 + private ConceptNode toConceptNodeWithChilds(Concept concept, Collection<ConceptNode> childs) {
  108 + get(concept);
74 109 return new ConceptNode(
75 110 concept.getConceptId(),
76 111 concept.getTerm(),
77   - false);
  112 + childs
  113 + );
  114 +
78 115 }
79 116
80 117 @RequestMapping(value = "search", produces = "application/json;charset=utf-8")
@@ -90,32 +127,13 @@ private ConceptNode toConceptNode(Concept concept) {
90 127
91 128 @RequestMapping(value = "node", produces = "application/json;charset=utf-8")
92 129 public ResponseEntity<String> nodeJson(@RequestParam("id") String conceptId) {
93   - Gson gson = new GsonBuilder().create();
94   - final Concept concept = conceptRepository.getByConceptId(conceptId);
95   - final Set<ConceptRelation> childs = new HashSet<ConceptRelation>(concept.getChilds());
96   - filter(childs, new Predicate<ConceptRelation>() {
97   - @Override
98   - public boolean evaluate(ConceptRelation relation) {
99   - return shouldInclude(relation);
100   - }
101   - });
102 130 return new ResponseEntity<String>(gson.toJson(
103   - new ConceptNode(
104   - concept.getConceptId(),
105   - concept.getTerm(),
106   - collect(childs, new Transformer<ConceptRelation, ConceptNode>() {
107   - @Override
108   - public ConceptNode transform(ConceptRelation relation) {
109   - Concept child = get(get(relation).getChild());
110   - return new ConceptNode(child.getConceptId(), child.getTerm(), childs.size() > 0);
111   - }
112   - })))
113   - , HttpStatus.OK);
  131 + toConceptNodeWithChilds(conceptRepository.getByConceptId(conceptId))),
  132 + HttpStatus.OK);
114 133 }
115 134
116 135 @RequestMapping(value = "tree", produces = "application/json;charset=utf-8")
117 136 public ResponseEntity<String> treeJson(@RequestParam("id") String conceptId) {
118   - Gson gson = new GsonBuilder().create();
119 137 return new ResponseEntity<String>(gson.toJson(conceptTree(conceptId).getRoot()), HttpStatus.OK);
120 138 }
121 139
@@ -127,46 +145,19 @@ public TreeResponse conceptTree(final String conceptId) {
127 145 }
128 146 List<Concept> levels = getThreeLevelsUpFrom(target);
129 147 reverse(levels);
130   - final Set<ConceptRelation> rootChilds = new HashSet<ConceptRelation>(target.getChilds());
131   - filter(rootChilds, new Predicate<ConceptRelation>() {
132   - @Override
133   - public boolean evaluate(ConceptRelation relation) {
134   - return shouldInclude(relation);
135   - }
136   - });
137   - ConceptNode root = new ConceptNode(
138   - target.getConceptId(),
139   - target.getTerm(),
140   - collect(rootChilds, new Transformer<ConceptRelation, ConceptNode>() {
141   - @Override
142   - public ConceptNode transform(ConceptRelation relation) {
143   - final Concept concept = get(get(relation).getChild());
144   - return new ConceptNode(concept.getConceptId(), concept.getTerm(), rootChilds.size() > 0);
145   - }
146   - }));
  148 + ConceptNode root = toConceptNodeWithChilds(target);
147 149
148 150 for (int i = 1; i <= 3; i++) {
149 151 if (i >= levels.size()) {
150 152 break;
151 153 }
152 154 Concept concept = levels.get(i);
153   - root = new ConceptNode(concept.getConceptId(), concept.getTerm(), root);
154   - if (i == 1) { //First parent
155   - final Set<ConceptRelation> childs = new HashSet<ConceptRelation>(concept.getChilds());
156   - filter(childs, new Predicate<ConceptRelation>() {
157   - @Override
158   - public boolean evaluate(ConceptRelation relation) {
159   - return shouldInclude(relation) && !get(get(relation).getChild()).getConceptId().equals(conceptId);
160   - }
161   - });
162   - for (ConceptRelation relation : childs) {
163   - Concept child = get(get(relation).getChild());
164   - if (!child.getConceptId().equals(root.conceptId)) {
165   - root.childs.add(new ConceptNode(child.getConceptId(), child.getTerm(), child.getChilds().size() > 0));
166   - }
167   - }
  155 + if (i == 1) {
  156 + root = toConceptNodeWithChilds(concept, root);
  157 + }
  158 + else {
  159 + root = toConceptNodeWithChilds(concept, asList(root));
168 160 }
169   - sort(root.childs, CONCEPTNAME_COMPARATOR);
170 161 }
171 162
172 163 return new TreeResponse(root);
@@ -202,10 +193,27 @@ private boolean shouldInclude(ConceptRelation relation) {
202 193 }
203 194
204 195 private ConceptRelation get(ConceptRelation relation) {
205   - return conceptRelationRepository.findOne(relation.getNodeId());
  196 + if (relation.getRelationId() == null) {
  197 + ConceptRelation dbRelation = conceptRelationRepository.findOne(relation.getNodeId());
  198 + relation.setRelationId(dbRelation.getRelationId());
  199 + relation.setChild(dbRelation.getChild());
  200 + relation.setType(dbRelation.getType());
  201 + }
  202 + return relation;
206 203 }
207 204
208 205 private Concept get(Concept concept) {
209   - return conceptRepository.findOne(concept.getNodeId());
  206 + if (concept.getConceptId() == null) {
  207 + Concept dbConcept = conceptRepository.findOne(concept.getNodeId());
  208 + concept.setConceptId(dbConcept.getConceptId());
  209 + concept.setSnomedId(dbConcept.getSnomedId());
  210 + concept.setCtv3Id(dbConcept.getCtv3Id());
  211 + concept.setFullyspecifiedName(dbConcept.getFullyspecifiedName());
  212 + concept.setPrimitive(dbConcept.isPrimitive());
  213 + concept.setStatus(dbConcept.getStatus());
  214 + concept.setTerm(dbConcept.getTerm());
  215 + concept.setChilds(dbConcept.getChilds());
  216 + }
  217 + return concept;
210 218 }
211 219 }

0 comments on commit d6505a7

Please sign in to comment.
Something went wrong with that request. Please try again.