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 org.hibernate.Session;
|
12
|
import org.springframework.context.event.EventListener;
|
13
|
import org.springframework.transaction.TransactionStatus;
|
14
|
|
15
|
import com.vaadin.data.fieldgroup.BeanFieldGroup;
|
16
|
import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
|
17
|
import com.vaadin.data.util.BeanItem;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
20
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
|
21
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent.Type;
|
22
|
|
23
|
/**
|
24
|
* @author a.kohlbecker
|
25
|
* @since Apr 5, 2017
|
26
|
*
|
27
|
*/
|
28
|
public abstract class AbstractCdmEditorPresenter<DTO extends CdmBase> extends AbstractEditorPresenter<DTO> {
|
29
|
|
30
|
private static final long serialVersionUID = 2218185546277084261L;
|
31
|
|
32
|
TransactionStatus tx = null;
|
33
|
|
34
|
@Override
|
35
|
@EventListener
|
36
|
public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
|
37
|
tx = getRepo().startTransaction(true);
|
38
|
// merge the bean and update the fieldGroup with the merged bean, so that updating
|
39
|
// of field values in turn of the commit are can not cause LazyInitializationExeptions
|
40
|
// the bean still has the original values at this point
|
41
|
mergedBean(preSaveEvent.getCommitEvent());
|
42
|
|
43
|
}
|
44
|
|
45
|
@Override
|
46
|
@EventListener
|
47
|
public void onEditorSaveEvent(EditorSaveEvent saveEvent){
|
48
|
// the bean is now updated with the changes made by the user
|
49
|
// merge the bean into the session, ...
|
50
|
DTO bean = mergedBean(saveEvent.getCommitEvent());
|
51
|
Type changeEventType;
|
52
|
if(bean.getId() > 1){
|
53
|
changeEventType = Type.MODIFIED;
|
54
|
} else {
|
55
|
changeEventType = Type.CREATED;
|
56
|
}
|
57
|
getRepo().getCommonService().saveOrUpdate(bean);
|
58
|
getSession().flush();
|
59
|
getRepo().commitTransaction(tx);
|
60
|
tx = null;
|
61
|
eventBus.publishEvent(new EntityChangeEvent(bean.getClass(), bean.getId(), changeEventType));
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Obtains the bean from the fieldGroup, merges the bean into the session and
|
66
|
* updates the fieldGroup with the merged bean.
|
67
|
*
|
68
|
* @param CommitEvent
|
69
|
* @return The bean merged to the session
|
70
|
*/
|
71
|
private DTO mergedBean(CommitEvent commitEvent) {
|
72
|
// using just some service to get hold of the session
|
73
|
Session session = getSession();
|
74
|
@SuppressWarnings("unchecked")
|
75
|
BeanItem<DTO> itemDataSource = ((BeanFieldGroup<DTO>)commitEvent.getFieldBinder()).getItemDataSource();
|
76
|
DTO bean = itemDataSource.getBean();
|
77
|
// evict bean before merge to avoid duplicate beans in same session
|
78
|
session.evict(bean);
|
79
|
@SuppressWarnings("unchecked")
|
80
|
DTO mergedBean = (DTO) session.merge(bean);
|
81
|
itemDataSource.setBean(mergedBean);
|
82
|
return mergedBean;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* @return
|
87
|
*/
|
88
|
private Session getSession() {
|
89
|
return getRepo().getUserService().getSession();
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
protected final void saveBean(DTO bean){
|
94
|
// blank implementation, since this is not needed in this or any sub class
|
95
|
}
|
96
|
|
97
|
}
|