Project

General

Profile

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

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

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

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

    
70
    private static final long serialVersionUID = 4255636253714476918L;
71

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

    
74
    CdmStore<Registration, IRegistrationService> store;
75

    
76

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

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

    
90
    private ICdmCacher cache;
91

    
92
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
93

    
94
    private CollectionPopupEditor collectionPopupEditor;
95

    
96
    private Set<CollectionRowItemCollection> collectionPopuEditorSourceRows = new HashSet<>();
97

    
98
    private ReferencePopupEditor referencePopupEditor;
99

    
100
    private java.util.Collection<CdmBase> rootEntities = new HashSet<>();
101

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

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

    
123
        cache = new CdmTransientEntityCacher(this);
124
        if(identifier != null){
125

    
126
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
127

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

    
147
        } else {
148
            workingSetDto = null;
149
        }
150

    
151
        return workingSetDto;
152
    }
153

    
154

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

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

    
165
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
166
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
167
        getView().getCollectorField().setFilterablePersonPagingProvider(personPagingProvider, this);
168
        getView().getCollectorField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
169

    
170
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
171
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
172

    
173

    
174
        collectionPopuEditorSourceRows.clear();
175
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
176

    
177
            CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
178

    
179
            CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
180

    
181
            @Override
182
            public SpecimenTypeDesignationDTORow create() {
183

    
184
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
185

    
186
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
187
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
188
                        );
189
                row.kindOfUnit.setNullSelectionAllowed(false);
190

    
191
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
192
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
193
                        );
194
                row.typeStatus.setNullSelectionAllowed(false);
195

    
196

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

    
213
                row.mediaSpecimenReference.loadFrom(
214
                        referencePagingProvider,
215
                        referencePagingProvider,
216
                        collectionPagingProvider.getPageSize()
217
                        );
218

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

    
230
                getView().applyDefaultComponentStyle(row.components());
231

    
232
                collectionPopuEditorSourceRows.add(row);
233

    
234
                return row;
235
            }
236

    
237
        });
238

    
239
    }
240

    
241

    
242
    /**
243
     * {@inheritDoc}
244
     */
245
    @Override
246
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
247

    
248
        if(crud != null){
249
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
250
        }
251

    
252
        specimenTypeDesignationWorkingSetService.save(dto);
253
    }
254

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

    
263
    /**
264
     * @param element
265
     * @return
266
     */
267
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
268
        getView().updateAllowDelete();
269
    }
270

    
271

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

    
283
        Registration reg = workingSetDto.getOwner();
284
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
285

    
286
        reg.getTypeDesignations().remove(std);
287

    
288
        getView().updateAllowDelete();
289
    }
290

    
291
    /**
292
     * @param crud
293
     */
294
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
295
        this.crud = crud;
296

    
297
    }
298

    
299
    /**
300
     * {@inheritDoc}
301
     */
302
    @Override
303
    public ICdmCacher getCache() {
304
        return cache;
305
    }
306

    
307
    public void doCollectionEditorAdd() {
308

    
309
        collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class, getView());
310

    
311
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
312
        collectionPopupEditor.withDeleteButton(true);
313
        collectionPopupEditor.loadInEditor(null);
314
    }
315

    
316
    public void doCollectionEditorEdit(UUID collectionUuid) {
317

    
318
        collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class, getView());
319

    
320
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
321
        collectionPopupEditor.withDeleteButton(true);
322
        collectionPopupEditor.loadInEditor(collectionUuid);
323
    }
324

    
325

    
326
    @EventBusListenerMethod(filter = EntityChangeEventFilter.OccurrenceCollectionFilter.class)
327
    public void onCollectionEvent(EntityChangeEvent event){
328

    
329
        Collection newCollection = getRepo().getCollectionService().load(
330
                event.getEntityUuid(), Arrays.asList(new String[]{"$.institute"})
331
                );
332
        cache.load(newCollection);
333

    
334
        for( CollectionRowItemCollection row : collectionPopuEditorSourceRows) {
335
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 2);
336
            combobox.reload();
337
        }
338
    }
339

    
340
    public void doReferenceEditorAdd() {
341

    
342
        referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
343

    
344
        referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
345
        referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
346
        referencePopupEditor.withDeleteButton(true);
347
        referencePopupEditor.loadInEditor(null);
348
    }
349

    
350
    public void doReferenceEditorEdit(UUID referenceUuid) {
351

    
352
        referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
353
        referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
354
        referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
355
        referencePopupEditor.withDeleteButton(true);
356
        referencePopupEditor.loadInEditor(referenceUuid);
357
    }
358

    
359
    @EventBusListenerMethod(filter = EntityChangeEventFilter.ReferenceFilter.class)
360
    public void onReferenceEvent(EntityChangeEvent event){
361

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

    
365
        for( CollectionRowItemCollection row : collectionPopuEditorSourceRows) {
366
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 6);
367
            combobox.reload();
368
        }
369
    }
370

    
371
    /**
372
     * {@inheritDoc}
373
     */
374
    @Override
375
    public void addRootEntity(CdmBase entity) {
376
        rootEntities.add(entity);
377
    }
378

    
379
    /**
380
     * {@inheritDoc}
381
     */
382
    @Override
383
    public java.util.Collection<CdmBase> getRootEntities() {
384
        return rootEntities ;
385
    }
386

    
387

    
388

    
389
}
(6-6/13)