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
import com.vaadin.ui.Notification;
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.util.CdmQueryFactory;
38
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
39
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
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
	    	Representation representation = term.getRepresentation(Language.DEFAULT());
69
	    	if(representation!=null){
70
	    		if(DistributionEditorUtil.isAbbreviatedLabels()){
71
	    			String label = representation.getLabel();
72
	    			String abbreviatedLabel = representation.getAbbreviatedLabel();
73
					if(abbreviatedLabel!=null && abbreviatedLabel.equalsIgnoreCase(distributionAreaString)){
74
	    				namedArea = (NamedArea) term;
75
	    				break;
76
	    			}
77
					else if(label!=null && label.equalsIgnoreCase(distributionAreaString)){
78
						namedArea = (NamedArea) term;
79
						break;
80
					}
81
	    		}
82
	    	}
83
	        if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
84
	        	namedArea = (NamedArea) term;
85
	        	break;
86
	        }
87
	    }
88
	    if(namedArea==null){
89
	    	Notification.show("Error during update of distribution term!");
90
	    	return -1;
91
	    }
92
	    List<Distribution> distributions = getDistributions(taxon);
93
	    Distribution distribution = null;
94
	    for(Distribution dist : distributions){
95
	        if(dist.getArea()!=null && dist.getArea().equals(namedArea)){
96
	            distribution = dist;
97
	            break;
98
	        }
99
	    }
100
	    if(distribution==null){
101
	    	//create new distribution
102
	    	distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
103
			Set<TaxonDescription> descriptions = taxon.getDescriptions();
104
			if (descriptions != null && !descriptions.isEmpty()) {
105
			    for (TaxonDescription desc : descriptions) {
106
			        // add to first taxon description
107
			        desc.addElement(distribution);
108
				    getTaxonService().saveOrUpdate(taxon);
109
			        return 0;
110
			    }
111
			} else {// there are no TaxonDescription yet.
112
			    TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
113
			    taxonDescription.addElement(distribution);
114
			    taxon.addDescription(taxonDescription);
115
			    getTaxonService().saveOrUpdate(taxon);
116
			    return 0;
117
			}
118
	    }
119
	    else if(comboValue == null){//delete descriptionElementBase
120
	    	distribution.getInDescription().removeElement(distribution);
121
            getTaxonService().saveOrUpdate(taxon);
122
            return 1;
123
	    }
124
	    else{
125
           distribution.setStatus((PresenceAbsenceTerm)comboValue);
126
           getTaxonService().saveOrUpdate(taxon);
127
           return 0;
128
        }
129
	    return -1;
130
	}
131

    
132
	public Set<DefinedTermBase> getChosenTerms() {
133
		VaadinSession session = VaadinSession.getCurrent();
134
		UUID termUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_VOCABULARY_UUID);
135
		TermVocabulary<DefinedTermBase> term = vocabularyService.load(termUUID);
136
		term = CdmBase.deproxy(term, TermVocabulary.class);
137
		return term.getTerms();
138
	}
139

    
140
	public List<String> getAbbreviatedTermList() {
141
		Set<NamedArea> terms = getTermSet();
142
		List<String> list = new ArrayList<String>();
143
		for(DefinedTermBase dtb: terms){
144
		    for(Representation r : dtb.getRepresentations()){
145
		        list.add(r.getAbbreviatedLabel());
146
		    }
147
		}
148
		return list;
149
	}
150

    
151
	public Set<NamedArea> getNamedAreas(){
152
	    Set<NamedArea> namedAreas = (Set<NamedArea>) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
153
	    if(namedAreas!=null && namedAreas.isEmpty()){
154
	        return getTermSet();
155
	    }
156
        return namedAreas;
157
	}
158

    
159
	private Set<NamedArea> getTermSet(){
160
	    VaadinSession session = VaadinSession.getCurrent();
161
	    UUID termUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_VOCABULARY_UUID);
162
	    TermVocabulary<NamedArea> vocabulary = vocabularyService.load(termUUID);
163
	    vocabulary = CdmBase.deproxy(vocabulary, TermVocabulary.class);
164
	    return vocabulary.getTermsOrderedByLabels(Language.DEFAULT());
165
	}
166

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

    
184
	public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
185
		List<DescriptionElementBase> listDescriptionElementsForTaxon = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
186
		sort(listDescriptionElementsForTaxon);
187
		return listDescriptionElementsForTaxon;
188
	}
189

    
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
	public List<TaxonNode> getAllNodes(){
198
		TaxonNode taxonNode = getChosenTaxonNode();
199
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
200
		if(taxonNode!=null){
201
			if(taxonNode.getTaxon()!=null){
202
				nodes.add(taxonNode);
203
			}
204
			nodes.addAll(taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null));
205
		}
206
		return nodes;
207
	}
208

    
209

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

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

    
222

    
223
	public CdmSQLContainer getSQLContainer() throws SQLException{
224
		List<Integer> nodeIds = new ArrayList<Integer>();
225
		for (TaxonNode taxonNode : getAllNodes()) {
226
			nodeIds.add(taxonNode.getId());
227
		}
228
		Set<NamedArea> namedAreas = getNamedAreas();
229
		if(namedAreas!=null){
230
			return new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namedAreas));
231
		}
232
		return null;
233
	}
234

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

    
259
	public IClassificationService getClassificationService() {
260
		return classificationService;
261
	}
262

    
263
	public IVocabularyService getVocabularyService() {
264
		return vocabularyService;
265
	}
266

    
267
	public IDescriptionService getDescriptionService() {
268
		return descriptionService;
269
	}
270

    
271
	public ITaxonNodeService getTaxonNodeService() {
272
		return taxonNodeService;
273
	}
274

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

    
282
	/**Helper Methods*/
283

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

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

    
296
				}
297
			}
298
		});
299
	}
300
}
    (1-1/1)