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
                cache.load(workingSetDto.getTypifiedName());
151
                rootEntities.add(workingSetDto.getOwner());
152
            }
153

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

    
158
        return workingSetDto;
159
    }
160

    
161

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

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

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

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

    
181

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

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

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

    
189
            @Override
190
            public SpecimenTypeDesignationDTORow create() {
191

    
192
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
193

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

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

    
204

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

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

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

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

    
240
                popuEditorTypeDesignationSourceRows.add(row);
241

    
242
                return row;
243
            }
244

    
245
        });
246

    
247
    }
248

    
249

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

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

    
260
        specimenTypeDesignationWorkingSetService.save(dto);
261
    }
262

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

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

    
279

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

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

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

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

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

    
305
    }
306

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

    
315
    public void doCollectionEditorAdd(SpecimenTypeDesignationDTORow row) {
316

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

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

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

    
326
    public void doCollectionEditorEdit(SpecimenTypeDesignationDTORow row) {
327

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

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

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

    
337

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

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

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

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

    
361
    public void doReferenceEditorAdd(SpecimenTypeDesignationDTORow row) {
362

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

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

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

    
373
    public void doReferenceEditorEdit(SpecimenTypeDesignationDTORow row) {
374

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

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

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

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

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

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

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

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

    
419

    
420

    
421
}
(6-6/13)