Project

General

Profile

Download (32 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
    public 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
            taxonName = createCdmEntity();
222
        }
223

    
224
        if(getView().isModeEnabled(TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY)){
225
            Reference nomRef = taxonName.getNomenclaturalReference();
226

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

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

    
252
        }
253

    
254
        return taxonName;
255
    }
256

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

    
266
    }
267

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

    
278
    /**
279
     * {@inheritDoc}
280
     */
281
    @Override
282
    protected INameService getService() {
283
        return getRepo().getNameService();
284
    }
285

    
286
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
287
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
288

    
289
        if(getView() == null || event.getSourceView() != getView() ){
290
            return;
291
        }
292

    
293
        ReferencePopupEditor referenceEditorPopup = openPopupEditor(ReferencePopupEditor.class, event);
294

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

    
307
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
308
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
309

    
310

    
311
        if(!isFromOwnView(event)){
312
            return;
313
        }
314
        ReferencePopupEditor referenceEditorPopup = openPopupEditor(ReferencePopupEditor.class, event);
315

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

    
327
    @EventBusListenerMethod
328
    public void onFieldReplaceEvent(FieldReplaceEvent<String> event){
329

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

    
381
            }
382
        }
383

    
384
    }
385

    
386
    @EventBusListenerMethod
387
    public void onEntityChangeEvent(EntityChangeEvent<?> event){
388

    
389
        if(event.getSourceView() instanceof AbstractPopupEditor) {
390

    
391
            BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
392

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

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

    
491
            }
492
        }
493
    }
494

    
495
    protected <CDM extends CdmBase> ReloadableLazyComboBox<CDM> asReloadableLazyComboBox(Field<CDM> field){
496

    
497
        if(field instanceof ToOneRelatedEntityCombobox){
498
            field = ((ToOneRelatedEntityCombobox<CDM>)field).getSelect();
499
        }
500
        return (ReloadableLazyComboBox<CDM>)field;
501
    }
502

    
503

    
504

    
505
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
506
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
507

    
508
        if(getView() == null || event.getSourceView() != getView() ){
509
            return;
510
        }
511

    
512
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
513

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

    
526
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
527
    public void onTaxonNameEditorActionStrRepEdit(TaxonNameEditorActionStrRep event) {
528

    
529
        if(getView() == null || event.getSourceView() != getView() ){
530
            return;
531
        }
532

    
533
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
534

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

    
547
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
548
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
549

    
550
        if(getView() == null || event.getSourceView() != getView() ){
551
            return;
552
        }
553

    
554
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
555

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

    
569
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
570
    public void onTaxonNameEditorActionStrRepAdd(TaxonNameEditorActionStrRep event) {
571

    
572
        if(getView() == null || event.getSourceView() != getView() ){
573
            return;
574
        }
575

    
576
        PropertyIdPath boundPropertyId = boundPropertyIdPath(event.getTarget());
577

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

    
612
    /**
613
     * {@inheritDoc}
614
     */
615
    @Override
616
    protected TaxonNameDTO createDTODecorator(TaxonName cdmEntitiy) {
617
        return new TaxonNameDTO(cdmEntitiy);
618
    }
619

    
620
    /**
621
     * {@inheritDoc}
622
     */
623
    @Override
624
    protected BeanInstantiator<TaxonName> defaultCdmEntityInstantiator() {
625
        return new BeanInstantiator<TaxonName>() {
626

    
627
            @Override
628
            public TaxonName createNewBean() {
629
                return  TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
630
            }
631
        };
632
    }
633

    
634

    
635

    
636

    
637
}
(9-9/13)