Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.vaadin.presenter.dbstatus;
2

    
3
import java.sql.SQLException;
4
import java.util.ArrayList;
5
import java.util.Arrays;
6
import java.util.Collections;
7
import java.util.Comparator;
8
import java.util.HashMap;
9
import java.util.HashSet;
10
import java.util.List;
11
import java.util.Set;
12
import java.util.SortedSet;
13
import java.util.UUID;
14

    
15
import com.vaadin.data.Container;
16
import com.vaadin.data.util.BeanItemContainer;
17
import com.vaadin.server.VaadinSession;
18

    
19
import eu.etaxonomy.cdm.api.service.IClassificationService;
20
import eu.etaxonomy.cdm.api.service.IDescriptionService;
21
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
22
import eu.etaxonomy.cdm.api.service.ITaxonService;
23
import eu.etaxonomy.cdm.api.service.ITermService;
24
import eu.etaxonomy.cdm.api.service.IVocabularyService;
25
import eu.etaxonomy.cdm.api.service.NodeSortMode;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.common.Representation;
30
import eu.etaxonomy.cdm.model.common.TermType;
31
import eu.etaxonomy.cdm.model.common.TermVocabulary;
32
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
33
import eu.etaxonomy.cdm.model.description.Distribution;
34
import eu.etaxonomy.cdm.model.description.Feature;
35
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
36
import eu.etaxonomy.cdm.model.description.TaxonDescription;
37
import eu.etaxonomy.cdm.model.location.NamedArea;
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
41
import eu.etaxonomy.cdm.vaadin.model.LazyLoadedContainer;
42
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
43
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
44
import eu.etaxonomy.cdm.vaadin.view.dbstatus.DistributionTableView;
45
import eu.etaxonomy.cdm.vaadin.view.dbstatus.IDistributionTableComponent;
46

    
47

    
48
public class DistributionTablePresenter implements IDistributionTableComponent.DistributionTableComponentListener{
49

    
50

    
51
	private final IClassificationService classificationService;
52
	private final IVocabularyService vocabularyService;
53
	private final IDescriptionService descriptionService;
54
	private final ITaxonNodeService taxonNodeService;
55
	private final ITermService termService;
56
	private final DistributionTableView view;
57
	private final ITaxonService taxonService;
58

    
59
	public DistributionTablePresenter(DistributionTableView dtv) throws SQLException{
60
	    this.view = dtv;
61
	    view.addListener(this);
62
	    taxonService = CdmSpringContextHelper.getTaxonService();
63
	    classificationService = CdmSpringContextHelper.getClassificationService();
64
	    taxonNodeService = CdmSpringContextHelper.getTaxonNodeService();
65
		vocabularyService = CdmSpringContextHelper.getVocabularyService();
66
		descriptionService = CdmSpringContextHelper.getDescriptionService();
67
		termService = CdmSpringContextHelper.getTermService();
68
		view.dataBinding();
69
	}
70

    
71

    
72
	@Override
73
    public int updateDistributionField(String distributionAreaString, Object comboValue, Taxon taxon) {
74
	    Set<DefinedTermBase> chosenTerms = getChosenTerms();
75
	    NamedArea namedArea = null;
76
	    for(DefinedTermBase term:chosenTerms){
77
	        if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
78
	            namedArea = (NamedArea) term;
79
	            break;
80
	        }
81
	    }
82
	    List<Distribution> distributions = getDistributions(taxon);
83
	    Distribution distribution = null;
84
	    for(Distribution dist : distributions){
85
	        if(dist.getArea().equals(namedArea)){
86
	            distribution = dist;
87
	            break;
88
	        }
89
	    }
90
	    if(distribution==null){
91
	    	//create new distribution
92
	    	distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
93
			Set<TaxonDescription> descriptions = taxon.getDescriptions();
94
			if (descriptions != null && !descriptions.isEmpty()) {
95
			    for (TaxonDescription desc : descriptions) {
96
			        // add to first taxon description
97
			        desc.addElement(distribution);
98
				    getTaxonService().saveOrUpdate(taxon);
99
			        return 0;
100
			    }
101
			} else {// there are no TaxonDescription yet.
102
			    TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
103
			    taxonDescription.addElement(distribution);
104
			    taxon.addDescription(taxonDescription);
105
			    getTaxonService().saveOrUpdate(taxon);
106
			    return 0;
107
			}
108
	    }
109
	    else if(comboValue == null){//delete descriptionElementBase
110
	    	distribution.getInDescription().removeElement(distribution);
111
            getTaxonService().saveOrUpdate(taxon);
112
            return 1;
113
	    }
114
	    else{
115
           distribution.setStatus((PresenceAbsenceTerm)comboValue);
116
           getTaxonService().saveOrUpdate(taxon);
117
           return 0;
118
        }
119
	    return -1;
120
	}
121

    
122
	@Override
123
	public Set<DefinedTermBase> getChosenTerms() {
124
		VaadinSession session = VaadinSession.getCurrent();
125
		UUID termUUID = (UUID) session.getAttribute("selectedTerm");
126
		TermVocabulary<DefinedTermBase> term = vocabularyService.load(termUUID);
127
		term = CdmBase.deproxy(term, TermVocabulary.class);
128
		return term.getTerms();
129
	}
130

    
131

    
132
	@Override
133
	public List<String> getAbbreviatedTermList() {
134
		SortedSet<DefinedTermBase> terms = getTermSet();
135
		List<String> list = new ArrayList<String>();
136
		for(DefinedTermBase dtb: terms){
137
		    for(Representation r : dtb.getRepresentations()){
138
		        list.add(r.getAbbreviatedLabel());
139
		    }
140
		}
141
//		Collections.sort(list);
142
		return list;
143
	}
144

    
145
    @Override
146
    public List<String> getTermList() {
147
        SortedSet<DefinedTermBase> terms = getTermSet();
148
        List<String> list = new ArrayList<String>();
149
        for(DefinedTermBase dtb: terms){
150
           list.add(dtb.getTitleCache());
151
        }
152
//      Collections.sort(list);
153
        return list;
154
    }
155

    
156

    
157
	private SortedSet<DefinedTermBase> getTermSet(){
158
	    VaadinSession session = VaadinSession.getCurrent();
159
	    UUID termUUID = (UUID) session.getAttribute("selectedTerm");
160
	    TermVocabulary<DefinedTermBase> term = vocabularyService.load(termUUID);
161
	    term = CdmBase.deproxy(term, TermVocabulary.class);
162
	    return term.getTermsOrderedByLabels(Language.DEFAULT());
163
	}
164

    
165
	@Override
166
	public HashMap<DescriptionElementBase, Distribution> getDistribution(DefinedTermBase dt, Taxon taxon) {
167
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
168
		List<DescriptionElementBase> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
169
		HashMap<DescriptionElementBase, Distribution> map = null;
170
		for(DescriptionElementBase deb : listTaxonDescription){
171
			if(deb instanceof Distribution){
172
				Distribution db = (Distribution)deb;
173
				String titleCache = dt.getTitleCache();
174
				if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
175
					map = new HashMap<DescriptionElementBase, Distribution>();
176
					map.put(deb, db);
177
				}
178
			}
179
		}
180
		return map;
181
	}
182
	@Override
183
	public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
184
		List<DescriptionElementBase> listDescriptionElementsForTaxon = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
185
		sort(listDescriptionElementsForTaxon);
186
		return listDescriptionElementsForTaxon;
187
	}
188

    
189
	@Override
190
	public List<Distribution> getDistributions(Taxon taxon) {
191
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
192
		List<Distribution> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
193
		return listTaxonDescription;
194

    
195
	}
196

    
197
	@Override
198
	public List<TaxonNode> getAllNodes(int start, int end){
199
		TaxonNode taxonNode = getChosenTaxonNode();
200
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
201
		if(taxonNode.getTaxon()!=null){
202
			nodes.add(taxonNode);
203
		}
204
		nodes.addAll(taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, NodeSortMode.NaturalOrder));
205
		return nodes;
206
	}
207

    
208

    
209
	@Override
210
	public TaxonNode getChosenTaxonNode() {
211
		VaadinSession session = VaadinSession.getCurrent();
212
		UUID taxonNodeUUID = (UUID) session.getAttribute("taxonNodeUUID");
213
		TaxonNode classificationNode = taxonNodeService.load(taxonNodeUUID);
214
		return classificationNode;
215
	}
216

    
217
	@Override
218
	public int getSizeOfTaxonNode(){
219
		TaxonNode taxonNode = getChosenTaxonNode();
220
		return taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null).size();
221
	}
222

    
223

    
224
	@Override
225
	public CdmSQLContainer getSQLContainer() throws SQLException{
226
		List<Integer> nodeIds = new ArrayList<Integer>();
227
		for (TaxonNode taxonNode : getAllNodes(0, 0)) {
228
			nodeIds.add(taxonNode.getId());
229
		}
230
		List<String> termList = getTermList();
231
		CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(termList, nodeIds));
232
		return container;
233
	}
234

    
235
	@Override
236
	public Container getPresenceAbsenceContainer(){
237
		BeanItemContainer<PresenceAbsenceTerm> termContainer = new BeanItemContainer<PresenceAbsenceTerm>(PresenceAbsenceTerm.class);
238
		termContainer.addAll(getPresenceAbsenceTerms());
239
		return termContainer;
240
	}
241

    
242

    
243
	@Override
244
	public List<PresenceAbsenceTerm> getPresenceAbsenceTerms() {
245
		return termService.listByTermType(TermType.PresenceAbsenceTerm, null, null, null, null);
246
	}
247

    
248
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
249
            "$",
250
            "elements.*",
251
            "elements.sources.citation.authorship.$",
252
            "elements.sources.nameUsedInSource.originalNameString",
253
            "elements.area.level",
254
            "elements.modifyingText",
255
            "elements.states.*",
256
            "elements.media",
257
            "elements.multilanguageText",
258
            "multilanguageText",
259
            "stateData.$",
260
            "annotations",
261
            "markers",
262
            "sources.citation.authorship",
263
            "sources.nameUsedInSource",
264
            "multilanguageText",
265
            "media",
266
            "name.$",
267
            "name.rank.representations",
268
            "name.status.type.representations",
269
            "taxon2.name"
270
    });
271

    
272

    
273
	@Override
274
	public IClassificationService getClassificationService() {
275
		return classificationService;
276
	}
277

    
278

    
279
	@Override
280
	public IVocabularyService getVocabularyService() {
281
		return vocabularyService;
282
	}
283

    
284

    
285
	@Override
286
	public IDescriptionService getDescriptionService() {
287
		return descriptionService;
288
	}
289

    
290

    
291
	@Override
292
	public ITaxonNodeService getTaxonNodeService() {
293
		return taxonNodeService;
294
	}
295

    
296

    
297
	@Override
298
	public ITermService getTermService() {
299
		return termService;
300
	}
301

    
302
	@Override
303
	public LazyLoadedContainer getTableContainer() {
304
		return null;
305
	}
306

    
307

    
308

    
309
	@Override
310
	public ITaxonService getTaxonService() {
311
		return taxonService;
312
	}
313

    
314

    
315
	/**Helper Methods*/
316

    
317
	private void sort(List<DescriptionElementBase> list){
318
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
319

    
320
			@Override
321
			public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
322
				String feature1 = o1.getFeature().getTitleCache();
323
				String feature2 = o2.getFeature().getTitleCache();
324
				if(feature1 !=null && feature2 !=null){
325
					return feature1.compareTo(feature2);
326
				}else{
327
					return 0;
328

    
329
				}
330
			}
331
		});
332

    
333
	}
334
}
(2-2/2)