Project

General

Profile

Download (5.32 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 com.vaadin.data.Property;
15
import com.vaadin.ui.Component;
16
import com.vaadin.ui.Field;
17

    
18
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
19
import eu.etaxonomy.vaadin.event.FieldReplaceEvent;
20
import eu.etaxonomy.vaadin.mvp.event.EditorDeleteEvent;
21
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
22
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
23
import eu.etaxonomy.vaadin.mvp.event.EditorViewEvent;
24
import eu.etaxonomy.vaadin.ui.view.PopupView;
25
import eu.etaxonomy.vaadin.util.PropertyIdPath;
26

    
27
/**
28
 * Presenters of this type are usually be used in conjunction with a  {@link AbstractPopupEditor}.
29
 * The presenter automatically handles save and delete operations. The methods {@link #saveBean(Object)} and
30
 * {@link AbstractEditorPresenter#deleteBean(Object)} are executed internally in turn of an
31
 * {@link EditorSaveEvent} or {@link EditorDeleteEvent} which are send by the {@link AbstractPopupEditor#save()}
32
 * or {@link AbstractPopupEditor#delete()} method.
33
 *
34
 * @author a.kohlbecker
35
 * @since Apr 5, 2017
36
 *
37
 */
38
public abstract class AbstractEditorPresenter<DTO extends Object, V extends ApplicationView<?>> extends AbstractPresenter<V> {
39

    
40

    
41
    private static final long serialVersionUID = -6677074110764145236L;
42

    
43
    FlushMode previousPreSaveEvenFlushMode = null;
44

    
45
    protected BeanInstantiator<DTO> beanInstantiator = null;
46

    
47
    /**
48
     * Load the bean to be edited in the editor freshly from the persistent storage.
49
     * Ore create an new empty instance in case the supplied <code>identifier</code> is <code>null</code>.
50
     *
51
     * @param identifier
52
     * @return
53
     */
54
    protected abstract DTO loadBeanById(Object identifier);
55

    
56
    /**
57
     * Set ui elements to readonly or disabled to adapt the editor to
58
     * the permissions that are given to the current user etc.
59
     *
60
     * @param beanToEdit
61
     */
62
    protected void adaptToUserPermission(DTO beanToEdit) {
63

    
64
    }
65

    
66
    /**
67
     * @param beanInstantiator the beanInstantiator to set
68
     */
69
    public void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
70
        this.beanInstantiator = beanInstantiator;
71
    }
72

    
73
    @EventBusListenerMethod
74
    public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
75
        if(!isFromOwnView(preSaveEvent)){
76
            return;
77
        }
78
    }
79

    
80
    @EventBusListenerMethod
81
    public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
82
        if(!isFromOwnView(saveEvent)){
83
            return;
84
        }
85
        DTO bean = saveEvent.getBean();
86
        saveBean(bean);
87
    }
88

    
89
   /**
90
    * @param saveEvent
91
    */
92
   @EventBusListenerMethod
93
   public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
94
       if(!isFromOwnView(deleteEvent)){
95
           return;
96
       }
97
       deleteBean(deleteEvent.getBean());
98
   }
99

    
100
    /**
101
     * @param saveEvent
102
     * @return
103
     */
104
    protected boolean isFromOwnView(EditorViewEvent saveEvent) {
105
        return saveEvent.getView().equals(getView());
106
    }
107

    
108
    protected Class<V> getViewType() {
109
        return (Class<V>) super.getView().getClass();
110
    }
111

    
112
    protected boolean isFromOwnView(AbstractEditorAction action){
113
        return action.getSourceView() != null && getView().equals(action.getSourceView());
114
    }
115

    
116
    protected boolean isFromOwnView(FieldReplaceEvent event){
117
        return event.getSourceView() != null && getView().equals(event.getSourceView());
118
    }
119

    
120
    protected abstract void saveBean(DTO bean);
121

    
122
    /**
123
     * @param bean
124
     */
125
    protected abstract void deleteBean(DTO bean);
126

    
127
    /**
128
     *
129
     * @param popupView
130
     * @return <code>null</code> in case no target field has been found for the supplied <code>popupView</code>.
131
     */
132
    protected BoundField boundTargetField(PopupView popupView) {
133
        Field<?> field = getNavigationManager().targetFieldOf(popupView);
134
        PropertyIdPath propertyIdPath = boundPropertyIdPath(field);
135
        if(field != null){
136
            return new BoundField(field, propertyIdPath);
137
        } else {
138
            return null;
139
        }
140
    }
141

    
142
    protected PropertyIdPath boundPropertyIdPath(Field<?> targetField){
143

    
144
        if(targetField == null || getView() == null){
145
            return null;
146
        }
147

    
148
        Field<?> boundField  = findBoundField(targetField);
149
        if(boundField != null) {
150
            return ((AbstractPopupEditor)getView()).boundPropertyIdPath(boundField);
151
        }
152
        return null;
153
    }
154

    
155
    /**
156
     * @param targetField
157
     * @return
158
     */
159
    protected Field<?> findBoundField(Field<?> targetField) {
160

    
161
        Component parentComponent = targetField;
162
        Property<?> p = null;
163
        while(parentComponent != null){
164
            if(Field.class.isAssignableFrom(parentComponent.getClass())){
165
                Field<?> parentField = (Field<?>)parentComponent;
166
                if(parentField.getPropertyDataSource() != null){
167
                    return parentField;
168
                }
169
            }
170
            parentComponent = parentComponent.getParent();
171
        }
172
        return null;
173
    }
174
}
(5-5/13)