ref #7046 removing prepareAsFieldGroupDataSource() which was opening connectinos...
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / vaadin / mvp / AbstractEditorPresenter.java
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 /**
49 * Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
50 *
51 * @param preSaveEvent
52 */
53 @EventListener
54 public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
55 if(!isFromOwnView(preSaveEvent)){
56 return;
57 }
58 getSession().setFlushMode(FlushMode.AUTO);
59
60 }
61
62 /**
63 * Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
64 *
65 * @param saveEvent
66 */
67 @EventListener
68 public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
69 if(!isFromOwnView(saveEvent)){
70 return;
71 }
72 DTO bean = saveEvent.getBean();
73 saveBean(bean);
74 getSession().setFlushMode(previousPreSaveEvenFlushMode);
75 previousPreSaveEvenFlushMode = null;
76 }
77
78 /**
79 * Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
80 *
81 * @param saveEvent
82 */
83 @EventListener
84 public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
85 if(!isFromOwnView(deleteEvent)){
86 return;
87 }
88 FlushMode previousFlushMode = getSession().getFlushMode();
89 getSession().setFlushMode(FlushMode.AUTO);
90 deleteBean(deleteEvent.getBean());
91 getSession().setFlushMode(previousFlushMode);
92 }
93
94 /**
95 * @param saveEvent
96 * @return
97 */
98 protected boolean isFromOwnView(EditorViewEvent saveEvent) {
99 return saveEvent.getView().equals(getView());
100 }
101
102 protected Class<V> getViewType() {
103 return (Class<V>) super.getView().getClass();
104 }
105
106 protected boolean isFromOwnView(AbstractEditorAction action){
107 return action.getSourceView() != null && getView().equals(action.getSourceView());
108 }
109
110
111 protected abstract void saveBean(DTO bean);
112
113 /**
114 * @param bean
115 */
116 protected abstract void deleteBean(DTO bean);
117
118 }