Project

General

Profile

Download (8.57 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 eu.etaxonomy.cdm.model.name.TaxonNameBase;
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.TaggedText;
34
import eu.etaxonomy.cdm.vaadin.component.ConceptRelationshipComposite;
35

    
36
/**
37
 * @author cmathew
38
 * @date 8 Apr 2015
39
 *
40
 */
41
@StyleSheet({"css/d3.conceptrelationshiptree.css"})
42
@JavaScript({"extlib/d3.min.js", "lib/d3.conceptrelationshiptree_connector.js"})
43
public class D3ConceptRelationshipTree extends AbstractJavaScriptComponent {
44

    
45
    private static final Logger logger = Logger.getLogger(D3ConceptRelationshipTree.class);
46

    
47
    public enum Mode {
48
        OneToOne,
49
        Group
50
    }
51

    
52
    private Mode mode;
53

    
54
    public enum Direction {
55
        LEFT_RIGHT("left-right"),
56
        RIGHT_LEFT("right-left");
57

    
58
        private final String name;
59

    
60
        private Direction(String s) {
61
            name = s;
62
        }
63

    
64
        @Override
65
        public String toString(){
66
           return name;
67
        }
68
    }
69

    
70
    private Direction direction;
71

    
72
    private ConceptRelationshipComposite conceptRelComposite;
73

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

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

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

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

    
98
    }
99

    
100
    public void setConceptRelComposite(ConceptRelationshipComposite conceptRelComposite) {
101
        this.conceptRelComposite = conceptRelComposite;
102
    }
103

    
104

    
105
    public void update(Taxon fromTaxon, Direction direction) throws JSONException {
106
        this.direction = direction;
107
        switch(mode) {
108
        case OneToOne:
109
            updateForOneToOne(fromTaxon);
110
            break;
111
        case Group:
112
            updateForGroup(fromTaxon);
113
            break;
114
        default:
115
            updateForOneToOne(fromTaxon);
116
        }
117
    }
118

    
119
    public void clear() {
120
        setConceptRelationshipTree("{}");
121
    }
122

    
123
    private void updateForOneToOne(Taxon fromTaxon) throws JSONException {
124
        Set<TaxonRelationship> relationsFromThisTaxon = fromTaxon.getRelationsFromThisTaxon();
125

    
126
        Map<TaxonRelationshipType, List<Taxon>> relToTaxonMap = new HashMap<TaxonRelationshipType, List<Taxon>>();
127

    
128

    
129
        JSONObject fromTaxonJO = new JSONObject();
130
        fromTaxonJO.put("name", getAbbreviatedName(fromTaxon.getName()));
131
        fromTaxonJO.put("uuid", fromTaxon.getUuid().toString());
132
        fromTaxonJO.put("type", "ftaxon");
133
        fromTaxonJO.put("direction", direction.toString());
134

    
135
        JSONArray ftChildren = new JSONArray();
136
        fromTaxonJO.put("children", ftChildren);
137

    
138
        int typeIndex = 0;
139
        if(relationsFromThisTaxon !=null && !relationsFromThisTaxon.isEmpty()) {
140
            for(TaxonRelationship tr : relationsFromThisTaxon) {
141
                if(tr != null && fromTaxon.equals(tr.getFromTaxon())) {
142

    
143

    
144
                    JSONObject crJO = new JSONObject();
145
                    crJO.put("name", tr.getType().getTitleCache());
146
                    crJO.put("uuid", tr.getUuid());
147
                    crJO.put("type", "conceptr");
148

    
149
                    ftChildren.put(typeIndex, crJO);
150

    
151
                    JSONArray crChildrenJA = new JSONArray();
152
                    crJO.put("children", crChildrenJA);
153

    
154
                    Taxon toTaxon = tr.getToTaxon();
155

    
156
                    JSONObject toTaxonJO = new JSONObject();
157
                    toTaxonJO.put("name", toTaxon.getName().getTitleCache());
158
                    toTaxonJO.put("uuid", toTaxon.getUuid());
159
                    toTaxonJO.put("type", "ttaxon");
160

    
161
                    crChildrenJA.put(0, toTaxonJO);
162
                    typeIndex++;
163
                }
164
            }
165
        }
166
        setConceptRelationshipTree(fromTaxonJO.toString());
167
    }
168

    
169
    private void updateForGroup(Taxon fromTaxon) throws JSONException {
170
        Set<TaxonRelationship> relationsFromThisTaxon = fromTaxon.getRelationsFromThisTaxon();
171

    
172
        Map<TaxonRelationshipType, List<Taxon>> relToTaxonMap = new HashMap<TaxonRelationshipType, List<Taxon>>();
173

    
174

    
175
        JSONObject fromTaxonJO = new JSONObject();
176
        fromTaxonJO.put("name", fromTaxon.getTitleCache());
177
        fromTaxonJO.put("uuid", fromTaxon.getUuid().toString());
178

    
179

    
180
        if(relationsFromThisTaxon !=null && !relationsFromThisTaxon.isEmpty()) {
181
            for(TaxonRelationship tr : relationsFromThisTaxon) {
182
                if(fromTaxon.equals(tr.getFromTaxon())) {
183
                    if(relToTaxonMap.containsKey(tr.getType())) {
184
                        relToTaxonMap.get(tr.getType()).add(tr.getToTaxon());
185
                    } else {
186
                        List<Taxon> toTaxonList = new ArrayList<Taxon>();
187
                        toTaxonList.add(tr.getToTaxon());
188
                        relToTaxonMap.put(tr.getType(), toTaxonList);
189
                    }
190
                }
191
            }
192

    
193
            int typeIndex = 0;
194
            JSONArray ftChildren = new JSONArray();
195
            fromTaxonJO.put("children", ftChildren);
196

    
197
            for (Map.Entry<TaxonRelationshipType, List<Taxon>> entry : relToTaxonMap.entrySet()) {
198

    
199
                JSONObject crJO = new JSONObject();
200
                crJO.put("name", entry.getKey().getTitleCache());
201
                crJO.put("uuid", entry.getKey().getUuid());
202

    
203
                JSONArray crChildrenJA = new JSONArray();
204
                crJO.put("children", crChildrenJA);
205

    
206
                int toTaxonIndex = 0;
207
                for(Taxon toTaxon: entry.getValue()) {
208
                    JSONObject toTaxonJO = new JSONObject();
209
                    toTaxonJO.put("name", toTaxon.getTitleCache());
210
                    toTaxonJO.put("uuid", toTaxon.getUuid());
211
                    crChildrenJA.put(toTaxonIndex, toTaxonJO);
212
                    toTaxonIndex++;
213
                }
214

    
215
                ftChildren.put(typeIndex, crJO);
216
                typeIndex++;
217
            }
218
        }
219
        setConceptRelationshipTree(fromTaxonJO.toString());
220
    }
221

    
222

    
223
    public String getAbbreviatedName(TaxonNameBase tnb) {
224
        List<TaggedText> taggedTextList = tnb.getTaggedName();
225
        StringBuffer nameSb = new StringBuffer();
226
        boolean foundGenusOrUninomial = false;
227
        boolean previousTagSeparator = false;
228
        for(TaggedText tt: taggedTextList) {
229
            if(!tt.isYear() &&!tt.isReference() && !tt.isAuthors()) {
230
                if(tt.isName() && !foundGenusOrUninomial) {
231
                    nameSb.append(tt.getText().charAt(0) + ".");
232
                    foundGenusOrUninomial = true;
233
                } else {
234
                    if(!previousTagSeparator) {
235
                        nameSb.append(" ");
236
                    }
237
                    nameSb.append(tt.getText());
238
                    previousTagSeparator = false;
239
                    if(tt.isSeparator()) {
240
                        previousTagSeparator = true;
241
                    }
242
                }
243
            }
244
        }
245
        return nameSb.toString();
246
    }
247

    
248
    public void setConceptRelationshipTree(String conceptRelationshipTree) {
249
        getState().setConceptRelationshipTree(conceptRelationshipTree);;
250
    }
251

    
252
    @Override
253
    public D3ConceptRelationshipTreeState getState() {
254
        return (D3ConceptRelationshipTreeState) super.getState();
255
    }
256

    
257
}
(1-1/2)