Project

General

Profile

Download (17.6 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.HashMap;
14
import java.util.HashSet;
15
import java.util.Map;
16
import java.util.Set;
17

    
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.context.annotation.Scope;
20
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
21
import org.vaadin.viritin.fields.AbstractElementCollection;
22

    
23
import com.vaadin.spring.annotation.SpringComponent;
24

    
25
import eu.etaxonomy.cdm.api.service.IRegistrationService;
26
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
27
import eu.etaxonomy.cdm.model.ICdmCacher;
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.CdmBase;
32
import eu.etaxonomy.cdm.model.location.Country;
33
import eu.etaxonomy.cdm.model.name.Registration;
34
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
35
import eu.etaxonomy.cdm.model.occurrence.Collection;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
38
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
39
import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
40
import eu.etaxonomy.cdm.service.CdmStore;
41
import eu.etaxonomy.cdm.service.ISpecimenTypeDesignationWorkingSetService;
42
import eu.etaxonomy.cdm.service.initstrategies.AgentBaseInit;
43
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
44
import eu.etaxonomy.cdm.vaadin.component.CollectionRowItemCollection;
45
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
46
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEventFilter;
47
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
48
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
49
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationTermLists;
50
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
51
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
52
import eu.etaxonomy.cdm.vaadin.permission.UserHelper;
53
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
54
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
55
import eu.etaxonomy.cdm.vaadin.view.occurrence.CollectionPopupEditor;
56
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
57
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityCombobox;
58
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
59
/**
60
 * SpecimenTypeDesignationWorkingsetPopupEditorView implementation must override the showInEditor() method,
61
 * see {@link #prepareAsFieldGroupDataSource()} for details.
62
 *
63
 * @author a.kohlbecker
64
 * @since Jun 13, 2017
65
 *
66
 */
67
@SpringComponent
68
@Scope("prototype")
69
public class SpecimenTypeDesignationWorkingsetEditorPresenter
70
    extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView>
71
    implements CachingPresenter {
72

    
73
    private static final long serialVersionUID = 4255636253714476918L;
74

    
75
    private static final EnumSet<CRUD> COLLECTION_EDITOR_CRUD = EnumSet.of(CRUD.UPDATE, CRUD.DELETE);
76

    
77
    CdmStore<Registration, IRegistrationService> store;
78

    
79

    
80
    /**
81
     * This object for this field will either be injected by the {@link PopupEditorFactory} or by a Spring
82
     * {@link BeanFactory}
83
     */
84
    @Autowired
85
    private ISpecimenTypeDesignationWorkingSetService specimenTypeDesignationWorkingSetService;
86

    
87
    /**
88
     * if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entitiy which will be
89
     * granted to the current use as long this grant is not assigned yet.
90
     */
91
    private EnumSet<CRUD> crud = null;
92

    
93
    private ICdmCacher cache;
94

    
95
    @Autowired
96
    protected CdmFilterablePagingProviderFactory pagingProviderFactory;
97

    
98
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
99

    
100
    private Map<CollectionPopupEditor, SpecimenTypeDesignationDTORow> collectionPopupEditorsRowMap = new HashMap<>();
101

    
102
    private Map<ReferencePopupEditor, SpecimenTypeDesignationDTORow> referencePopupEditorsRowMap = new HashMap<>();
103

    
104
    private Set<CollectionRowItemCollection> popuEditorTypeDesignationSourceRows = new HashSet<>();
105

    
106
    private java.util.Collection<CdmBase> rootEntities = new HashSet<>();
107

    
108
    protected CdmStore<Registration, IRegistrationService> getStore() {
109
        if(store == null){
110
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
111
        }
112
        return store;
113
    }
114

    
115
    /**
116
     * Loads an existing working set from the database. This process actually involves
117
     * loading the Registration specified by the <code>RegistrationAndWorkingsetId.registrationId</code> and in
118
     * a second step to find the workingset by the <code>registrationAndWorkingsetId.workingsetId</code>.
119
     * <p>
120
     * The <code>identifier</code> must be of the type {@link TypeDesignationWorkingsetEditorIdSet} whereas the field <code>egistrationId</code>
121
     * must be present, the field <code>workingsetId</code>,  however can be null. I this case a new workingset with a new {@link FieldUnit} as
122
     * base entity is being created.
123
     *
124
     * @param identifier a {@link TypeDesignationWorkingsetEditorIdSet}
125
     */
126
    @Override
127
    protected SpecimenTypeDesignationWorkingSetDTO<Registration> loadBeanById(Object identifier) {
128

    
129
        cache = new CdmTransientEntityCacher(this);
130
        if(identifier != null){
131

    
132
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
133

    
134
            if(idset.baseEntityRef != null){
135
                workingSetDto = specimenTypeDesignationWorkingSetService.load(idset.registrationUuid, idset.baseEntityRef);
136
                if(workingSetDto.getFieldUnit() == null){
137
                    workingSetDto = specimenTypeDesignationWorkingSetService.fixMissingFieldUnit(workingSetDto);
138
                        // FIXME open Dialog to warn user about adding an empty fieldUnit to the typeDesignations
139
                        //       This method must go again into the presenter !!!!
140
                        logger.info("Basing all typeDesignations on a new fieldUnit");
141
                }
142
                // need to use load but put see #7214
143
                cache.load(workingSetDto.getOwner());
144
                rootEntities.add(workingSetDto.getOwner());
145
            } else {
146
                // create a new workingset, for a new fieldunit which is the base for the workingset
147
                workingSetDto = specimenTypeDesignationWorkingSetService.create(idset.registrationUuid, idset.publicationUuid, idset.typifiedNameUuid);
148
                // need to use load but put see #7214
149
                cache.load(workingSetDto.getOwner());
150
                rootEntities.add(workingSetDto.getOwner());
151
            }
152

    
153
        } else {
154
            workingSetDto = null;
155
        }
156

    
157
        return workingSetDto;
158
    }
159

    
160

    
161
    /**
162
     * {@inheritDoc}
163
     */
164
    @SuppressWarnings("serial")
165
    @Override
166
    public void handleViewEntered() {
167

    
168
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
169
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
170

    
171
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
172
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
173
        termOrPersonPagingProvider.setInitStrategy(AgentBaseInit.TEAM_OR_PERSON_INIT_STRATEGY);
174
        getView().getCollectorField().setFilterablePersonPagingProvider(personPagingProvider, this);
175
        getView().getCollectorField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
176

    
177
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
178
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
179

    
180

    
181
        popuEditorTypeDesignationSourceRows.clear();
182
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
183

    
184
            CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
185

    
186
            CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = pagingProviderFactory.referencePagingProvider();
187

    
188
            @Override
189
            public SpecimenTypeDesignationDTORow create() {
190

    
191
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
192

    
193
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
194
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
195
                        );
196
                row.kindOfUnit.setNullSelectionAllowed(false);
197

    
198
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
199
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
200
                        );
201
                row.typeStatus.setNullSelectionAllowed(false);
202

    
203

    
204
                row.collection.loadFrom(
205
                        collectionPagingProvider,
206
                        collectionPagingProvider,
207
                        collectionPagingProvider.getPageSize()
208
                        );
209
                row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
210
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
211
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
212
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
213
                row.collection.addClickListenerAddEntity(e -> doCollectionEditorAdd(row));
214
                row.collection.addClickListenerEditEntity(e -> {
215
                        if(row.collection.getValue() != null){
216
                            doCollectionEditorEdit(row);
217
                        }
218
                    });
219

    
220
                row.mediaSpecimenReference.loadFrom(
221
                        referencePagingProvider,
222
                        referencePagingProvider,
223
                        collectionPagingProvider.getPageSize()
224
                        );
225

    
226
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
227
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
228
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
229
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
230
                row.mediaSpecimenReference.addClickListenerAddEntity(e -> doReferenceEditorAdd(row));
231
                row.mediaSpecimenReference.addClickListenerEditEntity(e -> {
232
                    if(row.mediaSpecimenReference.getValue() != null){
233
                        doReferenceEditorEdit(row);
234
                    }
235
                });
236

    
237
                getView().applyDefaultComponentStyle(row.components());
238

    
239
                popuEditorTypeDesignationSourceRows.add(row);
240

    
241
                return row;
242
            }
243

    
244
        });
245

    
246
    }
247

    
248

    
249
    /**
250
     * {@inheritDoc}
251
     */
252
    @Override
253
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
254

    
255
        if(crud != null){
256
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
257
        }
258

    
259
        specimenTypeDesignationWorkingSetService.save(dto);
260
    }
261

    
262
    /**
263
     * {@inheritDoc}
264
     */
265
    @Override
266
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
267
        specimenTypeDesignationWorkingSetService.delete(bean, true);
268
    }
269

    
270
    /**
271
     * @param element
272
     * @return
273
     */
274
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
275
        getView().updateAllowDelete();
276
    }
277

    
278

    
279
    /**
280
     * In this method the SpecimenTypeDesignation is dissociated from the Registration.
281
     * The actual deletion of the SpecimenTypeDesignation and DerivedUnit will take place in {@link #saveBean(SpecimenTypeDesignationWorkingSetDTO)}
282
     *
283
     * TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
284
     *
285
     * @param e
286
     * @return
287
     */
288
    private void deleteTypeDesignation(SpecimenTypeDesignationDTO element) {
289

    
290
        Registration reg = workingSetDto.getOwner();
291
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
292

    
293
        reg.getTypeDesignations().remove(std);
294

    
295
        getView().updateAllowDelete();
296
    }
297

    
298
    /**
299
     * @param crud
300
     */
301
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
302
        this.crud = crud;
303

    
304
    }
305

    
306
    /**
307
     * {@inheritDoc}
308
     */
309
    @Override
310
    public ICdmCacher getCache() {
311
        return cache;
312
    }
313

    
314
    public void doCollectionEditorAdd(SpecimenTypeDesignationDTORow row) {
315

    
316
        CollectionPopupEditor collectionPopupEditor = openPopupEditor(CollectionPopupEditor.class, null);
317

    
318
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
319
        collectionPopupEditor.withDeleteButton(true);
320
        collectionPopupEditor.loadInEditor(null);
321

    
322
        collectionPopupEditorsRowMap.put(collectionPopupEditor, row);
323
    }
324

    
325
    public void doCollectionEditorEdit(SpecimenTypeDesignationDTORow row) {
326

    
327
        CollectionPopupEditor collectionPopupEditor = openPopupEditor(CollectionPopupEditor.class, null);
328

    
329
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
330
        collectionPopupEditor.withDeleteButton(true);
331
        collectionPopupEditor.loadInEditor(row.collection.getValue().getUuid());
332

    
333
        collectionPopupEditorsRowMap.put(collectionPopupEditor, row);
334
    }
335

    
336

    
337
    @EventBusListenerMethod(filter = EntityChangeEventFilter.OccurrenceCollectionFilter.class)
338
    public void onCollectionEvent(EntityChangeEvent event){
339

    
340
        if(!isFromOwnView(event)){
341
            return;
342
        }
343

    
344
        Collection newCollection = getRepo().getCollectionService().load(
345
                event.getEntityUuid(), Arrays.asList(new String[]{"$.institute"})
346
                );
347
        cache.load(newCollection);
348

    
349
        if(event.isCreatedType()){
350
            SpecimenTypeDesignationDTORow row = collectionPopupEditorsRowMap.get(event.getSourceView());
351
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 3);
352
            combobox.setValue((Collection) event.getEntity());
353
        }
354
        for( CollectionRowItemCollection row : popuEditorTypeDesignationSourceRows) {
355
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 3);
356
            combobox.reload();
357
        }
358
    }
359

    
360
    public void doReferenceEditorAdd(SpecimenTypeDesignationDTORow row) {
361

    
362
        ReferencePopupEditor referencePopupEditor = openPopupEditor(ReferencePopupEditor.class, null);
363

    
364
        referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
365
        referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
366
        referencePopupEditor.withDeleteButton(true);
367
        referencePopupEditor.loadInEditor(null);
368

    
369
        referencePopupEditorsRowMap.put(referencePopupEditor, row);
370
    }
371

    
372
    public void doReferenceEditorEdit(SpecimenTypeDesignationDTORow row) {
373

    
374
        ReferencePopupEditor referencePopupEditor = openPopupEditor(ReferencePopupEditor.class, null);
375
        referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
376
        referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
377
        referencePopupEditor.withDeleteButton(true);
378
        referencePopupEditor.loadInEditor(row.mediaSpecimenReference.getValue().getUuid());
379

    
380
        referencePopupEditorsRowMap.put(referencePopupEditor, row);
381
    }
382

    
383
    @EventBusListenerMethod(filter = EntityChangeEventFilter.ReferenceFilter.class)
384
    public void onReferenceEvent(EntityChangeEvent event){
385

    
386
        Reference newRef = getRepo().getReferenceService().load(event.getEntityUuid(), Arrays.asList(new String[]{"$"}));
387
        cache.load(newRef);
388

    
389
        if(event.isCreatedType()){
390
            SpecimenTypeDesignationDTORow row = referencePopupEditorsRowMap.get(event.getSourceView());
391
            ToOneRelatedEntityCombobox<Reference> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 7);
392
            combobox.setValue((Reference) event.getEntity());
393

    
394
        } else {
395
            for( CollectionRowItemCollection row : popuEditorTypeDesignationSourceRows) {
396
                ToOneRelatedEntityCombobox<Reference> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 7);
397
                combobox.reload();
398
            }
399
        }
400
    }
401

    
402
    /**
403
     * {@inheritDoc}
404
     */
405
    @Override
406
    public void addRootEntity(CdmBase entity) {
407
        rootEntities.add(entity);
408
    }
409

    
410
    /**
411
     * {@inheritDoc}
412
     */
413
    @Override
414
    public java.util.Collection<CdmBase> getRootEntities() {
415
        return rootEntities ;
416
    }
417

    
418

    
419

    
420
}
(6-6/13)