Project

General

Profile

Download (3.77 KB) Statistics
| Branch: | Tag: | Revision:
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
    protected BeanInstantiator<DTO> beanInstantiator = null;
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
     * @param beanInstantiator the beanInstantiator to set
51
     */
52
    public void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
53
        this.beanInstantiator = beanInstantiator;
54
    }
55

    
56
    /**
57
     * Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
58
     *
59
     * @param preSaveEvent
60
     */
61
    @EventListener
62
    public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
63
        if(!isFromOwnView(preSaveEvent)){
64
            return;
65
        }
66
//        getSession().setFlushMode(FlushMode.AUTO);
67

    
68
    }
69

    
70
    @EventListener
71
    public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
72
        if(!isFromOwnView(saveEvent)){
73
            return;
74
        }
75
        DTO bean = saveEvent.getBean();
76
        try {
77
            saveBean(bean);
78
        } catch(Exception e){
79
//            if(getSession().isOpen()){
80
//                getSession().clear();
81
//            }
82
            throw e; // re-throw after cleaning up the session
83
        } finally {
84
//            if(getSession().isOpen()){
85
//                getSession().setFlushMode(previousPreSaveEvenFlushMode);
86
//            }
87
//            previousPreSaveEvenFlushMode = null;
88
        }
89
    }
90

    
91
    /**
92
    * Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
93
    *
94
    * @param saveEvent
95
    */
96
   @EventListener
97
   public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
98
       if(!isFromOwnView(deleteEvent)){
99
           return;
100
       }
101
       FlushMode previousFlushMode = getSession().getFlushMode();
102
       getSession().setFlushMode(FlushMode.AUTO);
103
       deleteBean(deleteEvent.getBean());
104
       getSession().setFlushMode(previousFlushMode);
105
   }
106

    
107
    /**
108
     * @param saveEvent
109
     * @return
110
     */
111
    protected boolean isFromOwnView(EditorViewEvent saveEvent) {
112
        return saveEvent.getView().equals(getView());
113
    }
114

    
115
    protected Class<V> getViewType() {
116
        return (Class<V>) super.getView().getClass();
117
    }
118

    
119
    protected boolean isFromOwnView(AbstractEditorAction action){
120
        return action.getSourceView() != null && getView().equals(action.getSourceView());
121
    }
122

    
123

    
124
    protected abstract void saveBean(DTO bean);
125

    
126
    /**
127
     * @param bean
128
     */
129
    protected abstract void deleteBean(DTO bean);
130

    
131
}
(3-3/9)