Project

General

Profile

Download (9.73 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.common.DefinedTerm;
20
import eu.etaxonomy.cdm.model.location.Country;
21
import eu.etaxonomy.cdm.model.name.Registration;
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.KindOfUnitTerms;
32
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationTermLists;
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
    protected CdmStore<Registration, IRegistrationService> getStore() {
71
        if(store == null){
72
            store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
73
        }
74
        return store;
75
    }
76

    
77

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

    
92
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
93
        if(identifier != null){
94

    
95
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
96

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

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

    
116
        return workingSetDto;
117
    }
118

    
119

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

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

    
130
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
131

    
132
            CdmFilterablePagingProvider<Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection>(getRepo().getCollectionService());
133

    
134
            CdmFilterablePagingProvider<Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference>(getRepo().getReferenceService());
135

    
136
            @Override
137
            public SpecimenTypeDesignationDTORow create() {
138

    
139
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
140

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

    
146
                row.kindOfUnit.addValueChangeListener(e -> {
147
                    SpecimenTypeDesignationDTORow currentRow = row;
148
                    updateRowItemEnablement(currentRow);
149
                });
150

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

    
156

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

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

    
172
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
173
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
174

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

    
177
                updateRowItemEnablement(row);
178

    
179
                return row;
180
            }
181

    
182
            private void updateRowItemEnablement(SpecimenTypeDesignationDTORow row) {
183

    
184
                DefinedTerm kindOfUnit = (DefinedTerm)row.kindOfUnit.getValue();
185

    
186
                boolean publishedImageType = kindOfUnit != null && kindOfUnit.equals(KindOfUnitTerms.PUBLISHED_IMAGE());
187
                boolean unPublishedImageType = kindOfUnit != null && kindOfUnit.equals(KindOfUnitTerms.UNPUBLISHED_IMAGE());
188

    
189
                row.mediaSpecimenReference.setEnabled(publishedImageType);
190
                row.mediaSpecimenReferenceDetail.setEnabled(publishedImageType);
191
                row.mediaUri.setEnabled(unPublishedImageType);
192

    
193
            }
194
        });
195
    }
196

    
197

    
198
    /**
199
     * {@inheritDoc}
200
     */
201
    @Override
202
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
203
        if(crud != null){
204
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
205
        }
206

    
207
        specimenTypeDesignationWorkingSetService.save(dto);
208
    }
209

    
210
    /**
211
     * {@inheritDoc}
212
     */
213
    @Override
214
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
215
        // TODO Auto-generated method stub
216

    
217
    }
218

    
219
    /**
220
     * @param crud
221
     */
222
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
223
        this.crud = crud;
224

    
225
    }
226

    
227
    /**
228
     * {@inheritDoc}
229
     */
230
    @Override
231
    public EntityCache getCache() {
232
        if(((AbstractPopupEditor)getView()).isBeanLoaded()){
233
            return cache;
234
        } else {
235
            return null;
236
        }
237
    }
238

    
239

    
240
}
(3-3/9)