Project

General

Profile

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

    
71
    private static final long serialVersionUID = 4255636253714476918L;
72

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

    
75
    CdmStore<Registration, IRegistrationService> store;
76

    
77

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

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

    
91
    private ICdmCacher cache;
92

    
93
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
94

    
95
    private CollectionPopupEditor collectionPopupEditor;
96

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

    
99
    private ReferencePopupEditor referencePopupEditor;
100

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

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

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

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

    
127
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
128

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

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

    
152
        return workingSetDto;
153
    }
154

    
155

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

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

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

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

    
175

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

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

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

    
183
            @Override
184
            public SpecimenTypeDesignationDTORow create() {
185

    
186
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
187

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

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

    
198

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

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

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

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

    
234
                collectionPopuEditorSourceRows.add(row);
235

    
236
                return row;
237
            }
238

    
239
        });
240

    
241
    }
242

    
243

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

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

    
254
        specimenTypeDesignationWorkingSetService.save(dto);
255
    }
256

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

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

    
273

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

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

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

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

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

    
299
    }
300

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

    
309
    public void doCollectionEditorAdd() {
310

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

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

    
318
    public void doCollectionEditorEdit(UUID collectionUuid) {
319

    
320
        collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class, getView());
321

    
322
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
323
        collectionPopupEditor.withDeleteButton(true);
324
        collectionPopupEditor.loadInEditor(collectionUuid);
325
    }
326

    
327

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

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

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

    
342
    public void doReferenceEditorAdd() {
343

    
344
        referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
345

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

    
352
    public void doReferenceEditorEdit(UUID referenceUuid) {
353

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

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

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

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

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

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

    
389

    
390

    
391
}
(6-6/13)