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.vaadin.spring.events.annotation.EventBusListenerMethod;
|
13
|
|
14
|
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
|
15
|
import eu.etaxonomy.vaadin.mvp.event.EditorDeleteEvent;
|
16
|
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
|
17
|
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
|
18
|
import eu.etaxonomy.vaadin.mvp.event.EditorViewEvent;
|
19
|
|
20
|
/**
|
21
|
*
|
22
|
* @author a.kohlbecker
|
23
|
* @since Apr 5, 2017
|
24
|
*
|
25
|
*/
|
26
|
public abstract class AbstractEditorPresenter<DTO extends Object, V extends ApplicationView<?>> extends AbstractPresenter<V> {
|
27
|
|
28
|
|
29
|
private static final long serialVersionUID = -6677074110764145236L;
|
30
|
|
31
|
FlushMode previousPreSaveEvenFlushMode = null;
|
32
|
|
33
|
protected BeanInstantiator<DTO> beanInstantiator = null;
|
34
|
|
35
|
/**
|
36
|
* Load the bean to be edited in the editor freshly from the persistent storage.
|
37
|
* Ore create an new empty instance in case the supplied <code>identifier</code> is <code>null</code>.
|
38
|
*
|
39
|
* @param identifier
|
40
|
* @return
|
41
|
*/
|
42
|
protected abstract DTO loadBeanById(Object identifier);
|
43
|
|
44
|
/**
|
45
|
* @param beanInstantiator the beanInstantiator to set
|
46
|
*/
|
47
|
public void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
|
48
|
this.beanInstantiator = beanInstantiator;
|
49
|
}
|
50
|
|
51
|
@EventBusListenerMethod
|
52
|
public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
|
53
|
}
|
54
|
|
55
|
@EventBusListenerMethod
|
56
|
public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
|
57
|
|
58
|
DTO bean = saveEvent.getBean();
|
59
|
saveBean(bean);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
|
64
|
*
|
65
|
* @param saveEvent
|
66
|
*/
|
67
|
@EventBusListenerMethod
|
68
|
public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
|
69
|
|
70
|
FlushMode previousFlushMode = getSession().getFlushMode();
|
71
|
getSession().setFlushMode(FlushMode.AUTO);
|
72
|
deleteBean(deleteEvent.getBean());
|
73
|
getSession().setFlushMode(previousFlushMode);
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* @param saveEvent
|
78
|
* @return
|
79
|
*/
|
80
|
protected boolean isFromOwnView(EditorViewEvent saveEvent) {
|
81
|
return saveEvent.getView().equals(getView());
|
82
|
}
|
83
|
|
84
|
protected Class<V> getViewType() {
|
85
|
return (Class<V>) super.getView().getClass();
|
86
|
}
|
87
|
|
88
|
protected boolean isFromOwnView(AbstractEditorAction action){
|
89
|
return action.getSourceView() != null && getView().equals(action.getSourceView());
|
90
|
}
|
91
|
|
92
|
|
93
|
protected abstract void saveBean(DTO bean);
|
94
|
|
95
|
/**
|
96
|
* @param bean
|
97
|
*/
|
98
|
protected abstract void deleteBean(DTO bean);
|
99
|
|
100
|
}
|