Project

General

Profile

Download (11.3 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.vaadin.presenter.distributionStatus;
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.Classification;
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.util.DistributionEditorUtil;
41
import eu.etaxonomy.cdm.vaadin.view.distributionStatus.DistributionTableView;
42

    
43

    
44
public class DistributionTablePresenter {
45

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

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

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

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

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

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

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

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

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

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

    
196
	}
197

    
198
	public List<TaxonNode> getAllNodes(){
199
		List<TaxonNode> allNodes = new ArrayList<TaxonNode>();
200

    
201
		List<TaxonNode> taxonNodes = getChosenTaxonNodes();
202
		for (TaxonNode taxonNode : taxonNodes) {
203
			if(taxonNode.getTaxon()!=null){
204
				allNodes.add(taxonNode);
205
			}
206
			allNodes.addAll(taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null));
207
		}
208
		return allNodes;
209
	}
210

    
211

    
212
	public List<TaxonNode> getChosenTaxonNodes() {
213
		VaadinSession session = VaadinSession.getCurrent();
214
		List<UUID> taxonNodeUUIDs = (List<UUID>) session.getAttribute(DistributionEditorUtil.SATTR_TAXON_NODES_UUID);
215
		UUID classificationUuid = (UUID)session.getAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION);
216
		if((taxonNodeUUIDs==null || taxonNodeUUIDs.isEmpty()) && classificationUuid!=null){
217
			Classification classification = classificationService.load(classificationUuid);
218
			if(classification!=null){
219
				taxonNodeUUIDs = Collections.singletonList(classification.getRootNode().getUuid());
220
			}
221
		}
222
		List<TaxonNode> loadedNodes = taxonNodeService.load(taxonNodeUUIDs, null);
223
		if(loadedNodes!=null){
224
			return loadedNodes;
225
		}
226
		return Collections.emptyList();
227
	}
228

    
229
	public CdmSQLContainer getSQLContainer() throws SQLException{
230
		List<Integer> nodeIds = new ArrayList<Integer>();
231
		for (TaxonNode taxonNode : getAllNodes()) {
232
			nodeIds.add(taxonNode.getId());
233
		}
234
		Set<NamedArea> namedAreas = getNamedAreas();
235
		if(namedAreas!=null){
236
			return new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namedAreas));
237
		}
238
		return null;
239
	}
240

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

    
265
	public IClassificationService getClassificationService() {
266
		return classificationService;
267
	}
268

    
269
	public IVocabularyService getVocabularyService() {
270
		return vocabularyService;
271
	}
272

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

    
277
	public ITaxonNodeService getTaxonNodeService() {
278
		return taxonNodeService;
279
	}
280

    
281
	public ITermService getTermService() {
282
		return termService;
283
	}
284
	public ITaxonService getTaxonService() {
285
		return taxonService;
286
	}
287

    
288
	/**Helper Methods*/
289

    
290
	private void sort(List<DescriptionElementBase> list){
291
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
292

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

    
302
				}
303
			}
304
		});
305
	}
306
}
    (1-1/1)