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

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

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

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

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

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

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

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

    
211
	}
212

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

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

    
226

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

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

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

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

    
284
	/**Helper Methods*/
285
	private void sort(List<DescriptionElementBase> list){
286
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
287

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

    
297
				}
298
			}
299
		});
300
	}
301

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

    
322
}
(3-3/7)