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.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.hibernate.criterion.Restrictions;
|
20
|
import org.springframework.context.annotation.Scope;
|
21
|
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
|
22
|
|
23
|
import com.vaadin.spring.annotation.SpringComponent;
|
24
|
import com.vaadin.ui.AbstractField;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.INameService;
|
27
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
28
|
import eu.etaxonomy.cdm.model.agent.Person;
|
29
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
30
|
import eu.etaxonomy.cdm.model.common.TermType;
|
31
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
32
|
import eu.etaxonomy.cdm.model.name.Rank;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
34
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
35
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
36
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
37
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
38
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
39
|
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
|
40
|
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
|
41
|
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
|
42
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
|
43
|
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
|
44
|
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
|
45
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
|
46
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
|
47
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
48
|
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
|
49
|
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
|
50
|
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
|
51
|
import eu.etaxonomy.vaadin.component.ReloadableSelect;
|
52
|
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
|
53
|
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
|
54
|
|
55
|
/**
|
56
|
* @author a.kohlbecker
|
57
|
* @since May 22, 2017
|
58
|
*
|
59
|
*/
|
60
|
@SpringComponent
|
61
|
@Scope("prototype")
|
62
|
public class TaxonNameEditorPresenter extends AbstractCdmEditorPresenter<TaxonName, TaxonNamePopupEditorView> {
|
63
|
|
64
|
|
65
|
private static final List<String> BASIONYM_INIT_STRATEGY = Arrays.asList(
|
66
|
"$",
|
67
|
"relationsFromThisName",
|
68
|
"relationsToThisName.type",
|
69
|
"homotypicalGroup.typifiedNames"
|
70
|
);
|
71
|
|
72
|
private static final List<String> REFERENCE_INIT_STRATEGY = Arrays.asList("authorship", "inReference.authorship", "inReference.inReference.authorship", "inReference.inReference.inReference.authorship");
|
73
|
|
74
|
private static final List<String> TEAM_OR_PERSON_INIT_STRATEGY = Arrays.asList(
|
75
|
"$",
|
76
|
"teamMembers.$"
|
77
|
);
|
78
|
|
79
|
private static final long serialVersionUID = -3538980627079389221L;
|
80
|
|
81
|
private static final Logger logger = Logger.getLogger(TaxonNameEditorPresenter.class);
|
82
|
|
83
|
private ReferencePopupEditor referenceEditorPopup = null;
|
84
|
|
85
|
private TaxonNamePopupEditor basionymNamePopup = null;
|
86
|
|
87
|
private CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider;
|
88
|
|
89
|
private Reference publishedUnit;
|
90
|
|
91
|
private BeanInstantiator<Reference> newReferenceInstantiator;
|
92
|
|
93
|
private BeanInstantiator<TaxonName> newBasionymNameInstantiator;
|
94
|
|
95
|
private AbstractField<TaxonName> basionymSourceField;
|
96
|
|
97
|
/**
|
98
|
* {@inheritDoc}
|
99
|
*/
|
100
|
@Override
|
101
|
public void handleViewEntered() {
|
102
|
|
103
|
super.handleViewEntered();
|
104
|
|
105
|
CdmBeanItemContainerFactory selectFieldFactory = new CdmBeanItemContainerFactory(getRepo());
|
106
|
getView().getRankSelect().setContainerDataSource(selectFieldFactory.buildBeanItemContainer(TermType.Rank));
|
107
|
getView().getRankSelect().setItemCaptionPropertyId("label");
|
108
|
|
109
|
CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
|
110
|
termOrPersonPagingProvider.setInitStrategy(TEAM_OR_PERSON_INIT_STRATEGY);
|
111
|
CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
|
112
|
|
113
|
getView().getCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
114
|
getView().getCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
115
|
|
116
|
getView().getExCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
117
|
getView().getExCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
118
|
|
119
|
getView().getBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
120
|
getView().getBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
121
|
|
122
|
getView().getExBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
123
|
getView().getExBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
124
|
|
125
|
getView().getNomReferenceCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
|
126
|
referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
|
127
|
referencePagingProvider.setInitStrategy(REFERENCE_INIT_STRATEGY);
|
128
|
getView().getNomReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
|
129
|
getView().getNomReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getNomReferenceCombobox()));
|
130
|
getView().getNomReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getNomReferenceCombobox(), this));
|
131
|
|
132
|
getView().getBasionymComboboxSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<TaxonName>());
|
133
|
|
134
|
CdmFilterablePagingProvider<TaxonName, TaxonName> basionymPagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
|
135
|
basionymPagingProvider.setInitStrategy(BASIONYM_INIT_STRATEGY);
|
136
|
getView().getBasionymComboboxSelect().setPagingProviders(basionymPagingProvider, basionymPagingProvider, basionymPagingProvider.getPageSize(), this);
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* {@inheritDoc}
|
141
|
*/
|
142
|
@Override
|
143
|
protected TaxonName loadCdmEntity(UUID identifier) {
|
144
|
|
145
|
List<String> initStrategy = Arrays.asList(new String []{
|
146
|
|
147
|
"$",
|
148
|
"rank.vocabulary", // needed for comparing ranks
|
149
|
|
150
|
"nomenclaturalReference.authorship",
|
151
|
"nomenclaturalReference.inReference.authorship",
|
152
|
"nomenclaturalReference.inReference.inReference.authorship",
|
153
|
"nomenclaturalReference.inReference.inReference.inReference.authorship",
|
154
|
|
155
|
"status.type",
|
156
|
|
157
|
"combinationAuthorship.teamMembers",
|
158
|
"exCombinationAuthorship.teamMembers",
|
159
|
"basionymAuthorship.teamMembers",
|
160
|
"exBasionymAuthorship.teamMembers",
|
161
|
|
162
|
// basionyms: relationsToThisName.fromName
|
163
|
"relationsToThisName.type",
|
164
|
"relationsToThisName.fromName.rank",
|
165
|
"relationsToThisName.fromName.combinationAuthorship.teamMembers",
|
166
|
"relationsToThisName.fromName.exCombinationAuthorship.teamMembers",
|
167
|
"relationsToThisName.fromName.nomenclaturalReference.authorship.teamMembers",
|
168
|
"relationsToThisName.fromName.nomenclaturalReference.inReference.authorship.teamMembers",
|
169
|
"relationsToThisName.fromName.nomenclaturalReference.inReference.inReference.inReference.authorship.teamMembers",
|
170
|
"relationsToThisName.fromName.relationsToThisName",
|
171
|
"relationsToThisName.fromName.relationsFromThisName",
|
172
|
|
173
|
"relationsFromThisName",
|
174
|
"homotypicalGroup.typifiedNames"
|
175
|
|
176
|
}
|
177
|
);
|
178
|
|
179
|
TaxonName taxonName;
|
180
|
if(identifier != null){
|
181
|
taxonName = getRepo().getNameService().load(identifier, initStrategy);
|
182
|
} else {
|
183
|
taxonName = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
|
184
|
}
|
185
|
|
186
|
if(getView().isModeEnabled(TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY)){
|
187
|
if(taxonName.getNomenclaturalReference() != null){
|
188
|
Reference nomRef = (Reference)taxonName.getNomenclaturalReference();
|
189
|
//getView().getNomReferenceCombobox().setEnabled(nomRef.isOfType(ReferenceType.Section));
|
190
|
publishedUnit = nomRef;
|
191
|
while(publishedUnit.isOfType(ReferenceType.Section) && publishedUnit.getInReference() != null){
|
192
|
publishedUnit = nomRef.getInReference();
|
193
|
}
|
194
|
// reduce available references to those which are sections of the publishedUnit and the publishedUnit itself
|
195
|
// referencePagingProvider
|
196
|
referencePagingProvider.getCriteria().add(Restrictions.or(
|
197
|
Restrictions.and(Restrictions.eq("inReference", publishedUnit), Restrictions.eq("type", ReferenceType.Section)),
|
198
|
Restrictions.idEq(publishedUnit.getId())
|
199
|
)
|
200
|
);
|
201
|
// and remove the empty option
|
202
|
getView().getNomReferenceCombobox().getSelect().setNullSelectionAllowed(false);
|
203
|
|
204
|
// new Reference only a sub sections of the publishedUnit
|
205
|
newReferenceInstantiator = new BeanInstantiator<Reference>() {
|
206
|
@Override
|
207
|
public Reference createNewBean() {
|
208
|
Reference newRef = ReferenceFactory.newSection();
|
209
|
newRef.setInReference(publishedUnit);
|
210
|
return newRef;
|
211
|
}
|
212
|
};
|
213
|
|
214
|
}
|
215
|
}
|
216
|
|
217
|
return taxonName;
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* {@inheritDoc}
|
222
|
*/
|
223
|
@Override
|
224
|
protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
|
225
|
if(crud != null){
|
226
|
newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(TaxonName.class, identifier, crud, null);
|
227
|
}
|
228
|
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* {@inheritDoc}
|
233
|
*/
|
234
|
@Override
|
235
|
protected void guaranteePerEntityCRUDPermissions(TaxonName bean) {
|
236
|
if(crud != null){
|
237
|
newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(bean, crud, null);
|
238
|
}
|
239
|
}
|
240
|
|
241
|
@Override
|
242
|
protected TaxonName handleTransientProperties(TaxonName bean) {
|
243
|
|
244
|
logger.trace(this._toString() + ".onEditorSaveEvent - handling transient properties");
|
245
|
|
246
|
|
247
|
List<TaxonName> newBasionymNames = getView().getBasionymComboboxSelect().getValueFromNestedFields();
|
248
|
Set<TaxonName> oldBasionyms = bean.getBasionyms();
|
249
|
Set<TaxonName> updateBasionyms = new HashSet<>();
|
250
|
Set<TaxonName> removeBasionyms = new HashSet<>();
|
251
|
|
252
|
for(TaxonName newB : newBasionymNames){
|
253
|
if(!oldBasionyms.contains(newB)){
|
254
|
updateBasionyms.add(newB);
|
255
|
}
|
256
|
}
|
257
|
|
258
|
for(TaxonName oldB : oldBasionyms){
|
259
|
if(!newBasionymNames.contains(oldB)){
|
260
|
removeBasionyms.add(oldB);
|
261
|
}
|
262
|
}
|
263
|
for(TaxonName removeBasionym :removeBasionyms){
|
264
|
Set<NameRelationship> removeRelations = new HashSet<NameRelationship>();
|
265
|
for (NameRelationship nameRelation : bean.getRelationsToThisName()){
|
266
|
if (nameRelation.getType().isBasionymRelation() && nameRelation.getFromName().equals(removeBasionym)){
|
267
|
removeRelations.add(nameRelation);
|
268
|
}
|
269
|
}
|
270
|
for (NameRelationship relation : removeRelations){
|
271
|
bean.removeNameRelationship(relation);
|
272
|
}
|
273
|
}
|
274
|
getRepo().getSession().clear();
|
275
|
for(TaxonName addBasionymName :updateBasionyms){
|
276
|
if(addBasionymName != null){
|
277
|
bean.addBasionym(addBasionymName);
|
278
|
}
|
279
|
}
|
280
|
return bean;
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* {@inheritDoc}
|
285
|
*/
|
286
|
@Override
|
287
|
protected INameService getService() {
|
288
|
return getRepo().getNameService();
|
289
|
}
|
290
|
|
291
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
|
292
|
public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
|
293
|
|
294
|
if(getView() == null || event.getSourceView() != getView() ){
|
295
|
return;
|
296
|
}
|
297
|
|
298
|
referenceEditorPopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
|
299
|
|
300
|
referenceEditorPopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
301
|
referenceEditorPopup.withDeleteButton(true);
|
302
|
referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
|
303
|
referenceEditorPopup.loadInEditor(null);
|
304
|
if(newReferenceInstantiator != null){
|
305
|
// this is a bit clumsy, we actually need to inject something like a view configurer
|
306
|
// which can enable, disable fields
|
307
|
referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
|
308
|
referenceEditorPopup.getTypeSelect().setEnabled(false);
|
309
|
}
|
310
|
}
|
311
|
|
312
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
|
313
|
public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
|
314
|
|
315
|
|
316
|
if(getView() == null || event.getSourceView() != getView() ){
|
317
|
return;
|
318
|
}
|
319
|
referenceEditorPopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
|
320
|
|
321
|
referenceEditorPopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
322
|
referenceEditorPopup.withDeleteButton(true);
|
323
|
referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
|
324
|
referenceEditorPopup.loadInEditor(event.getEntityUuid());
|
325
|
if(newReferenceInstantiator != null){
|
326
|
// this is a bit clumsy, we actually need to inject something like a view configurer
|
327
|
// which can enable, disable fields
|
328
|
referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
|
329
|
referenceEditorPopup.getTypeSelect().setEnabled(false);
|
330
|
}
|
331
|
}
|
332
|
|
333
|
@EventBusListenerMethod
|
334
|
public void onEntityChangeEvent(EntityChangeEvent<?> event){
|
335
|
|
336
|
if(event.getSourceView() == referenceEditorPopup){
|
337
|
if(event.isCreateOrModifiedType()){
|
338
|
|
339
|
getCache().load(event.getEntity());
|
340
|
getView().getNomReferenceCombobox().reload(); // refreshSelectedValue(modifiedReference);
|
341
|
getView().getCombinationAuthorshipField().discard(); //refresh from the datasource
|
342
|
getView().updateAuthorshipFields();
|
343
|
}
|
344
|
|
345
|
referenceEditorPopup = null;
|
346
|
}
|
347
|
if(event.getSourceView() == basionymNamePopup){
|
348
|
if(event.isCreateOrModifiedType()){
|
349
|
|
350
|
getCache().load(event.getEntity());
|
351
|
((ReloadableSelect)basionymSourceField).reload();
|
352
|
getView().getBasionymAuthorshipField().discard(); //refresh from the datasource
|
353
|
getView().getExBasionymAuthorshipField().discard(); //refresh from the datasource
|
354
|
getView().updateAuthorshipFields();
|
355
|
|
356
|
}
|
357
|
if(event.isRemovedType()){
|
358
|
basionymSourceField.setValue(null);
|
359
|
getView().updateAuthorshipFields();
|
360
|
}
|
361
|
|
362
|
basionymNamePopup = null;
|
363
|
basionymSourceField = null;
|
364
|
}
|
365
|
}
|
366
|
|
367
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
|
368
|
public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
|
369
|
|
370
|
if(getView() == null || event.getSourceView() != getView() ){
|
371
|
return;
|
372
|
}
|
373
|
|
374
|
basionymSourceField = (AbstractField<TaxonName>)event.getSourceComponent();
|
375
|
|
376
|
basionymNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
|
377
|
basionymNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
378
|
basionymNamePopup.withDeleteButton(true);
|
379
|
getView().getModesActive().stream()
|
380
|
.filter(
|
381
|
m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
|
382
|
.forEach(m -> basionymNamePopup.enableMode(m));
|
383
|
basionymNamePopup.loadInEditor(event.getEntityUuid());
|
384
|
basionymNamePopup.getBasionymToggle().setVisible(false);
|
385
|
|
386
|
}
|
387
|
|
388
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
|
389
|
public void oTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
|
390
|
|
391
|
if(getView() == null || event.getSourceView() != getView() ){
|
392
|
return;
|
393
|
}
|
394
|
|
395
|
basionymSourceField = (AbstractField<TaxonName>)event.getSourceComponent();
|
396
|
|
397
|
basionymNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
|
398
|
basionymNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
399
|
basionymNamePopup.withDeleteButton(true);
|
400
|
getView().getModesActive().stream()
|
401
|
.filter(
|
402
|
m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
|
403
|
.forEach(m -> basionymNamePopup.enableMode(m));
|
404
|
basionymNamePopup.loadInEditor(null);
|
405
|
basionymNamePopup.getBasionymToggle().setVisible(false);
|
406
|
}
|
407
|
|
408
|
|
409
|
|
410
|
|
411
|
}
|