1 |
513ad98e
|
Cherian Mathew
|
// $Id$
|
2 |
|
|
/**
|
3 |
|
|
* Copyright (C) 2015 EDIT
|
4 |
|
|
* European Distributed Institute of Taxonomy
|
5 |
|
|
* http://www.e-taxonomy.eu
|
6 |
|
|
*
|
7 |
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9 |
|
|
*/
|
10 |
|
|
package eu.etaxonomy.cdm.vaadin.jscomponent;
|
11 |
|
|
|
12 |
|
|
import java.util.ArrayList;
|
13 |
|
|
import java.util.HashMap;
|
14 |
|
|
import java.util.List;
|
15 |
|
|
import java.util.Map;
|
16 |
|
|
import java.util.Set;
|
17 |
08af4f8a
|
Cherian Mathew
|
import java.util.UUID;
|
18 |
513ad98e
|
Cherian Mathew
|
|
19 |
|
|
import org.apache.log4j.Logger;
|
20 |
|
|
import org.json.JSONArray;
|
21 |
|
|
import org.json.JSONException;
|
22 |
|
|
import org.json.JSONObject;
|
23 |
|
|
|
24 |
|
|
import com.vaadin.annotations.JavaScript;
|
25 |
|
|
import com.vaadin.annotations.StyleSheet;
|
26 |
|
|
import com.vaadin.ui.AbstractJavaScriptComponent;
|
27 |
|
|
import com.vaadin.ui.JavaScriptFunction;
|
28 |
|
|
|
29 |
c839d071
|
Katja Luther
|
import elemental.json.JsonArray;
|
30 |
f1a1f6de
|
Cherian Mathew
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
31 |
513ad98e
|
Cherian Mathew
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
32 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
33 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
34 |
084507a8
|
Cherian Mathew
|
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
|
35 |
f1a1f6de
|
Cherian Mathew
|
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
|
36 |
08af4f8a
|
Cherian Mathew
|
import eu.etaxonomy.cdm.vaadin.component.ConceptRelationshipComposite;
|
37 |
513ad98e
|
Cherian Mathew
|
|
38 |
|
|
/**
|
39 |
|
|
* @author cmathew
|
40 |
|
|
* @date 8 Apr 2015
|
41 |
|
|
*
|
42 |
|
|
*/
|
43 |
|
|
@StyleSheet({"css/d3.conceptrelationshiptree.css"})
|
44 |
|
|
@JavaScript({"extlib/d3.min.js", "lib/d3.conceptrelationshiptree_connector.js"})
|
45 |
|
|
public class D3ConceptRelationshipTree extends AbstractJavaScriptComponent {
|
46 |
|
|
|
47 |
|
|
private static final Logger logger = Logger.getLogger(D3ConceptRelationshipTree.class);
|
48 |
|
|
|
49 |
08af4f8a
|
Cherian Mathew
|
public enum Mode {
|
50 |
|
|
OneToOne,
|
51 |
|
|
Group
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
private Mode mode;
|
55 |
|
|
|
56 |
6809e6bd
|
Cherian Mathew
|
public enum Direction {
|
57 |
|
|
LEFT_RIGHT("left-right"),
|
58 |
|
|
RIGHT_LEFT("right-left");
|
59 |
|
|
|
60 |
|
|
private final String name;
|
61 |
|
|
|
62 |
|
|
private Direction(String s) {
|
63 |
|
|
name = s;
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
@Override
|
67 |
|
|
public String toString(){
|
68 |
|
|
return name;
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
private Direction direction;
|
73 |
|
|
|
74 |
08af4f8a
|
Cherian Mathew
|
private ConceptRelationshipComposite conceptRelComposite;
|
75 |
|
|
|
76 |
513ad98e
|
Cherian Mathew
|
public D3ConceptRelationshipTree() {
|
77 |
6809e6bd
|
Cherian Mathew
|
this(Mode.OneToOne, Direction.LEFT_RIGHT);
|
78 |
08af4f8a
|
Cherian Mathew
|
}
|
79 |
|
|
|
80 |
6809e6bd
|
Cherian Mathew
|
public D3ConceptRelationshipTree(Direction direction) {
|
81 |
|
|
this(Mode.OneToOne, direction);
|
82 |
|
|
}
|
83 |
08af4f8a
|
Cherian Mathew
|
|
84 |
6809e6bd
|
Cherian Mathew
|
public D3ConceptRelationshipTree(Mode mode, Direction direction) {
|
85 |
|
|
this.mode = mode;
|
86 |
|
|
this.direction = direction;
|
87 |
08af4f8a
|
Cherian Mathew
|
addFunction("select", new JavaScriptFunction() {
|
88 |
513ad98e
|
Cherian Mathew
|
|
89 |
|
|
@Override
|
90 |
c839d071
|
Katja Luther
|
public void call(JsonArray arguments) {
|
91 |
|
|
//Notification.show("Store selected","uuid : " + arguments.getJSONObject(0).getString("uuid"), Type.WARNING_MESSAGE);
|
92 |
08af4f8a
|
Cherian Mathew
|
if(conceptRelComposite != null) {
|
93 |
|
|
UUID relUuid = UUID.fromString(arguments.getString(0));
|
94 |
|
|
conceptRelComposite.setSelectedTaxonRelUuid(relUuid);
|
95 |
|
|
}
|
96 |
c839d071
|
Katja Luther
|
|
97 |
|
|
}
|
98 |
513ad98e
|
Cherian Mathew
|
});
|
99 |
|
|
setConceptRelationshipTree("");
|
100 |
|
|
|
101 |
|
|
}
|
102 |
|
|
|
103 |
08af4f8a
|
Cherian Mathew
|
public void setConceptRelComposite(ConceptRelationshipComposite conceptRelComposite) {
|
104 |
|
|
this.conceptRelComposite = conceptRelComposite;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
2ba67e3f
|
Cherian Mathew
|
|
108 |
6809e6bd
|
Cherian Mathew
|
public void update(Taxon fromTaxon, Direction direction) throws JSONException {
|
109 |
|
|
this.direction = direction;
|
110 |
08af4f8a
|
Cherian Mathew
|
switch(mode) {
|
111 |
|
|
case OneToOne:
|
112 |
|
|
updateForOneToOne(fromTaxon);
|
113 |
|
|
break;
|
114 |
|
|
case Group:
|
115 |
|
|
updateForGroup(fromTaxon);
|
116 |
|
|
break;
|
117 |
|
|
default:
|
118 |
|
|
updateForOneToOne(fromTaxon);
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
9d11084b
|
Cherian Mathew
|
public void clear() {
|
123 |
|
|
setConceptRelationshipTree("{}");
|
124 |
|
|
}
|
125 |
|
|
|
126 |
08af4f8a
|
Cherian Mathew
|
private void updateForOneToOne(Taxon fromTaxon) throws JSONException {
|
127 |
|
|
Set<TaxonRelationship> relationsFromThisTaxon = fromTaxon.getRelationsFromThisTaxon();
|
128 |
|
|
|
129 |
|
|
Map<TaxonRelationshipType, List<Taxon>> relToTaxonMap = new HashMap<TaxonRelationshipType, List<Taxon>>();
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
JSONObject fromTaxonJO = new JSONObject();
|
133 |
f1a1f6de
|
Cherian Mathew
|
fromTaxonJO.put("name", getAbbreviatedName(fromTaxon.getName()));
|
134 |
08af4f8a
|
Cherian Mathew
|
fromTaxonJO.put("uuid", fromTaxon.getUuid().toString());
|
135 |
f1a1f6de
|
Cherian Mathew
|
fromTaxonJO.put("type", "ftaxon");
|
136 |
6809e6bd
|
Cherian Mathew
|
fromTaxonJO.put("direction", direction.toString());
|
137 |
08af4f8a
|
Cherian Mathew
|
|
138 |
|
|
JSONArray ftChildren = new JSONArray();
|
139 |
|
|
fromTaxonJO.put("children", ftChildren);
|
140 |
|
|
|
141 |
|
|
int typeIndex = 0;
|
142 |
|
|
if(relationsFromThisTaxon !=null && !relationsFromThisTaxon.isEmpty()) {
|
143 |
|
|
for(TaxonRelationship tr : relationsFromThisTaxon) {
|
144 |
|
|
if(tr != null && fromTaxon.equals(tr.getFromTaxon())) {
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
JSONObject crJO = new JSONObject();
|
148 |
|
|
crJO.put("name", tr.getType().getTitleCache());
|
149 |
|
|
crJO.put("uuid", tr.getUuid());
|
150 |
|
|
crJO.put("type", "conceptr");
|
151 |
|
|
|
152 |
|
|
ftChildren.put(typeIndex, crJO);
|
153 |
|
|
|
154 |
|
|
JSONArray crChildrenJA = new JSONArray();
|
155 |
|
|
crJO.put("children", crChildrenJA);
|
156 |
|
|
|
157 |
|
|
Taxon toTaxon = tr.getToTaxon();
|
158 |
|
|
|
159 |
|
|
JSONObject toTaxonJO = new JSONObject();
|
160 |
|
|
toTaxonJO.put("name", toTaxon.getName().getTitleCache());
|
161 |
|
|
toTaxonJO.put("uuid", toTaxon.getUuid());
|
162 |
f1a1f6de
|
Cherian Mathew
|
toTaxonJO.put("type", "ttaxon");
|
163 |
08af4f8a
|
Cherian Mathew
|
|
164 |
|
|
crChildrenJA.put(0, toTaxonJO);
|
165 |
|
|
typeIndex++;
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
setConceptRelationshipTree(fromTaxonJO.toString());
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
private void updateForGroup(Taxon fromTaxon) throws JSONException {
|
173 |
513ad98e
|
Cherian Mathew
|
Set<TaxonRelationship> relationsFromThisTaxon = fromTaxon.getRelationsFromThisTaxon();
|
174 |
|
|
|
175 |
|
|
Map<TaxonRelationshipType, List<Taxon>> relToTaxonMap = new HashMap<TaxonRelationshipType, List<Taxon>>();
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
JSONObject fromTaxonJO = new JSONObject();
|
179 |
|
|
fromTaxonJO.put("name", fromTaxon.getTitleCache());
|
180 |
|
|
fromTaxonJO.put("uuid", fromTaxon.getUuid().toString());
|
181 |
|
|
|
182 |
08af4f8a
|
Cherian Mathew
|
|
183 |
513ad98e
|
Cherian Mathew
|
if(relationsFromThisTaxon !=null && !relationsFromThisTaxon.isEmpty()) {
|
184 |
|
|
for(TaxonRelationship tr : relationsFromThisTaxon) {
|
185 |
|
|
if(fromTaxon.equals(tr.getFromTaxon())) {
|
186 |
|
|
if(relToTaxonMap.containsKey(tr.getType())) {
|
187 |
|
|
relToTaxonMap.get(tr.getType()).add(tr.getToTaxon());
|
188 |
|
|
} else {
|
189 |
|
|
List<Taxon> toTaxonList = new ArrayList<Taxon>();
|
190 |
|
|
toTaxonList.add(tr.getToTaxon());
|
191 |
|
|
relToTaxonMap.put(tr.getType(), toTaxonList);
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
int typeIndex = 0;
|
197 |
|
|
JSONArray ftChildren = new JSONArray();
|
198 |
|
|
fromTaxonJO.put("children", ftChildren);
|
199 |
|
|
|
200 |
|
|
for (Map.Entry<TaxonRelationshipType, List<Taxon>> entry : relToTaxonMap.entrySet()) {
|
201 |
|
|
|
202 |
|
|
JSONObject crJO = new JSONObject();
|
203 |
|
|
crJO.put("name", entry.getKey().getTitleCache());
|
204 |
|
|
crJO.put("uuid", entry.getKey().getUuid());
|
205 |
|
|
|
206 |
|
|
JSONArray crChildrenJA = new JSONArray();
|
207 |
|
|
crJO.put("children", crChildrenJA);
|
208 |
|
|
|
209 |
|
|
int toTaxonIndex = 0;
|
210 |
|
|
for(Taxon toTaxon: entry.getValue()) {
|
211 |
|
|
JSONObject toTaxonJO = new JSONObject();
|
212 |
|
|
toTaxonJO.put("name", toTaxon.getTitleCache());
|
213 |
|
|
toTaxonJO.put("uuid", toTaxon.getUuid());
|
214 |
|
|
crChildrenJA.put(toTaxonIndex, toTaxonJO);
|
215 |
|
|
toTaxonIndex++;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
ftChildren.put(typeIndex, crJO);
|
219 |
|
|
typeIndex++;
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
2ba67e3f
|
Cherian Mathew
|
setConceptRelationshipTree(fromTaxonJO.toString());
|
223 |
513ad98e
|
Cherian Mathew
|
}
|
224 |
|
|
|
225 |
08af4f8a
|
Cherian Mathew
|
|
226 |
f1a1f6de
|
Cherian Mathew
|
public String getAbbreviatedName(TaxonNameBase tnb) {
|
227 |
|
|
List<TaggedText> taggedTextList = tnb.getTaggedName();
|
228 |
|
|
StringBuffer nameSb = new StringBuffer();
|
229 |
|
|
boolean foundGenusOrUninomial = false;
|
230 |
|
|
boolean previousTagSeparator = false;
|
231 |
|
|
for(TaggedText tt: taggedTextList) {
|
232 |
084507a8
|
Cherian Mathew
|
if(!(tt.getType() == TagEnum.year) &&
|
233 |
|
|
!(tt.getType() == TagEnum.reference) &&
|
234 |
|
|
!(tt.getType() == TagEnum.authors)) {
|
235 |
|
|
if(tt.getType() == TagEnum.name && !foundGenusOrUninomial) {
|
236 |
f1a1f6de
|
Cherian Mathew
|
nameSb.append(tt.getText().charAt(0) + ".");
|
237 |
|
|
foundGenusOrUninomial = true;
|
238 |
|
|
} else {
|
239 |
|
|
if(!previousTagSeparator) {
|
240 |
|
|
nameSb.append(" ");
|
241 |
|
|
}
|
242 |
|
|
nameSb.append(tt.getText());
|
243 |
|
|
previousTagSeparator = false;
|
244 |
084507a8
|
Cherian Mathew
|
if(tt.getType() == TagEnum.separator) {
|
245 |
f1a1f6de
|
Cherian Mathew
|
previousTagSeparator = true;
|
246 |
|
|
}
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
return nameSb.toString();
|
251 |
|
|
}
|
252 |
|
|
|
253 |
513ad98e
|
Cherian Mathew
|
public void setConceptRelationshipTree(String conceptRelationshipTree) {
|
254 |
|
|
getState().setConceptRelationshipTree(conceptRelationshipTree);;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
@Override
|
258 |
|
|
public D3ConceptRelationshipTreeState getState() {
|
259 |
|
|
return (D3ConceptRelationshipTreeState) super.getState();
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
}
|