Project

General

Profile

Download (17.5 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.CdmStore;
40
import eu.etaxonomy.cdm.service.ISpecimenTypeDesignationWorkingSetService;
41
import eu.etaxonomy.cdm.service.initstrategies.AgentBaseInit;
42
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
43
import eu.etaxonomy.cdm.vaadin.component.CollectionRowItemCollection;
44
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
45
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEventFilter;
46
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
47
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
48
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationTermLists;
49
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
50
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
51
import eu.etaxonomy.cdm.vaadin.permission.UserHelper;
52
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
53
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
54
import eu.etaxonomy.cdm.vaadin.view.occurrence.CollectionPopupEditor;
55
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
56
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityCombobox;
57
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
58
/**
59
 * SpecimenTypeDesignationWorkingsetPopupEditorView implementation must override the showInEditor() method,
60
 * see {@link #prepareAsFieldGroupDataSource()} for details.
61
 *
62
 * @author a.kohlbecker
63
 * @since Jun 13, 2017
64
 *
65
 */
66
@SpringComponent
67
@Scope("prototype")
68
public class SpecimenTypeDesignationWorkingsetEditorPresenter
69
    extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView>
70
    implements CachingPresenter {
71

    
72
    private static final long serialVersionUID = 4255636253714476918L;
73

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

    
76
    CdmStore<Registration, IRegistrationService> store;
77

    
78

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

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

    
92
    private ICdmCacher cache;
93

    
94
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
95

    
96
    private Map<CollectionPopupEditor, SpecimenTypeDesignationDTORow> collectionPopupEditorsRowMap = new HashMap<>();
97

    
98
    private Map<ReferencePopupEditor, SpecimenTypeDesignationDTORow> referencePopupEditorsRowMap = new HashMap<>();
99

    
100
    private Set<CollectionRowItemCollection> popuEditorTypeDesignationSourceRows = new HashSet<>();
101

    
102
    private java.util.Collection<CdmBase> rootEntities = new HashSet<>();
103

    
104
    protected CdmStore<Registration, IRegistrationService> getStore() {
105
        if(store == null){
106
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
107
        }
108
        return store;
109
    }
110

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

    
125
        cache = new CdmTransientEntityCacher(this);
126
        if(identifier != null){
127

    
128
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
129

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

    
149
        } else {
150
            workingSetDto = null;
151
        }
152

    
153
        return workingSetDto;
154
    }
155

    
156

    
157
    /**
158
     * {@inheritDoc}
159
     */
160
    @SuppressWarnings("serial")
161
    @Override
162
    public void handleViewEntered() {
163

    
164
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
165
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
166

    
167
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
168
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
169
        termOrPersonPagingProvider.setInitStrategy(AgentBaseInit.TEAM_OR_PERSON_INIT_STRATEGY);
170
        getView().getCollectorField().setFilterablePersonPagingProvider(personPagingProvider, this);
171
        getView().getCollectorField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
172

    
173
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
174
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
175

    
176

    
177
        popuEditorTypeDesignationSourceRows.clear();
178
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
179

    
180
            CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
181

    
182
            CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
183

    
184
            @Override
185
            public SpecimenTypeDesignationDTORow create() {
186

    
187
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
188

    
189
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
190
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
191
                        );
192
                row.kindOfUnit.setNullSelectionAllowed(false);
193

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

    
199

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

    
216
                row.mediaSpecimenReference.loadFrom(
217
                        referencePagingProvider,
218
                        referencePagingProvider,
219
                        collectionPagingProvider.getPageSize()
220
                        );
221

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

    
233
                getView().applyDefaultComponentStyle(row.components());
234

    
235
                popuEditorTypeDesignationSourceRows.add(row);
236

    
237
                return row;
238
            }
239

    
240
        });
241

    
242
    }
243

    
244

    
245
    /**
246
     * {@inheritDoc}
247
     */
248
    @Override
249
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
250

    
251
        if(crud != null){
252
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
253
        }
254

    
255
        specimenTypeDesignationWorkingSetService.save(dto);
256
    }
257

    
258
    /**
259
     * {@inheritDoc}
260
     */
261
    @Override
262
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
263
        specimenTypeDesignationWorkingSetService.delete(bean, true);
264
    }
265

    
266
    /**
267
     * @param element
268
     * @return
269
     */
270
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
271
        getView().updateAllowDelete();
272
    }
273

    
274

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

    
286
        Registration reg = workingSetDto.getOwner();
287
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
288

    
289
        reg.getTypeDesignations().remove(std);
290

    
291
        getView().updateAllowDelete();
292
    }
293

    
294
    /**
295
     * @param crud
296
     */
297
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
298
        this.crud = crud;
299

    
300
    }
301

    
302
    /**
303
     * {@inheritDoc}
304
     */
305
    @Override
306
    public ICdmCacher getCache() {
307
        return cache;
308
    }
309

    
310
    public void doCollectionEditorAdd(SpecimenTypeDesignationDTORow row) {
311

    
312
        CollectionPopupEditor collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class, getView());
313

    
314
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
315
        collectionPopupEditor.withDeleteButton(true);
316
        collectionPopupEditor.loadInEditor(null);
317

    
318
        collectionPopupEditorsRowMap.put(collectionPopupEditor, row);
319
    }
320

    
321
    public void doCollectionEditorEdit(SpecimenTypeDesignationDTORow row) {
322

    
323
        CollectionPopupEditor collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class, getView());
324

    
325
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
326
        collectionPopupEditor.withDeleteButton(true);
327
        collectionPopupEditor.loadInEditor(row.collection.getValue().getUuid());
328

    
329
        collectionPopupEditorsRowMap.put(collectionPopupEditor, row);
330
    }
331

    
332

    
333
    @EventBusListenerMethod(filter = EntityChangeEventFilter.OccurrenceCollectionFilter.class)
334
    public void onCollectionEvent(EntityChangeEvent event){
335

    
336
        Collection newCollection = getRepo().getCollectionService().load(
337
                event.getEntityUuid(), Arrays.asList(new String[]{"$.institute"})
338
                );
339
        cache.load(newCollection);
340

    
341
        if(event.isCreatedType()){
342
            SpecimenTypeDesignationDTORow row = collectionPopupEditorsRowMap.get(event.getSourceView());
343
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 3);
344
            combobox.setValue((Collection) event.getEntity());
345
        }
346
        for( CollectionRowItemCollection row : popuEditorTypeDesignationSourceRows) {
347
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 3);
348
            combobox.reload();
349
        }
350
    }
351

    
352
    public void doReferenceEditorAdd(SpecimenTypeDesignationDTORow row) {
353

    
354
        ReferencePopupEditor referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
355

    
356
        referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
357
        referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
358
        referencePopupEditor.withDeleteButton(true);
359
        referencePopupEditor.loadInEditor(null);
360

    
361
        referencePopupEditorsRowMap.put(referencePopupEditor, row);
362
    }
363

    
364
    public void doReferenceEditorEdit(SpecimenTypeDesignationDTORow row) {
365

    
366
        ReferencePopupEditor referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
367
        referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
368
        referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
369
        referencePopupEditor.withDeleteButton(true);
370
        referencePopupEditor.loadInEditor(row.mediaSpecimenReference.getValue().getUuid());
371

    
372
        referencePopupEditorsRowMap.put(referencePopupEditor, row);
373
    }
374

    
375
    @EventBusListenerMethod(filter = EntityChangeEventFilter.ReferenceFilter.class)
376
    public void onReferenceEvent(EntityChangeEvent event){
377

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

    
381
        if(event.isCreatedType()){
382
            SpecimenTypeDesignationDTORow row = referencePopupEditorsRowMap.get(event.getSourceView());
383
            ToOneRelatedEntityCombobox<Reference> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 7);
384
            combobox.setValue((Reference) event.getEntity());
385

    
386
        } else {
387
            for( CollectionRowItemCollection row : popuEditorTypeDesignationSourceRows) {
388
                ToOneRelatedEntityCombobox<Reference> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 7);
389
                combobox.reload();
390
            }
391
        }
392
    }
393

    
394
    /**
395
     * {@inheritDoc}
396
     */
397
    @Override
398
    public void addRootEntity(CdmBase entity) {
399
        rootEntities.add(entity);
400
    }
401

    
402
    /**
403
     * {@inheritDoc}
404
     */
405
    @Override
406
    public java.util.Collection<CdmBase> getRootEntities() {
407
        return rootEntities ;
408
    }
409

    
410

    
411

    
412
}
(6-6/13)