Project

General

Profile

Download (4.83 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
    CdmStore<DTO, IService<DTO>> store ;
35

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

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

    
48

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

    
54
    @SuppressWarnings("unchecked")
55
    @Override
56
    @EventListener // the generic type parameter <DTO> must not be used here otherwise events will not be received
57
    public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
58
        if(!isFromOwnView(preSaveEvent)){
59
            return;
60
        }
61
        logger.trace("===================================================================");
62
        getStore().startConversationalTransaction();
63
        logger.trace(this._toString() + ".onEditorPreSaveEvent - starting conversational transaction");
64
        // getStore().startTransaction(false);
65
        // merge the bean and update the fieldGroup with the merged bean, so that updating
66
        // of field values in turn of the commit are can not cause LazyInitializationExeptions
67
        // the bean still has the original values at this point
68
        logger.trace(this._toString() + ".onEditorPreSaveEvent - merging bean into session");
69
        mergedBean((DTO) preSaveEvent.getBean());
70
    }
71

    
72
    @SuppressWarnings("unchecked")
73
    @Override
74
    @EventListener // the generic type parameter <DTO> must not be used here otherwise events will not be received
75
    public void onEditorSaveEvent(EditorSaveEvent saveEvent){
76
        if(!isFromOwnView(saveEvent)){
77
            return;
78
        }
79
        // the bean is now updated with the changes made by the user
80
        DTO bean = (DTO) saveEvent.getBean();
81
        bean = handleTransientProperties(bean);
82
        EntityChangeEvent changeEvent = getStore().saveBean(bean);
83

    
84
        logger.trace("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
85
        if(changeEvent != null){
86
            eventBus.publishEvent(changeEvent);
87
        }
88
    }
89

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

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

    
119
    }
120

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

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

    
134
    }
135

    
136
}
(1-1/8)