Project

General

Profile

Download (10.9 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.UUID;
13

    
14
import com.vaadin.server.VaadinSession;
15

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

    
41

    
42
public class DistributionTablePresenter {
43

    
44
    private final IClassificationService classificationService;
45
	private final IVocabularyService vocabularyService;
46
	private final IDescriptionService descriptionService;
47
	private final ITaxonNodeService taxonNodeService;
48
	private final ITermService termService;
49
	private final DistributionTableView view;
50
	private final ITaxonService taxonService;
51

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

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

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

    
124
	public List<String> getAbbreviatedTermList() {
125
		Set<NamedArea> terms = getTermSet();
126
		List<String> list = new ArrayList<String>();
127
		for(DefinedTermBase dtb: terms){
128
		    for(Representation r : dtb.getRepresentations()){
129
		        list.add(r.getAbbreviatedLabel());
130
		    }
131
		}
132
		return list;
133
	}
134

    
135
	public Set<NamedArea> getNamedAreas(){
136
	    Set<NamedArea> namedAreas = (Set<NamedArea>) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
137
	    if(namedAreas.isEmpty()){
138
	        return getTermSet();
139
	    }
140
        return namedAreas;
141
	}
142

    
143
    public List<String> getNamedAreasLabels(boolean abbreviated){
144
        Set<NamedArea> selectedAreas = getNamedAreas();
145
    	List<String> namedAreaTitles = new ArrayList<>();
146
    	for (NamedArea namedArea : selectedAreas) {
147
    		String title = null;
148
    	    Representation representation = namedArea.getRepresentation(Language.DEFAULT());
149
    	    if(representation!=null){
150
    	    	if(abbreviated){
151
    	    		title = representation.getAbbreviatedLabel();
152
    	    	}
153
    	    	else{
154
    	    		title = representation.getLabel();
155
    	    	}
156
    	    }
157
    	    if(title==null){
158
    	    	title = namedArea.getTitleCache();
159
    	    }
160
    	    namedAreaTitles.add(title);
161
        }
162
    	return namedAreaTitles;
163
    }
164

    
165
	private Set<NamedArea> getTermSet(){
166
	    VaadinSession session = VaadinSession.getCurrent();
167
	    UUID termUUID = (UUID) session.getAttribute("selectedTerm");
168
	    TermVocabulary<NamedArea> vocabulary = vocabularyService.load(termUUID);
169
	    vocabulary = CdmBase.deproxy(vocabulary, TermVocabulary.class);
170
	    return vocabulary.getTermsOrderedByLabels(Language.DEFAULT());
171
	}
172

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

    
190
	public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
191
		List<DescriptionElementBase> listDescriptionElementsForTaxon = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
192
		sort(listDescriptionElementsForTaxon);
193
		return listDescriptionElementsForTaxon;
194
	}
195

    
196
	public List<Distribution> getDistributions(Taxon taxon) {
197
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
198
		List<Distribution> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
199
		return listTaxonDescription;
200

    
201
	}
202

    
203
	public List<TaxonNode> getAllNodes(){
204
		TaxonNode taxonNode = getChosenTaxonNode();
205
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
206
		if(taxonNode.getTaxon()!=null){
207
			nodes.add(taxonNode);
208
		}
209
		nodes.addAll(taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null));
210
		return nodes;
211
	}
212

    
213

    
214
	public TaxonNode getChosenTaxonNode() {
215
		VaadinSession session = VaadinSession.getCurrent();
216
		UUID taxonNodeUUID = (UUID) session.getAttribute("taxonNodeUUID");
217
		TaxonNode classificationNode = taxonNodeService.load(taxonNodeUUID);
218
		return classificationNode;
219
	}
220

    
221
	public int getSizeOfTaxonNode(){
222
		TaxonNode taxonNode = getChosenTaxonNode();
223
		return taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null).size();
224
	}
225

    
226

    
227
	public CdmSQLContainer getSQLContainer() throws SQLException{
228
		List<Integer> nodeIds = new ArrayList<Integer>();
229
		for (TaxonNode taxonNode : getAllNodes()) {
230
			nodeIds.add(taxonNode.getId());
231
		}
232
		Set<NamedArea> namesAreas = getNamedAreas();
233
		CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namesAreas, true));
234
		return container;
235
	}
236

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

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

    
265
	public IVocabularyService getVocabularyService() {
266
		return vocabularyService;
267
	}
268

    
269
	public IDescriptionService getDescriptionService() {
270
		return descriptionService;
271
	}
272

    
273
	public ITaxonNodeService getTaxonNodeService() {
274
		return taxonNodeService;
275
	}
276

    
277
	public ITermService getTermService() {
278
		return termService;
279
	}
280
	public ITaxonService getTaxonService() {
281
		return taxonService;
282
	}
283

    
284
	/**Helper Methods*/
285

    
286
	private void sort(List<DescriptionElementBase> list){
287
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
288

    
289
			@Override
290
			public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
291
				String feature1 = o1.getFeature().getTitleCache();
292
				String feature2 = o2.getFeature().getTitleCache();
293
				if(feature1 !=null && feature2 !=null){
294
					return feature1.compareTo(feature2);
295
				}else{
296
					return 0;
297

    
298
				}
299
			}
300
		});
301
	}
302
}
(2-2/2)