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.FlushMode;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
import org.springframework.context.ApplicationEventPublisher;
|
14
|
import org.springframework.context.event.EventListener;
|
15
|
|
16
|
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
|
17
|
import eu.etaxonomy.vaadin.mvp.event.EditorDeleteEvent;
|
18
|
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
|
19
|
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
|
20
|
import eu.etaxonomy.vaadin.mvp.event.EditorViewEvent;
|
21
|
|
22
|
/**
|
23
|
*
|
24
|
* @author a.kohlbecker
|
25
|
* @since Apr 5, 2017
|
26
|
*
|
27
|
*/
|
28
|
public abstract class AbstractEditorPresenter<DTO extends Object, V extends ApplicationView<?>> extends AbstractPresenter<V> {
|
29
|
|
30
|
|
31
|
private static final long serialVersionUID = -6677074110764145236L;
|
32
|
|
33
|
FlushMode previousPreSaveEvenFlushMode = null;
|
34
|
|
35
|
@Autowired
|
36
|
protected ApplicationEventPublisher eventBus;
|
37
|
|
38
|
/**
|
39
|
* Load the bean to be edited in the editor freshly from the persistent storage.
|
40
|
* Ore create an new empty instance in case the supplied <code>identifier</code> is <code>null</code>.
|
41
|
*
|
42
|
* @param identifier
|
43
|
* @return
|
44
|
*/
|
45
|
protected abstract DTO loadBeanById(Object identifier);
|
46
|
|
47
|
/**
|
48
|
* This method is called directly before setting the bean as item data source to
|
49
|
* the field group of the editor.
|
50
|
* <p>
|
51
|
* Override this method to pre-process the bean if needed. This can be the case if
|
52
|
* you are using a persistence layer with short running session like Hibernate.
|
53
|
*
|
54
|
* @param bean
|
55
|
* @return
|
56
|
*/
|
57
|
protected DTO prepareAsFieldGroupDataSource(DTO bean){
|
58
|
|
59
|
return bean;
|
60
|
}
|
61
|
|
62
|
// @Override
|
63
|
// protected TransactionDefinition getTransactionDefinition(){
|
64
|
// super.getTransactionDefinition();
|
65
|
// if(definition.isReadOnly()){
|
66
|
// definition.setReadOnly(false);
|
67
|
// }
|
68
|
// return definition;
|
69
|
// }
|
70
|
|
71
|
/**
|
72
|
* Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
|
73
|
*
|
74
|
* @param preSaveEvent
|
75
|
*/
|
76
|
@EventListener
|
77
|
public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
|
78
|
if(!isFromOwnView(preSaveEvent)){
|
79
|
return;
|
80
|
}
|
81
|
getSession().setFlushMode(FlushMode.AUTO);
|
82
|
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
|
87
|
*
|
88
|
* @param saveEvent
|
89
|
*/
|
90
|
@EventListener
|
91
|
public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
|
92
|
if(!isFromOwnView(saveEvent)){
|
93
|
return;
|
94
|
}
|
95
|
DTO bean = saveEvent.getBean();
|
96
|
saveBean(bean);
|
97
|
getSession().setFlushMode(previousPreSaveEvenFlushMode);
|
98
|
previousPreSaveEvenFlushMode = null;
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
|
103
|
*
|
104
|
* @param saveEvent
|
105
|
*/
|
106
|
@EventListener
|
107
|
public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
|
108
|
if(!isFromOwnView(deleteEvent)){
|
109
|
return;
|
110
|
}
|
111
|
FlushMode previousFlushMode = getSession().getFlushMode();
|
112
|
getSession().setFlushMode(FlushMode.AUTO);
|
113
|
deleteBean(deleteEvent.getBean());
|
114
|
getSession().setFlushMode(previousFlushMode);
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* @param saveEvent
|
119
|
* @return
|
120
|
*/
|
121
|
protected boolean isFromOwnView(EditorViewEvent saveEvent) {
|
122
|
return saveEvent.getView().equals(getView());
|
123
|
}
|
124
|
|
125
|
protected Class<V> getViewType() {
|
126
|
return (Class<V>) super.getView().getClass();
|
127
|
}
|
128
|
|
129
|
protected boolean isFromOwnView(AbstractEditorAction action){
|
130
|
return action.getSourceView() != null && getView().equals(action.getSourceView());
|
131
|
}
|
132
|
|
133
|
|
134
|
protected abstract void saveBean(DTO bean);
|
135
|
|
136
|
/**
|
137
|
* @param bean
|
138
|
*/
|
139
|
protected abstract void deleteBean(DTO bean);
|
140
|
|
141
|
}
|