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.vaadin.mvp;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.hibernate.HibernateException;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.service.IService;
|
21
|
import eu.etaxonomy.cdm.api.util.UserHelper;
|
22
|
import eu.etaxonomy.cdm.cache.CdmTransientEntityAndUuidCacher;
|
23
|
import eu.etaxonomy.cdm.debug.PersistentContextAnalyzer;
|
24
|
import eu.etaxonomy.cdm.model.ICdmEntityUuidCacher;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.permission.CRUD;
|
27
|
import eu.etaxonomy.cdm.persistence.permission.CdmAuthority;
|
28
|
import eu.etaxonomy.cdm.service.CdmBeanItemContainerFactory;
|
29
|
import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
|
30
|
import eu.etaxonomy.cdm.service.CdmStore;
|
31
|
import eu.etaxonomy.cdm.service.UserHelperAccess;
|
32
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
|
33
|
import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
|
34
|
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
|
35
|
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
|
36
|
|
37
|
/**
|
38
|
* Provides generic save operations of modified cdm entities.
|
39
|
*
|
40
|
* @author a.kohlbecker
|
41
|
* @since Apr 5, 2017
|
42
|
*/
|
43
|
public abstract class CdmEditorPresenterBase<DTO, CDM extends CdmBase, V extends ApplicationView<?>> extends AbstractEditorPresenter<DTO, V>
|
44
|
implements CachingPresenter {
|
45
|
|
46
|
private static final long serialVersionUID = 2218185546277084261L;
|
47
|
|
48
|
private static final Logger logger = Logger.getLogger(CdmEditorPresenterBase.class);
|
49
|
|
50
|
protected BeanInstantiator<DTO> beanInstantiator = null;
|
51
|
|
52
|
@Autowired
|
53
|
protected CdmBeanItemContainerFactory cdmBeanItemContainerFactory;
|
54
|
|
55
|
@Autowired
|
56
|
protected CdmFilterablePagingProviderFactory pagingProviderFactory;
|
57
|
|
58
|
/**
|
59
|
* @param beanInstantiator the beanInstantiator to set
|
60
|
*/
|
61
|
public void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
|
62
|
this.beanInstantiator = beanInstantiator;
|
63
|
}
|
64
|
|
65
|
|
66
|
protected DTO createNewBean() {
|
67
|
if(this.beanInstantiator != null){
|
68
|
return beanInstantiator.createNewBean();
|
69
|
}
|
70
|
return defaultBeanInstantiator().createNewBean();
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* @return
|
75
|
*/
|
76
|
protected abstract BeanInstantiator<DTO> defaultBeanInstantiator();
|
77
|
|
78
|
/**
|
79
|
* if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entity which will be
|
80
|
* granted to the current use as long this grant is not assigned yet.
|
81
|
*/
|
82
|
protected EnumSet<CRUD> crud = null;
|
83
|
|
84
|
|
85
|
private ICdmEntityUuidCacher cache;
|
86
|
|
87
|
private java.util.Collection<CdmBase> rootEntities = new HashSet<>();
|
88
|
|
89
|
public CdmEditorPresenterBase() {
|
90
|
super();
|
91
|
logger.trace(this._toString() + " constructor");
|
92
|
}
|
93
|
|
94
|
@Autowired
|
95
|
protected CdmStore cdmStore;
|
96
|
|
97
|
protected CdmAuthority newAuthorityCreated;
|
98
|
|
99
|
@Override
|
100
|
protected DTO loadBeanById(Object identifier) {
|
101
|
|
102
|
CDM cdmEntitiy;
|
103
|
if(identifier != null) {
|
104
|
UUID uuidIdentifier = (UUID)identifier;
|
105
|
// CdmAuthority is needed before the bean is loaded into the session.
|
106
|
// otherwise adding the authority to the user would cause a flush
|
107
|
cdmEntitiy = loadCdmEntity(uuidIdentifier);
|
108
|
guaranteePerEntityCRUDPermissions(cdmEntitiy);
|
109
|
} else {
|
110
|
cdmEntitiy = loadCdmEntity(null);
|
111
|
if(cdmEntitiy != null){
|
112
|
guaranteePerEntityCRUDPermissions(cdmEntitiy);
|
113
|
}
|
114
|
}
|
115
|
DTO dto = initializeCache(cdmEntitiy);
|
116
|
|
117
|
return dto;
|
118
|
}
|
119
|
|
120
|
|
121
|
/**
|
122
|
* @param cdmEntitiy the CDM entity to initialize the cache with.
|
123
|
*/
|
124
|
protected final DTO initializeCache(CDM cdmEntitiy) {
|
125
|
cache = new CdmTransientEntityAndUuidCacher(this);
|
126
|
// need to use load but put see #7214
|
127
|
cdmEntitiy = cache.load(cdmEntitiy);
|
128
|
addRootEntity(cdmEntitiy);
|
129
|
|
130
|
DTO dto = createDTODecorator(cdmEntitiy);
|
131
|
return dto;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* @param cdmEntitiy
|
136
|
* @return
|
137
|
*/
|
138
|
protected abstract DTO createDTODecorator(CDM cdmEntitiy);
|
139
|
|
140
|
/**
|
141
|
* @param cdmEntitiy
|
142
|
*/
|
143
|
@Override
|
144
|
protected void adaptToUserPermission(DTO dto) {
|
145
|
|
146
|
CDM cdmEntitiy = cdmEntity(dto);
|
147
|
|
148
|
UserHelper userHelper = UserHelperAccess.userHelper();
|
149
|
boolean canDelte = userHelper.userHasPermission(cdmEntitiy, CRUD.DELETE);
|
150
|
boolean canEdit = userHelper.userHasPermission(cdmEntitiy, CRUD.UPDATE);
|
151
|
|
152
|
if(AbstractPopupEditor.class.isAssignableFrom(getView().getClass())){
|
153
|
AbstractPopupEditor popupView = ((AbstractPopupEditor)getView());
|
154
|
|
155
|
if(cdmEntitiy.isPersited() && !canEdit){
|
156
|
popupView.setReadOnly(true); // never reset true to false here!
|
157
|
logger.debug("setting editor to readonly");
|
158
|
}
|
159
|
if(!cdmEntitiy.isPersited() || !canDelte){
|
160
|
popupView.withDeleteButton(false);
|
161
|
logger.debug("removing delete button");
|
162
|
}
|
163
|
}
|
164
|
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* @param dto
|
169
|
* @return
|
170
|
*/
|
171
|
protected abstract CDM cdmEntity(DTO dto);
|
172
|
|
173
|
/**
|
174
|
* @param identifier
|
175
|
* @return
|
176
|
*/
|
177
|
protected abstract CDM loadCdmEntity(UUID uuid);
|
178
|
|
179
|
/**
|
180
|
* Grant per entity CdmAuthority to the current user <b>for the bean which is not yet loaded</b>
|
181
|
* into the editor. The <code>CRUD</code> to be granted are stored in the <code>crud</code> field.
|
182
|
*/
|
183
|
protected abstract void guaranteePerEntityCRUDPermissions(UUID identifier);
|
184
|
|
185
|
/**
|
186
|
* Grant per entity CdmAuthority to the current user for the bean which is loaded
|
187
|
* into the editor. The <code>CRUD</code> to be granted are stored in the <code>crud</code> field.
|
188
|
*/
|
189
|
protected abstract void guaranteePerEntityCRUDPermissions(CDM bean);
|
190
|
|
191
|
/**
|
192
|
* @return
|
193
|
*/
|
194
|
protected abstract IService<CDM> getService();
|
195
|
|
196
|
@SuppressWarnings("unchecked")
|
197
|
@Override
|
198
|
// @EventBusListenerMethod // already annotated at super class
|
199
|
public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
|
200
|
|
201
|
if(!isFromOwnView(preSaveEvent)){
|
202
|
return;
|
203
|
}
|
204
|
super.onEditorPreSaveEvent(preSaveEvent);
|
205
|
}
|
206
|
|
207
|
@Override
|
208
|
// @EventBusListenerMethod // already annotated at super class
|
209
|
public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
|
210
|
|
211
|
if(!isFromOwnView(saveEvent)){
|
212
|
return;
|
213
|
}
|
214
|
|
215
|
// the bean is now updated with the changes made by the user
|
216
|
DTO dto = saveEvent.getBean();
|
217
|
CDM cdmEntity = cdmEntity(dto);
|
218
|
|
219
|
if(logger.isTraceEnabled()){
|
220
|
PersistentContextAnalyzer pca = new PersistentContextAnalyzer(cdmEntity);
|
221
|
pca.printEntityGraph(System.err);
|
222
|
pca.printCopyEntities(System.err);
|
223
|
}
|
224
|
|
225
|
if(logger.isTraceEnabled()){
|
226
|
PersistentContextAnalyzer pca = new PersistentContextAnalyzer(cdmEntity);
|
227
|
pca.printEntityGraph(System.err);
|
228
|
pca.printCopyEntities(System.err);
|
229
|
}
|
230
|
EntityChangeEvent<?> changeEvent = null;
|
231
|
try {
|
232
|
dto = preSaveBean(dto);
|
233
|
changeEvent = cdmStore.saveBean(cdmEntity, (AbstractView<?>) getView());
|
234
|
|
235
|
if(changeEvent != null){
|
236
|
viewEventBus.publish(this, changeEvent);
|
237
|
}
|
238
|
} catch (HibernateException e){
|
239
|
if(newAuthorityCreated != null){
|
240
|
UserHelperAccess.userHelper().removeAuthorityForCurrentUser(newAuthorityCreated);
|
241
|
}
|
242
|
throw e;
|
243
|
} finally {
|
244
|
postSaveBean(changeEvent);
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* This method is intended to be used for the following purposes:
|
250
|
* <ol>
|
251
|
* <li>
|
252
|
* EditorPresenters for beans with transient properties can overwrite this method to
|
253
|
* update the beanItem with the changes made to the transient properties.
|
254
|
* This can be necessary because Vaadin MethodProperties are readonly when no setter is
|
255
|
* available. This can be the case with transient properties. Automatic updating
|
256
|
* of the property during the fieldGroup commit does not work in this case.
|
257
|
* Presenters, however, should <b>operate on DTOs instead, which can implement the missing setter</b>.</li>
|
258
|
*
|
259
|
* <li>When modifying a bi-directional relation between two instances the user would
|
260
|
* need to have GrantedAuthorities for both sides of the relationship. This, however is not
|
261
|
* always possible. As a temporary solution the user can be granted the missing authority just
|
262
|
* for the time of saving the new relationship. You may also want to implement
|
263
|
* {@link #postSaveBean(EntityChangeEvent)} in this case.
|
264
|
* See {@link https://dev.e-taxonomy.eu/redmine/issues/7390 #7390}
|
265
|
* </li>
|
266
|
* </ol>
|
267
|
*
|
268
|
*/
|
269
|
protected DTO preSaveBean(DTO bean) {
|
270
|
// blank implementation, to be implemented by sub classes if needed
|
271
|
return bean;
|
272
|
}
|
273
|
|
274
|
@Override
|
275
|
protected
|
276
|
final void saveBean(DTO bean){
|
277
|
// blank implementation, since this is not needed in this or any sub class
|
278
|
// see onEditorSaveEvent() instead
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* Called after saving the DTO to the persistent storage.
|
283
|
* This method is called in any case even if the save operation failed.
|
284
|
* See {@link #postSaveBean(EntityChangeEvent)}.
|
285
|
*
|
286
|
* @param changeEvent may be null in case of errors during the save operation
|
287
|
*/
|
288
|
protected void postSaveBean(EntityChangeEvent changeEvent) {
|
289
|
// blank implementation, to be implemented by sub classes if needed
|
290
|
}
|
291
|
|
292
|
@Override
|
293
|
protected void deleteBean(DTO bean){
|
294
|
CDM cdmEntity = cdmEntity(bean);
|
295
|
EntityChangeEvent<?> changeEvent = cdmStore.deleteBean(cdmEntity, (AbstractView) getView());
|
296
|
if(changeEvent != null){
|
297
|
viewEventBus.publish(this, changeEvent);
|
298
|
}
|
299
|
}
|
300
|
|
301
|
public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
|
302
|
this.crud = crud;
|
303
|
|
304
|
}
|
305
|
|
306
|
@Override
|
307
|
public ICdmEntityUuidCacher getCache() {
|
308
|
return cache;
|
309
|
}
|
310
|
|
311
|
@Override
|
312
|
public void addRootEntity(CdmBase entity) {
|
313
|
rootEntities.add(entity);
|
314
|
}
|
315
|
|
316
|
@Override
|
317
|
public Collection<CdmBase> getRootEntities() {
|
318
|
return rootEntities;
|
319
|
}
|
320
|
|
321
|
@Override
|
322
|
public void destroy() throws Exception {
|
323
|
super.destroy();
|
324
|
disposeCache();
|
325
|
}
|
326
|
|
327
|
@Override
|
328
|
public void disposeCache() {
|
329
|
cache.dispose();
|
330
|
}
|
331
|
}
|