Project

General

Profile

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

    
48
    private static final long serialVersionUID = 4255636253714476918L;
49

    
50
    CdmStore<Registration, IRegistrationService> store;
51

    
52

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

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

    
66
    private CdmEntityCache cache = null;
67

    
68
    protected CdmStore<Registration, IRegistrationService> getStore() {
69
        if(store == null){
70
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
71
        }
72
        return store;
73
    }
74

    
75

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

    
90
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
91
        if(identifier != null){
92

    
93
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
94

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

    
110
        } else {
111
            workingSetDto = null;
112
        }
113

    
114
        return workingSetDto;
115
    }
116

    
117

    
118
    /**
119
     * {@inheritDoc}
120
     */
121
    @SuppressWarnings("serial")
122
    @Override
123
    public void handleViewEntered() {
124

    
125
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
126
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
127

    
128
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
129

    
130
            CdmFilterablePagingProvider<Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection>(getRepo().getCollectionService());
131

    
132
            CdmFilterablePagingProvider<Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference>(getRepo().getReferenceService());
133

    
134
            @Override
135
            public SpecimenTypeDesignationDTORow create() {
136

    
137
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
138

    
139
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
140
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
141
                        );
142
                row.kindOfUnit.setNullSelectionAllowed(false);
143

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

    
149

    
150
                row.collection.loadFrom(
151
                        collectionPagingProvider,
152
                        collectionPagingProvider,
153
                        collectionPagingProvider.getPageSize()
154
                        );
155
                row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
156
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
157
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
158
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
159

    
160
                row.mediaSpecimenReference.loadFrom(
161
                        referencePagingProvider,
162
                        referencePagingProvider,
163
                        collectionPagingProvider.getPageSize()
164
                        );
165

    
166
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
167
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
168
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
169
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
170

    
171
                getView().applyDefaultComponentStyle(row.components());
172

    
173
                return row;
174
            }
175

    
176
        });
177

    
178
    }
179

    
180

    
181
    /**
182
     * {@inheritDoc}
183
     */
184
    @Override
185
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
186
        if(crud != null){
187
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
188
        }
189

    
190
        specimenTypeDesignationWorkingSetService.save(dto);
191
    }
192

    
193
    /**
194
     * {@inheritDoc}
195
     */
196
    @Override
197
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
198
        // TODO Auto-generated method stub
199

    
200
    }
201

    
202
    /**
203
     * @param crud
204
     */
205
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
206
        this.crud = crud;
207

    
208
    }
209

    
210
    /**
211
     * {@inheritDoc}
212
     */
213
    @Override
214
    public EntityCache getCache() {
215
        if(((AbstractPopupEditor)getView()).isBeanLoaded()){
216
            return cache;
217
        } else {
218
            return null;
219
        }
220
    }
221

    
222
}
(3-3/9)