Project

General

Profile

Download (5.11 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.mvp.event.EditorDeleteEvent;
20
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
21
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
22
import eu.etaxonomy.vaadin.mvp.event.EditorViewEvent;
23
import eu.etaxonomy.vaadin.ui.view.PopupView;
24
import eu.etaxonomy.vaadin.util.PropertyIdPath;
25

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

    
39

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

    
42
    FlushMode previousPreSaveEvenFlushMode = null;
43

    
44
    protected BeanInstantiator<DTO> beanInstantiator = null;
45

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

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

    
63
    }
64

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

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

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

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

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

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

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

    
115

    
116
    protected abstract void saveBean(DTO bean);
117

    
118
    /**
119
     * @param bean
120
     */
121
    protected abstract void deleteBean(DTO bean);
122

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

    
138
    protected PropertyIdPath boundPropertyIdPath(Field<?> targetField){
139

    
140
        if(targetField == null || getView() == null){
141
            return null;
142
        }
143

    
144
        Field<?> boundField  = findBoundField(targetField);
145
        if(boundField != null) {
146
            return ((AbstractPopupEditor)getView()).boundPropertyIdPath(boundField);
147
        }
148
        return null;
149
    }
150

    
151
    /**
152
     * @param targetField
153
     * @return
154
     */
155
    protected Field<?> findBoundField(Field<?> targetField) {
156

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