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.CdmTransientEntityCacher;
|
22
|
import eu.etaxonomy.cdm.model.ICdmCacher;
|
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.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.location.Country;
|
28
|
import eu.etaxonomy.cdm.model.name.Registration;
|
29
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
30
|
import eu.etaxonomy.cdm.model.occurrence.Collection;
|
31
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
32
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
33
|
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
|
34
|
import eu.etaxonomy.cdm.service.CdmStore;
|
35
|
import eu.etaxonomy.cdm.service.ISpecimenTypeDesignationWorkingSetService;
|
36
|
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
|
37
|
import eu.etaxonomy.cdm.vaadin.component.CollectionRowItemCollection;
|
38
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
|
39
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
|
40
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
|
41
|
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationTermLists;
|
42
|
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
|
43
|
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
|
44
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
45
|
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
|
46
|
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
|
47
|
import eu.etaxonomy.cdm.vaadin.view.occurrence.CollectionPopupEditor;
|
48
|
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
|
49
|
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityCombobox;
|
50
|
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
|
51
|
/**
|
52
|
* SpecimenTypeDesignationWorkingsetPopupEditorView implementation must override the showInEditor() method,
|
53
|
* see {@link #prepareAsFieldGroupDataSource()} for details.
|
54
|
*
|
55
|
* @author a.kohlbecker
|
56
|
* @since Jun 13, 2017
|
57
|
*
|
58
|
*/
|
59
|
public class SpecimenTypeDesignationWorkingsetEditorPresenter
|
60
|
extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView>
|
61
|
implements CachingPresenter {
|
62
|
|
63
|
private static final long serialVersionUID = 4255636253714476918L;
|
64
|
|
65
|
private static final EnumSet<CRUD> COLLECTION_EDITOR_CRUD = EnumSet.of(CRUD.UPDATE, CRUD.DELETE);
|
66
|
|
67
|
CdmStore<Registration, IRegistrationService> store;
|
68
|
|
69
|
|
70
|
/**
|
71
|
* This object for this field will either be injected by the {@link PopupEditorFactory} or by a Spring
|
72
|
* {@link BeanFactory}
|
73
|
*/
|
74
|
@Autowired
|
75
|
private ISpecimenTypeDesignationWorkingSetService specimenTypeDesignationWorkingSetService;
|
76
|
|
77
|
/**
|
78
|
* if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entitiy which will be
|
79
|
* granted to the current use as long this grant is not assigned yet.
|
80
|
*/
|
81
|
private EnumSet<CRUD> crud = null;
|
82
|
|
83
|
private ICdmCacher cache;
|
84
|
|
85
|
SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
|
86
|
|
87
|
private CollectionPopupEditor collectionPopupEditor;
|
88
|
|
89
|
private Set<CollectionRowItemCollection> collectionPopuEditorSourceRows = new HashSet<>();
|
90
|
|
91
|
private ReferencePopupEditor referencePopupEditor;
|
92
|
|
93
|
private java.util.Collection<CdmBase> rootEntities = new HashSet<>();
|
94
|
|
95
|
protected CdmStore<Registration, IRegistrationService> getStore() {
|
96
|
if(store == null){
|
97
|
store = new CdmStore<>(getRepo(), getRepo().getRegistrationService());
|
98
|
}
|
99
|
return store;
|
100
|
}
|
101
|
|
102
|
|
103
|
/**
|
104
|
* Loads an existing working set from the database. This process actually involves
|
105
|
* loading the Registration specified by the <code>RegistrationAndWorkingsetId.registrationId</code> and in
|
106
|
* a second step to find the workingset by the <code>registrationAndWorkingsetId.workingsetId</code>.
|
107
|
* <p>
|
108
|
* The <code>identifier</code> must be of the type {@link TypeDesignationWorkingsetEditorIdSet} whereas the field <code>egistrationId</code>
|
109
|
* must be present, the field <code>workingsetId</code>, however can be null. I this case a new workingset with a new {@link FieldUnit} as
|
110
|
* base entity is being created.
|
111
|
*
|
112
|
* @param identifier a {@link TypeDesignationWorkingsetEditorIdSet}
|
113
|
*/
|
114
|
@Override
|
115
|
protected SpecimenTypeDesignationWorkingSetDTO<Registration> loadBeanById(Object identifier) {
|
116
|
|
117
|
cache = new CdmTransientEntityCacher(this);
|
118
|
if(identifier != null){
|
119
|
|
120
|
TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
|
121
|
|
122
|
if(idset.workingsetId != null){
|
123
|
workingSetDto = specimenTypeDesignationWorkingSetService.loadDtoByIds(idset.registrationId, idset.workingsetId);
|
124
|
if(workingSetDto.getFieldUnit() == null){
|
125
|
workingSetDto = specimenTypeDesignationWorkingSetService.fixMissingFieldUnit(workingSetDto);
|
126
|
// FIXME open Dialog to warn user about adding an empty fieldUnit to the typeDesignations
|
127
|
// This method must go again into the presenter !!!!
|
128
|
logger.info("Basing all typeDesignations on a new fieldUnit");
|
129
|
}
|
130
|
cache.put(workingSetDto.getOwner());
|
131
|
rootEntities.add(workingSetDto.getOwner());
|
132
|
} else {
|
133
|
// create a new workingset, for a new fieldunit which is the base for the workingset
|
134
|
workingSetDto = specimenTypeDesignationWorkingSetService.create(idset.registrationId, idset.publicationId, idset.typifiedNameId);
|
135
|
cache.put(workingSetDto.getOwner());
|
136
|
rootEntities.add(workingSetDto.getOwner());
|
137
|
}
|
138
|
|
139
|
} else {
|
140
|
workingSetDto = null;
|
141
|
}
|
142
|
|
143
|
return workingSetDto;
|
144
|
}
|
145
|
|
146
|
|
147
|
/**
|
148
|
* {@inheritDoc}
|
149
|
*/
|
150
|
@SuppressWarnings("serial")
|
151
|
@Override
|
152
|
public void handleViewEntered() {
|
153
|
|
154
|
CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
|
155
|
getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
|
156
|
|
157
|
CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> termOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
|
158
|
CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
|
159
|
getView().getCollectorField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
160
|
getView().getCollectorField().setFilterableTeamPagingProvider(termOrPersonPagingProvider, this);
|
161
|
|
162
|
getView().getTypeDesignationsCollectionField().addElementRemovedListener(e -> deleteTypeDesignation(e.getElement()));
|
163
|
getView().getTypeDesignationsCollectionField().addElementAddedListener(e -> addTypeDesignation(e.getElement()));
|
164
|
|
165
|
|
166
|
collectionPopuEditorSourceRows.clear();
|
167
|
getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
|
168
|
|
169
|
CdmFilterablePagingProvider<Collection, Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection, Collection>(getRepo().getCollectionService());
|
170
|
|
171
|
CdmFilterablePagingProvider<Reference, Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
|
172
|
|
173
|
@Override
|
174
|
public SpecimenTypeDesignationDTORow create() {
|
175
|
|
176
|
SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
|
177
|
|
178
|
row.kindOfUnit.setContainerDataSource(selectFactory.buildTermItemContainer(
|
179
|
RegistrationTermLists.KIND_OF_UNIT_TERM_UUIDS())
|
180
|
);
|
181
|
row.kindOfUnit.setNullSelectionAllowed(false);
|
182
|
|
183
|
row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
|
184
|
RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
|
185
|
);
|
186
|
row.typeStatus.setNullSelectionAllowed(false);
|
187
|
|
188
|
|
189
|
row.collection.loadFrom(
|
190
|
collectionPagingProvider,
|
191
|
collectionPagingProvider,
|
192
|
collectionPagingProvider.getPageSize()
|
193
|
);
|
194
|
row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
|
195
|
row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
|
196
|
row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
|
197
|
SpecimenTypeDesignationWorkingsetEditorPresenter.this));
|
198
|
row.collection.addClickListenerAddEntity(e -> doCollectionEditorAdd());
|
199
|
row.collection.addClickListenerEditEntity(e -> {
|
200
|
if(row.collection.getValue() != null){
|
201
|
doCollectionEditorEdit(row.collection.getValue().getId());
|
202
|
}
|
203
|
});
|
204
|
|
205
|
row.mediaSpecimenReference.loadFrom(
|
206
|
referencePagingProvider,
|
207
|
referencePagingProvider,
|
208
|
collectionPagingProvider.getPageSize()
|
209
|
);
|
210
|
|
211
|
row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
|
212
|
row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
|
213
|
row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
|
214
|
SpecimenTypeDesignationWorkingsetEditorPresenter.this));
|
215
|
row.mediaSpecimenReference.addClickListenerAddEntity(e -> doReferenceEditorAdd());
|
216
|
row.mediaSpecimenReference.addClickListenerEditEntity(e -> {
|
217
|
if(row.mediaSpecimenReference.getValue() != null){
|
218
|
doReferenceEditorEdit(row.mediaSpecimenReference.getValue().getId());
|
219
|
}
|
220
|
});
|
221
|
|
222
|
getView().applyDefaultComponentStyle(row.components());
|
223
|
|
224
|
collectionPopuEditorSourceRows.add(row);
|
225
|
|
226
|
return row;
|
227
|
}
|
228
|
|
229
|
});
|
230
|
|
231
|
}
|
232
|
|
233
|
|
234
|
/**
|
235
|
* {@inheritDoc}
|
236
|
*/
|
237
|
@Override
|
238
|
protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) {
|
239
|
|
240
|
if(crud != null){
|
241
|
UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null);
|
242
|
}
|
243
|
|
244
|
specimenTypeDesignationWorkingSetService.save(dto);
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* {@inheritDoc}
|
249
|
*/
|
250
|
@Override
|
251
|
protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
|
252
|
specimenTypeDesignationWorkingSetService.delete(bean, true);
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* @param element
|
257
|
* @return
|
258
|
*/
|
259
|
private void addTypeDesignation(SpecimenTypeDesignationDTO element) {
|
260
|
getView().updateAllowDelete();
|
261
|
}
|
262
|
|
263
|
|
264
|
/**
|
265
|
* In this method the SpecimenTypeDesignation is dissociated from the Registration.
|
266
|
* The actual deletion of the SpecimenTypeDesignation and DerivedUnit will take place in {@link #saveBean(SpecimenTypeDesignationWorkingSetDTO)}
|
267
|
*
|
268
|
* TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
|
269
|
*
|
270
|
* @param e
|
271
|
* @return
|
272
|
*/
|
273
|
private void deleteTypeDesignation(SpecimenTypeDesignationDTO element) {
|
274
|
|
275
|
Registration reg = workingSetDto.getOwner();
|
276
|
SpecimenTypeDesignation std = element.asSpecimenTypeDesignation();
|
277
|
|
278
|
reg.getTypeDesignations().remove(std);
|
279
|
|
280
|
getView().updateAllowDelete();
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* @param crud
|
285
|
*/
|
286
|
public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
|
287
|
this.crud = crud;
|
288
|
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* {@inheritDoc}
|
293
|
*/
|
294
|
@Override
|
295
|
public ICdmCacher getCache() {
|
296
|
return cache;
|
297
|
}
|
298
|
|
299
|
public void doCollectionEditorAdd() {
|
300
|
|
301
|
collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
|
302
|
|
303
|
collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
|
304
|
collectionPopupEditor.withDeleteButton(true);
|
305
|
collectionPopupEditor.loadInEditor(null);
|
306
|
}
|
307
|
|
308
|
public void doCollectionEditorEdit(int collectionId) {
|
309
|
|
310
|
collectionPopupEditor = getNavigationManager().showInPopup(CollectionPopupEditor.class);
|
311
|
|
312
|
collectionPopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
|
313
|
collectionPopupEditor.withDeleteButton(true);
|
314
|
collectionPopupEditor.loadInEditor(collectionId);
|
315
|
}
|
316
|
|
317
|
@EventListener(condition = "#event.entityType == T(eu.etaxonomy.cdm.model.occurrence.Collection)")
|
318
|
public void onCollectionEvent(EntityChangeEvent event){
|
319
|
|
320
|
Collection newCollection = getRepo().getCollectionService().load(event.getEntityId(), Arrays.asList(new String[]{"$.institute"}));
|
321
|
cache.getFromCache(newCollection);
|
322
|
|
323
|
for( CollectionRowItemCollection row : collectionPopuEditorSourceRows) {
|
324
|
ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 2);
|
325
|
combobox.reload();
|
326
|
}
|
327
|
}
|
328
|
|
329
|
public void doReferenceEditorAdd() {
|
330
|
|
331
|
referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class);
|
332
|
|
333
|
referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
|
334
|
referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
|
335
|
referencePopupEditor.withDeleteButton(true);
|
336
|
referencePopupEditor.loadInEditor(null);
|
337
|
}
|
338
|
|
339
|
public void doReferenceEditorEdit(int referenceId) {
|
340
|
|
341
|
referencePopupEditor = getNavigationManager().showInPopup(ReferencePopupEditor.class);
|
342
|
referencePopupEditor.withReferenceTypes(RegistrationUIDefaults.MEDIA_REFERENCE_TYPES);
|
343
|
referencePopupEditor.grantToCurrentUser(COLLECTION_EDITOR_CRUD);
|
344
|
referencePopupEditor.withDeleteButton(true);
|
345
|
referencePopupEditor.loadInEditor(referenceId);
|
346
|
}
|
347
|
|
348
|
@EventListener(condition = "#event.entityType == T(eu.etaxonomy.cdm.model.reference.Reference)")
|
349
|
public void onReferenceEvent(EntityChangeEvent event){
|
350
|
|
351
|
Reference newRef = getRepo().getReferenceService().load(event.getEntityId(), Arrays.asList(new String[]{"$"}));
|
352
|
cache.getFromCache(newRef);
|
353
|
|
354
|
for( CollectionRowItemCollection row : collectionPopuEditorSourceRows) {
|
355
|
ToOneRelatedEntityCombobox<Collection> combobox = row.getComponent(ToOneRelatedEntityCombobox.class, 6);
|
356
|
combobox.reload();
|
357
|
}
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* {@inheritDoc}
|
362
|
*/
|
363
|
@Override
|
364
|
public void addRootEntity(CdmBase entity) {
|
365
|
rootEntities.add(entity);
|
366
|
}
|
367
|
|
368
|
/**
|
369
|
* {@inheritDoc}
|
370
|
*/
|
371
|
@Override
|
372
|
public java.util.Collection<CdmBase> getRootEntities() {
|
373
|
return rootEntities ;
|
374
|
}
|
375
|
|
376
|
|
377
|
|
378
|
}
|