Project

General

Profile

Download (10.3 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.EnumSet;
12

    
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.vaadin.viritin.fields.AbstractElementCollection;
15

    
16
import eu.etaxonomy.cdm.api.service.IRegistrationService;
17
import eu.etaxonomy.cdm.cache.CdmEntityCache;
18
import eu.etaxonomy.cdm.cache.EntityCache;
19
import eu.etaxonomy.cdm.model.location.Country;
20
import eu.etaxonomy.cdm.model.name.Registration;
21
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
22
import eu.etaxonomy.cdm.model.occurrence.Collection;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
25
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
26
import eu.etaxonomy.cdm.service.CdmStore;
27
import eu.etaxonomy.cdm.service.ISpecimenTypeDesignationWorkingSetService;
28
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
29
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
30
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
31
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationTermLists;
32
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
33
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
34
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
35
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
36
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
37
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
38
/**
39
 * SpecimenTypeDesignationWorkingsetPopupEditorView implementation must override the showInEditor() method,
40
 * see {@link #prepareAsFieldGroupDataSource()} for details.
41
 *
42
 * @author a.kohlbecker
43
 * @since Jun 13, 2017
44
 *
45
 */
46
public class SpecimenTypeDesignationWorkingsetEditorPresenter
47
    extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView>
48
    implements CachingPresenter {
49

    
50
    private static final long serialVersionUID = 4255636253714476918L;
51

    
52
    CdmStore<Registration, IRegistrationService> store;
53

    
54

    
55
    /**
56
     * This object for this field will either be injected by the {@link PopupEditorFactory} or by a Spring
57
     * {@link BeanFactory}
58
     */
59
    @Autowired
60
    private ISpecimenTypeDesignationWorkingSetService specimenTypeDesignationWorkingSetService;
61

    
62
    /**
63
     * if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entitiy which will be
64
     * granted to the current use as long this grant is not assigned yet.
65
     */
66
    private EnumSet<CRUD> crud = null;
67

    
68
    private CdmEntityCache cache = null;
69

    
70
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
71

    
72
    protected CdmStore<Registration, IRegistrationService> getStore() {
73
        if(store == null){
74
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
75
        }
76
        return store;
77
    }
78

    
79

    
80
    /**
81
     * Loads an existing working set from the database. This process actually involves
82
     * loading the Registration specified by the <code>RegistrationAndWorkingsetId.registrationId</code> and in
83
     * a second step to find the workingset by the <code>registrationAndWorkingsetId.workingsetId</code>.
84
     * <p>
85
     * The <code>identifier</code> must be of the type {@link TypeDesignationWorkingsetEditorIdSet} whereas the field <code>egistrationId</code>
86
     * must be present, the field <code>workingsetId</code>,  however can be null. I this case a new workingset with a new {@link FieldUnit} as
87
     * base entity is being created.
88
     *
89
     * @param identifier a {@link TypeDesignationWorkingsetEditorIdSet}
90
     */
91
    @Override
92
    protected SpecimenTypeDesignationWorkingSetDTO<Registration> loadBeanById(Object identifier) {
93

    
94
        if(identifier != null){
95

    
96
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
97

    
98
            if(idset.workingsetId != null){
99
                workingSetDto = specimenTypeDesignationWorkingSetService.loadDtoByIds(idset.registrationId, idset.workingsetId);
100
                if(workingSetDto.getFieldUnit() == null){
101
                    workingSetDto = specimenTypeDesignationWorkingSetService.fixMissingFieldUnit(workingSetDto);
102
                        // FIXME open Dialog to warn user about adding an empty fieldUnit to the typeDesignations
103
                        //       This method must go again into the presenter !!!!
104
                        logger.info("Basing all typeDesignations on a new fieldUnit");
105
                }
106
                cache = new CdmEntityCache(workingSetDto.getOwner());
107
            } else {
108
                // create a new workingset, for a new fieldunit which is the base for the workingset
109
                workingSetDto = specimenTypeDesignationWorkingSetService.create(idset.registrationId, idset.publicationId, idset.typifiedNameId);
110
                cache = new CdmEntityCache(workingSetDto.getOwner());
111
            }
112

    
113
        } else {
114
            workingSetDto = null;
115
        }
116

    
117
        return workingSetDto;
118
    }
119

    
120

    
121
    /**
122
     * {@inheritDoc}
123
     */
124
    @SuppressWarnings("serial")
125
    @Override
126
    public void handleViewEntered() {
127

    
128
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
129
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
130

    
131
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
132
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
133
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
134

    
135
            CdmFilterablePagingProvider<Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection>(getRepo().getCollectionService());
136

    
137
            CdmFilterablePagingProvider<Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference>(getRepo().getReferenceService());
138

    
139
            @Override
140
            public SpecimenTypeDesignationDTORow create() {
141

    
142
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
143

    
144
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
145
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
146
                        );
147
                row.kindOfUnit.setNullSelectionAllowed(false);
148

    
149
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
150
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
151
                        );
152
                row.typeStatus.setNullSelectionAllowed(false);
153

    
154

    
155
                row.collection.loadFrom(
156
                        collectionPagingProvider,
157
                        collectionPagingProvider,
158
                        collectionPagingProvider.getPageSize()
159
                        );
160
                row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
161
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
162
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
163
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
164

    
165
                row.mediaSpecimenReference.loadFrom(
166
                        referencePagingProvider,
167
                        referencePagingProvider,
168
                        collectionPagingProvider.getPageSize()
169
                        );
170

    
171
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
172
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
173
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
174
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
175

    
176
                getView().applyDefaultComponentStyle(row.components());
177

    
178
                return row;
179
            }
180

    
181
        });
182

    
183
    }
184

    
185

    
186
    /**
187
     * {@inheritDoc}
188
     */
189
    @Override
190
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
191

    
192
        if(crud != null){
193
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
194
        }
195

    
196
        specimenTypeDesignationWorkingSetService.save(dto);
197
    }
198

    
199
    /**
200
     * {@inheritDoc}
201
     */
202
    @Override
203
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
204
        specimenTypeDesignationWorkingSetService.delete(bean);
205
    }
206

    
207
    /**
208
     * @param element
209
     * @return
210
     */
211
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
212
        getView().updateAllowDelete();
213
    }
214

    
215

    
216
    /**
217
     * In this method the SpecimenTypeDesignation is dissociated from the Registration.
218
     * The actual deletion of the SpecimenTypeDesignation and DerivedUnit will take place in {@link #saveBean(SpecimenTypeDesignationWorkingSetDTO)}
219
     *
220
     * TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
221
     *
222
     * @param e
223
     * @return
224
     */
225
    private void deleteTypeDesignation(SpecimenTypeDesignationDTO element) {
226

    
227
        Registration reg = workingSetDto.getOwner();
228
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
229

    
230
        reg.getTypeDesignations().remove(std);
231

    
232
        getView().updateAllowDelete();
233
    }
234

    
235
    /**
236
     * @param crud
237
     */
238
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
239
        this.crud = crud;
240

    
241
    }
242

    
243
    /**
244
     * {@inheritDoc}
245
     */
246
    @Override
247
    public EntityCache getCache() {
248
        if(((AbstractPopupEditor)getView()).isBeanLoaded()){
249
            return cache;
250
        } else {
251
            return null;
252
        }
253
    }
254

    
255
}
(3-3/9)