1 package eu
.etaxonomy
.cdm
.remote
.vaadin
.components
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.HashMap
;
6 import java
.util
.HashSet
;
10 import java
.util
.UUID
;
12 import javax
.annotation
.PostConstruct
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
16 import org
.springframework
.context
.annotation
.Scope
;
17 import org
.springframework
.stereotype
.Component
;
19 import com
.vaadin
.annotations
.PreserveOnRefresh
;
20 import com
.vaadin
.data
.util
.BeanItemContainer
;
21 import com
.vaadin
.server
.VaadinSession
;
22 import com
.vaadin
.ui
.ComboBox
;
23 import com
.vaadin
.ui
.Label
;
24 import com
.vaadin
.ui
.Notification
;
25 import com
.vaadin
.ui
.Table
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
33 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
34 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
36 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
37 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
38 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
39 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
41 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
44 import eu
.etaxonomy
.cdm
.remote
.dto
.vaadin
.CdmTaxonTableCollection
;
45 import eu
.etaxonomy
.cdm
.remote
.vaadin
.data
.LazyLoadedContainer
;
49 * This class is a Vaadin Component. It starts a long running session at the moment.
50 * This might change in the future, but for now it beautifully works for this prototype.<p>
51 * This class takes advantage of the dto and fills a container with data from the DB. Lazyloading or
52 * Paging needs to be used!!!!
54 * Further clarification is needed about the exact process when marking this component as dirty.
55 * What will happen to the bound session? Why are changed Object saved without calling services explicitly.
65 public class DemoTaxonTable
extends Table
{
68 * automatic generated ID
71 IVocabularyService vocabularyService
;
73 ITaxonService taxonService
;
75 IDescriptionService descriptionService
;
77 ITermService termService
;
79 IClassificationService clService
;
81 ITaxonNodeService taxonNodeService
;
83 IClassificationService classificationService
;
85 Logger logger
= Logger
.getLogger(DemoTaxonTable
.class);
88 private static final long serialVersionUID
= 1L;
91 @SuppressWarnings("rawtypes")
94 // final BeanItemContainer<CdmTaxonTableCollection> redListContainer = new BeanItemContainer<CdmTaxonTableCollection>(CdmTaxonTableCollection.class);
95 //TODO: Make use of paging
96 // VaadinSession session = VaadinSession.getCurrent();
97 // UUID uuid = UUID.fromString(session.getAttribute("classificationUUID").toString());
98 // Classification classification = clService.load(uuid);
99 // List<TaxonNode> listAllNodes = taxonNodeService.listAllNodesForClassification(classification, null, null);
101 // Collection<Taxon> listTaxon = taxonService.list(Taxon.class, null, null, null, NODE_INIT_STRATEGY);
102 // for(TaxonNode taxonNode:listAllNodes){
104 // Taxon taxon = taxonNode.getTaxon();
105 // List<PresenceAbsenceTermBase> termList = termService.list(PresenceAbsenceTermBase.class, null, null, null, DESCRIPTION_INIT_STRATEGY);
106 // List<DescriptionElementBase> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, null, null, null, null, DESCRIPTION_INIT_STRATEGY);
107 // CdmTaxonTableCollection tableCollection = new CdmTaxonTableCollection(taxon, listTaxonDescription, termList);
108 // redListContainer.addBean(tableCollection);
111 VaadinSession session
= VaadinSession
.getCurrent();
112 UUID termUUID
= (UUID
) session
.getAttribute("selectedTerm");
113 TermVocabulary
<DefinedTermBase
> term
= vocabularyService
.load(termUUID
);
114 term
= CdmBase
.deproxy(term
, TermVocabulary
.class);
115 Set
<DefinedTermBase
> terms
= term
.getTerms();
117 final LazyLoadedContainer container
= new LazyLoadedContainer(CdmTaxonTableCollection
.class, classificationService
, taxonNodeService
);
119 // container.addContainerProperty("fullTitleCache", String.class, null);
120 // container.addContainerProperty("rank", String.class, null);
122 //String[] columns = ; //,"distributionStatus"
123 ArrayList
<String
> columnList
= new ArrayList
<String
>(Arrays
.asList(new String
[]{"fullTitleCache","rank"}));
124 ArrayList
<String
> headerList
= new ArrayList
<String
>(Arrays
.asList(new String
[]{"Taxon","Rang"}));
125 for(final DefinedTermBase dt
: terms
){
126 columnList
.add(dt
.getTitleCache());
127 headerList
.add(dt
.getTitleCache());
128 // container.addContainerProperty(dt.getTitleCache(), String.class, null);
130 addContainerProperty(dt
.getTitleCache(), String
.class, null);
132 addGeneratedColumn(dt
.getTitleCache(), new ColumnGenerator() {
133 public Object
generateCell(Table source
, Object itemId
, Object columnId
) {
134 Label tf
= new Label();
136 if(itemId
instanceof TaxonNode
){
137 TaxonNode tn
= CdmBase
.deproxy((TaxonNode
) itemId
, TaxonNode
.class);
138 Taxon taxon
= CdmBase
.deproxy(tn
.getTaxon(), Taxon
.class);
139 taxon
=(Taxon
) taxonService
.load(taxon
.getUuid());
140 HashMap
<DescriptionElementBase
, Distribution
> map
= getDistribution(taxon
);
142 List
<PresenceAbsenceTermBase
> listTerm
= termService
.list(PresenceAbsenceTermBase
.class, null, null, null, DESCRIPTION_INIT_STRATEGY
);
143 BeanItemContainer
<PresenceAbsenceTermBase
> termContainer
= new BeanItemContainer
<PresenceAbsenceTermBase
>(PresenceAbsenceTermBase
.class);
144 termContainer
.addAll(listTerm
);
145 box
= new ComboBox("Occurrence Status: ", termContainer
);
146 box
.setImmediate(true);
150 DescriptionElementBase deb
= null;
151 Distribution db
= null;
152 for(Map
.Entry
<DescriptionElementBase
, Distribution
> entry
: map
.entrySet()){
153 deb
= entry
.getKey();
154 db
= entry
.getValue();
157 box
= updateDistributionField(deb
, db
, termContainer
, box
, taxon
);
159 if(db
.getStatus() != null){
160 tf
.setValue(db
.getStatus().toString());
162 //FIXME: case for delete?
163 Notification
.show("Possible Error for " +taxon
.getTitleCache() + " for Distribution: " + db
.getArea().getTitleCache(), Notification
.Type
.TRAY_NOTIFICATION
);
168 /** create distribution if it does not exist and set the status **/
170 box
= createDistributionField(taxon
, termContainer
, box
);
185 private ComboBox
createDistributionField(
187 BeanItemContainer
<PresenceAbsenceTermBase
> termContainer
,ComboBox box
) {
188 final ComboBox box2
= box
;
189 box
.addValueChangeListener(new ValueChangeListener() {
192 public void valueChange(com
.vaadin
.data
.Property
.ValueChangeEvent event
) {
193 NamedArea area
= (NamedArea
) dt
;
194 Distribution db
= Distribution
.NewInstance(area
, (PresenceAbsenceTermBase
<?
>) box2
.getValue());
195 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
196 if (descriptions
!= null) {
197 for (TaxonDescription desc
: descriptions
) {
198 // add to first taxon description
200 descriptionService
.saveOrUpdate(desc
);
203 } else {// there are no TaxonDescription yet.
204 TaxonDescription td
= TaxonDescription
.NewInstance(taxon
);
206 taxon
.addDescription(td
);
207 taxonService
.saveOrUpdate(taxon
);
217 private HashMap
<DescriptionElementBase
, Distribution
> getDistribution(Taxon taxon
){
218 Set
<Feature
> setFeature
= new HashSet
<Feature
>(Arrays
.asList(Feature
.DISTRIBUTION()));
219 List
<DescriptionElementBase
> listTaxonDescription
= descriptionService
.listDescriptionElementsForTaxon(taxon
, setFeature
, null, null, null, DESCRIPTION_INIT_STRATEGY
);
220 for(DescriptionElementBase deb
: listTaxonDescription
){
221 if(deb
instanceof Distribution
){
222 Distribution db
= (Distribution
)deb
;
223 String titleCache
= dt
.getTitleCache();
224 if(db
.getArea().getTitleCache().equalsIgnoreCase(titleCache
)){
225 HashMap
<DescriptionElementBase
, Distribution
> map
= new HashMap
<DescriptionElementBase
, Distribution
>();
234 private ComboBox
updateDistributionField(DescriptionElementBase deb
, Distribution db
, BeanItemContainer
<PresenceAbsenceTermBase
> termContainer
, ComboBox box
, Taxon taxon
) {
235 final Distribution db2
= db
;
236 final DescriptionElementBase deb2
= deb
;
237 box
.setValue(db
.getStatus());
238 final ComboBox box2
= box
;
239 final Taxon taxon2
= taxon
;
240 box
.addValueChangeListener(new ValueChangeListener() {
242 private static final long serialVersionUID
= 1L;
245 public void valueChange(com
.vaadin
.data
.Property
.ValueChangeEvent event
) {
246 if(box2
.getValue() == null){//delete descriptionElementBase
247 descriptionService
.deleteDescriptionElement(deb2
);
248 taxonService
.saveOrUpdate(taxon2
);
249 Notification
.show("Delete Status", Notification
.Type
.TRAY_NOTIFICATION
);
251 db2
.setStatus((PresenceAbsenceTermBase
)box2
.getValue());
252 descriptionService
.saveDescriptionElement(deb2
);
253 Notification
.show("DescriptionService wrote", Notification
.Type
.TRAY_NOTIFICATION
);
260 }catch(IllegalArgumentException e
){
265 setContainerDataSource(container
);
266 setColumnReorderingAllowed(true);
267 setSortEnabled(false);
268 // setVisibleColumns(columns);
269 Object
[] visibleColumns
= columnList
.toArray();
270 setVisibleColumns(visibleColumns
);
271 setColumnHeaders(headerList
.toArray(new String
[headerList
.size()]));//new String[]{"Taxon", "Rang"});// ,"Deutschland"
272 setColumnCollapsingAllowed(true);
277 setFooterVisible(true);
278 setColumnFooter("fullTitleCache", "Total amount of Taxa displayed: " + container
.size());
283 private static final List
<String
> NODE_INIT_STRATEGY
= Arrays
.asList(new String
[]{
291 "childNodes.taxon.name",
294 "taxonNodes.taxon.*",
296 "taxon.descriptions",
299 "taxon.synonymRelations",
302 "name.rank.representations",
303 "name.status.type.representations",
308 protected static final List
<String
> DESCRIPTION_INIT_STRATEGY
= Arrays
.asList(new String
[]{
311 "elements.sources.citation.authorship.$",
312 "elements.sources.nameUsedInSource.originalNameString",
313 "elements.area.level",
314 "elements.modifyingText",
317 "elements.multilanguageText",