Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.vaadin.view.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

    
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
		List<TaxonNode> allNodes = new ArrayList<TaxonNode>();
199

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

    
210

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

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

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

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

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

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

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

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

    
287
	/**Helper Methods*/
288

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

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

    
301
				}
302
			}
303
		});
304
	}
305
}
(3-3/6)