Permalink
Browse files

Massive refactoring

  • Loading branch information...
1 parent 1928fd8 commit d6505a74605eada925a6a771a6f06e3079931576 @mwl mwl committed Sep 12, 2012
View
4 sc-core/src/main/java/dk/sst/snomedcave/model/Concept.java
@@ -123,4 +123,8 @@ public String toString() {
append("childs", childs).
toString();
}
+
+ public void setChilds(Set<ConceptRelation> childs) {
+ this.childs = childs;
+ }
}
View
128 sc-web/src/main/java/dk/sst/snomedcave/controllers/ConceptController.java
@@ -6,7 +6,6 @@
import dk.sst.snomedcave.dao.ConceptRepository;
import dk.sst.snomedcave.model.Concept;
import dk.sst.snomedcave.model.ConceptRelation;
-import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.Predicate;
import org.apache.commons.collections15.Transformer;
import org.apache.log4j.Logger;
@@ -27,10 +26,10 @@
import javax.inject.Inject;
import java.util.*;
+import static java.util.Arrays.asList;
import static java.util.Collections.reverse;
import static java.util.Collections.sort;
-import static org.apache.commons.collections15.CollectionUtils.collect;
-import static org.apache.commons.collections15.CollectionUtils.filter;
+import static org.apache.commons.collections15.CollectionUtils.*;
import static org.neo4j.graphdb.DynamicRelationshipType.withName;
import static org.neo4j.graphdb.traversal.Evaluators.returnWhereEndNodeIs;
@@ -58,6 +57,8 @@ public int compare(ConceptNode o1, ConceptNode o2) {
private Concept isAType;
+ private Gson gson = new GsonBuilder().create();
+
final TraversalDescription td = Traversal.description()
.breadthFirst()
.relationships(withName("childs"), Direction.OUTGOING)
@@ -71,10 +72,46 @@ private Concept getIsA() {
}
private ConceptNode toConceptNode(Concept concept) {
+ get(concept);
+ boolean hasChilds = exists(concept.getChilds(), new Predicate<ConceptRelation>() {
+ @Override
+ public boolean evaluate(ConceptRelation relation) {
+ return shouldInclude(relation);
+ }
+ });
+ return new ConceptNode(
+ concept.getConceptId(),
+ concept.getTerm(),
+ hasChilds);
+ }
+
+ private ConceptNode toConceptNodeWithChilds(Concept concept) {
+ return toConceptNodeWithChilds(concept, new ConceptNode("", null, false));
+ }
+
+ private ConceptNode toConceptNodeWithChilds(Concept concept, final ConceptNode included) {
+ get(concept);
+ //TODO: filter childs
+ return toConceptNodeWithChilds(
+ concept,
+ collect(concept.getChilds(), new Transformer<ConceptRelation, ConceptNode>() {
+ @Override
+ public ConceptNode transform(ConceptRelation relation) {
+ final Concept child = get(relation).getChild();
+ return get(child).getConceptId().equals(included.conceptId) ? included : toConceptNode(child);
+ }
+ })
+ );
+ }
+
+ private ConceptNode toConceptNodeWithChilds(Concept concept, Collection<ConceptNode> childs) {
+ get(concept);
return new ConceptNode(
concept.getConceptId(),
concept.getTerm(),
- false);
+ childs
+ );
+
}
@RequestMapping(value = "search", produces = "application/json;charset=utf-8")
@@ -90,32 +127,13 @@ private ConceptNode toConceptNode(Concept concept) {
@RequestMapping(value = "node", produces = "application/json;charset=utf-8")
public ResponseEntity<String> nodeJson(@RequestParam("id") String conceptId) {
- Gson gson = new GsonBuilder().create();
- final Concept concept = conceptRepository.getByConceptId(conceptId);
- final Set<ConceptRelation> childs = new HashSet<ConceptRelation>(concept.getChilds());
- filter(childs, new Predicate<ConceptRelation>() {
- @Override
- public boolean evaluate(ConceptRelation relation) {
- return shouldInclude(relation);
- }
- });
return new ResponseEntity<String>(gson.toJson(
- new ConceptNode(
- concept.getConceptId(),
- concept.getTerm(),
- collect(childs, new Transformer<ConceptRelation, ConceptNode>() {
- @Override
- public ConceptNode transform(ConceptRelation relation) {
- Concept child = get(get(relation).getChild());
- return new ConceptNode(child.getConceptId(), child.getTerm(), childs.size() > 0);
- }
- })))
- , HttpStatus.OK);
+ toConceptNodeWithChilds(conceptRepository.getByConceptId(conceptId))),
+ HttpStatus.OK);
}
@RequestMapping(value = "tree", produces = "application/json;charset=utf-8")
public ResponseEntity<String> treeJson(@RequestParam("id") String conceptId) {
- Gson gson = new GsonBuilder().create();
return new ResponseEntity<String>(gson.toJson(conceptTree(conceptId).getRoot()), HttpStatus.OK);
}
@@ -127,46 +145,19 @@ public TreeResponse conceptTree(final String conceptId) {
}
List<Concept> levels = getThreeLevelsUpFrom(target);
reverse(levels);
- final Set<ConceptRelation> rootChilds = new HashSet<ConceptRelation>(target.getChilds());
- filter(rootChilds, new Predicate<ConceptRelation>() {
- @Override
- public boolean evaluate(ConceptRelation relation) {
- return shouldInclude(relation);
- }
- });
- ConceptNode root = new ConceptNode(
- target.getConceptId(),
- target.getTerm(),
- collect(rootChilds, new Transformer<ConceptRelation, ConceptNode>() {
- @Override
- public ConceptNode transform(ConceptRelation relation) {
- final Concept concept = get(get(relation).getChild());
- return new ConceptNode(concept.getConceptId(), concept.getTerm(), rootChilds.size() > 0);
- }
- }));
+ ConceptNode root = toConceptNodeWithChilds(target);
for (int i = 1; i <= 3; i++) {
if (i >= levels.size()) {
break;
}
Concept concept = levels.get(i);
- root = new ConceptNode(concept.getConceptId(), concept.getTerm(), root);
- if (i == 1) { //First parent
- final Set<ConceptRelation> childs = new HashSet<ConceptRelation>(concept.getChilds());
- filter(childs, new Predicate<ConceptRelation>() {
- @Override
- public boolean evaluate(ConceptRelation relation) {
- return shouldInclude(relation) && !get(get(relation).getChild()).getConceptId().equals(conceptId);
- }
- });
- for (ConceptRelation relation : childs) {
- Concept child = get(get(relation).getChild());
- if (!child.getConceptId().equals(root.conceptId)) {
- root.childs.add(new ConceptNode(child.getConceptId(), child.getTerm(), child.getChilds().size() > 0));
- }
- }
+ if (i == 1) {
+ root = toConceptNodeWithChilds(concept, root);
+ }
+ else {
+ root = toConceptNodeWithChilds(concept, asList(root));
}
- sort(root.childs, CONCEPTNAME_COMPARATOR);
}
return new TreeResponse(root);
@@ -202,10 +193,27 @@ private boolean shouldInclude(ConceptRelation relation) {
}
private ConceptRelation get(ConceptRelation relation) {
- return conceptRelationRepository.findOne(relation.getNodeId());
+ if (relation.getRelationId() == null) {
+ ConceptRelation dbRelation = conceptRelationRepository.findOne(relation.getNodeId());
+ relation.setRelationId(dbRelation.getRelationId());
+ relation.setChild(dbRelation.getChild());
+ relation.setType(dbRelation.getType());
+ }
+ return relation;
}
private Concept get(Concept concept) {
- return conceptRepository.findOne(concept.getNodeId());
+ if (concept.getConceptId() == null) {
+ Concept dbConcept = conceptRepository.findOne(concept.getNodeId());
+ concept.setConceptId(dbConcept.getConceptId());
+ concept.setSnomedId(dbConcept.getSnomedId());
+ concept.setCtv3Id(dbConcept.getCtv3Id());
+ concept.setFullyspecifiedName(dbConcept.getFullyspecifiedName());
+ concept.setPrimitive(dbConcept.isPrimitive());
+ concept.setStatus(dbConcept.getStatus());
+ concept.setTerm(dbConcept.getTerm());
+ concept.setChilds(dbConcept.getChilds());
+ }
+ return concept;
}
}

0 comments on commit d6505a7

Please sign in to comment.