Project

General

Profile

Download (18.1 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.log4j.Logger;
17
import org.hibernate.criterion.Restrictions;
18
import org.springframework.context.annotation.Scope;
19
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
20

    
21
import com.vaadin.spring.annotation.SpringComponent;
22
import com.vaadin.ui.AbstractField;
23

    
24
import eu.etaxonomy.cdm.api.service.INameService;
25
import eu.etaxonomy.cdm.model.agent.AgentBase;
26
import eu.etaxonomy.cdm.model.agent.Person;
27
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
28
import eu.etaxonomy.cdm.model.common.TermType;
29
import eu.etaxonomy.cdm.model.name.Rank;
30
import eu.etaxonomy.cdm.model.name.TaxonName;
31
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
34
import eu.etaxonomy.cdm.model.reference.ReferenceType;
35
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
36
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
37
import eu.etaxonomy.cdm.service.initstrategies.AgentBaseInit;
38
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
39
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
40
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
41
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
42
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
43
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
44
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
45
import eu.etaxonomy.cdm.vaadin.model.name.TaxonNameDTO;
46
import eu.etaxonomy.cdm.vaadin.permission.UserHelper;
47
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
48
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
49
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
50
import eu.etaxonomy.vaadin.component.ReloadableLazyComboBox;
51
import eu.etaxonomy.vaadin.component.ReloadableSelect;
52
import eu.etaxonomy.vaadin.mvp.AbstractCdmDTOEditorPresenter;
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 AbstractCdmDTOEditorPresenter<TaxonNameDTO, 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 long serialVersionUID = -3538980627079389221L;
75

    
76
    private static final Logger logger = Logger.getLogger(TaxonNameEditorPresenter.class);
77

    
78
    private ReferencePopupEditor referenceEditorPopup = null;
79

    
80
    private TaxonNamePopupEditor basionymNamePopup = null;
81

    
82
    private CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider;
83

    
84
    private Reference publishedUnit;
85

    
86
    private BeanInstantiator<Reference> newReferenceInstantiator;
87

    
88
    private AbstractField<TaxonName> basionymSourceField;
89

    
90
    /**
91
     * {@inheritDoc}
92
     */
93
    @Override
94
    public void handleViewEntered() {
95

    
96
        super.handleViewEntered();
97

    
98
        CdmBeanItemContainerFactory selectFieldFactory = new CdmBeanItemContainerFactory(getRepo());
99
        getView().getRankSelect().setContainerDataSource(selectFieldFactory.buildBeanItemContainer(TermType.Rank));
100
        getView().getRankSelect().setItemCaptionPropertyId("label");
101

    
102
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
103
        termOrPersonPagingProvider.setInitStrategy(AgentBaseInit.TEAM_OR_PERSON_INIT_STRATEGY);
104
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
105

    
106
        getView().getCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
107
        getView().getCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
108

    
109
        getView().getExCombinationAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
110
        getView().getExCombinationAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
111

    
112
        getView().getBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
113
        getView().getBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
114

    
115
        getView().getExBasionymAuthorshipField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
116
        getView().getExBasionymAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
117

    
118
        getView().getNomReferenceCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
119
        referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
120
        referencePagingProvider.setInitStrategy(REFERENCE_INIT_STRATEGY);
121
        getView().getNomReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
122
        getView().getNomReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getNomReferenceCombobox()));
123
        getView().getNomReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getNomReferenceCombobox(), this));
124

    
125
        getView().getBasionymComboboxSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<TaxonName>());
126
        CdmFilterablePagingProvider<TaxonName, TaxonName> basionymPagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
127
        basionymPagingProvider.setInitStrategy(BASIONYM_INIT_STRATEGY);
128
        getView().getBasionymComboboxSelect().setPagingProviders(basionymPagingProvider, basionymPagingProvider, basionymPagingProvider.getPageSize(), this);
129

    
130
        getView().getReplacedSynonymsComboboxSelect().setCaptionGenerator( new CdmTitleCacheCaptionGenerator<TaxonName>());
131
        // reusing the basionymPagingProvider for the replaced synonyms to benefit from caching
132
        getView().getReplacedSynonymsComboboxSelect().setPagingProviders(basionymPagingProvider, basionymPagingProvider, basionymPagingProvider.getPageSize(), this);
133

    
134
        getView().getValidationField().getValidatedNameComboBox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<TaxonName>());
135
        // reusing the basionymPagingProvider for the replaced synonyms to benefit from caching
136
        getView().getValidationField().getValidatedNameComboBox().loadFrom(basionymPagingProvider, basionymPagingProvider, basionymPagingProvider.getPageSize());
137
        getView().getValidationField().getValidatedNameComboBox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getValidationField().getValidatedNameComboBox(), this));
138

    
139

    
140
        getView().getValidationField().getCitatonComboBox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
141
        getView().getValidationField().getCitatonComboBox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
142
        getView().getValidationField().getCitatonComboBox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getValidationField().getCitatonComboBox(), this));
143

    
144
    }
145

    
146
    /**
147
     * {@inheritDoc}
148
     */
149
    @Override
150
    protected TaxonName loadCdmEntity(UUID identifier) {
151

    
152
        List<String> initStrategy = Arrays.asList(new String []{
153

    
154
                "$",
155
                "rank.vocabulary", // needed for comparing ranks
156

    
157
                "nomenclaturalReference.authorship",
158
                "nomenclaturalReference.inReference.authorship",
159
                "nomenclaturalReference.inReference.inReference.authorship",
160
                "nomenclaturalReference.inReference.inReference.inReference.authorship",
161

    
162
                "status.type",
163

    
164
                "combinationAuthorship",
165
                "exCombinationAuthorship",
166
                "basionymAuthorship",
167
                "exBasionymAuthorship",
168

    
169
                // basionyms: relationsToThisName.fromName
170
                "relationsToThisName.type",
171
                "relationsToThisName.fromName.rank",
172
                "relationsToThisName.fromName.combinationAuthorship",
173
                "relationsToThisName.fromName.exCombinationAuthorship",
174
                "relationsToThisName.fromName.nomenclaturalReference.authorship",
175
                "relationsToThisName.fromName.nomenclaturalReference.inReference.authorship",
176
                "relationsToThisName.fromName.nomenclaturalReference.inReference.inReference.inReference.authorship",
177
                "relationsToThisName.fromName.relationsToThisName",
178
                "relationsToThisName.fromName.relationsFromThisName",
179

    
180
                "relationsFromThisName",
181
                "homotypicalGroup.typifiedNames"
182

    
183
                }
184
        );
185

    
186
        TaxonName taxonName;
187
        if(identifier != null){
188
            taxonName = getRepo().getNameService().load(identifier, initStrategy);
189
        } else {
190
            taxonName = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
191
        }
192

    
193
        if(getView().isModeEnabled(TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY)){
194
            if(taxonName.getNomenclaturalReference() != null){
195
                Reference nomRef = (Reference)taxonName.getNomenclaturalReference();
196
                //getView().getNomReferenceCombobox().setEnabled(nomRef.isOfType(ReferenceType.Section));
197
                publishedUnit = nomRef;
198
                while(publishedUnit.isOfType(ReferenceType.Section) && publishedUnit.getInReference() != null){
199
                    publishedUnit = nomRef.getInReference();
200
                }
201
                // reduce available references to those which are sections of the publishedUnit and the publishedUnit itself
202
                // referencePagingProvider
203
                referencePagingProvider.getCriteria().add(Restrictions.or(
204
                        Restrictions.and(Restrictions.eq("inReference", publishedUnit), Restrictions.eq("type", ReferenceType.Section)),
205
                        Restrictions.idEq(publishedUnit.getId())
206
                        )
207
                );
208
                // and remove the empty option
209
                getView().getNomReferenceCombobox().getSelect().setNullSelectionAllowed(false);
210

    
211
                // new Reference only a sub sections of the publishedUnit
212
                newReferenceInstantiator = new BeanInstantiator<Reference>() {
213
                    @Override
214
                    public Reference createNewBean() {
215
                        Reference newRef = ReferenceFactory.newSection();
216
                        newRef.setInReference(publishedUnit);
217
                        return newRef;
218
                    }
219
                };
220

    
221
            }
222
        }
223

    
224
        return taxonName;
225
    }
226

    
227
    /**
228
     * {@inheritDoc}
229
     */
230
    @Override
231
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
232
        if(crud != null){
233
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(TaxonName.class, identifier, crud, null);
234
        }
235

    
236
    }
237

    
238
    /**
239
     * {@inheritDoc}
240
     */
241
    @Override
242
    protected void guaranteePerEntityCRUDPermissions(TaxonName bean) {
243
        if(crud != null){
244
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(bean, crud, null);
245
        }
246
    }
247

    
248
    /**
249
     * {@inheritDoc}
250
     */
251
    @Override
252
    protected INameService getService() {
253
        return getRepo().getNameService();
254
    }
255

    
256
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
257
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
258

    
259
        if(getView() == null || event.getSourceView() != getView() ){
260
            return;
261
        }
262

    
263
        referenceEditorPopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
264

    
265
        referenceEditorPopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
266
        referenceEditorPopup.withDeleteButton(true);
267
        referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
268
        referenceEditorPopup.loadInEditor(null);
269
        if(newReferenceInstantiator != null){
270
            // this is a bit clumsy, we actually need to inject something like a view configurer
271
            // which can enable, disable fields
272
            referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
273
            referenceEditorPopup.getTypeSelect().setEnabled(false);
274
        }
275
    }
276

    
277
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
278
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
279

    
280

    
281
        if(getView() == null || event.getSourceView() != getView() ){
282
            return;
283
        }
284
        referenceEditorPopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
285

    
286
        referenceEditorPopup.withDeleteButton(true);
287
        referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
288
        referenceEditorPopup.loadInEditor(event.getEntityUuid());
289
        if(newReferenceInstantiator != null){
290
            // this is a bit clumsy, we actually need to inject something like a view configurer
291
            // which can enable, disable fields
292
            referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
293
            referenceEditorPopup.getTypeSelect().setEnabled(false);
294
        }
295
    }
296

    
297
    @EventBusListenerMethod
298
    public void onEntityChangeEvent(EntityChangeEvent<?> event){
299

    
300
        if(event.getSourceView() == referenceEditorPopup){
301
            if(event.isCreateOrModifiedType()){
302

    
303
                getCache().load(event.getEntity());
304
                if(event.isCreatedType()){
305
                    getView().getNomReferenceCombobox().setValue((Reference) event.getEntity());
306
                } else {
307
                    getView().getNomReferenceCombobox().reload(); // refreshSelectedValue(modifiedReference);
308
                }
309
                getView().getCombinationAuthorshipField().discard(); //refresh from the datasource
310
                getView().updateAuthorshipFields();
311
            }
312

    
313
            referenceEditorPopup = null;
314
        }
315
        if(event.getSourceView()  == basionymNamePopup){
316
            if(event.isCreateOrModifiedType()){
317

    
318
                getCache().load(event.getEntity());
319
                if(event.isCreatedType()){
320
                    basionymSourceField.setValue((TaxonName) event.getEntity());
321
                } else {
322
                    ((ReloadableSelect)basionymSourceField).reload();
323
                }
324
                getView().getBasionymAuthorshipField().discard(); //refresh from the datasource
325
                getView().getExBasionymAuthorshipField().discard(); //refresh from the datasource
326
                getView().updateAuthorshipFields();
327

    
328
            }
329
            if(event.isRemovedType()){
330
                basionymSourceField.setValue(null);
331
                getView().updateAuthorshipFields();
332
            }
333

    
334
            basionymNamePopup = null;
335
            basionymSourceField = null;
336
        }
337
    }
338

    
339
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
340
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
341

    
342
        if(getView() == null || event.getSourceView() != getView() ){
343
            return;
344
        }
345
        ReloadableLazyComboBox<TaxonName> sourceField = (ReloadableLazyComboBox<TaxonName>)event.getSourceComponent();
346

    
347
        if(sourceField == getView().getValidationField().getValidatedNameComboBox().getSelect()){
348
            // validatedNameSourceField .. this is awkward, better use a map to correlate fields to popup editors!!!!
349

    
350
        } else {
351
            basionymSourceField = sourceField;
352

    
353
            basionymNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
354
            basionymNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
355
            basionymNamePopup.withDeleteButton(true);
356
            getView().getModesActive().stream()
357
                    .filter(
358
                            m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
359
                    .forEach(m -> basionymNamePopup.enableMode(m));
360
            basionymNamePopup.loadInEditor(event.getEntityUuid());
361
            basionymNamePopup.getBasionymToggle().setVisible(false);
362
        }
363

    
364
    }
365

    
366
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
367
    public void oTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
368

    
369
        if(getView() == null || event.getSourceView() != getView() ){
370
            return;
371
        }
372

    
373
        basionymSourceField = (AbstractField<TaxonName>)event.getSourceComponent();
374

    
375
        basionymNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
376
        basionymNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
377
        basionymNamePopup.withDeleteButton(true);
378
        getView().getModesActive().stream()
379
                .filter(
380
                        m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
381
                .forEach(m -> basionymNamePopup.enableMode(m));
382
        basionymNamePopup.loadInEditor(null);
383
        basionymNamePopup.getBasionymToggle().setVisible(false);
384
    }
385

    
386
    /**
387
     * {@inheritDoc}
388
     */
389
    @Override
390
    protected TaxonNameDTO createDTODecorator(TaxonName cdmEntitiy) {
391
        return new TaxonNameDTO(cdmEntitiy);
392
    }
393

    
394

    
395

    
396

    
397
}
(9-9/13)