Project

General

Profile

Download (15.6 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.event.EventListener;
18
import org.vaadin.viritin.fields.AbstractElementCollection;
19

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

    
63
    private static final long serialVersionUID = 4255636253714476918L;
64

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

    
67
    CdmStore<Registration, IRegistrationService> store;
68

    
69

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

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

    
83
    private CdmEntityCache cache = null;
84

    
85
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
86

    
87
    private CollectionPopupEditor collectionPopupEditor;
88

    
89
    private Set<CollectionRowItemCollection> collectionPopuEditorSourceRows = new HashSet<>();
90

    
91
    private ReferencePopupEditor referencePopupEditor;
92

    
93
    protected CdmStore<Registration, IRegistrationService> getStore() {
94
        if(store == null){
95
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
96
        }
97
        return store;
98
    }
99

    
100

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

    
115
        if(identifier != null){
116

    
117
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
118

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

    
134
        } else {
135
            workingSetDto = null;
136
        }
137

    
138
        return workingSetDto;
139
    }
140

    
141

    
142
    /**
143
     * {@inheritDoc}
144
     */
145
    @SuppressWarnings("serial")
146
    @Override
147
    public void handleViewEntered() {
148

    
149
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
150
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
151

    
152
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
153
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
154
        getView().getCollectorField().setFilterablePersonPagingProvider(personPagingProvider, this);
155
        getView().getCollectorField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
156

    
157
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
158
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
159

    
160

    
161
        collectionPopuEditorSourceRows.clear();
162
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
163

    
164
            CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
165

    
166
            CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
167

    
168
            @Override
169
            public SpecimenTypeDesignationDTORow create() {
170

    
171
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
172

    
173
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
174
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
175
                        );
176
                row.kindOfUnit.setNullSelectionAllowed(false);
177

    
178
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
179
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
180
                        );
181
                row.typeStatus.setNullSelectionAllowed(false);
182

    
183

    
184
                row.collection.loadFrom(
185
                        collectionPagingProvider,
186
                        collectionPagingProvider,
187
                        collectionPagingProvider.getPageSize()
188
                        );
189
                row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
190
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
191
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
192
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
193
                row.collection.addClickListenerAddEntity(e -> doCollectionEditorAdd());
194
                row.collection.addClickListenerEditEntity(e -> {
195
                        if(row.collection.getValue() != null){
196
                            doCollectionEditorEdit(row.collection.getValue().getId());
197
                        }
198
                    });
199

    
200
                row.mediaSpecimenReference.loadFrom(
201
                        referencePagingProvider,
202
                        referencePagingProvider,
203
                        collectionPagingProvider.getPageSize()
204
                        );
205

    
206
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
207
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
208
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
209
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
210
                row.mediaSpecimenReference.addClickListenerAddEntity(e -> doReferenceEditorAdd());
211
                row.mediaSpecimenReference.addClickListenerEditEntity(e -> {
212
                    if(row.mediaSpecimenReference.getValue() != null){
213
                        doReferenceEditorEdit(row.mediaSpecimenReference.getValue().getId());
214
                    }
215
                });
216

    
217
                getView().applyDefaultComponentStyle(row.components());
218

    
219
                collectionPopuEditorSourceRows.add(row);
220

    
221
                return row;
222
            }
223

    
224
        });
225

    
226
    }
227

    
228

    
229
    /**
230
     * {@inheritDoc}
231
     */
232
    @Override
233
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
234

    
235
        if(crud != null){
236
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
237
        }
238

    
239
        specimenTypeDesignationWorkingSetService.save(dto);
240
    }
241

    
242
    /**
243
     * {@inheritDoc}
244
     */
245
    @Override
246
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
247
        specimenTypeDesignationWorkingSetService.delete(bean, true);
248
    }
249

    
250
    /**
251
     * @param element
252
     * @return
253
     */
254
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
255
        getView().updateAllowDelete();
256
    }
257

    
258

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

    
270
        Registration reg = workingSetDto.getOwner();
271
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
272

    
273
        reg.getTypeDesignations().remove(std);
274

    
275
        getView().updateAllowDelete();
276
    }
277

    
278
    /**
279
     * @param crud
280
     */
281
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
282
        this.crud = crud;
283

    
284
    }
285

    
286
    /**
287
     * {@inheritDoc}
288
     */
289
    @Override
290
    public EntityCache getCache() {
291
        return cache;
292
//        if(((AbstractPopupEditor)getView()).isBeanLoaded()){
293
//        } else {
294
//            return null;
295
//        }
296
    }
297

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

    
308

    
309
    public void doCollectionEditorAdd() {
310

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

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

    
318
    public void doCollectionEditorEdit(int collectionId) {
319

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

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

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

    
330
        Collection newCollection = getRepo().getCollectionService().load(event.getEntityId(), Arrays.asList(new String[]{"$.institute"}));
331
        cache.findAndUpdate(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
    @EventListener(condition = "#event.entityType == T(eu.etaxonomy.cdm.model.reference.Reference)")
359
    public void onReferenceEvent(EntityChangeEvent event){
360

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

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

    
370

    
371

    
372
}
(3-3/10)