Project

General

Profile

Download (16 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

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

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

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

    
69
    private static final long serialVersionUID = 4255636253714476918L;
70

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

    
73
    CdmStore<Registration, IRegistrationService> store;
74

    
75

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

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

    
89
    private ICdmCacher cache;
90

    
91
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
92

    
93
    private CollectionPopupEditor collectionPopupEditor;
94

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

    
97
    private ReferencePopupEditor referencePopupEditor;
98

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

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

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

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

    
125
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
126

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

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

    
150
        return workingSetDto;
151
    }
152

    
153

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

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

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

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

    
172

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

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

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

    
180
            @Override
181
            public SpecimenTypeDesignationDTORow create() {
182

    
183
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
184

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

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

    
195

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

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

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

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

    
231
                collectionPopuEditorSourceRows.add(row);
232

    
233
                return row;
234
            }
235

    
236
        });
237

    
238
    }
239

    
240

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

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

    
251
        specimenTypeDesignationWorkingSetService.save(dto);
252
    }
253

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

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

    
270

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

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

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

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

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

    
296
    }
297

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

    
306
    public void doCollectionEditorAdd() {
307

    
308
        collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
309

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

    
315
    public void doCollectionEditorEdit(int collectionId) {
316

    
317
        collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
318

    
319
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
320
        collectionPopupEditor.withDeleteButton(true);
321
        collectionPopupEditor.loadInEditor(collectionId);
322
    }
323

    
324

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

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

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

    
339
    public void doReferenceEditorAdd() {
340

    
341
        referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class);
342

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

    
349
    public void doReferenceEditorEdit(int referenceId) {
350

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

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

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

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

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

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

    
386

    
387

    
388
}
(6-6/13)