Project

General

Profile

Download (15.8 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.ArrayList;
12
import java.util.Arrays;
13
import java.util.EnumSet;
14
import java.util.HashSet;
15
import java.util.Set;
16

    
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.context.event.EventListener;
19
import org.vaadin.viritin.fields.AbstractElementCollection;
20

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

    
65
    private static final long serialVersionUID = 4255636253714476918L;
66

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

    
69
    CdmStore<Registration, IRegistrationService> store;
70

    
71

    
72
    /**
73
     * This object for this field will either be injected by the {@link PopupEditorFactory} or by a Spring
74
     * {@link BeanFactory}
75
     */
76
    @Autowired
77
    private ISpecimenTypeDesignationWorkingSetService specimenTypeDesignationWorkingSetService;
78

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

    
85
    private ICdmCacher cache = new CdmTransientEntityCacher(this);
86

    
87
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
88

    
89
    private CollectionPopupEditor collectionPopupEditor;
90

    
91
    private Set<CollectionRowItemCollection> collectionPopuEditorSourceRows = new HashSet<>();
92

    
93
    private ReferencePopupEditor referencePopupEditor;
94

    
95
    private java.util.Collection<CdmBase> rootEntities = new ArrayList<>();
96

    
97
    protected CdmStore<Registration, IRegistrationService> getStore() {
98
        if(store == null){
99
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
100
        }
101
        return store;
102
    }
103

    
104

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

    
119
        if(identifier != null){
120

    
121
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
122

    
123
            if(idset.workingsetId != null){
124
                workingSetDto = specimenTypeDesignationWorkingSetService.loadDtoByIds(idset.registrationId, idset.workingsetId);
125
                if(workingSetDto.getFieldUnit() == null){
126
                    workingSetDto = specimenTypeDesignationWorkingSetService.fixMissingFieldUnit(workingSetDto);
127
                        // FIXME open Dialog to warn user about adding an empty fieldUnit to the typeDesignations
128
                        //       This method must go again into the presenter !!!!
129
                        logger.info("Basing all typeDesignations on a new fieldUnit");
130
                }
131
                cache.put(workingSetDto.getOwner());
132
                rootEntities.add(workingSetDto.getOwner());
133
            } else {
134
                // create a new workingset, for a new fieldunit which is the base for the workingset
135
                workingSetDto = specimenTypeDesignationWorkingSetService.create(idset.registrationId, idset.publicationId, idset.typifiedNameId);
136
                cache.put(workingSetDto.getOwner());
137
                rootEntities.add(workingSetDto.getOwner());
138
            }
139

    
140
        } else {
141
            workingSetDto = null;
142
        }
143

    
144
        return workingSetDto;
145
    }
146

    
147

    
148
    /**
149
     * {@inheritDoc}
150
     */
151
    @SuppressWarnings("serial")
152
    @Override
153
    public void handleViewEntered() {
154

    
155
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
156
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
157

    
158
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
159
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
160
        getView().getCollectorField().setFilterablePersonPagingProvider(personPagingProvider, this);
161
        getView().getCollectorField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
162

    
163
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
164
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
165

    
166

    
167
        collectionPopuEditorSourceRows.clear();
168
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
169

    
170
            CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
171

    
172
            CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
173

    
174
            @Override
175
            public SpecimenTypeDesignationDTORow create() {
176

    
177
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
178

    
179
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
180
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
181
                        );
182
                row.kindOfUnit.setNullSelectionAllowed(false);
183

    
184
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
185
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
186
                        );
187
                row.typeStatus.setNullSelectionAllowed(false);
188

    
189

    
190
                row.collection.loadFrom(
191
                        collectionPagingProvider,
192
                        collectionPagingProvider,
193
                        collectionPagingProvider.getPageSize()
194
                        );
195
                row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
196
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
197
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
198
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
199
                row.collection.addClickListenerAddEntity(e -> doCollectionEditorAdd());
200
                row.collection.addClickListenerEditEntity(e -> {
201
                        if(row.collection.getValue() != null){
202
                            doCollectionEditorEdit(row.collection.getValue().getId());
203
                        }
204
                    });
205

    
206
                row.mediaSpecimenReference.loadFrom(
207
                        referencePagingProvider,
208
                        referencePagingProvider,
209
                        collectionPagingProvider.getPageSize()
210
                        );
211

    
212
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
213
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
214
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
215
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
216
                row.mediaSpecimenReference.addClickListenerAddEntity(e -> doReferenceEditorAdd());
217
                row.mediaSpecimenReference.addClickListenerEditEntity(e -> {
218
                    if(row.mediaSpecimenReference.getValue() != null){
219
                        doReferenceEditorEdit(row.mediaSpecimenReference.getValue().getId());
220
                    }
221
                });
222

    
223
                getView().applyDefaultComponentStyle(row.components());
224

    
225
                collectionPopuEditorSourceRows.add(row);
226

    
227
                return row;
228
            }
229

    
230
        });
231

    
232
    }
233

    
234

    
235
    /**
236
     * {@inheritDoc}
237
     */
238
    @Override
239
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
240

    
241
        if(crud != null){
242
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
243
        }
244

    
245
        specimenTypeDesignationWorkingSetService.save(dto);
246
    }
247

    
248
    /**
249
     * {@inheritDoc}
250
     */
251
    @Override
252
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
253
        specimenTypeDesignationWorkingSetService.delete(bean, true);
254
    }
255

    
256
    /**
257
     * @param element
258
     * @return
259
     */
260
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
261
        getView().updateAllowDelete();
262
    }
263

    
264

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

    
276
        Registration reg = workingSetDto.getOwner();
277
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
278

    
279
        reg.getTypeDesignations().remove(std);
280

    
281
        getView().updateAllowDelete();
282
    }
283

    
284
    /**
285
     * @param crud
286
     */
287
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
288
        this.crud = crud;
289

    
290
    }
291

    
292
    /**
293
     * {@inheritDoc}
294
     */
295
    @Override
296
    public ICdmCacher getCache() {
297
        return cache;
298
    }
299

    
300
    /**
301
     * Returns true only after the view method {@link AbstractPopupEditor#loadInEditor()}
302
     * has completed.
303
     *
304
     */
305
    @Override
306
    public boolean isCacheInitialized() {
307
        return ((AbstractPopupEditor)getView()).isBeanLoaded();
308
    }
309

    
310

    
311
    public void doCollectionEditorAdd() {
312

    
313
        collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
314

    
315
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
316
        collectionPopupEditor.withDeleteButton(true);
317
        collectionPopupEditor.loadInEditor(null);
318
    }
319

    
320
    public void doCollectionEditorEdit(int collectionId) {
321

    
322
        collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
323

    
324
        collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
325
        collectionPopupEditor.withDeleteButton(true);
326
        collectionPopupEditor.loadInEditor(collectionId);
327
    }
328

    
329
    @EventListener(condition = "#event.entityType == T(eu.etaxonomy.cdm.model.occurrence.Collection)")
330
    public void onCollectionEvent(EntityChangeEvent event){
331

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

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

    
341
    public void doReferenceEditorAdd() {
342

    
343
        referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class);
344

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

    
351
    public void doReferenceEditorEdit(int referenceId) {
352

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

    
360
    @EventListener(condition = "#event.entityType == T(eu.etaxonomy.cdm.model.reference.Reference)")
361
    public void onReferenceEvent(EntityChangeEvent event){
362

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

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

    
372

    
373
    /**
374
     * {@inheritDoc}
375
     */
376
    @Override
377
    public java.util.Collection<CdmBase> getRootEntities() {
378
        return rootEntities ;
379
    }
380

    
381

    
382

    
383
}
(3-3/10)