Project

General

Profile

Download (16.7 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

    
135
    /**
136
     * {@inheritDoc}
137
     */
138
    @Override
139
    protected TaxonName loadCdmEntity(UUID identifier) {
140

    
141
        List<String> initStrategy = Arrays.asList(new String []{
142

    
143
                "$",
144
                "rank.vocabulary", // needed for comparing ranks
145

    
146
                "nomenclaturalReference.authorship",
147
                "nomenclaturalReference.inReference.authorship",
148
                "nomenclaturalReference.inReference.inReference.authorship",
149
                "nomenclaturalReference.inReference.inReference.inReference.authorship",
150

    
151
                "status.type",
152

    
153
                "combinationAuthorship",
154
                "exCombinationAuthorship",
155
                "basionymAuthorship",
156
                "exBasionymAuthorship",
157

    
158
                // basionyms: relationsToThisName.fromName
159
                "relationsToThisName.type",
160
                "relationsToThisName.fromName.rank",
161
                "relationsToThisName.fromName.combinationAuthorship",
162
                "relationsToThisName.fromName.exCombinationAuthorship",
163
                "relationsToThisName.fromName.nomenclaturalReference.authorship",
164
                "relationsToThisName.fromName.nomenclaturalReference.inReference.authorship",
165
                "relationsToThisName.fromName.nomenclaturalReference.inReference.inReference.inReference.authorship",
166
                "relationsToThisName.fromName.relationsToThisName",
167
                "relationsToThisName.fromName.relationsFromThisName",
168

    
169
                "relationsFromThisName",
170
                "homotypicalGroup.typifiedNames"
171

    
172
                }
173
        );
174

    
175
        TaxonName taxonName;
176
        if(identifier != null){
177
            taxonName = getRepo().getNameService().load(identifier, initStrategy);
178
        } else {
179
            taxonName = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
180
        }
181

    
182
        if(getView().isModeEnabled(TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY)){
183
            if(taxonName.getNomenclaturalReference() != null){
184
                Reference nomRef = (Reference)taxonName.getNomenclaturalReference();
185
                //getView().getNomReferenceCombobox().setEnabled(nomRef.isOfType(ReferenceType.Section));
186
                publishedUnit = nomRef;
187
                while(publishedUnit.isOfType(ReferenceType.Section) && publishedUnit.getInReference() != null){
188
                    publishedUnit = nomRef.getInReference();
189
                }
190
                // reduce available references to those which are sections of the publishedUnit and the publishedUnit itself
191
                // referencePagingProvider
192
                referencePagingProvider.getCriteria().add(Restrictions.or(
193
                        Restrictions.and(Restrictions.eq("inReference", publishedUnit), Restrictions.eq("type", ReferenceType.Section)),
194
                        Restrictions.idEq(publishedUnit.getId())
195
                        )
196
                );
197
                // and remove the empty option
198
                getView().getNomReferenceCombobox().getSelect().setNullSelectionAllowed(false);
199

    
200
                // new Reference only a sub sections of the publishedUnit
201
                newReferenceInstantiator = new BeanInstantiator<Reference>() {
202
                    @Override
203
                    public Reference createNewBean() {
204
                        Reference newRef = ReferenceFactory.newSection();
205
                        newRef.setInReference(publishedUnit);
206
                        return newRef;
207
                    }
208
                };
209

    
210
            }
211
        }
212

    
213
        return taxonName;
214
    }
215

    
216
    /**
217
     * {@inheritDoc}
218
     */
219
    @Override
220
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
221
        if(crud != null){
222
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(TaxonName.class, identifier, crud, null);
223
        }
224

    
225
    }
226

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

    
237
    /**
238
     * {@inheritDoc}
239
     */
240
    @Override
241
    protected INameService getService() {
242
        return getRepo().getNameService();
243
    }
244

    
245
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
246
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
247

    
248
        if(getView() == null || event.getSourceView() != getView() ){
249
            return;
250
        }
251

    
252
        referenceEditorPopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
253

    
254
        referenceEditorPopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
255
        referenceEditorPopup.withDeleteButton(true);
256
        referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
257
        referenceEditorPopup.loadInEditor(null);
258
        if(newReferenceInstantiator != null){
259
            // this is a bit clumsy, we actually need to inject something like a view configurer
260
            // which can enable, disable fields
261
            referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
262
            referenceEditorPopup.getTypeSelect().setEnabled(false);
263
        }
264
    }
265

    
266
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
267
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
268

    
269

    
270
        if(getView() == null || event.getSourceView() != getView() ){
271
            return;
272
        }
273
        referenceEditorPopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
274

    
275
        referenceEditorPopup.withDeleteButton(true);
276
        referenceEditorPopup.setBeanInstantiator(newReferenceInstantiator);
277
        referenceEditorPopup.loadInEditor(event.getEntityUuid());
278
        if(newReferenceInstantiator != null){
279
            // this is a bit clumsy, we actually need to inject something like a view configurer
280
            // which can enable, disable fields
281
            referenceEditorPopup.getInReferenceCombobox().setEnabled(false);
282
            referenceEditorPopup.getTypeSelect().setEnabled(false);
283
        }
284
    }
285

    
286
    @EventBusListenerMethod
287
    public void onEntityChangeEvent(EntityChangeEvent<?> event){
288

    
289
        if(event.getSourceView() == referenceEditorPopup){
290
            if(event.isCreateOrModifiedType()){
291

    
292
                getCache().load(event.getEntity());
293
                if(event.isCreatedType()){
294
                    getView().getNomReferenceCombobox().setValue((Reference) event.getEntity());
295
                } else {
296
                    getView().getNomReferenceCombobox().reload(); // refreshSelectedValue(modifiedReference);
297
                }
298
                getView().getCombinationAuthorshipField().discard(); //refresh from the datasource
299
                getView().updateAuthorshipFields();
300
            }
301

    
302
            referenceEditorPopup = null;
303
        }
304
        if(event.getSourceView()  == basionymNamePopup){
305
            if(event.isCreateOrModifiedType()){
306

    
307
                getCache().load(event.getEntity());
308
                if(event.isCreatedType()){
309
                    basionymSourceField.setValue((TaxonName) event.getEntity());
310
                } else {
311
                    ((ReloadableSelect)basionymSourceField).reload();
312
                }
313
                getView().getBasionymAuthorshipField().discard(); //refresh from the datasource
314
                getView().getExBasionymAuthorshipField().discard(); //refresh from the datasource
315
                getView().updateAuthorshipFields();
316

    
317
            }
318
            if(event.isRemovedType()){
319
                basionymSourceField.setValue(null);
320
                getView().updateAuthorshipFields();
321
            }
322

    
323
            basionymNamePopup = null;
324
            basionymSourceField = null;
325
        }
326
    }
327

    
328
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
329
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
330

    
331
        if(getView() == null || event.getSourceView() != getView() ){
332
            return;
333
        }
334

    
335
        basionymSourceField = (ReloadableLazyComboBox<TaxonName>)event.getSourceComponent();
336

    
337
        basionymNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
338
        basionymNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
339
        basionymNamePopup.withDeleteButton(true);
340
        getView().getModesActive().stream()
341
                .filter(
342
                        m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
343
                .forEach(m -> basionymNamePopup.enableMode(m));
344
        basionymNamePopup.loadInEditor(event.getEntityUuid());
345
        basionymNamePopup.getBasionymToggle().setVisible(false);
346

    
347
    }
348

    
349
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
350
    public void oTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
351

    
352
        if(getView() == null || event.getSourceView() != getView() ){
353
            return;
354
        }
355

    
356
        basionymSourceField = (AbstractField<TaxonName>)event.getSourceComponent();
357

    
358
        basionymNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
359
        basionymNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
360
        basionymNamePopup.withDeleteButton(true);
361
        getView().getModesActive().stream()
362
                .filter(
363
                        m -> !TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY.equals(m))
364
                .forEach(m -> basionymNamePopup.enableMode(m));
365
        basionymNamePopup.loadInEditor(null);
366
        basionymNamePopup.getBasionymToggle().setVisible(false);
367
    }
368

    
369
    /**
370
     * {@inheritDoc}
371
     */
372
    @Override
373
    protected TaxonNameDTO createDTODecorator(TaxonName cdmEntitiy) {
374
        return new TaxonNameDTO(cdmEntitiy);
375
    }
376

    
377

    
378

    
379

    
380
}
(9-9/13)