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