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