Project

General

Profile

Download (2.75 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.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.context.ApplicationEventPublisher;
13
import org.springframework.context.event.EventListener;
14

    
15
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
16
import eu.etaxonomy.vaadin.mvp.event.EditorDeleteEvent;
17
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
18
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
19
import eu.etaxonomy.vaadin.mvp.event.EditorViewEvent;
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
    @Autowired
32
    protected ApplicationEventPublisher eventBus;
33

    
34
    /**
35
     * This method is called directly before setting the bean as item data source to
36
     * the field group of the editor.
37
     * <p>
38
     * Override this method to pre-process the bean if needed. This can be the case if
39
     * you are using a persistence layer with short running session like Hibernate.
40
     *
41
     * @param bean
42
     * @return
43
     */
44
    protected DTO prepareAsFieldGroupDataSource(DTO bean){
45
        return bean;
46
    }
47

    
48
    @EventListener
49
    public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
50
        if(!isFromOwnView(preSaveEvent)){
51
            return;
52
        }
53
    }
54

    
55
    /**
56
     *
57
     * @param saveEvent
58
     */
59
    @EventListener
60
    public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
61
        if(!isFromOwnView(saveEvent)){
62
            return;
63
        }
64
        DTO bean = saveEvent.getBean();
65
        saveBean(bean);
66
    }
67

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

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

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

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

    
96
    protected abstract void saveBean(DTO bean);
97

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

    
103
}
(3-3/8)