Project

General

Profile

Download (4.85 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.hibernate.Session;
13
import org.springframework.context.event.EventListener;
14
import org.springframework.transaction.TransactionStatus;
15

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

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

    
32
    private static final long serialVersionUID = 2218185546277084261L;
33

    
34
    private static final Logger logger = Logger.getLogger(AbstractCdmEditorPresenter.class);
35

    
36
    TransactionStatus tx = null;
37

    
38
    Session session = null;
39

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

    
42
    public AbstractCdmEditorPresenter() {
43
        super();
44
        logger.trace(this._toString() + " constructor");
45
    }
46

    
47
    protected CdmStore<DTO, IService<DTO>> getStore() {
48
        if(store == null){
49
            store = new CdmStore<>(getRepo(), getService());
50
        }
51
        return store;
52
    }
53

    
54

    
55
    /**
56
     * @return
57
     */
58
    protected abstract IService<DTO> getService();
59

    
60
    @SuppressWarnings("unchecked")
61
    @Override
62
    @EventListener // the generic type parameter <DTO> must not be used here otherwise events will not be received
63
    public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
64
        if(!isFromOwnView(preSaveEvent)){
65
            return;
66
        }
67

    
68
        session = getSession();
69
        logger.trace(this._toString() + ".onEditorPreSaveEvent - session: " + session);
70
        logger.trace(this._toString() + ".onEditorPreSaveEvent - starting transaction");
71
        tx = getStore().startTransaction();
72
        // merge the bean and update the fieldGroup with the merged bean, so that updating
73
        // of field values in turn of the commit are can not cause LazyInitializationExeptions
74
        // the bean still has the original values at this point
75
        logger.trace(this._toString() + ".onEditorPreSaveEvent - merging bean into session");
76
        mergedBean((DTO) preSaveEvent.getBean());
77
    }
78

    
79
    @SuppressWarnings("unchecked")
80
    @Override
81
    @EventListener // the generic type parameter <DTO> must not be used here otherwise events will not be received
82
    public void onEditorSaveEvent(EditorSaveEvent saveEvent){
83
        if(!isFromOwnView(saveEvent)){
84
            return;
85
        }
86
        // the bean is now updated with the changes made by the user
87
        DTO bean = (DTO) saveEvent.getBean();
88
        bean = handleTransientProperties(bean);
89
        EntityChangeEvent changeEvent = getStore().saveBean(bean);
90
        if(changeEvent != null){
91
            eventBus.publishEvent(changeEvent);
92
        }
93
    }
94

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

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

    
124
    }
125

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

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

    
139
    }
140

    
141
}
(1-1/8)