Project

General

Profile

Download (2.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.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
        if(!isFromOwnView(preSaveEvent)){
54
            return;
55
        }
56
    }
57

    
58
    @EventBusListenerMethod
59
    public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
60
        if(!isFromOwnView(saveEvent)){
61
            return;
62
        }
63
        DTO bean = saveEvent.getBean();
64
        saveBean(bean);
65
    }
66

    
67
   /**
68
    * @param saveEvent
69
    */
70
   @EventBusListenerMethod
71
   public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
72
       if(!isFromOwnView(deleteEvent)){
73
           return;
74
       }
75
       deleteBean(deleteEvent.getBean());
76
   }
77

    
78
    /**
79
     * @param saveEvent
80
     * @return
81
     */
82
    protected boolean isFromOwnView(EditorViewEvent saveEvent) {
83
        return saveEvent.getView().equals(getView());
84
    }
85

    
86
    protected Class<V> getViewType() {
87
        return (Class<V>) super.getView().getClass();
88
    }
89

    
90
    protected boolean isFromOwnView(AbstractEditorAction action){
91
        return action.getSourceView() != null && getView().equals(action.getSourceView());
92
    }
93

    
94

    
95
    protected abstract void saveBean(DTO bean);
96

    
97
    /**
98
     * @param bean
99
     */
100
    protected abstract void deleteBean(DTO bean);
101

    
102
}
(3-3/9)