Project

General

Profile

Download (31.9 KB) Statistics
| Branch: | Tag: | Revision:
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.UUID;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.apache.log4j.Logger;
18
import org.hibernate.criterion.Restrictions;
19
import org.springframework.context.annotation.Scope;
20
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
21

    
22
import com.vaadin.data.Property;
23
import com.vaadin.spring.annotation.SpringComponent;
24
import com.vaadin.ui.AbstractField;
25
import com.vaadin.ui.Field;
26

    
27
import eu.etaxonomy.cdm.api.service.INameService;
28
import eu.etaxonomy.cdm.model.agent.AgentBase;
29
import eu.etaxonomy.cdm.model.agent.Person;
30
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31
import eu.etaxonomy.cdm.model.common.AnnotationType;
32
import eu.etaxonomy.cdm.model.common.CdmBase;
33
import eu.etaxonomy.cdm.model.common.TermType;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
37
import eu.etaxonomy.cdm.model.reference.Reference;
38
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
39
import eu.etaxonomy.cdm.model.reference.ReferenceType;
40
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
41
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
42
import eu.etaxonomy.cdm.service.TaxonNameStringFilterablePagingProvider;
43
import eu.etaxonomy.cdm.service.UserHelperAccess;
44
import eu.etaxonomy.cdm.service.initstrategies.AgentBaseInit;
45
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
46
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
47
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
48
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
49
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
50
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorActionStrRep;
51
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
52
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
53
import eu.etaxonomy.cdm.vaadin.model.name.TaxonNameDTO;
54
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
55
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
56
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
57
import eu.etaxonomy.vaadin.component.CompositeCustomField;
58
import eu.etaxonomy.vaadin.component.ReloadableLazyComboBox;
59
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityCombobox;
60
import eu.etaxonomy.vaadin.component.WeaklyRelatedEntityCombobox;
61
import eu.etaxonomy.vaadin.component.WeaklyRelatedEntityField;
62
import eu.etaxonomy.vaadin.event.FieldReplaceEvent;
63
import eu.etaxonomy.vaadin.mvp.AbstractCdmDTOEditorPresenter;
64
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
65
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
66
import eu.etaxonomy.vaadin.mvp.BoundField;
67
import eu.etaxonomy.vaadin.ui.view.PopupView;
68
import eu.etaxonomy.vaadin.util.PropertyIdPath;
69

    
70
/**
71
 * @author a.kohlbecker
72
 * @since May 22, 2017
73
 *
74
 */
75
@SpringComponent
76
@Scope("prototype")
77
public class TaxonNameEditorPresenter extends AbstractCdmDTOEditorPresenter<TaxonNameDTO, TaxonName, TaxonNamePopupEditorView> {
78

    
79

    
80
    private static final List<String> BASIONYM_INIT_STRATEGY = Arrays.asList(
81
            "$",
82
            "relationsFromThisName",
83
            "relationsToThisName.type",
84
            "homotypicalGroup.typifiedNames"
85
            );
86

    
87
    private static final List<String> REFERENCE_INIT_STRATEGY = Arrays.asList("authorship", "inReference.authorship", "inReference.inReference.authorship", "inReference.inReference.inReference.authorship");
88

    
89
    private static final long serialVersionUID = -3538980627079389221L;
90

    
91
    private static final Logger logger = Logger.getLogger(TaxonNameEditorPresenter.class);
92

    
93
    private CdmFilterablePagingProvider<Reference, Reference> nomReferencePagingProvider;
94

    
95
    private Reference publishedUnit;
96

    
97
    private BeanInstantiator<Reference> newReferenceInstantiator;
98

    
99
    private TaxonNameStringFilterablePagingProvider genusOrUninomialPartPagingProvider;
100

    
101
    private TaxonNameStringFilterablePagingProvider specificEpithetPartPagingProvider;
102

    
103
    private Property.ValueChangeListener refreshSpecificEpithetComboBoxListener;
104

    
105
    /**
106
     * {@inheritDoc}
107
     */
108
    @Override
109
    public void handleViewEntered() {
110

    
111
        super.handleViewEntered();
112

    
113
        CdmBeanItemContainerFactory selectFieldFactory = new CdmBeanItemContainerFactory(getRepo());
114
        getView().getRankSelect().setContainerDataSource(selectFieldFactory.buildBeanItemContainer(TermType.Rank));
115
        getView().getRankSelect().setItemCaptionPropertyId("label");
116

    
117
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
118
        termOrPersonPagingProvider.setInitStrategy(AgentBaseInit.TEAM_OR_PERSON_INIT_STRATEGY);
119
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
120

    
121
        getView().getCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
122
        getView().getCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
123

    
124
        getView().getExCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
125
        getView().getExCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
126

    
127
        getView().getBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
128
        getView().getBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
129

    
130
        getView().getExBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
131
        getView().getExBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
132

    
133
        getView().getNomReferenceCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
134
        nomReferencePagingProvider = pagingProviderFactory.referencePagingProvider();
135
        nomReferencePagingProvider.setInitStrategy(REFERENCE_INIT_STRATEGY);
136
        getView().getNomReferenceCombobox().loadFrom(nomReferencePagingProvider, nomReferencePagingProvider, nomReferencePagingProvider.getPageSize());
137
        getView().getNomReferenceCombobox().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getNomReferenceCombobox()));
138
        getView().getNomReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getNomReferenceCombobox(), this));
139

    
140
        getView().getBasionymComboboxSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<TaxonName>());
141

    
142
        CdmFilterablePagingProvider<TaxonName, TaxonName> basionymPagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
143
        basionymPagingProvider.setInitStrategy(BASIONYM_INIT_STRATEGY);
144
        getView().getBasionymComboboxSelect().setPagingProviders(basionymPagingProvider, basionymPagingProvider, basionymPagingProvider.getPageSize(), this);
145

    
146
        getView().getReplacedSynonymsComboboxSelect().setCaptionGenerator( new CdmTitleCacheCaptionGenerator<TaxonName>());
147
        // reusing the basionymPagingProvider for the replaced synonyms to benefit from caching
148
        getView().getReplacedSynonymsComboboxSelect().setPagingProviders(basionymPagingProvider, basionymPagingProvider, basionymPagingProvider.getPageSize(), this);
149

    
150
        getView().getValidationField().getValidatedNameComboBox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<TaxonName>());
151
        // reusing the basionymPagingProvider for the replaced synonyms to benefit from caching
152
        getView().getValidationField().getValidatedNameComboBox().loadFrom(basionymPagingProvider, basionymPagingProvider, basionymPagingProvider.getPageSize());
153
        getView().getValidationField().getValidatedNameComboBox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getValidationField().getValidatedNameComboBox(), this));
154

    
155
        getView().getNomReferenceCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
156
        CdmFilterablePagingProvider<Reference, Reference> icbnCodesPagingProvider = pagingProviderFactory.referencePagingProvider();
157
        icbnCodesPagingProvider.setInitStrategy(REFERENCE_INIT_STRATEGY);
158
        // @formatter:off
159
        // TODO use markers on references instead of isbn. The marker type MarkerType.NOMENCLATURAL_RELEVANT() has already prepared (#7466)
160
        icbnCodesPagingProvider.getCriteria().add(Restrictions.in("isbn", new String[]{
161
                "3-904144-22-7",     // Saint Louis Code
162
                "3-906166-48-1",     // Vienna Code
163
                "978-3-87429-425-6", // Melbourne Code
164
                "978-3-946583-16-5", // Shenzhen Code
165
                "0-85301-006-4"      // ICZN 1999
166
                                     // ICNB
167
                }));
168
        // @formatter:on
169
        getView().getValidationField().getCitatonComboBox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
170
        getView().getValidationField().getCitatonComboBox().loadFrom(icbnCodesPagingProvider, icbnCodesPagingProvider, icbnCodesPagingProvider.getPageSize());
171
        getView().getValidationField().getCitatonComboBox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getValidationField().getCitatonComboBox(), this));
172

    
173
        getView().getAnnotationsField().setAnnotationTypeItemContainer(selectFieldFactory.buildTermItemContainer(
174
                AnnotationType.EDITORIAL().getUuid(), AnnotationType.TECHNICAL().getUuid()));
175
    }
176

    
177
    /**
178
     * {@inheritDoc}
179
     */
180
    @Override
181
    protected TaxonName loadCdmEntity(UUID identifier) {
182

    
183
        List<String> initStrategy = Arrays.asList(
184
                "$",
185
                "annotations.type",
186
                "annotations.*", // needed as log as we are using a table in FilterableAnnotationsField
187
                "rank.vocabulary", // needed for comparing ranks
188

    
189
                "nomenclaturalReference.authorship",
190
                "nomenclaturalReference.inReference.authorship",
191
                "nomenclaturalReference.inReference.inReference.authorship",
192
                "nomenclaturalReference.inReference.inReference.inReference.authorship",
193

    
194
                "status.type",
195

    
196
                "combinationAuthorship",
197
                "exCombinationAuthorship",
198
                "basionymAuthorship",
199
                "exBasionymAuthorship",
200

    
201
                // basionyms: relationsToThisName.fromName
202
                "relationsToThisName.type",
203
                "relationsToThisName.fromName.rank",
204
                "relationsToThisName.fromName.combinationAuthorship",
205
                "relationsToThisName.fromName.exCombinationAuthorship",
206
                "relationsToThisName.fromName.nomenclaturalReference.authorship",
207
                "relationsToThisName.fromName.nomenclaturalReference.inReference.authorship",
208
                "relationsToThisName.fromName.nomenclaturalReference.inReference.inReference.inReference.authorship",
209
                "relationsToThisName.fromName.relationsToThisName",
210
                "relationsToThisName.fromName.relationsFromThisName",
211
                "relationsToThisName.citation",
212

    
213
                "relationsFromThisName",
214
                "homotypicalGroup.typifiedNames"
215
        );
216

    
217
        TaxonName taxonName;
218
        if(identifier != null){
219
            taxonName = getRepo().getNameService().load(identifier, initStrategy);
220
        } else {
221
            if(cdmEntityInstantiator != null) {
222
                taxonName = cdmEntityInstantiator.createNewBean();
223
            } else {
224
                taxonName = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
225
            }
226
        }
227

    
228
        if(getView().isModeEnabled(TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY)){
229
            Reference nomRef = taxonName.getNomenclaturalReference();
230

    
231
            //getView().getNomReferenceCombobox().setEnabled(nomRef.isOfType(ReferenceType.Section));
232
            publishedUnit = nomRef;
233
            while(publishedUnit.isOfType(ReferenceType.Section) && publishedUnit.getInReference() != null){
234
                publishedUnit = nomRef.getInReference();
235
            }
236
            // reduce available references to those which are sections of the publishedUnit and the publishedUnit itself
237
            // nomReferencePagingProvider
238
            nomReferencePagingProvider.getCriteria().add(Restrictions.or(
239
                    Restrictions.and(Restrictions.eq("inReference", publishedUnit), Restrictions.eq("type", ReferenceType.Section)),
240
                    Restrictions.idEq(publishedUnit.getId())
241
                    )
242
            );
243
            // and remove the empty option
244
            getView().getNomReferenceCombobox().getSelect().setNullSelectionAllowed(false);
245

    
246
            // new Reference only a sub sections of the publishedUnit
247
            newReferenceInstantiator = new BeanInstantiator<Reference>() {
248
                @Override
249
                public Reference createNewBean() {
250
                    Reference newRef = ReferenceFactory.newSection();
251
                    newRef.setInReference(publishedUnit);
252
                    return newRef;
253
                }
254
            };
255

    
256
        }
257
        return taxonName;
258
    }
259

    
260
    /**
261
     * {@inheritDoc}
262
     */
263
    @Override
264
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
265
        if(crud != null){
266
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(TaxonName.class, identifier, crud, null);
267
        }
268

    
269
    }
270

    
271
    /**
272
     * {@inheritDoc}
273
     */
274
    @Override
275
    protected void guaranteePerEntityCRUDPermissions(TaxonName bean) {
276
        if(crud != null){
277
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(bean, crud, null);
278
        }
279
    }
280

    
281
    /**
282
     * {@inheritDoc}
283
     */
284
    @Override
285
    protected INameService getService() {
286
        return getRepo().getNameService();
287
    }
288

    
289
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
290
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
291

    
292
        if(getView() == null || event.getSourceView() != getView() ){
293
            return;
294
        }
295

    
296
        ReferencePopupEditor referenceEditorPopup = openPopupEditor(ReferencePopupEditor.class, event);
297

    
298
        referenceEditorPopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
299
        referenceEditorPopup.withDeleteButton(true);
300
        referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
301
        referenceEditorPopup.loadInEditor(null);
302
        if(newReferenceInstantiator != null){
303
            // this is a bit clumsy, we actually need to inject something like a view configurer
304
            // which can enable, disable fields
305
            referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
306
            referenceEditorPopup.getTypeSelect().setEnabled(false);
307
        }
308
    }
309

    
310
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
311
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
312

    
313

    
314
        if(!isFromOwnView(event)){
315
            return;
316
        }
317
        ReferencePopupEditor referenceEditorPopup = openPopupEditor(ReferencePopupEditor.class, event);
318

    
319
        referenceEditorPopup.withDeleteButton(true);
320
        referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
321
        referenceEditorPopup.loadInEditor(event.getEntityUuid());
322
        if(newReferenceInstantiator != null){
323
            // this is a bit clumsy, we actually need to inject something like a view configurator
324
            // which can enable, disable fields
325
            referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
326
            referenceEditorPopup.getTypeSelect().setEnabled(false);
327
        }
328
    }
329

    
330
    @EventBusListenerMethod
331
    public void onFieldReplaceEvent(FieldReplaceEvent<String> event){
332

    
333
        PropertyIdPath boundPropertyIdPath = boundPropertyIdPath(event.getNewField());
334
        if(boundPropertyIdPath != null){
335
            TaxonNameDTO taxonNamedto = ((AbstractPopupEditor<TaxonNameDTO, AbstractCdmDTOEditorPresenter<TaxonNameDTO, TaxonName,?>>)getView()).getBean();
336
            if(boundPropertyIdPath.matches("specificEpithet")){
337
                AbstractField<String> genusOrUninomialField = getView().getGenusOrUninomialField();
338
                if(event.getNewField() instanceof CompositeCustomField){
339
                    if(specificEpithetPartPagingProvider == null){
340
                        specificEpithetPartPagingProvider = new TaxonNameStringFilterablePagingProvider(getRepo().getNameService(), Rank.SPECIES());
341
                    }
342
                    specificEpithetPartPagingProvider.listenToFields(
343
                            genusOrUninomialField,
344
                            null, null, null);
345
                    specificEpithetPartPagingProvider.excludeNames(taxonNamedto.cdmEntity());
346
                    specificEpithetPartPagingProvider.updateFromFields();
347
                    WeaklyRelatedEntityCombobox<TaxonName> specificEpithetField = (WeaklyRelatedEntityCombobox<TaxonName>)event.getNewField();
348
                    refreshSpecificEpithetComboBoxListener = e -> { specificEpithetField.getSelect().refresh(); specificEpithetField.setValue(null);};
349
                    specificEpithetField.loadFrom(specificEpithetPartPagingProvider, specificEpithetPartPagingProvider, specificEpithetPartPagingProvider.getPageSize());
350
                    specificEpithetField.setValue(event.getOldField().getValue());
351
                    specificEpithetField.reload();
352
                    genusOrUninomialField.addValueChangeListener(refreshSpecificEpithetComboBoxListener);
353
                } else {
354
                    if(specificEpithetPartPagingProvider != null){
355
                        specificEpithetPartPagingProvider.unlistenAllFields();
356
                    }
357
                    if(refreshSpecificEpithetComboBoxListener != null){
358
                        genusOrUninomialField.removeValueChangeListener(refreshSpecificEpithetComboBoxListener);
359
                        refreshSpecificEpithetComboBoxListener = null;
360
                    }
361
                }
362
            } else if(boundPropertyIdPath.matches("genusOrUninomial")) {
363
                if(event.getNewField() instanceof CompositeCustomField){
364
                    if(genusOrUninomialPartPagingProvider  == null){
365
                        genusOrUninomialPartPagingProvider = new TaxonNameStringFilterablePagingProvider(getRepo().getNameService());
366
                    }
367
                    genusOrUninomialPartPagingProvider.listenToFields(
368
                                null,
369
                                getView().getInfraGenericEpithetField(),
370
                                getView().getSpecificEpithetField(),
371
                                getView().getInfraSpecificEpithetField()
372
                               );
373
                    genusOrUninomialPartPagingProvider.excludeNames(taxonNamedto.cdmEntity());
374
                    WeaklyRelatedEntityCombobox<TaxonName> genusOrUninomialField = (WeaklyRelatedEntityCombobox<TaxonName>)event.getNewField();
375
                    genusOrUninomialField.loadFrom(genusOrUninomialPartPagingProvider, genusOrUninomialPartPagingProvider, genusOrUninomialPartPagingProvider.getPageSize());
376
                    genusOrUninomialField.setValue(event.getOldField().getValue());
377
                    genusOrUninomialField.reload();
378
                }else {
379
                    if(genusOrUninomialPartPagingProvider != null){
380
                        genusOrUninomialPartPagingProvider.unlistenAllFields();
381
                    }
382
                }
383

    
384
            }
385
        }
386

    
387
    }
388

    
389
    @EventBusListenerMethod
390
    public void onEntityChangeEvent(EntityChangeEvent<?> event){
391

    
392
        if(event.getSourceView() instanceof AbstractPopupEditor) {
393

    
394
            BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
395

    
396
            if(boundTargetField != null){
397
                if(boundTargetField.matchesPropertyIdPath("genusOrUninomial")){
398
                    if(event.isCreateOrModifiedType()){
399
                        getCache().load(event.getEntity());
400
                        if(getView().getGenusOrUninomialField() instanceof WeaklyRelatedEntityCombobox){
401
                            WeaklyRelatedEntityCombobox<TaxonName> weaklyRelatedEntityCombobox = (WeaklyRelatedEntityCombobox<TaxonName>)getView().getGenusOrUninomialField();
402
                            if(event.isCreatedType()){
403
                                weaklyRelatedEntityCombobox.setValue(((TaxonName)event.getEntity()).getGenusOrUninomial());
404
                                weaklyRelatedEntityCombobox.reload();
405
                            } else {
406
                                weaklyRelatedEntityCombobox.reload();
407
                            }
408
                            // NOTE: in constrast to the ToOneRelatedEntityCombobox the .discard() does not
409
                            // work here since no datasource is bound to the field, see weaklyRelatedEntityCombobox.reload()
410
                            weaklyRelatedEntityCombobox.updateButtons();
411
                        }
412
                    }
413
                } else
414
                if(boundTargetField.matchesPropertyIdPath("specificEpithet")){
415
                    if(event.isCreateOrModifiedType()){
416
                        getCache().load(event.getEntity());
417

    
418
                        if(getView().getSpecificEpithetField() instanceof WeaklyRelatedEntityCombobox){
419
                            WeaklyRelatedEntityCombobox weaklyRelatedEntityCombobox = (WeaklyRelatedEntityCombobox)getView().getSpecificEpithetField();
420
                            if(event.isCreatedType()){
421
                                getView().getSpecificEpithetField().setValue(((TaxonName)event.getEntity()).getSpecificEpithet());
422
                                weaklyRelatedEntityCombobox.reload();
423
                            } else {
424
                                weaklyRelatedEntityCombobox.reload();
425
                            }
426
                            // NOTE: in constrast to the ToOneRelatedEntityCombobox the .discard() does not
427
                            // work here since no datasource is bound to the field, see weaklyRelatedEntityCombobox.reload()
428
                            weaklyRelatedEntityCombobox.updateButtons();
429
                        }
430
                    }
431
                } else
432
                if(boundTargetField.matchesPropertyIdPath("nomenclaturalReference")){
433
                    if(event.isCreateOrModifiedType()){
434
                        getCache().load(event.getEntity());
435
                        if(event.isCreatedType()){
436
                            getView().getNomReferenceCombobox().setValue((Reference) event.getEntity());
437
                        } else {
438
                            getView().getNomReferenceCombobox().reload(); // refreshSelectedValue(modifiedReference);
439
                        }
440
                        getView().getCombinationAuthorshipField().discard(); //refresh from the datasource
441
                        getView().updateAuthorshipFields();
442
                    }
443
                } else
444
                if(boundTargetField.matchesPropertyIdPath("validationFor.otherName")){
445
                    ReloadableLazyComboBox<TaxonName> otherNameField = asReloadableLazyComboBox(boundTargetField.getField(TaxonName.class));
446
                    if(event.isCreateOrModifiedType()){
447
                        getCache().load(event.getEntity());
448
                        if(event.isCreatedType()){
449
                            otherNameField.setValue((TaxonName) event.getEntity());
450
                        } else {
451
                            otherNameField.reload();
452
                        }
453
                    } else
454
                    if(event.isRemovedType()){
455
                        otherNameField.setValue(null);
456
                    }
457
                } else
458
                if(boundTargetField.matchesPropertyIdPath("basionyms")){
459
                    ReloadableLazyComboBox<TaxonName> basionymSourceField = asReloadableLazyComboBox(boundTargetField.getField(TaxonName.class));
460
                    if(event.isCreateOrModifiedType()){
461
                        getCache().load(event.getEntity());
462
                        if(event.isCreatedType()){
463
                            basionymSourceField .setValue((TaxonName) event.getEntity());
464
                        } else {
465
                            basionymSourceField.reload();
466
                        }
467
                        getView().getBasionymAuthorshipField().discard(); //refresh from the datasource
468
                        getView().getExBasionymAuthorshipField().discard(); //refresh from the datasource
469
                        getView().updateAuthorshipFields();
470
                    } else
471
                    if(event.isRemovedType()){
472
                        basionymSourceField.setValue(null);
473
                        getView().updateAuthorshipFields();
474
                    }
475
                } else
476
                if(boundTargetField.matchesPropertyIdPath("replacedSynonyms")){
477
                    ReloadableLazyComboBox<TaxonName> replacedSynonyms = asReloadableLazyComboBox(boundTargetField.getField(TaxonName.class));
478
                    if(event.isCreateOrModifiedType()){
479
                        getCache().load(event.getEntity());
480
                        if(event.isCreatedType()){
481
                            replacedSynonyms .setValue((TaxonName) event.getEntity());
482
                        } else {
483
                            replacedSynonyms.reload();
484
                        }
485
                        getView().getExCombinationAuthorshipField().discard(); //refresh from the datasource
486
                        getView().updateAuthorshipFields();
487
                    } else
488
                    if(event.isRemovedType()){
489
                        replacedSynonyms.setValue(null);
490
                        getView().updateAuthorshipFields();
491
                    }
492
                }
493

    
494
            }
495
        }
496
    }
497

    
498
    protected <CDM extends CdmBase> ReloadableLazyComboBox<CDM> asReloadableLazyComboBox(Field<CDM> field){
499

    
500
        if(field instanceof ToOneRelatedEntityCombobox){
501
            field = ((ToOneRelatedEntityCombobox<CDM>)field).getSelect();
502
        }
503
        return (ReloadableLazyComboBox<CDM>)field;
504
    }
505

    
506

    
507

    
508
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
509
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
510

    
511
        if(getView() == null || event.getSourceView() != getView() ){
512
            return;
513
        }
514

    
515
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
516

    
517
        if(boundPropertyId != null){
518
            if(boundPropertyId.matches("validationFor.otherName") || boundPropertyId.matches("basionyms") || boundPropertyId.matches("replacedSynonyms")){
519
                TaxonNamePopupEditor namePopup = openPopupEditor(TaxonNamePopupEditor.class, event);
520
                namePopup.withDeleteButton(true);
521
                getView().getModesActive().stream()
522
                    .filter(m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
523
                    .forEach(m -> namePopup.enableMode(m));
524
                namePopup.loadInEditor(event.getEntityUuid());
525
            }
526
        }
527
    }
528

    
529
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
530
    public void onTaxonNameEditorActionStrRepEdit(TaxonNameEditorActionStrRep event) {
531

    
532
        if(getView() == null || event.getSourceView() != getView() ){
533
            return;
534
        }
535

    
536
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
537

    
538
        if(boundPropertyId != null){
539
            if(boundPropertyId.matches("genusOrUninomial") || boundPropertyId.matches("specificEpithet")){
540
                TaxonNamePopupEditor namePopup = openPopupEditor(TaxonNamePopupEditor.class, event);
541
                namePopup.withDeleteButton(true);
542
                getView().getModesActive().stream()
543
                    .filter(m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
544
                    .forEach(m -> namePopup.enableMode(m));
545
                namePopup.loadInEditor(event.getEntityUuid());
546
            }
547
        }
548
    }
549

    
550
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
551
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
552

    
553
        if(getView() == null || event.getSourceView() != getView() ){
554
            return;
555
        }
556

    
557
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
558

    
559
        if(boundPropertyId != null){
560
            if(boundPropertyId.matches("validationFor.otherName") || boundPropertyId.matches("basionyms") || boundPropertyId.matches("replacedSynonyms")){
561
                TaxonNamePopupEditor namePopup = openPopupEditor(TaxonNamePopupEditor.class, event);
562
                namePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
563
                namePopup.withDeleteButton(true);
564
                getView().getModesActive().stream()
565
                        .filter(m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
566
                        .forEach(m -> namePopup.enableMode(m));
567
                namePopup.loadInEditor(null);
568
            }
569
        }
570
    }
571

    
572
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
573
    public void onTaxonNameEditorActionStrRepAdd(TaxonNameEditorActionStrRep event) {
574

    
575
        if(getView() == null || event.getSourceView() != getView() ){
576
            return;
577
        }
578

    
579
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
580

    
581
        if(boundPropertyId != null){
582
            if(boundPropertyId.matches("genusOrUninomial") || boundPropertyId.matches("specificEpithet")){
583
                TaxonNamePopupEditor namePopup = openPopupEditor(TaxonNamePopupEditor.class, event);
584
                namePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
585
                namePopup.withDeleteButton(true);
586
                getView().getModesActive().stream()
587
                .filter(m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
588
                .forEach(m -> namePopup.enableMode(m));
589
                namePopup.loadInEditor(null);
590
                if(boundPropertyId.matches("genusOrUninomial")){
591
                    namePopup.getRankSelect().setValue(Rank.GENUS());
592
                }
593
                if(boundPropertyId.matches("specificEpithet")){
594
                    namePopup.getGenusOrUninomialField().setValue(getView().getGenusOrUninomialField().getValue());
595
                    namePopup.getRankSelect().setValue(Rank.SPECIES());
596
                }
597
                if(WeaklyRelatedEntityField.class.isAssignableFrom(event.getTarget().getClass())){
598
                    WeaklyRelatedEntityField<TaxonName> taxoNameField = (WeaklyRelatedEntityField<TaxonName>)event.getTarget();
599
                    if(!taxoNameField.isValueInOptions()){
600
                        String nameString = event.getTarget().getValue();
601
                        if(StringUtils.isNotEmpty(nameString)){
602
                            if(boundPropertyId.matches("genusOrUninomial")){
603
                                namePopup.getGenusOrUninomialField().setValue(nameString);
604
                            }
605
                            if(boundPropertyId.matches("specificEpithet")){
606
                                namePopup.getSpecificEpithetField().setValue(nameString);
607
                            }
608
                        }
609
                    }
610
                }
611
            }
612
        }
613
    }
614

    
615
    /**
616
     * {@inheritDoc}
617
     */
618
    @Override
619
    protected TaxonNameDTO createDTODecorator(TaxonName cdmEntitiy) {
620
        return new TaxonNameDTO(cdmEntitiy);
621
    }
622

    
623

    
624

    
625

    
626
}
(9-9/13)