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.name;
|
10
|
|
11
|
import java.util.Arrays;
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.context.event.EventListener;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.service.INameService;
|
20
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
21
|
import eu.etaxonomy.cdm.model.agent.Person;
|
22
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
23
|
import eu.etaxonomy.cdm.model.common.TermType;
|
24
|
import eu.etaxonomy.cdm.model.name.Rank;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
27
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
28
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
29
|
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
|
30
|
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
|
31
|
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
|
32
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
|
33
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
34
|
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
|
35
|
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
|
36
|
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
|
37
|
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
|
38
|
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
|
39
|
|
40
|
/**
|
41
|
* @author a.kohlbecker
|
42
|
* @since May 22, 2017
|
43
|
*
|
44
|
*/
|
45
|
public class TaxonNameEditorPresenter extends AbstractCdmEditorPresenter<TaxonName, TaxonNamePopupEditorView> {
|
46
|
|
47
|
private static final long serialVersionUID = -3538980627079389221L;
|
48
|
|
49
|
private static final Logger logger = Logger.getLogger(TaxonNameEditorPresenter.class);
|
50
|
|
51
|
ReferencePopupEditor newReferencePopup = null;
|
52
|
|
53
|
/**
|
54
|
* {@inheritDoc}
|
55
|
*/
|
56
|
@Override
|
57
|
public void handleViewEntered() {
|
58
|
|
59
|
super.handleViewEntered();
|
60
|
|
61
|
CdmBeanItemContainerFactory selectFieldFactory = new CdmBeanItemContainerFactory(getRepo());
|
62
|
getView().getRankSelect().setContainerDataSource(selectFieldFactory.buildBeanItemContainer(TermType.Rank));
|
63
|
getView().getRankSelect().setItemCaptionPropertyId("label");
|
64
|
|
65
|
CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
|
66
|
CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
|
67
|
|
68
|
getView().getCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
69
|
getView().getCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
70
|
|
71
|
getView().getExCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
72
|
getView().getExCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
73
|
|
74
|
getView().getBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
75
|
getView().getBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
76
|
|
77
|
getView().getExBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
78
|
getView().getExBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
79
|
|
80
|
getView().getNomReferenceCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
|
81
|
CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
|
82
|
getView().getNomReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
|
83
|
getView().getNomReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getNomReferenceCombobox()));
|
84
|
|
85
|
|
86
|
getView().getBasionymCombobox().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<TaxonName>());
|
87
|
CdmFilterablePagingProvider<TaxonName, TaxonName> namePagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
|
88
|
|
89
|
getView().getBasionymCombobox().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize());
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* {@inheritDoc}
|
94
|
*/
|
95
|
@Override
|
96
|
protected TaxonName loadCdmEntityById(Integer identifier) {
|
97
|
|
98
|
List<String> initStrategy = Arrays.asList(new String []{
|
99
|
|
100
|
"$",
|
101
|
"rank.vocabulary", // needed for comparing ranks
|
102
|
|
103
|
"nomenclaturalReference.authorship",
|
104
|
"nomenclaturalReference.inReference",
|
105
|
|
106
|
"status.type",
|
107
|
|
108
|
"combinationAuthorship",
|
109
|
"exCombinationAuthorship",
|
110
|
"basionymAuthorship",
|
111
|
"exBasionymAuthorship",
|
112
|
|
113
|
"basionyms.rank",
|
114
|
"basionyms.nomenclaturalReference.authorship",
|
115
|
"basionyms.nomenclaturalReference.inReference",
|
116
|
|
117
|
}
|
118
|
);
|
119
|
|
120
|
TaxonName bean;
|
121
|
if(identifier != null){
|
122
|
bean = getRepo().getNameService().load(identifier, initStrategy);
|
123
|
} else {
|
124
|
bean = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
125
|
}
|
126
|
return bean;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* {@inheritDoc}
|
131
|
*/
|
132
|
@Override
|
133
|
protected void guaranteePerEntityCRUDPermissions(Integer identifier) {
|
134
|
if(crud != null){
|
135
|
newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(TaxonName.class, identifier, crud, null);
|
136
|
}
|
137
|
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* {@inheritDoc}
|
142
|
*/
|
143
|
@Override
|
144
|
protected void guaranteePerEntityCRUDPermissions(TaxonName bean) {
|
145
|
if(crud != null){
|
146
|
newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(bean, crud, null);
|
147
|
}
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
protected TaxonName handleTransientProperties(TaxonName bean) {
|
152
|
logger.trace(this._toString() + ".onEditorSaveEvent - handling transient properties");
|
153
|
List<TaxonName> newBasionymNames = getView().getBasionymCombobox().getValueFromNestedFields();
|
154
|
Set<TaxonName> oldBasionyms = bean.getBasionyms();
|
155
|
boolean updateBasionyms = false;
|
156
|
for(TaxonName newB : newBasionymNames){
|
157
|
|
158
|
updateBasionyms = updateBasionyms || !oldBasionyms.contains(newB);
|
159
|
}
|
160
|
for(TaxonName oldB : oldBasionyms){
|
161
|
updateBasionyms = updateBasionyms || !newBasionymNames.contains(oldB);
|
162
|
}
|
163
|
if(updateBasionyms){
|
164
|
bean.removeBasionyms();
|
165
|
for(TaxonName basionymName :newBasionymNames){
|
166
|
if(basionymName != null){
|
167
|
if(basionymName .getUuid() != null){
|
168
|
// reload
|
169
|
basionymName = getRepo().getNameService().load(basionymName.getUuid());
|
170
|
}
|
171
|
bean.addBasionym(basionymName);
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
return bean;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* {@inheritDoc}
|
180
|
*/
|
181
|
@Override
|
182
|
protected INameService getService() {
|
183
|
return getRepo().getNameService();
|
184
|
}
|
185
|
|
186
|
@EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
|
187
|
public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
|
188
|
|
189
|
if(getView() == null || event.getSourceView() != getView() ){
|
190
|
return;
|
191
|
}
|
192
|
newReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
|
193
|
|
194
|
newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
195
|
newReferencePopup.withDeleteButton(true);
|
196
|
newReferencePopup.loadInEditor(null);
|
197
|
}
|
198
|
|
199
|
@EventListener
|
200
|
public void onDoneWithPopupEvent(DoneWithPopupEvent event){
|
201
|
if(event.getPopup() == newReferencePopup){
|
202
|
if(event.getReason() == Reason.SAVE){
|
203
|
|
204
|
Reference newReference = newReferencePopup.getBean();
|
205
|
|
206
|
// TODO the bean contained in the popup editor is not yet updated at this point.
|
207
|
// so re reload it using the uuid since new beans will not have an Id at this point.
|
208
|
newReference = getRepo().getReferenceService().find(newReference.getUuid());
|
209
|
getView().getNomReferenceCombobox().setValue(newReference);
|
210
|
}
|
211
|
|
212
|
newReferencePopup = null;
|
213
|
}
|
214
|
}
|
215
|
|
216
|
|
217
|
|
218
|
}
|