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
|
|
19
|
import eu.etaxonomy.cdm.api.service.IService;
|
20
|
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
|
21
|
import eu.etaxonomy.cdm.debug.PersistentContextAnalyzer;
|
22
|
import eu.etaxonomy.cdm.model.ICdmCacher;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.common.User;
|
25
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
26
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
|
27
|
import eu.etaxonomy.cdm.service.CdmStore;
|
28
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
|
29
|
import eu.etaxonomy.cdm.vaadin.permission.UserHelper;
|
30
|
import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
|
31
|
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
|
32
|
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
|
33
|
|
34
|
/**
|
35
|
* Provides generic save operations of modified cdm entities.
|
36
|
*
|
37
|
* @author a.kohlbecker
|
38
|
* @since Apr 5, 2017
|
39
|
*
|
40
|
*/
|
41
|
public abstract class AbstractCdmEditorPresenter<DTO extends CdmBase, V extends ApplicationView<?>> extends AbstractEditorPresenter<DTO, V>
|
42
|
implements CachingPresenter {
|
43
|
|
44
|
private static final long serialVersionUID = 2218185546277084261L;
|
45
|
|
46
|
private static final Logger logger = Logger.getLogger(AbstractCdmEditorPresenter.class);
|
47
|
|
48
|
/**
|
49
|
* if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entity which will be
|
50
|
* granted to the current use as long this grant is not assigned yet.
|
51
|
*/
|
52
|
protected EnumSet<CRUD> crud = null;
|
53
|
|
54
|
|
55
|
private ICdmCacher cache;
|
56
|
|
57
|
private java.util.Collection<CdmBase> rootEntities = new HashSet<>();
|
58
|
|
59
|
public AbstractCdmEditorPresenter() {
|
60
|
super();
|
61
|
logger.trace(this._toString() + " constructor");
|
62
|
}
|
63
|
|
64
|
CdmStore<DTO, IService<DTO>> store ;
|
65
|
|
66
|
protected CdmAuthority newAuthorityCreated;
|
67
|
|
68
|
protected CdmStore<DTO, IService<DTO>> getStore() {
|
69
|
if(store == null){
|
70
|
store = new CdmStore<>(getRepo(), getService());
|
71
|
}
|
72
|
return store;
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
protected DTO loadBeanById(Object identifier) {
|
77
|
|
78
|
DTO cdmEntitiy;
|
79
|
if(identifier != null) {
|
80
|
UUID uuidIdentifier = (UUID)identifier;
|
81
|
// CdmAuthority is needed before the bean is loaded into the session.
|
82
|
// otherwise adding the authority to the user would cause a flush
|
83
|
cdmEntitiy = loadCdmEntity(uuidIdentifier);
|
84
|
} else {
|
85
|
cdmEntitiy = loadCdmEntity(null);
|
86
|
if(cdmEntitiy != null){
|
87
|
guaranteePerEntityCRUDPermissions(cdmEntitiy);
|
88
|
}
|
89
|
}
|
90
|
cache = new CdmTransientEntityCacher(this);
|
91
|
// need to use load but put see #7214
|
92
|
cdmEntitiy = cache.load(cdmEntitiy);
|
93
|
addRootEntity(cdmEntitiy);
|
94
|
|
95
|
return cdmEntitiy;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* @param cdmEntitiy
|
100
|
*/
|
101
|
@Override
|
102
|
protected void adaptToUserPermission(DTO cdmEntitiy) {
|
103
|
UserHelper userHelper = UserHelper.fromSession();
|
104
|
boolean canDelte = userHelper.userHasPermission(cdmEntitiy, CRUD.DELETE);
|
105
|
boolean canEdit = userHelper.userHasPermission(cdmEntitiy, CRUD.UPDATE);
|
106
|
|
107
|
User user = userHelper.user();
|
108
|
|
109
|
if(AbstractCdmPopupEditor.class.isAssignableFrom(getView().getClass())){
|
110
|
AbstractCdmPopupEditor popupView = ((AbstractCdmPopupEditor)getView());
|
111
|
|
112
|
if(!canEdit){
|
113
|
popupView.setReadOnly(true); // never reset true to false here!
|
114
|
logger.debug("setting editor to readonly");
|
115
|
}
|
116
|
if(!canDelte){
|
117
|
popupView.withDeleteButton(false);
|
118
|
logger.debug("removing delete button");
|
119
|
}
|
120
|
}
|
121
|
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* @param identifier
|
126
|
* @return
|
127
|
*/
|
128
|
protected abstract DTO loadCdmEntity(UUID uuid);
|
129
|
|
130
|
/**
|
131
|
* Grant per entity CdmAuthority to the current user <b>for the bean which is not yet loaded</b>
|
132
|
* into the editor. The <code>CRUD</code> to be granted are stored in the <code>crud</code> field.
|
133
|
*/
|
134
|
protected abstract void guaranteePerEntityCRUDPermissions(UUID identifier);
|
135
|
|
136
|
/**
|
137
|
* Grant per entity CdmAuthority to the current user for the bean which is loaded
|
138
|
* into the editor. The <code>CRUD</code> to be granted are stored in the <code>crud</code> field.
|
139
|
*/
|
140
|
protected abstract void guaranteePerEntityCRUDPermissions(DTO bean);
|
141
|
|
142
|
/**
|
143
|
* @return
|
144
|
*/
|
145
|
protected abstract IService<DTO> getService();
|
146
|
|
147
|
@SuppressWarnings("unchecked")
|
148
|
@Override
|
149
|
// @EventBusListenerMethod // already annotated at super class
|
150
|
public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
|
151
|
|
152
|
if(!isFromOwnView(preSaveEvent)){
|
153
|
return;
|
154
|
}
|
155
|
super.onEditorPreSaveEvent(preSaveEvent);
|
156
|
}
|
157
|
|
158
|
@SuppressWarnings("unchecked")
|
159
|
@Override
|
160
|
// @EventBusListenerMethod // already annotated at super class
|
161
|
public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
|
162
|
|
163
|
if(!isFromOwnView(saveEvent)){
|
164
|
return;
|
165
|
}
|
166
|
|
167
|
// the bean is now updated with the changes made by the user
|
168
|
DTO bean = saveEvent.getBean();
|
169
|
|
170
|
if(logger.isTraceEnabled()){
|
171
|
PersistentContextAnalyzer pca = new PersistentContextAnalyzer(bean);
|
172
|
pca.printEntityGraph(System.err);
|
173
|
pca.printCopyEntities(System.err);
|
174
|
}
|
175
|
bean = handleTransientProperties(bean);
|
176
|
|
177
|
if(logger.isTraceEnabled()){
|
178
|
PersistentContextAnalyzer pca = new PersistentContextAnalyzer(bean);
|
179
|
pca.printEntityGraph(System.err);
|
180
|
pca.printCopyEntities(System.err);
|
181
|
}
|
182
|
try {
|
183
|
EntityChangeEvent changeEvent = getStore().saveBean(bean, (AbstractView) getView());
|
184
|
|
185
|
if(changeEvent != null){
|
186
|
viewEventBus.publish(this, changeEvent);
|
187
|
}
|
188
|
} catch (HibernateException e){
|
189
|
if(newAuthorityCreated != null){
|
190
|
UserHelper.fromSession().removeAuthorityForCurrentUser(newAuthorityCreated);
|
191
|
}
|
192
|
throw e;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* EditorPresenters for beans with transient properties should overwrite this method to
|
198
|
* update the beanItem with the changes made to the transient properties.
|
199
|
* <p>
|
200
|
* This is necessary because Vaadin MethodProperties are readonly when no setter is
|
201
|
* available. This can be the case with transient properties. Automatic updating
|
202
|
* of the property during the fieldGroup commit does not work in this case.
|
203
|
*
|
204
|
* @deprecated editors should operate on DTOs instead, remove this method if unused.
|
205
|
*/
|
206
|
@Deprecated
|
207
|
protected DTO handleTransientProperties(DTO bean) {
|
208
|
// no need to handle transient properties in the generic case
|
209
|
return bean;
|
210
|
}
|
211
|
|
212
|
// @Override
|
213
|
// protected DTO prepareAsFieldGroupDataSource(DTO bean){
|
214
|
// DTO mergedBean = getStore().mergedBean(bean);
|
215
|
// // DTO mergedBean = bean;
|
216
|
// return mergedBean;
|
217
|
// }
|
218
|
|
219
|
/**
|
220
|
* If the bean is contained in the session it is being updated by
|
221
|
* doing an evict and merge. The fieldGroup is updated with the merged bean.
|
222
|
*
|
223
|
* @param bean
|
224
|
*
|
225
|
* @return The bean merged to the session or original bean in case a merge was not necessary.
|
226
|
*/
|
227
|
private DTO mergedBean(DTO bean) {
|
228
|
DTO mergedBean = getStore().mergedBean(bean);
|
229
|
((AbstractPopupEditor<DTO, AbstractCdmEditorPresenter<DTO, V>>)getView()).updateItemDataSource(mergedBean);
|
230
|
return mergedBean;
|
231
|
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
protected
|
236
|
final void saveBean(DTO bean){
|
237
|
// blank implementation, since this is not needed in this or any sub class
|
238
|
// see onEditorSaveEvent() instead
|
239
|
}
|
240
|
|
241
|
@Override
|
242
|
protected void deleteBean(DTO bean){
|
243
|
EntityChangeEvent changeEvent = getStore().deleteBean(bean, (AbstractView) getView());
|
244
|
if(changeEvent != null){
|
245
|
viewEventBus.publish(this, changeEvent);
|
246
|
}
|
247
|
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* @param crud
|
252
|
*/
|
253
|
public void setGrantsForCurrentUser(EnumSet<CRUD> crud) {
|
254
|
this.crud = crud;
|
255
|
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* {@inheritDoc}
|
260
|
*/
|
261
|
@Override
|
262
|
public ICdmCacher getCache() {
|
263
|
return cache;
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* {@inheritDoc}
|
268
|
*/
|
269
|
@Override
|
270
|
public void addRootEntity(CdmBase entity) {
|
271
|
rootEntities.add(entity);
|
272
|
}
|
273
|
|
274
|
|
275
|
/**
|
276
|
* {@inheritDoc}
|
277
|
*/
|
278
|
@Override
|
279
|
public Collection<CdmBase> getRootEntities() {
|
280
|
return rootEntities;
|
281
|
}
|
282
|
|
283
|
|
284
|
}
|