Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:
1 4c82bcaa Alexander Oppermann
package eu.etaxonomy.cdm.vaadin.presenter.dbstatus;
2
3 8ef14383 Alexander Oppermann
import java.sql.SQLException;
4
import java.util.ArrayList;
5 7cef91f1 Alexander Oppermann
import java.util.Arrays;
6 1b8dfb1b Alexander Oppermann
import java.util.Collections;
7 1a3e979c Alexander Oppermann
import java.util.Comparator;
8 7cef91f1 Alexander Oppermann
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
16 1b9d7954 Alexander Oppermann
import eu.etaxonomy.cdm.api.service.IClassificationService;
17 7cef91f1 Alexander Oppermann
import eu.etaxonomy.cdm.api.service.IDescriptionService;
18 1b9d7954 Alexander Oppermann
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
19 1746c51d Alexander Oppermann
import eu.etaxonomy.cdm.api.service.ITaxonService;
20 7cef91f1 Alexander Oppermann
import eu.etaxonomy.cdm.api.service.ITermService;
21
import eu.etaxonomy.cdm.api.service.IVocabularyService;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24 1a3e979c Alexander Oppermann
import eu.etaxonomy.cdm.model.common.Language;
25
import eu.etaxonomy.cdm.model.common.Representation;
26 7cef91f1 Alexander Oppermann
import eu.etaxonomy.cdm.model.common.TermVocabulary;
27
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
28
import eu.etaxonomy.cdm.model.description.Distribution;
29
import eu.etaxonomy.cdm.model.description.Feature;
30
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
31 1a3e979c Alexander Oppermann
import eu.etaxonomy.cdm.model.description.TaxonDescription;
32
import eu.etaxonomy.cdm.model.location.NamedArea;
33 7cef91f1 Alexander Oppermann
import eu.etaxonomy.cdm.model.taxon.Taxon;
34 1b9d7954 Alexander Oppermann
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35 8ef14383 Alexander Oppermann
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
36
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
37 7cef91f1 Alexander Oppermann
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
38 6689ac7b Alexander Oppermann
import eu.etaxonomy.cdm.vaadin.view.dbstatus.DistributionTableView;
39 4c82bcaa Alexander Oppermann
40
41 49a35b7d Patrick Plitzner
public class DistributionTablePresenter {
42 1746c51d Alexander Oppermann
43 785fc5d7 Patrick Plitzner
    private final IClassificationService classificationService;
44 6689ac7b Alexander Oppermann
	private final IVocabularyService vocabularyService;
45
	private final IDescriptionService descriptionService;
46 1b9d7954 Alexander Oppermann
	private final ITaxonNodeService taxonNodeService;
47 6689ac7b Alexander Oppermann
	private final ITermService termService;
48
	private final DistributionTableView view;
49 5cf75203 Cherian Mathew
	private final ITaxonService taxonService;
50 6689ac7b Alexander Oppermann
51 49a35b7d Patrick Plitzner
	public DistributionTablePresenter(DistributionTableView dtv){
52 6689ac7b Alexander Oppermann
	    this.view = dtv;
53
	    view.addListener(this);
54 5cf75203 Cherian Mathew
	    taxonService = CdmSpringContextHelper.getTaxonService();
55
	    classificationService = CdmSpringContextHelper.getClassificationService();
56
	    taxonNodeService = CdmSpringContextHelper.getTaxonNodeService();
57
		vocabularyService = CdmSpringContextHelper.getVocabularyService();
58
		descriptionService = CdmSpringContextHelper.getDescriptionService();
59
		termService = CdmSpringContextHelper.getTermService();
60 7cef91f1 Alexander Oppermann
	}
61 6689ac7b Alexander Oppermann
62 b2063ede Patrick Plitzner
    public int updateDistributionField(String distributionAreaString, Object comboValue, Taxon taxon) {
63 1a3e979c Alexander Oppermann
	    Set<DefinedTermBase> chosenTerms = getChosenTerms();
64 b2063ede Patrick Plitzner
	    NamedArea namedArea = null;
65
	    for(DefinedTermBase term:chosenTerms){
66
	        if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
67
	            namedArea = (NamedArea) term;
68 1a3e979c Alexander Oppermann
	            break;
69
	        }
70
	    }
71 b2063ede Patrick Plitzner
	    List<Distribution> distributions = getDistributions(taxon);
72
	    Distribution distribution = null;
73
	    for(Distribution dist : distributions){
74
	        if(dist.getArea().equals(namedArea)){
75
	            distribution = dist;
76 1a3e979c Alexander Oppermann
	            break;
77
	        }
78
	    }
79 b2063ede Patrick Plitzner
	    if(distribution==null){
80
	    	//create new distribution
81
	    	distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
82
			Set<TaxonDescription> descriptions = taxon.getDescriptions();
83 1323a4df Patrick Plitzner
			if (descriptions != null && !descriptions.isEmpty()) {
84 b2063ede Patrick Plitzner
			    for (TaxonDescription desc : descriptions) {
85
			        // add to first taxon description
86
			        desc.addElement(distribution);
87
				    getTaxonService().saveOrUpdate(taxon);
88
			        return 0;
89
			    }
90
			} else {// there are no TaxonDescription yet.
91
			    TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
92
			    taxonDescription.addElement(distribution);
93
			    taxon.addDescription(taxonDescription);
94
			    getTaxonService().saveOrUpdate(taxon);
95
			    return 0;
96
			}
97
	    }
98
	    else if(comboValue == null){//delete descriptionElementBase
99
	    	distribution.getInDescription().removeElement(distribution);
100 1a3e979c Alexander Oppermann
            getTaxonService().saveOrUpdate(taxon);
101
            return 1;
102 b2063ede Patrick Plitzner
	    }
103
	    else{
104
           distribution.setStatus((PresenceAbsenceTerm)comboValue);
105
           getTaxonService().saveOrUpdate(taxon);
106 1a3e979c Alexander Oppermann
           return 0;
107
        }
108 b2063ede Patrick Plitzner
	    return -1;
109 7cef91f1 Alexander Oppermann
	}
110 6689ac7b Alexander Oppermann
111 7cef91f1 Alexander Oppermann
	public Set<DefinedTermBase> getChosenTerms() {
112
		VaadinSession session = VaadinSession.getCurrent();
113
		UUID termUUID = (UUID) session.getAttribute("selectedTerm");
114
		TermVocabulary<DefinedTermBase> term = vocabularyService.load(termUUID);
115
		term = CdmBase.deproxy(term, TermVocabulary.class);
116
		return term.getTerms();
117
	}
118
119 1a3e979c Alexander Oppermann
	public List<String> getAbbreviatedTermList() {
120 785fc5d7 Patrick Plitzner
		Set<NamedArea> terms = getTermSet();
121 8ef14383 Alexander Oppermann
		List<String> list = new ArrayList<String>();
122
		for(DefinedTermBase dtb: terms){
123 1a3e979c Alexander Oppermann
		    for(Representation r : dtb.getRepresentations()){
124
		        list.add(r.getAbbreviatedLabel());
125
		    }
126 8ef14383 Alexander Oppermann
		}
127
		return list;
128
	}
129 5cf75203 Cherian Mathew
130 785fc5d7 Patrick Plitzner
	public Set<NamedArea> getNamedAreas(){
131
	    Set<NamedArea> namedAreas = (Set<NamedArea>) VaadinSession.getCurrent().getAttribute("selectedAreas");
132
	    if(namedAreas.isEmpty()){
133
	        return getTermSet();
134
	    }
135
        return namedAreas;
136
	}
137
138
    public List<String> getNamedAreasLabels(boolean abbreviated){
139
        Set<NamedArea> selectedAreas = getNamedAreas();
140
    	List<String> namedAreaTitles = new ArrayList<>();
141
    	for (NamedArea namedArea : selectedAreas) {
142
    	    if(abbreviated){
143
    	        namedAreaTitles.add(namedArea.getRepresentation(Language.DEFAULT()).getAbbreviatedLabel());
144
    	    }
145
    	    else{
146
    	        namedAreaTitles.add(namedArea.getRepresentation(Language.DEFAULT()).getLabel());
147
    	    }
148
        }
149
    	return namedAreaTitles;
150 dd433955 Patrick Plitzner
    }
151 1a3e979c Alexander Oppermann
152 785fc5d7 Patrick Plitzner
	private Set<NamedArea> getTermSet(){
153 1a3e979c Alexander Oppermann
	    VaadinSession session = VaadinSession.getCurrent();
154
	    UUID termUUID = (UUID) session.getAttribute("selectedTerm");
155 785fc5d7 Patrick Plitzner
	    TermVocabulary<NamedArea> vocabulary = vocabularyService.load(termUUID);
156
	    vocabulary = CdmBase.deproxy(vocabulary, TermVocabulary.class);
157
	    return vocabulary.getTermsOrderedByLabels(Language.DEFAULT());
158 1a3e979c Alexander Oppermann
	}
159
160 7cef91f1 Alexander Oppermann
	public HashMap<DescriptionElementBase, Distribution> getDistribution(DefinedTermBase dt, Taxon taxon) {
161
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
162
		List<DescriptionElementBase> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
163 1b9d7954 Alexander Oppermann
		HashMap<DescriptionElementBase, Distribution> map = null;
164 7cef91f1 Alexander Oppermann
		for(DescriptionElementBase deb : listTaxonDescription){
165
			if(deb instanceof Distribution){
166
				Distribution db = (Distribution)deb;
167
				String titleCache = dt.getTitleCache();
168
				if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
169 1b9d7954 Alexander Oppermann
					map = new HashMap<DescriptionElementBase, Distribution>();
170 7cef91f1 Alexander Oppermann
					map.put(deb, db);
171
				}
172
			}
173
		}
174 1b9d7954 Alexander Oppermann
		return map;
175 7cef91f1 Alexander Oppermann
	}
176 49a35b7d Patrick Plitzner
177 1746c51d Alexander Oppermann
	public List<DescriptionElementBase> listDescriptionElementsForTaxon(Taxon taxon, Set<Feature> setFeature){
178 1a3e979c Alexander Oppermann
		List<DescriptionElementBase> listDescriptionElementsForTaxon = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
179
		sort(listDescriptionElementsForTaxon);
180
		return listDescriptionElementsForTaxon;
181 1746c51d Alexander Oppermann
	}
182 5cf75203 Cherian Mathew
183 b2063ede Patrick Plitzner
	public List<Distribution> getDistributions(Taxon taxon) {
184 1b9d7954 Alexander Oppermann
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
185
		List<Distribution> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
186
		return listTaxonDescription;
187 5cf75203 Cherian Mathew
188 1b9d7954 Alexander Oppermann
	}
189 5cf75203 Cherian Mathew
190 9fde8b52 Patrick Plitzner
	public List<TaxonNode> getAllNodes(){
191 1e201891 Patrick Plitzner
		TaxonNode taxonNode = getChosenTaxonNode();
192 e0787689 Patrick Plitzner
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
193
		if(taxonNode.getTaxon()!=null){
194
			nodes.add(taxonNode);
195
		}
196 427bdbbc Patrick Plitzner
		nodes.addAll(taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null));
197 e0787689 Patrick Plitzner
		return nodes;
198 1b9d7954 Alexander Oppermann
	}
199
200
201 1e201891 Patrick Plitzner
	public TaxonNode getChosenTaxonNode() {
202 1b9d7954 Alexander Oppermann
		VaadinSession session = VaadinSession.getCurrent();
203 1e201891 Patrick Plitzner
		UUID taxonNodeUUID = (UUID) session.getAttribute("taxonNodeUUID");
204
		TaxonNode classificationNode = taxonNodeService.load(taxonNodeUUID);
205 e3f9b0ed Patrick Plitzner
		return classificationNode;
206 1b9d7954 Alexander Oppermann
	}
207 5cf75203 Cherian Mathew
208 1e201891 Patrick Plitzner
	public int getSizeOfTaxonNode(){
209
		TaxonNode taxonNode = getChosenTaxonNode();
210 e3f9b0ed Patrick Plitzner
		return taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, null, true, null).size();
211 1b9d7954 Alexander Oppermann
	}
212 5cf75203 Cherian Mathew
213
214 8ef14383 Alexander Oppermann
	public CdmSQLContainer getSQLContainer() throws SQLException{
215 e0787689 Patrick Plitzner
		List<Integer> nodeIds = new ArrayList<Integer>();
216 9fde8b52 Patrick Plitzner
		for (TaxonNode taxonNode : getAllNodes()) {
217 e0787689 Patrick Plitzner
			nodeIds.add(taxonNode.getId());
218
		}
219 785fc5d7 Patrick Plitzner
		Set<NamedArea> namesAreas = getNamedAreas();
220
		CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namesAreas, true));
221 8ef14383 Alexander Oppermann
		return container;
222
	}
223 5cf75203 Cherian Mathew
224 7cef91f1 Alexander Oppermann
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
225
            "$",
226
            "elements.*",
227
            "elements.sources.citation.authorship.$",
228
            "elements.sources.nameUsedInSource.originalNameString",
229
            "elements.area.level",
230
            "elements.modifyingText",
231
            "elements.states.*",
232
            "elements.media",
233
            "elements.multilanguageText",
234
            "multilanguageText",
235 1a3e979c Alexander Oppermann
            "stateData.$",
236
            "annotations",
237
            "markers",
238
            "sources.citation.authorship",
239
            "sources.nameUsedInSource",
240
            "multilanguageText",
241
            "media",
242
            "name.$",
243
            "name.rank.representations",
244
            "name.status.type.representations",
245
            "taxon2.name"
246 7cef91f1 Alexander Oppermann
    });
247
248 1746c51d Alexander Oppermann
	public IClassificationService getClassificationService() {
249
		return classificationService;
250
	}
251
252
	public IVocabularyService getVocabularyService() {
253
		return vocabularyService;
254
	}
255
256
	public IDescriptionService getDescriptionService() {
257
		return descriptionService;
258
	}
259
260
	public ITaxonNodeService getTaxonNodeService() {
261
		return taxonNodeService;
262
	}
263
264
	public ITermService getTermService() {
265
		return termService;
266
	}
267
	public ITaxonService getTaxonService() {
268
		return taxonService;
269
	}
270
271 1a3e979c Alexander Oppermann
	/**Helper Methods*/
272
273
	private void sort(List<DescriptionElementBase> list){
274
		Collections.sort(list, new Comparator<DescriptionElementBase>() {
275
276
			@Override
277
			public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
278
				String feature1 = o1.getFeature().getTitleCache();
279
				String feature2 = o2.getFeature().getTitleCache();
280
				if(feature1 !=null && feature2 !=null){
281
					return feature1.compareTo(feature2);
282
				}else{
283
					return 0;
284
285
				}
286
			}
287
		});
288
	}
289 4c82bcaa Alexander Oppermann
}