Project

General

Profile

Download (10.7 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.server.VaadinSession;
16

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

    
43

    
44
public class DistributionTablePresenter implements IDistributionTableComponent.DistributionTableComponentListener{
45

    
46

    
47
	private final IClassificationService classificationService;
48
	private final IVocabularyService vocabularyService;
49
	private final IDescriptionService descriptionService;
50
	private final ITaxonNodeService taxonNodeService;
51
	private final ITermService termService;
52
	private final DistributionTableView view;
53
	private final ITaxonService taxonService;
54

    
55
	public DistributionTablePresenter(DistributionTableView dtv) throws SQLException{
56
	    this.view = dtv;
57
	    view.addListener(this);
58
	    taxonService = CdmSpringContextHelper.getTaxonService();
59
	    classificationService = CdmSpringContextHelper.getClassificationService();
60
	    taxonNodeService = CdmSpringContextHelper.getTaxonNodeService();
61
		vocabularyService = CdmSpringContextHelper.getVocabularyService();
62
		descriptionService = CdmSpringContextHelper.getDescriptionService();
63
		termService = CdmSpringContextHelper.getTermService();
64
	}
65

    
66

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

    
117
	@Override
118
	public Set<DefinedTermBase> getChosenTerms() {
119
		VaadinSession session = VaadinSession.getCurrent();
120
		UUID termUUID = (UUID) session.getAttribute("selectedTerm");
121
		TermVocabulary<DefinedTermBase> term = vocabularyService.load(termUUID);
122
		term = CdmBase.deproxy(term, TermVocabulary.class);
123
		return term.getTerms();
124
	}
125

    
126

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

    
140
    @Override
141
    public List<String> getTermList() {
142
        SortedSet<DefinedTermBase> terms = getTermSet();
143
        List<String> list = new ArrayList<String>();
144
        for(DefinedTermBase dtb: terms){
145
           list.add(dtb.getTitleCache());
146
        }
147
//      Collections.sort(list);
148
        return list;
149
    }
150
    
151
    private List<String> getNamedAreas(){
152
    	String selectedAreas = (String) VaadinSession.getCurrent().getAttribute("selectedAreas");
153
    	return Arrays.asList(selectedAreas.split(","));
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(){
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, null));
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()) {
228
			nodeIds.add(taxonNode.getId());
229
		}
230
		List<String> termList = getTermList();
231
		List<String> namesAreaUuids = getNamedAreas();
232
		CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(termList, nodeIds, namesAreaUuids));
233
		return container;
234
	}
235

    
236
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
237
            "$",
238
            "elements.*",
239
            "elements.sources.citation.authorship.$",
240
            "elements.sources.nameUsedInSource.originalNameString",
241
            "elements.area.level",
242
            "elements.modifyingText",
243
            "elements.states.*",
244
            "elements.media",
245
            "elements.multilanguageText",
246
            "multilanguageText",
247
            "stateData.$",
248
            "annotations",
249
            "markers",
250
            "sources.citation.authorship",
251
            "sources.nameUsedInSource",
252
            "multilanguageText",
253
            "media",
254
            "name.$",
255
            "name.rank.representations",
256
            "name.status.type.representations",
257
            "taxon2.name"
258
    });
259

    
260

    
261
	@Override
262
	public IClassificationService getClassificationService() {
263
		return classificationService;
264
	}
265

    
266

    
267
	@Override
268
	public IVocabularyService getVocabularyService() {
269
		return vocabularyService;
270
	}
271

    
272

    
273
	@Override
274
	public IDescriptionService getDescriptionService() {
275
		return descriptionService;
276
	}
277

    
278

    
279
	@Override
280
	public ITaxonNodeService getTaxonNodeService() {
281
		return taxonNodeService;
282
	}
283

    
284

    
285
	@Override
286
	public ITermService getTermService() {
287
		return termService;
288
	}
289

    
290
	@Override
291
	public LazyLoadedContainer getTableContainer() {
292
		return null;
293
	}
294

    
295

    
296

    
297
	@Override
298
	public ITaxonService getTaxonService() {
299
		return taxonService;
300
	}
301

    
302

    
303
	/**Helper Methods*/
304

    
305
	private void sort(List<DescriptionElementBase> list){
306
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
307

    
308
			@Override
309
			public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
310
				String feature1 = o1.getFeature().getTitleCache();
311
				String feature2 = o2.getFeature().getTitleCache();
312
				if(feature1 !=null && feature2 !=null){
313
					return feature1.compareTo(feature2);
314
				}else{
315
					return 0;
316

    
317
				}
318
			}
319
		});
320

    
321
	}
322
}
(2-2/2)