Project

General

Profile

Download (4.31 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.apache.log4j.Logger;
12
import org.springframework.context.event.EventListener;
13

    
14
import eu.etaxonomy.cdm.api.service.IService;
15
import eu.etaxonomy.cdm.model.common.CdmBase;
16
import eu.etaxonomy.cdm.service.CdmStore;
17
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
18
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
19
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
20

    
21
/**
22
 * Provides generic save operations of modified cdm entities.
23
 *
24
 * @author a.kohlbecker
25
 * @since Apr 5, 2017
26
 *
27
 */
28
public abstract class AbstractCdmEditorPresenter<DTO extends CdmBase, V extends ApplicationView<?>> extends AbstractEditorPresenter<DTO, V> {
29

    
30
    private static final long serialVersionUID = 2218185546277084261L;
31

    
32
    private static final Logger logger = Logger.getLogger(AbstractCdmEditorPresenter.class);
33

    
34

    
35
    public AbstractCdmEditorPresenter() {
36
        super();
37
        logger.trace(this._toString() + " constructor");
38
    }
39

    
40
    CdmStore<DTO, IService<DTO>> store ;
41

    
42
    protected CdmStore<DTO, IService<DTO>> getStore() {
43
        if(store == null){
44
            store = new CdmStore<>(getRepo(), getService());
45
        }
46
        return store;
47
    }
48

    
49

    
50
    /**
51
     * @return
52
     */
53
    protected abstract IService<DTO> getService();
54

    
55
    @SuppressWarnings("unchecked")
56
    @Override
57
    @EventListener // the generic type parameter <DTO> must not be used here otherwise events will not be received
58
    public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
59

    
60
        if(!isFromOwnView(preSaveEvent)){
61
            return;
62
        }
63
        super.onEditorPreSaveEvent(preSaveEvent);
64
    }
65

    
66
    @SuppressWarnings("unchecked")
67
    @Override
68
    @EventListener // the generic type parameter <DTO> must not be used here otherwise events will not be received
69
    public void onEditorSaveEvent(EditorSaveEvent saveEvent){
70

    
71
        if(!isFromOwnView(saveEvent)){
72
            return;
73
        }
74

    
75
        // the bean is now updated with the changes made by the user
76
        DTO bean = (DTO) saveEvent.getBean();
77
        bean = handleTransientProperties(bean);
78
        EntityChangeEvent changeEvent = getStore().saveBean(bean);
79

    
80
        if(changeEvent != null){
81
            eventBus.publishEvent(changeEvent);
82
        }
83
    }
84

    
85
    /**
86
     * EditorPresneters for beans with transient properties should overwrite this method to
87
     * update the beanItem with the changes made to the transient properties.
88
     * <p>
89
     * This is necessary because Vaadin MethodProperties are readonly when no setter is
90
     * available. This can be the case with transient properties. Automatic updating
91
     * of the property during the fieldGroup commit does not work in this case.
92
     *
93
     * @deprecated editors should operate on DTOs instead, remove this method if unused.
94
     */
95
    @Deprecated
96
    protected DTO handleTransientProperties(DTO bean) {
97
        // no need to handle transient properties in the generic case
98
        return bean;
99
    }
100

    
101
    @Override
102
    protected DTO prepareAsFieldGroupDataSource(DTO bean){
103
        DTO mergedBean = getStore().mergedBean(bean);
104
        // DTO mergedBean = bean;
105
        return mergedBean;
106
    }
107

    
108
    /**
109
     * If the bean is contained in the session it is being updated by
110
     * doing an evict and merge. The fieldGroup is updated with the merged bean.
111
     *
112
     * @param bean
113
     *
114
     * @return The bean merged to the session or original bean in case a merge was not necessary.
115
     */
116
    private DTO mergedBean(DTO bean) {
117
        DTO mergedBean = getStore().mergedBean(bean);
118
        ((AbstractPopupEditor<DTO, AbstractCdmEditorPresenter<DTO, V>>)getView()).updateItemDataSource(mergedBean);
119
        return mergedBean;
120

    
121
    }
122

    
123
    @Override
124
    protected final void saveBean(DTO bean){
125
        // blank implementation, since this is not needed in this or any sub class
126
        // see onEditorSaveEvent() instead
127
    }
128

    
129
    @Override
130
    protected final void deleteBean(DTO bean){
131
        EntityChangeEvent changeEvent = getStore().deleteBean(bean);
132
        if(changeEvent != null){
133
            eventBus.publishEvent(changeEvent);
134
        }
135

    
136
    }
137

    
138
}
(1-1/8)