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
|
/**
|
46
|
* Load the bean to be edited in the editor freshly from the persistent storage.
|
47
|
* Ore create an new empty instance in case the supplied <code>identifier</code> is <code>null</code>.
|
48
|
*
|
49
|
* @param identifier
|
50
|
* @return
|
51
|
*/
|
52
|
protected abstract DTO loadBeanById(Object identifier);
|
53
|
|
54
|
/**
|
55
|
* Set ui elements to readonly or disabled to adapt the editor to
|
56
|
* the permissions that are given to the current user etc.
|
57
|
*
|
58
|
* @param beanToEdit
|
59
|
*/
|
60
|
protected void adaptToUserPermission(DTO beanToEdit) {
|
61
|
|
62
|
}
|
63
|
|
64
|
|
65
|
@EventBusListenerMethod
|
66
|
public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
|
67
|
if(!isFromOwnView(preSaveEvent)){
|
68
|
return;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
@EventBusListenerMethod
|
73
|
public void onEditorSaveEvent(EditorSaveEvent<DTO> saveEvent){
|
74
|
if(!isFromOwnView(saveEvent)){
|
75
|
return;
|
76
|
}
|
77
|
DTO bean = saveEvent.getBean();
|
78
|
saveBean(bean);
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* @param saveEvent
|
83
|
*/
|
84
|
@EventBusListenerMethod
|
85
|
public void onEditorDeleteEvent(EditorDeleteEvent<DTO> deleteEvent){
|
86
|
if(!isFromOwnView(deleteEvent)){
|
87
|
return;
|
88
|
}
|
89
|
deleteBean(deleteEvent.getBean());
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* @param saveEvent
|
94
|
* @return
|
95
|
*/
|
96
|
protected boolean isFromOwnView(EditorViewEvent saveEvent) {
|
97
|
return saveEvent.getView().equals(getView());
|
98
|
}
|
99
|
|
100
|
protected Class<V> getViewType() {
|
101
|
return (Class<V>) super.getView().getClass();
|
102
|
}
|
103
|
|
104
|
protected boolean isFromOwnView(AbstractEditorAction action){
|
105
|
return action.getSourceView() != null && getView().equals(action.getSourceView());
|
106
|
}
|
107
|
|
108
|
protected boolean isFromOwnView(FieldReplaceEvent event){
|
109
|
return event.getSourceView() != null && getView().equals(event.getSourceView());
|
110
|
}
|
111
|
|
112
|
protected abstract void saveBean(DTO bean);
|
113
|
|
114
|
/**
|
115
|
* @param bean
|
116
|
*/
|
117
|
protected abstract void deleteBean(DTO bean);
|
118
|
|
119
|
/**
|
120
|
*
|
121
|
* @param popupView
|
122
|
* @return <code>null</code> in case no target field has been found for the supplied <code>popupView</code>.
|
123
|
*/
|
124
|
protected BoundField boundTargetField(PopupView popupView) {
|
125
|
Field<?> field = getNavigationManager().targetFieldOf(popupView);
|
126
|
PropertyIdPath propertyIdPath = boundPropertyIdPath(field);
|
127
|
if(field != null){
|
128
|
return new BoundField(field, propertyIdPath);
|
129
|
} else {
|
130
|
return null;
|
131
|
}
|
132
|
}
|
133
|
|
134
|
protected PropertyIdPath boundPropertyIdPath(Field<?> targetField){
|
135
|
|
136
|
if(targetField == null || getView() == null){
|
137
|
return null;
|
138
|
}
|
139
|
|
140
|
Field<?> boundField = findBoundField(targetField);
|
141
|
if(boundField != null) {
|
142
|
return ((AbstractPopupEditor)getView()).boundPropertyIdPath(boundField);
|
143
|
}
|
144
|
return null;
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* @param targetField
|
149
|
* @return
|
150
|
*/
|
151
|
protected Field<?> findBoundField(Field<?> targetField) {
|
152
|
|
153
|
Component parentComponent = targetField;
|
154
|
Property<?> p = null;
|
155
|
while(parentComponent != null){
|
156
|
if(Field.class.isAssignableFrom(parentComponent.getClass())){
|
157
|
Field<?> parentField = (Field<?>)parentComponent;
|
158
|
if(parentField.getPropertyDataSource() != null){
|
159
|
return parentField;
|
160
|
}
|
161
|
}
|
162
|
parentComponent = parentComponent.getParent();
|
163
|
}
|
164
|
return null;
|
165
|
}
|
166
|
|
167
|
}
|