Project

General

Profile

Download (13.4 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.util.CdmTitleCacheCaptionGenerator;
45
import eu.etaxonomy.cdm.vaadin.view.occurrence.CollectionPopupEditor;
46
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityCombobox;
47
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
48
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
49
/**
50
 * SpecimenTypeDesignationWorkingsetPopupEditorView implementation must override the showInEditor() method,
51
 * see {@link #prepareAsFieldGroupDataSource()} for details.
52
 *
53
 * @author a.kohlbecker
54
 * @since Jun 13, 2017
55
 *
56
 */
57
public class SpecimenTypeDesignationWorkingsetEditorPresenter
58
    extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView>
59
    implements CachingPresenter {
60

    
61
    private static final long serialVersionUID = 4255636253714476918L;
62

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

    
65
    CdmStore<Registration, IRegistrationService> store;
66

    
67

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

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

    
81
    private CdmEntityCache cache = null;
82

    
83
    SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
84

    
85
    private CollectionPopupEditor collectionPopuEditor;
86

    
87
    private Set<CollectionRowItemCollection> collectionPopuEditorSourceRows = new HashSet<>();
88

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

    
96

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

    
111
        if(identifier != null){
112

    
113
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
114

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

    
130
        } else {
131
            workingSetDto = null;
132
        }
133

    
134
        return workingSetDto;
135
    }
136

    
137

    
138
    /**
139
     * {@inheritDoc}
140
     */
141
    @SuppressWarnings("serial")
142
    @Override
143
    public void handleViewEntered() {
144

    
145
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
146
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
147

    
148
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
149
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
150
        getView().getCollectorField().setFilterablePersonPagingProvider(personPagingProvider, this);
151
        getView().getCollectorField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
152

    
153
        getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
154
        getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
155

    
156

    
157
        collectionPopuEditorSourceRows.clear();
158
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
159

    
160
            CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
161

    
162
            CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
163

    
164
            @Override
165
            public SpecimenTypeDesignationDTORow create() {
166

    
167
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
168

    
169
                row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
170
                        RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
171
                        );
172
                row.kindOfUnit.setNullSelectionAllowed(false);
173

    
174
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
175
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
176
                        );
177
                row.typeStatus.setNullSelectionAllowed(false);
178

    
179

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

    
196
                row.mediaSpecimenReference.loadFrom(
197
                        referencePagingProvider,
198
                        referencePagingProvider,
199
                        collectionPagingProvider.getPageSize()
200
                        );
201

    
202
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
203
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
204
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
205
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
206

    
207
                getView().applyDefaultComponentStyle(row.components());
208

    
209
                collectionPopuEditorSourceRows.add(row);
210

    
211
                return row;
212
            }
213

    
214
        });
215

    
216
    }
217

    
218

    
219
    /**
220
     * {@inheritDoc}
221
     */
222
    @Override
223
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
224

    
225
        if(crud != null){
226
            UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
227
        }
228

    
229
        specimenTypeDesignationWorkingSetService.save(dto);
230
    }
231

    
232
    /**
233
     * {@inheritDoc}
234
     */
235
    @Override
236
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
237
        specimenTypeDesignationWorkingSetService.delete(bean, true);
238
    }
239

    
240
    /**
241
     * @param element
242
     * @return
243
     */
244
    private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
245
        getView().updateAllowDelete();
246
    }
247

    
248

    
249
    /**
250
     * In this method the SpecimenTypeDesignation is dissociated from the Registration.
251
     * The actual deletion of the SpecimenTypeDesignation and DerivedUnit will take place in {@link #saveBean(SpecimenTypeDesignationWorkingSetDTO)}
252
     *
253
     * TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
254
     *
255
     * @param e
256
     * @return
257
     */
258
    private void deleteTypeDesignation(SpecimenTypeDesignationDTO element) {
259

    
260
        Registration reg = workingSetDto.getOwner();
261
        SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
262

    
263
        reg.getTypeDesignations().remove(std);
264

    
265
        getView().updateAllowDelete();
266
    }
267

    
268
    /**
269
     * @param crud
270
     */
271
    public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
272
        this.crud = crud;
273

    
274
    }
275

    
276
    /**
277
     * {@inheritDoc}
278
     */
279
    @Override
280
    public EntityCache getCache() {
281
        if(((AbstractPopupEditor)getView()).isBeanLoaded()){
282
            return cache;
283
        } else {
284
            return null;
285
        }
286
    }
287

    
288
    public void doCollectionEditorAdd() {
289

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

    
292
        collectionPopuEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
293
        collectionPopuEditor.withDeleteButton(true);
294
        collectionPopuEditor.loadInEditor(null);
295
    }
296

    
297
    public void doCollectionEditorEdit(int collectionId) {
298

    
299
        collectionPopuEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
300

    
301
        collectionPopuEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
302
        collectionPopuEditor.withDeleteButton(true);
303
        collectionPopuEditor.loadInEditor(collectionId);
304
    }
305

    
306
    @EventListener(condition = "#event.entityType == T(eu.etaxonomy.cdm.model.occurrence.Collection)")
307
    public void onCollectionEvent(EntityChangeEvent event){
308

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

    
312
        for( CollectionRowItemCollection row : collectionPopuEditorSourceRows) {
313
            ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 2);
314
            combobox.reload();
315
        }
316
    }
317

    
318

    
319

    
320
}
(3-3/10)