Project

General

Profile

Download (3.46 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
     * Set ui elements to readonly or disabled to adapt the editor to
51
     * the permissions that are given to the current user etc.
52
     *
53
     * @param beanToEdit
54
     */
55
    protected void adaptToUserPermission(DTO beanToEdit) {
56

    
57
    }
58

    
59
    /**
60
     * @param beanInstantiator the beanInstantiator to set
61
     */
62
    public void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
63
        this.beanInstantiator = beanInstantiator;
64
    }
65

    
66
    @EventBusListenerMethod
67
    public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
68
        if(!isFromOwnView(preSaveEvent)){
69
            return;
70
        }
71
    }
72

    
73
    @EventBusListenerMethod
74
    public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
75
        if(!isFromOwnView(saveEvent)){
76
            return;
77
        }
78
        DTO bean = saveEvent.getBean();
79
        saveBean(bean);
80
    }
81

    
82
   /**
83
    * @param saveEvent
84
    */
85
   @EventBusListenerMethod
86
   public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
87
       if(!isFromOwnView(deleteEvent)){
88
           return;
89
       }
90
       deleteBean(deleteEvent.getBean());
91
   }
92

    
93
    /**
94
     * @param saveEvent
95
     * @return
96
     */
97
    protected boolean isFromOwnView(EditorViewEvent saveEvent) {
98
        return saveEvent.getView().equals(getView());
99
    }
100

    
101
    protected Class<V> getViewType() {
102
        return (Class<V>) super.getView().getClass();
103
    }
104

    
105
    protected boolean isFromOwnView(AbstractEditorAction action){
106
        return action.getSourceView() != null && getView().equals(action.getSourceView());
107
    }
108

    
109

    
110
    protected abstract void saveBean(DTO bean);
111

    
112
    /**
113
     * @param bean
114
     */
115
    protected abstract void deleteBean(DTO bean);
116

    
117
}
(3-3/9)