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