Project

General

Profile

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

    
58
    private static final long serialVersionUID = 4255636253714476918L;
59

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

    
62
    CdmStore<Registration, IRegistrationService> store;
63

    
64

    
65
    /**
66
     * This object for this field will either be injected by the {@link PopupEditorFactory} or by a Spring
67
     * {@link BeanFactory}
68
     */
69
    @Autowired
70
    private ISpecimenTypeDesignationWorkingSetService specimenTypeDesignationWorkingSetService;
71

    
72
    /**
73
     * if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entitiy which will be
74
     * granted to the current use as long this grant is not assigned yet.
75
     */
76
    private EnumSet<CRUD> crud = null;
77

    
78
    private CdmEntityCache cache = null;
79

    
80
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
81

    
82
    private CollectionPopupEditor collectionPopuEditor;
83

    
84
    private Set<CollectionRowItemCollection> collectionPopuEditorSourceRows = new HashSet<>();
85

    
86
    protected CdmStore<Registration, IRegistrationService> getStore() {
87
        if(store == null){
88
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
89
        }
90
        return store;
91
    }
92

    
93

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

    
108
        if(identifier != null){
109

    
110
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
111

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

    
127
        } else {
128
            workingSetDto = null;
129
        }
130

    
131
        return workingSetDto;
132
    }
133

    
134

    
135
    /**
136
     * {@inheritDoc}
137
     */
138
    @SuppressWarnings("serial")
139
    @Override
140
    public void handleViewEntered() {
141

    
142
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
143
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
144

    
145
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
146
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
147

    
148

    
149
        collectionPopuEditorSourceRows.clear();
150
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
151

    
152
            CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
153

    
154
            CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
155

    
156
            @Override
157
            public SpecimenTypeDesignationDTORow create() {
158

    
159
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
160

    
161
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
162
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
163
                        );
164
                row.kindOfUnit.setNullSelectionAllowed(false);
165

    
166
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
167
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
168
                        );
169
                row.typeStatus.setNullSelectionAllowed(false);
170

    
171

    
172
                row.collection.loadFrom(
173
                        collectionPagingProvider,
174
                        collectionPagingProvider,
175
                        collectionPagingProvider.getPageSize()
176
                        );
177
                row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
178
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
179
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
180
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
181
                row.collection.addClickListenerAddEntity( e -> doCollectionEditorAdd());
182
                row.collection.addClickListenerEditEntity(e -> {
183
                        if(row.collection.getValue() != null){
184
                            doCollectionEditorEdit(row.collection.getValue().getId());
185
                        }
186
                    });
187

    
188
                row.mediaSpecimenReference.loadFrom(
189
                        referencePagingProvider,
190
                        referencePagingProvider,
191
                        collectionPagingProvider.getPageSize()
192
                        );
193

    
194
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
195
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
196
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
197
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
198

    
199
                getView().applyDefaultComponentStyle(row.components());
200

    
201
                collectionPopuEditorSourceRows.add(row);
202

    
203
                return row;
204
            }
205

    
206
        });
207

    
208
    }
209

    
210

    
211
    /**
212
     * {@inheritDoc}
213
     */
214
    @Override
215
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
216

    
217
        if(crud != null){
218
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
219
        }
220

    
221
        specimenTypeDesignationWorkingSetService.save(dto);
222
    }
223

    
224
    /**
225
     * {@inheritDoc}
226
     */
227
    @Override
228
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
229
        specimenTypeDesignationWorkingSetService.delete(bean, true);
230
    }
231

    
232
    /**
233
     * @param element
234
     * @return
235
     */
236
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
237
        getView().updateAllowDelete();
238
    }
239

    
240

    
241
    /**
242
     * In this method the SpecimenTypeDesignation is dissociated from the Registration.
243
     * The actual deletion of the SpecimenTypeDesignation and DerivedUnit will take place in {@link #saveBean(SpecimenTypeDesignationWorkingSetDTO)}
244
     *
245
     * TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
246
     *
247
     * @param e
248
     * @return
249
     */
250
    private void deleteTypeDesignation(SpecimenTypeDesignationDTO element) {
251

    
252
        Registration reg = workingSetDto.getOwner();
253
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
254

    
255
        reg.getTypeDesignations().remove(std);
256

    
257
        getView().updateAllowDelete();
258
    }
259

    
260
    /**
261
     * @param crud
262
     */
263
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
264
        this.crud = crud;
265

    
266
    }
267

    
268
    /**
269
     * {@inheritDoc}
270
     */
271
    @Override
272
    public EntityCache getCache() {
273
        if(((AbstractPopupEditor)getView()).isBeanLoaded()){
274
            return cache;
275
        } else {
276
            return null;
277
        }
278
    }
279

    
280
    public void doCollectionEditorAdd() {
281

    
282
        collectionPopuEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
283

    
284
        collectionPopuEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
285
        collectionPopuEditor.withDeleteButton(true);
286
        collectionPopuEditor.loadInEditor(null);
287
    }
288

    
289
    public void doCollectionEditorEdit(int collectionId) {
290

    
291
        collectionPopuEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
292

    
293
        collectionPopuEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
294
        collectionPopuEditor.withDeleteButton(true);
295
        collectionPopuEditor.loadInEditor(collectionId);
296
    }
297

    
298
    @EventListener(condition = "#event.entityType == T(eu.etaxonomy.cdm.model.occurrence.Collection)")
299
    public void onCollectionEvent(EntityChangeEvent event){
300

    
301
        Collection newCollection = getRepo().getCollectionService().load(event.getEntityId(), Arrays.asList(new String[]{"$.institute"}));
302
        cache.findAndUpdate(newCollection);
303

    
304
        for( CollectionRowItemCollection row : collectionPopuEditorSourceRows) {
305
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 2);
306
            combobox.reload();
307
        }
308
    }
309

    
310

    
311

    
312
}
(3-3/9)