Project

General

Profile

Download (12.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.view.distributionStatus;
10

    
11
import java.sql.SQLException;
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.beans.factory.annotation.Qualifier;
24
import org.springframework.transaction.TransactionStatus;
25

    
26
import com.vaadin.server.VaadinSession;
27
import com.vaadin.spring.annotation.SpringComponent;
28
import com.vaadin.spring.annotation.ViewScope;
29
import com.vaadin.ui.Notification;
30

    
31
import eu.etaxonomy.cdm.api.application.CdmRepository;
32
import eu.etaxonomy.cdm.model.common.CdmBase;
33
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
34
import eu.etaxonomy.cdm.model.common.Language;
35
import eu.etaxonomy.cdm.model.common.Representation;
36
import eu.etaxonomy.cdm.model.common.TermVocabulary;
37
import eu.etaxonomy.cdm.model.description.DescriptionBase;
38
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
39
import eu.etaxonomy.cdm.model.description.Distribution;
40
import eu.etaxonomy.cdm.model.description.Feature;
41
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
42
import eu.etaxonomy.cdm.model.description.TaxonDescription;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44
import eu.etaxonomy.cdm.model.taxon.Classification;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
47
import eu.etaxonomy.cdm.service.CdmUserHelper;
48
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
49
import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
50
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
51
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
52
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
53
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
54

    
55
/**
56
 * @author freimeier
57
 * @since 18.10.2017
58
 *
59
 */
60
@SpringComponent
61
@ViewScope
62
public class DistributionTablePresenter extends AbstractPresenter<IDistributionTableView> {
63

    
64
	private static final long serialVersionUID = 3313043335587777217L;
65

    
66
    @Autowired
67
    private CdmUserHelper userHelper;
68

    
69
    @Autowired
70
    @Qualifier("cdmRepository")
71
    private CdmRepository repo;
72

    
73

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

    
147
	public Set<DefinedTermBase> getChosenTerms() {
148
		VaadinSession session = VaadinSession.getCurrent();
149
		UUID vocUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID);
150
//		getConversationHolder().getSession();
151
		TermVocabulary<DefinedTermBase> voc = CdmSpringContextHelper.getVocabularyService().load(vocUUID, Arrays.asList("terms.representations"));
152
//		voc = CdmBase.deproxy(voc);
153
		return voc.getTerms();
154
	}
155

    
156
	public List<String> getAbbreviatedTermList() {
157
		Set<NamedArea> terms = getTermSet();
158
		List<String> list = new ArrayList<>();
159
		for(DefinedTermBase<?> dtb: terms){
160
		    for(Representation r : dtb.getRepresentations()){
161
		        list.add(r.getAbbreviatedLabel());
162
		    }
163
		}
164
		return list;
165
	}
166

    
167
	public Set<NamedArea> getNamedAreas(){
168
	    Set<NamedArea> namedAreas = (Set<NamedArea>) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
169
	    if(namedAreas!=null && namedAreas.isEmpty()){
170
	        return getTermSet();
171
	    }
172
        return namedAreas;
173
	}
174

    
175
	private Set<NamedArea> getTermSet(){
176
	    VaadinSession session = VaadinSession.getCurrent();
177
	    UUID vocUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID);
178
	    TermVocabulary<NamedArea> vocabulary = CdmSpringContextHelper.getVocabularyService().load(vocUUID, Arrays.asList("terms.representations"));
179
	    vocabulary = CdmBase.deproxy(vocabulary, TermVocabulary.class);
180
	    return vocabulary.getTermsOrderedByLabels(Language.DEFAULT());
181
	}
182

    
183
	public HashMap<DescriptionElementBase, Distribution> getDistribution(DefinedTermBase dt, Taxon taxon) {
184
		Set<Feature> setFeature = new HashSet<>(Arrays.asList(Feature.DISTRIBUTION()));
185
		List<DescriptionElementBase> listTaxonDescription = CdmSpringContextHelper.getDescriptionService().listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
186
		HashMap<DescriptionElementBase, Distribution> map = null;
187
		for(DescriptionElementBase deb : listTaxonDescription){
188
			if(deb instanceof Distribution){
189
				Distribution db = (Distribution)deb;
190
				String titleCache = dt.getTitleCache();
191
				if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
192
					map = new HashMap<DescriptionElementBase, Distribution>();
193
					map.put(deb, db);
194
				}
195
			}
196
		}
197
		return map;
198
	}
199

    
200
	public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
201
		List<DescriptionElementBase> listDescriptionElementsForTaxon = CdmSpringContextHelper.getDescriptionService().listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
202
		sort(listDescriptionElementsForTaxon);
203
		return listDescriptionElementsForTaxon;
204
	}
205

    
206
	public List<Distribution> getDistributions(Taxon taxon) {
207
		Set<Feature> setFeature = new HashSet<>(Arrays.asList(Feature.DISTRIBUTION()));
208
		List<Distribution> listTaxonDescription = CdmSpringContextHelper.getDescriptionService()
209
		        .listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
210
		return listTaxonDescription;
211

    
212
	}
213

    
214
	public List<TaxonNode> getAllNodes(){
215
		List<TaxonNode> allNodes = new ArrayList<>();
216

    
217
		List<TaxonNode> taxonNodes = getChosenTaxonNodes();
218
		for (TaxonNode taxonNode : taxonNodes) {
219
			if(taxonNode.getTaxon()!=null){
220
				allNodes.add(taxonNode);
221
			}
222
			allNodes.addAll(CdmSpringContextHelper.getTaxonNodeService().loadChildNodesOfTaxonNode(taxonNode, null, true, null));
223
		}
224
		return allNodes;
225
	}
226

    
227

    
228
	public List<TaxonNode> getChosenTaxonNodes() {
229
		VaadinSession session = VaadinSession.getCurrent();
230
		List<UUID> taxonNodeUUIDs = (List<UUID>) session.getAttribute(DistributionEditorUtil.SATTR_TAXON_NODES_UUID);
231
		UUID classificationUuid = (UUID)session.getAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION);
232
		if((taxonNodeUUIDs==null || taxonNodeUUIDs.isEmpty()) && classificationUuid!=null){
233
			Classification classification = CdmSpringContextHelper.getClassificationService().load(classificationUuid);
234
			if(classification!=null){
235
				taxonNodeUUIDs = Collections.singletonList(classification.getRootNode().getUuid());
236
			}
237
		}
238
		List<TaxonNode> loadedNodes = CdmSpringContextHelper.getTaxonNodeService().load(taxonNodeUUIDs, null);
239
		if(loadedNodes!=null){
240
			return loadedNodes;
241
		}
242
		return Collections.emptyList();
243
	}
244

    
245
	public CdmSQLContainer getSQLContainer() throws SQLException{
246
		List<Integer> nodeIds = new ArrayList<>();
247
		for (TaxonNode taxonNode : getAllNodes()) {
248
			nodeIds.add(taxonNode.getId());
249
		}
250
		Set<NamedArea> namedAreas = getNamedAreas();
251
		if(namedAreas!=null){
252
			return new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namedAreas));
253
		}
254
		return null;
255
	}
256

    
257
	public PresenceAbsenceTermContainer getPresenceAbsenceTermContainer() {
258
	    return PresenceAbsenceTermContainer.getInstance();
259
	}
260

    
261
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
262
            "$",
263
            "elements.*",
264
            "elements.sources.citation.authorship.$",
265
            "elements.sources.nameUsedInSource.originalNameString",
266
            "elements.area.level",
267
            "elements.modifyingText",
268
            "elements.states.*",
269
            "elements.media",
270
            "elements.multilanguageText",
271
            "multilanguageText",
272
            "stateData.$",
273
            "annotations",
274
            "markers",
275
            "sources.citation.authorship",
276
            "sources.nameUsedInSource",
277
            "multilanguageText",
278
            "media",
279
            "name.$",
280
            "name.rank.representations",
281
            "name.status.type.representations",
282
            "taxon2.name",
283
    });
284

    
285
	/**Helper Methods*/
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

    
303
	/**
304
	 *
305
	 * {@inheritDoc}
306
	 */
307
	@Override
308
	protected void onPresenterReady() {
309
	    /*
310
         * The area and taxon settings window should only be displayed after login
311
         * and only when no classification and areas are chosen yet.
312
         */
313
	    VaadinSession vaadinSession = VaadinSession.getCurrent();
314
	    if(userHelper.userIsAutheticated()
315
	            && !userHelper.userIsAnnonymous()
316
	            && (vaadinSession.getAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION) == null
317
	            || vaadinSession.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID) == null
318
	            || vaadinSession.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS) == null)) {
319
            getView().openAreaAndTaxonSettings();
320
        }
321
    }
322

    
323
}
(3-3/7)