Project

General

Profile

Download (8.73 KB) Statistics
| Branch: | Tag: | Revision:
1
// $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
import java.util.UUID;
18

    
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
import elemental.json.JsonArray;
30
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
33
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
34
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
35
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
36
import eu.etaxonomy.cdm.vaadin.component.ConceptRelationshipComposite;
37

    
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
    public enum Mode {
50
        OneToOne,
51
        Group
52
    }
53

    
54
    private Mode mode;
55

    
56
    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
    private ConceptRelationshipComposite conceptRelComposite;
75

    
76
    public D3ConceptRelationshipTree() {
77
        this(Mode.OneToOne, Direction.LEFT_RIGHT);
78
    }
79

    
80
    public D3ConceptRelationshipTree(Direction direction) {
81
        this(Mode.OneToOne, direction);
82
    }
83

    
84
    public D3ConceptRelationshipTree(Mode mode, Direction direction) {
85
        this.mode = mode;
86
        this.direction = direction;
87
        addFunction("select", new JavaScriptFunction() {
88

    
89
            @Override
90
			public void call(JsonArray arguments) {
91
				 //Notification.show("Store selected","uuid : " + arguments.getJSONObject(0).getString("uuid"), Type.WARNING_MESSAGE);
92
                if(conceptRelComposite != null) {
93
                    UUID relUuid = UUID.fromString(arguments.getString(0));
94
                    conceptRelComposite.setSelectedTaxonRelUuid(relUuid);
95
                }
96
				
97
			}
98
        });
99
        setConceptRelationshipTree("");
100

    
101
    }
102

    
103
    public void setConceptRelComposite(ConceptRelationshipComposite conceptRelComposite) {
104
        this.conceptRelComposite = conceptRelComposite;
105
    }
106

    
107

    
108
    public void update(Taxon fromTaxon, Direction direction) throws JSONException {
109
        this.direction = direction;
110
        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
    public void clear() {
123
        setConceptRelationshipTree("{}");
124
    }
125

    
126
    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
        fromTaxonJO.put("name", getAbbreviatedName(fromTaxon.getName()));
134
        fromTaxonJO.put("uuid", fromTaxon.getUuid().toString());
135
        fromTaxonJO.put("type", "ftaxon");
136
        fromTaxonJO.put("direction", direction.toString());
137

    
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
                    toTaxonJO.put("type", "ttaxon");
163

    
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
        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

    
183
        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
        setConceptRelationshipTree(fromTaxonJO.toString());
223
    }
224

    
225

    
226
    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
            if(!(tt.getType() == TagEnum.year) &&
233
                    !(tt.getType() == TagEnum.reference) &&
234
                    !(tt.getType() == TagEnum.authors)) {
235
                if(tt.getType() == TagEnum.name && !foundGenusOrUninomial) {
236
                    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
                    if(tt.getType() == TagEnum.separator) {
245
                        previousTagSeparator = true;
246
                    }
247
                }
248
            }
249
        }
250
        return nameSb.toString();
251
    }
252

    
253
    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
}
(1-1/2)