Project

General

Profile

Download (3.22 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
 * Presenters of this type are usually be used in conjunction with a  {@link AbstractPopupEditor}.
22
 * The presenter automatically handles save and delete operations. The methods {@link #saveBean(Object)} and
23
 * {@link AbstractEditorPresenter#deleteBean(Object)} are executed internally in turn of an
24
 * {@link EditorSaveEvent} or {@link EditorDeleteEvent} which are send by the {@link AbstractPopupEditor#save()}
25
 * or {@link AbstractPopupEditor#delete()} method.
26
 *
27
 * @author a.kohlbecker
28
 * @since Apr 5, 2017
29
 *
30
 */
31
public abstract class AbstractEditorPresenter<DTO extends Object, V extends ApplicationView<?>> extends AbstractPresenter<V> {
32

    
33

    
34
    private static final long serialVersionUID = -6677074110764145236L;
35

    
36
    FlushMode previousPreSaveEvenFlushMode = null;
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
    @EventBusListenerMethod
57
    public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
58
        if(!isFromOwnView(preSaveEvent)){
59
            return;
60
        }
61
    }
62

    
63
    @EventBusListenerMethod
64
    public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
65
        if(!isFromOwnView(saveEvent)){
66
            return;
67
        }
68
        DTO bean = saveEvent.getBean();
69
        saveBean(bean);
70
    }
71

    
72
   /**
73
    * @param saveEvent
74
    */
75
   @EventBusListenerMethod
76
   public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
77
       if(!isFromOwnView(deleteEvent)){
78
           return;
79
       }
80
       deleteBean(deleteEvent.getBean());
81
   }
82

    
83
    /**
84
     * @param saveEvent
85
     * @return
86
     */
87
    protected boolean isFromOwnView(EditorViewEvent saveEvent) {
88
        return saveEvent.getView().equals(getView());
89
    }
90

    
91
    protected Class<V> getViewType() {
92
        return (Class<V>) super.getView().getClass();
93
    }
94

    
95
    protected boolean isFromOwnView(AbstractEditorAction action){
96
        return action.getSourceView() != null && getView().equals(action.getSourceView());
97
    }
98

    
99

    
100
    protected abstract void saveBean(DTO bean);
101

    
102
    /**
103
     * @param bean
104
     */
105
    protected abstract void deleteBean(DTO bean);
106

    
107
}
(3-3/9)