1
|
/**
|
2
|
* Copyright (C) 2018 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.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.EnumSet;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.UUID;
|
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
|
|
22
|
import com.vaadin.spring.annotation.SpringComponent;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
25
|
import eu.etaxonomy.cdm.api.service.IService;
|
26
|
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
|
27
|
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager.TypeDesignationWorkingSet;
|
28
|
import eu.etaxonomy.cdm.api.service.registration.IRegistrationWorkingSetService;
|
29
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
30
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
|
31
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
|
32
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
35
|
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
|
36
|
import eu.etaxonomy.cdm.service.CdmStore;
|
37
|
import eu.etaxonomy.cdm.service.UserHelperAccess;
|
38
|
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
|
39
|
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
|
40
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
|
41
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent.Type;
|
42
|
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
|
43
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
|
44
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
|
45
|
import eu.etaxonomy.cdm.vaadin.ui.config.TaxonNamePopupEditorConfig;
|
46
|
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
|
47
|
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
|
48
|
import eu.etaxonomy.vaadin.mvp.AbstractView;
|
49
|
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
|
50
|
import eu.etaxonomy.vaadin.mvp.BoundField;
|
51
|
import eu.etaxonomy.vaadin.ui.view.PopupView;
|
52
|
|
53
|
/**
|
54
|
* @author a.kohlbecker
|
55
|
* @since Jan 26, 2018
|
56
|
*
|
57
|
*/
|
58
|
@SpringComponent
|
59
|
@Scope("prototype")
|
60
|
public class NameTypeDesignationPresenter
|
61
|
extends AbstractCdmEditorPresenter<NameTypeDesignation, NameTypeDesignationEditorView> {
|
62
|
|
63
|
@Autowired
|
64
|
private IRegistrationWorkingSetService registrationWorkingSetService;
|
65
|
|
66
|
HashSet<TaxonName> typifiedNamesAsLoaded;
|
67
|
|
68
|
private TaxonName typifiedNameInContext;
|
69
|
|
70
|
protected static BeanInstantiator<NameTypeDesignation> defaultBeanInstantiator = new BeanInstantiator<NameTypeDesignation>() {
|
71
|
|
72
|
@Override
|
73
|
public NameTypeDesignation createNewBean() {
|
74
|
return NameTypeDesignation.NewInstance();
|
75
|
}
|
76
|
};
|
77
|
|
78
|
|
79
|
@Override
|
80
|
protected BeanInstantiator<NameTypeDesignation> defaultBeanInstantiator(){
|
81
|
return defaultBeanInstantiator;
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* {@inheritDoc}
|
86
|
*/
|
87
|
@Override
|
88
|
protected NameTypeDesignation loadBeanById(Object identifier) {
|
89
|
if(identifier instanceof Integer || identifier == null){
|
90
|
return super.loadBeanById(identifier);
|
91
|
// } else if(identifier instanceof TypedEntityReference && ((TypedEntityReference)identifier).getType().equals(TaxonName.class)) {
|
92
|
// typifiedNameInContext = getRepo().getNameService().find(((TypedEntityReference)identifier).getUuid());
|
93
|
// bean = super.loadBeanById(null);
|
94
|
} else {
|
95
|
TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
|
96
|
RegistrationDTO regDTO = registrationWorkingSetService.loadDtoByUuid(idset.registrationUuid);
|
97
|
typifiedNameInContext = regDTO.getTypifiedName();
|
98
|
// find the working set
|
99
|
TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(idset.baseEntityRef);
|
100
|
|
101
|
// NameTypeDesignation bameTypeDesignation = regDTO.getNameTypeDesignation(typeDesignationWorkingSet.getBaseEntityReference());
|
102
|
if(!typeDesignationWorkingSet.getBaseEntityReference().getType().equals(NameTypeDesignation.class)){
|
103
|
throw new RuntimeException("TypeDesignationWorkingsetEditorIdSet references not a NameTypeDesignation");
|
104
|
}
|
105
|
// TypeDesignationWorkingSet for NameTyped only contain one item!!!
|
106
|
UUID nameTypeDesignationUuid = typeDesignationWorkingSet.getTypeDesignations().get(0).getUuid();
|
107
|
return super.loadBeanById(nameTypeDesignationUuid);
|
108
|
}
|
109
|
}
|
110
|
|
111
|
|
112
|
/**
|
113
|
* {@inheritDoc}
|
114
|
*/
|
115
|
@Override
|
116
|
protected NameTypeDesignation loadCdmEntity(UUID uuid) {
|
117
|
List<String> initStrategy = Arrays.asList(new String []{
|
118
|
"$",
|
119
|
"annotations.*", // * is needed as log as we are using a table in FilterableAnnotationsField
|
120
|
"typifiedNames.typeDesignations", // important !!
|
121
|
"typeName.$",
|
122
|
"citation.authorship.$",
|
123
|
}
|
124
|
);
|
125
|
|
126
|
NameTypeDesignation typeDesignation;
|
127
|
if(uuid != null){
|
128
|
typeDesignation = (NameTypeDesignation) getRepo().getNameService().loadTypeDesignation(uuid, initStrategy);
|
129
|
} else {
|
130
|
typeDesignation = createNewBean();
|
131
|
}
|
132
|
|
133
|
typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
|
134
|
|
135
|
return typeDesignation;
|
136
|
}
|
137
|
|
138
|
|
139
|
|
140
|
|
141
|
/**
|
142
|
* {@inheritDoc}
|
143
|
*/
|
144
|
@Override
|
145
|
public void handleViewEntered() {
|
146
|
|
147
|
CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
|
148
|
getView().getTypeStatusSelect().setContainerDataSource(selectFactory.buildBeanItemContainer(NameTypeDesignationStatus.class));
|
149
|
getView().getTypeStatusSelect().setItemCaptionPropertyId("description");
|
150
|
|
151
|
getView().getCitationCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
|
152
|
CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider = pagingProviderFactory.referencePagingProvider();
|
153
|
getView().getCitationCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
|
154
|
getView().getCitationCombobox().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getCitationCombobox()));
|
155
|
getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getCitationCombobox(), this));
|
156
|
|
157
|
CdmFilterablePagingProvider<TaxonName,TaxonName> namePagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
|
158
|
getView().getTypeNameField().loadFrom(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize());
|
159
|
getView().getTypeNameField().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<TaxonName>(getView().getTypeNameField()));
|
160
|
getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getTypeNameField(), this));
|
161
|
|
162
|
getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
|
163
|
|
164
|
}
|
165
|
|
166
|
|
167
|
/**
|
168
|
* {@inheritDoc}
|
169
|
*/
|
170
|
@Override
|
171
|
protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
|
172
|
if(crud != null){
|
173
|
newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(NameTypeDesignation.class, identifier, crud, null);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* {@inheritDoc}
|
179
|
*/
|
180
|
@Override
|
181
|
protected void guaranteePerEntityCRUDPermissions(NameTypeDesignation bean) {
|
182
|
// TODO Auto-generated method stub
|
183
|
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* {@inheritDoc}
|
188
|
*/
|
189
|
@Override
|
190
|
protected IService<NameTypeDesignation> getService() {
|
191
|
// No TypeDesignationService :( so I need override the generic save and delete methods
|
192
|
return null;
|
193
|
}
|
194
|
|
195
|
@Override
|
196
|
protected void deleteBean(NameTypeDesignation bean){
|
197
|
// deleteTypedesignation(uuid, uuid) needs to be called so the name is loaded in the transaction of the method and is saved.
|
198
|
DeleteResult deletResult = getRepo().getNameService().deleteTypeDesignation(typifiedNameInContext.getUuid(), bean.getUuid());
|
199
|
if(deletResult.isOk()){
|
200
|
EntityChangeEvent changeEvent = new EntityChangeEvent(bean, Type.REMOVED, (AbstractView) getView());
|
201
|
viewEventBus.publish(this, changeEvent);
|
202
|
} else {
|
203
|
CdmStore.handleDeleteresultInError(deletResult, getRepo().getSession());
|
204
|
}
|
205
|
}
|
206
|
|
207
|
|
208
|
/**
|
209
|
* {@inheritDoc}
|
210
|
*/
|
211
|
@Override
|
212
|
protected NameTypeDesignation preSaveBean(NameTypeDesignation bean) {
|
213
|
|
214
|
// the typifiedNames can only be set on the name side, so we need to
|
215
|
// handle changes explicitly here
|
216
|
HashSet<TaxonName> typifiedNames = new HashSet<>(bean.getTypifiedNames());
|
217
|
|
218
|
// handle adds
|
219
|
for(TaxonName name : typifiedNames){
|
220
|
if(name == null){
|
221
|
throw new NullPointerException("typifiedName must not be null");
|
222
|
}
|
223
|
if(!name.getTypeDesignations().contains(bean)){
|
224
|
name.addTypeDesignation(bean, false);
|
225
|
}
|
226
|
}
|
227
|
// handle removed
|
228
|
for(TaxonName name : typifiedNamesAsLoaded){
|
229
|
if(!typifiedNames.contains(name)){
|
230
|
name.removeTypeDesignation(bean);
|
231
|
}
|
232
|
// FIXME do we need to save the names here or is the delete cascaded from the typedesignation to the name?
|
233
|
}
|
234
|
|
235
|
// handle annotation changes
|
236
|
List<Annotation> annotations = getView().getAnnotationsField().getValue();
|
237
|
List<Annotation> currentAnnotations = new ArrayList<>(bean.getAnnotations());
|
238
|
List<Annotation> annotationsSeen = new ArrayList<>();
|
239
|
for(Annotation a : annotations){
|
240
|
if(a == null){
|
241
|
continue;
|
242
|
}
|
243
|
if(!currentAnnotations.contains(a)){
|
244
|
bean.addAnnotation(a);
|
245
|
}
|
246
|
annotationsSeen.add(a);
|
247
|
}
|
248
|
for(Annotation a : currentAnnotations){
|
249
|
if(!annotationsSeen.contains(a)){
|
250
|
bean.removeAnnotation(a);
|
251
|
}
|
252
|
}
|
253
|
|
254
|
|
255
|
return bean;
|
256
|
}
|
257
|
|
258
|
|
259
|
|
260
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
|
261
|
public void onTaxonNameEditorActionAdd(TaxonNameEditorAction action){
|
262
|
|
263
|
if(!isFromOwnView(action)){
|
264
|
return;
|
265
|
}
|
266
|
|
267
|
TaxonNamePopupEditor typeNamePopup = openPopupEditor(TaxonNamePopupEditor.class, action);
|
268
|
typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
269
|
typeNamePopup.withDeleteButton(true);
|
270
|
TaxonNamePopupEditorConfig.configure(typeNamePopup);
|
271
|
typeNamePopup.loadInEditor(null);
|
272
|
|
273
|
}
|
274
|
|
275
|
|
276
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
|
277
|
public void onTaxonNameEditorActionEdit(TaxonNameEditorAction action){
|
278
|
|
279
|
if(!isFromOwnView(action)){
|
280
|
return;
|
281
|
}
|
282
|
|
283
|
TaxonNamePopupEditor typeNamePopup = openPopupEditor(TaxonNamePopupEditor.class, action);
|
284
|
typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
285
|
typeNamePopup.withDeleteButton(true);
|
286
|
TaxonNamePopupEditorConfig.configure(typeNamePopup);
|
287
|
typeNamePopup.loadInEditor(action.getEntityUuid());
|
288
|
|
289
|
}
|
290
|
|
291
|
@EventBusListenerMethod
|
292
|
public void onEntityChangeEvent(EntityChangeEvent<?>event){
|
293
|
|
294
|
BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
|
295
|
|
296
|
if(boundTargetField != null){
|
297
|
if(boundTargetField.matchesPropertyIdPath("typeName")){
|
298
|
if(event.isCreateOrModifiedType()){
|
299
|
getCache().load(event.getEntity());
|
300
|
if(event.isCreatedType()){
|
301
|
getView().getTypeNameField().setValue((TaxonName) event.getEntity());
|
302
|
} else {
|
303
|
getView().getTypeNameField().reload();
|
304
|
}
|
305
|
}
|
306
|
if(event.isRemovedType()){
|
307
|
getView().getTypeNameField().selectNewItem(null);
|
308
|
}
|
309
|
|
310
|
}
|
311
|
}
|
312
|
}
|
313
|
|
314
|
}
|