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