Project

General

Profile

Download (9.99 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.model.common.CdmBase;
18
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
19
import eu.etaxonomy.cdm.model.common.Language;
20
import eu.etaxonomy.cdm.model.common.Representation;
21
import eu.etaxonomy.cdm.model.common.TermVocabulary;
22
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23
import eu.etaxonomy.cdm.model.description.Distribution;
24
import eu.etaxonomy.cdm.model.description.Feature;
25
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.location.NamedArea;
28
import eu.etaxonomy.cdm.model.taxon.Classification;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
32
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
33
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
34
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
35

    
36

    
37
public class DistributionTablePresenter {
38

    
39
	private final DistributionTableView view;
40

    
41
	public DistributionTablePresenter(DistributionTableView dtv){
42
	    this.view = dtv;
43
	    view.addListener(this);
44
	}
45

    
46
    public int updateDistributionField(String distributionAreaString, Object comboValue, Taxon taxon) {
47
	    Set<DefinedTermBase> chosenTerms = getChosenTerms();
48
	    NamedArea namedArea = null;
49
	    for(DefinedTermBase term:chosenTerms){
50
	    	Representation representation = term.getRepresentation(Language.DEFAULT());
51
	    	if(representation!=null){
52
	    		if(DistributionEditorUtil.isAbbreviatedLabels()){
53
	    			String label = representation.getLabel();
54
	    			String abbreviatedLabel = representation.getAbbreviatedLabel();
55
					if(abbreviatedLabel!=null && abbreviatedLabel.equalsIgnoreCase(distributionAreaString)){
56
	    				namedArea = (NamedArea) term;
57
	    				break;
58
	    			}
59
					else if(label!=null && label.equalsIgnoreCase(distributionAreaString)){
60
						namedArea = (NamedArea) term;
61
						break;
62
					}
63
	    		}
64
	    	}
65
	        if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
66
	        	namedArea = (NamedArea) term;
67
	        	break;
68
	        }
69
	    }
70
	    if(namedArea==null){
71
	    	Notification.show("Error during update of distribution term!");
72
	    	return -1;
73
	    }
74
	    List<Distribution> distributions = getDistributions(taxon);
75
	    Distribution distribution = null;
76
	    for(Distribution dist : distributions){
77
	        if(dist.getArea()!=null && dist.getArea().equals(namedArea)){
78
	            distribution = dist;
79
	            break;
80
	        }
81
	    }
82
	    if(distribution==null){
83
	    	//create new distribution
84
	    	distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
85
			Set<TaxonDescription> descriptions = taxon.getDescriptions();
86
			if (descriptions != null && !descriptions.isEmpty()) {
87
			    for (TaxonDescription desc : descriptions) {
88
			        // add to first taxon description
89
			        desc.addElement(distribution);
90
			        CdmSpringContextHelper.getTaxonService().saveOrUpdate(taxon);
91
			        return 0;
92
			    }
93
			} else {// there are no TaxonDescription yet.
94
			    TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
95
			    taxonDescription.addElement(distribution);
96
			    taxon.addDescription(taxonDescription);
97
			    CdmSpringContextHelper.getTaxonService().saveOrUpdate(taxon);
98
			    return 0;
99
			}
100
	    }
101
	    else if(comboValue == null){//delete descriptionElementBase
102
	    	distribution.getInDescription().removeElement(distribution);
103
	    	CdmSpringContextHelper.getTaxonService().saveOrUpdate(taxon);
104
            return 1;
105
	    }
106
	    else{
107
           distribution.setStatus((PresenceAbsenceTerm)comboValue);
108
           CdmSpringContextHelper.getTaxonService().saveOrUpdate(taxon);
109
           return 0;
110
        }
111
	    return -1;
112
	}
113

    
114
	public Set<DefinedTermBase> getChosenTerms() {
115
		VaadinSession session = VaadinSession.getCurrent();
116
		UUID termUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_VOCABULARY_UUID);
117
		TermVocabulary<DefinedTermBase> term = CdmSpringContextHelper.getVocabularyService().load(termUUID);
118
		term = CdmBase.deproxy(term, TermVocabulary.class);
119
		return term.getTerms();
120
	}
121

    
122
	public List<String> getAbbreviatedTermList() {
123
		Set<NamedArea> terms = getTermSet();
124
		List<String> list = new ArrayList<String>();
125
		for(DefinedTermBase dtb: terms){
126
		    for(Representation r : dtb.getRepresentations()){
127
		        list.add(r.getAbbreviatedLabel());
128
		    }
129
		}
130
		return list;
131
	}
132

    
133
	public Set<NamedArea> getNamedAreas(){
134
	    Set<NamedArea> namedAreas = (Set<NamedArea>) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
135
	    if(namedAreas!=null && namedAreas.isEmpty()){
136
	        return getTermSet();
137
	    }
138
        return namedAreas;
139
	}
140

    
141
	private Set<NamedArea> getTermSet(){
142
	    VaadinSession session = VaadinSession.getCurrent();
143
	    UUID termUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_VOCABULARY_UUID);
144
	    TermVocabulary<NamedArea> vocabulary = CdmSpringContextHelper.getVocabularyService().load(termUUID);
145
	    vocabulary = CdmBase.deproxy(vocabulary, TermVocabulary.class);
146
	    return vocabulary.getTermsOrderedByLabels(Language.DEFAULT());
147
	}
148

    
149
	public HashMap<DescriptionElementBase, Distribution> getDistribution(DefinedTermBase dt, Taxon taxon) {
150
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
151
		List<DescriptionElementBase> listTaxonDescription = CdmSpringContextHelper.getDescriptionService().listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
152
		HashMap<DescriptionElementBase, Distribution> map = null;
153
		for(DescriptionElementBase deb : listTaxonDescription){
154
			if(deb instanceof Distribution){
155
				Distribution db = (Distribution)deb;
156
				String titleCache = dt.getTitleCache();
157
				if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
158
					map = new HashMap<DescriptionElementBase, Distribution>();
159
					map.put(deb, db);
160
				}
161
			}
162
		}
163
		return map;
164
	}
165

    
166
	public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
167
		List<DescriptionElementBase> listDescriptionElementsForTaxon = CdmSpringContextHelper.getDescriptionService().listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
168
		sort(listDescriptionElementsForTaxon);
169
		return listDescriptionElementsForTaxon;
170
	}
171

    
172
	public List<Distribution> getDistributions(Taxon taxon) {
173
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
174
		List<Distribution> listTaxonDescription = CdmSpringContextHelper.getDescriptionService().listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
175
		return listTaxonDescription;
176

    
177
	}
178

    
179
	public List<TaxonNode> getAllNodes(){
180
		List<TaxonNode> allNodes = new ArrayList<TaxonNode>();
181

    
182
		List<TaxonNode> taxonNodes = getChosenTaxonNodes();
183
		for (TaxonNode taxonNode : taxonNodes) {
184
			if(taxonNode.getTaxon()!=null){
185
				allNodes.add(taxonNode);
186
			}
187
			allNodes.addAll(CdmSpringContextHelper.getTaxonNodeService().loadChildNodesOfTaxonNode(taxonNode, null, true, null));
188
		}
189
		return allNodes;
190
	}
191

    
192

    
193
	public List<TaxonNode> getChosenTaxonNodes() {
194
		VaadinSession session = VaadinSession.getCurrent();
195
		List<UUID> taxonNodeUUIDs = (List<UUID>) session.getAttribute(DistributionEditorUtil.SATTR_TAXON_NODES_UUID);
196
		UUID classificationUuid = (UUID)session.getAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION);
197
		if((taxonNodeUUIDs==null || taxonNodeUUIDs.isEmpty()) && classificationUuid!=null){
198
			Classification classification = CdmSpringContextHelper.getClassificationService().load(classificationUuid);
199
			if(classification!=null){
200
				taxonNodeUUIDs = Collections.singletonList(classification.getRootNode().getUuid());
201
			}
202
		}
203
		List<TaxonNode> loadedNodes = CdmSpringContextHelper.getTaxonNodeService().load(taxonNodeUUIDs, null);
204
		if(loadedNodes!=null){
205
			return loadedNodes;
206
		}
207
		return Collections.emptyList();
208
	}
209

    
210
	public CdmSQLContainer getSQLContainer() throws SQLException{
211
		List<Integer> nodeIds = new ArrayList<Integer>();
212
		for (TaxonNode taxonNode : getAllNodes()) {
213
			nodeIds.add(taxonNode.getId());
214
		}
215
		Set<NamedArea> namedAreas = getNamedAreas();
216
		if(namedAreas!=null){
217
			return new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namedAreas));
218
		}
219
		return null;
220
	}
221

    
222
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
223
            "$",
224
            "elements.*",
225
            "elements.sources.citation.authorship.$",
226
            "elements.sources.nameUsedInSource.originalNameString",
227
            "elements.area.level",
228
            "elements.modifyingText",
229
            "elements.states.*",
230
            "elements.media",
231
            "elements.multilanguageText",
232
            "multilanguageText",
233
            "stateData.$",
234
            "annotations",
235
            "markers",
236
            "sources.citation.authorship",
237
            "sources.nameUsedInSource",
238
            "multilanguageText",
239
            "media",
240
            "name.$",
241
            "name.rank.representations",
242
            "name.status.type.representations",
243
            "taxon2.name"
244
    });
245

    
246
	/**Helper Methods*/
247

    
248
	private void sort(List<DescriptionElementBase> list){
249
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
250

    
251
			@Override
252
			public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
253
				String feature1 = o1.getFeature().getTitleCache();
254
				String feature2 = o2.getFeature().getTitleCache();
255
				if(feature1 !=null && feature2 !=null){
256
					return feature1.compareTo(feature2);
257
				}else{
258
					return 0;
259

    
260
				}
261
			}
262
		});
263
	}
264
}
(3-3/6)