Project

General

Profile

Download (10.2 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.common.CdmUtils;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.common.Representation;
28
import eu.etaxonomy.cdm.model.common.TermVocabulary;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30
import eu.etaxonomy.cdm.model.description.Distribution;
31
import eu.etaxonomy.cdm.model.description.Feature;
32
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
33
import eu.etaxonomy.cdm.model.description.TaxonDescription;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
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

    
42

    
43
public class DistributionTablePresenter {
44

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

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

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

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

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

    
132
    public List<String> getNamedAreas(){
133
    	String selectedAreas = (String) VaadinSession.getCurrent().getAttribute("selectedAreas");
134
    	if(CdmUtils.isBlank(selectedAreas)){
135
    	    SortedSet<DefinedTermBase> terms = getTermSet();
136
            List<String> list = new ArrayList<String>();
137
            for(DefinedTermBase dtb: terms){
138
               list.add(dtb.getTitleCache());
139
            }
140
    	    return list;
141
    	}
142
    	return Arrays.asList(selectedAreas.split(","));
143
    }
144

    
145
	private SortedSet<DefinedTermBase> getTermSet(){
146
	    VaadinSession session = VaadinSession.getCurrent();
147
	    UUID termUUID = (UUID) session.getAttribute("selectedTerm");
148
	    TermVocabulary<DefinedTermBase> term = vocabularyService.load(termUUID);
149
	    term = CdmBase.deproxy(term, TermVocabulary.class);
150
	    return term.getTermsOrderedByLabels(Language.DEFAULT());
151
	}
152

    
153
	public HashMap<DescriptionElementBase, Distribution> getDistribution(DefinedTermBase dt, Taxon taxon) {
154
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
155
		List<DescriptionElementBase> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
156
		HashMap<DescriptionElementBase, Distribution> map = null;
157
		for(DescriptionElementBase deb : listTaxonDescription){
158
			if(deb instanceof Distribution){
159
				Distribution db = (Distribution)deb;
160
				String titleCache = dt.getTitleCache();
161
				if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
162
					map = new HashMap<DescriptionElementBase, Distribution>();
163
					map.put(deb, db);
164
				}
165
			}
166
		}
167
		return map;
168
	}
169

    
170
	public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
171
		List<DescriptionElementBase> listDescriptionElementsForTaxon = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
172
		sort(listDescriptionElementsForTaxon);
173
		return listDescriptionElementsForTaxon;
174
	}
175

    
176
	public List<Distribution> getDistributions(Taxon taxon) {
177
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
178
		List<Distribution> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
179
		return listTaxonDescription;
180

    
181
	}
182

    
183
	public List<TaxonNode> getAllNodes(){
184
		TaxonNode taxonNode = getChosenTaxonNode();
185
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
186
		if(taxonNode.getTaxon()!=null){
187
			nodes.add(taxonNode);
188
		}
189
		nodes.addAll(taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null));
190
		return nodes;
191
	}
192

    
193

    
194
	public TaxonNode getChosenTaxonNode() {
195
		VaadinSession session = VaadinSession.getCurrent();
196
		UUID taxonNodeUUID = (UUID) session.getAttribute("taxonNodeUUID");
197
		TaxonNode classificationNode = taxonNodeService.load(taxonNodeUUID);
198
		return classificationNode;
199
	}
200

    
201
	public int getSizeOfTaxonNode(){
202
		TaxonNode taxonNode = getChosenTaxonNode();
203
		return taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null).size();
204
	}
205

    
206

    
207
	public CdmSQLContainer getSQLContainer() throws SQLException{
208
		List<Integer> nodeIds = new ArrayList<Integer>();
209
		for (TaxonNode taxonNode : getAllNodes()) {
210
			nodeIds.add(taxonNode.getId());
211
		}
212
		List<String> namesAreaUuids = getNamedAreas();
213
		CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namesAreaUuids));
214
		return container;
215
	}
216

    
217
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
218
            "$",
219
            "elements.*",
220
            "elements.sources.citation.authorship.$",
221
            "elements.sources.nameUsedInSource.originalNameString",
222
            "elements.area.level",
223
            "elements.modifyingText",
224
            "elements.states.*",
225
            "elements.media",
226
            "elements.multilanguageText",
227
            "multilanguageText",
228
            "stateData.$",
229
            "annotations",
230
            "markers",
231
            "sources.citation.authorship",
232
            "sources.nameUsedInSource",
233
            "multilanguageText",
234
            "media",
235
            "name.$",
236
            "name.rank.representations",
237
            "name.status.type.representations",
238
            "taxon2.name"
239
    });
240

    
241
	public IClassificationService getClassificationService() {
242
		return classificationService;
243
	}
244

    
245
	public IVocabularyService getVocabularyService() {
246
		return vocabularyService;
247
	}
248

    
249
	public IDescriptionService getDescriptionService() {
250
		return descriptionService;
251
	}
252

    
253
	public ITaxonNodeService getTaxonNodeService() {
254
		return taxonNodeService;
255
	}
256

    
257
	public ITermService getTermService() {
258
		return termService;
259
	}
260
	public ITaxonService getTaxonService() {
261
		return taxonService;
262
	}
263

    
264
	/**Helper Methods*/
265

    
266
	private void sort(List<DescriptionElementBase> list){
267
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
268

    
269
			@Override
270
			public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
271
				String feature1 = o1.getFeature().getTitleCache();
272
				String feature2 = o2.getFeature().getTitleCache();
273
				if(feature1 !=null && feature2 !=null){
274
					return feature1.compareTo(feature2);
275
				}else{
276
					return 0;
277

    
278
				}
279
			}
280
		});
281
	}
282
}
(2-2/2)