Project

General

Profile

Download (9.6 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.cdm.service;
10

    
11
import org.apache.log4j.Logger;
12
import org.hibernate.Session;
13
import org.hibernate.engine.spi.SessionImplementor;
14
import org.springframework.transaction.TransactionStatus;
15
import org.springframework.transaction.support.DefaultTransactionDefinition;
16

    
17
import com.vaadin.ui.Notification;
18
import com.vaadin.ui.UI;
19

    
20
import eu.etaxonomy.cdm.api.application.CdmRepository;
21
import eu.etaxonomy.cdm.api.service.DeleteResult;
22
import eu.etaxonomy.cdm.api.service.IService;
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
26
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent.Type;
27

    
28
/**
29
 * @author a.kohlbecker
30
 * @since Jun 26, 2017
31
 *
32
 * TODO better naming of this class, ServiceWrapper, ServiceOperator, ...?
33
 *
34
 */
35
public class CdmStore<T extends CdmBase, S extends IService<T>> {
36

    
37
    private static final Logger logger = Logger.getLogger(CdmStore.class);
38

    
39
    private CdmRepository repo;
40

    
41
    private S service;
42

    
43
    TransactionStatus txStatus = null;
44

    
45
//    ConversationHolder conversationHolder = null;
46
//
47
//    /**
48
//     * @return the conversationHolder
49
//     */
50
//    public ConversationHolder getConversationHolder() {
51
//        return conversationHolder;
52
//    }
53

    
54
    protected DefaultTransactionDefinition txDefinition = null;
55

    
56
    /**
57
     *
58
     * @param repo
59
     * @param service
60
     *            may be <code>null</code>, but delete operations will fail with
61
     *            a NullPointerException in this case.
62
     */
63
    public CdmStore(CdmRepository repo, S service) {
64

    
65
        this.repo = repo;
66
        this.service = service;
67

    
68
    }
69

    
70
//    /**
71
//     * constructor which takes a ConversationHolder. The supplying class of the conversationHolder needs
72
//     * to care for <code>bind()</code>, <code>unbind()</code> and <code>close()</code> since the store is
73
//     * only responsible for starting and committing of transactions.
74
//     *
75
//     * @param repo
76
//     * @param service
77
//     * @param conversationHolder
78
//     */
79
//    public CdmStore(CdmRepository repo, S service, ConversationHolder conversationHolder) {
80
//
81
//        this.repo = repo;
82
//        this.service = service;
83
//        this.conversationHolder = conversationHolder;
84
//
85
//    }
86

    
87
    /**
88
     * @return
89
     *
90
     */
91
    public TransactionStatus startTransaction() {
92
//        if(conversationHolder != null && !conversationHolder.isTransactionActive()){
93
//            //conversationHolder.setDefinition(getTransactionDefinition());
94
//            return conversationHolder.startTransaction();
95
//        } else {
96
            checkExistingTransaction();
97
            txStatus = repo.startTransaction();
98
            return txStatus;
99
//        }
100
    }
101

    
102
    /**
103
     *
104
     */
105
    protected void checkExistingTransaction() {
106
        if (txStatus != null) {
107
            // @formatter:off
108
            // holding the TransactionStatus as state is not good design. we
109
            // should change the save operation
110
            // in the EditorView so that the presenter can process the save in
111
            // one method call.
112
            // Problems:
113
            // 1. the fieldGroup needs a open session and read transaction
114
            // during the validation, otherwise
115
            // LazyInitialisationExceptions occur.
116
            // 2. passing the TransactionState to the view also doesn't seem
117
            // like a good idea.
118
            // @formatter:on
119
            throw new RuntimeException("Opening a second transaction in the same" + this.getClass().getSimpleName() + " is not supported");
120
        }
121
    }
122

    
123
    /**
124
     * If the bean is contained in the session it is being updated by doing an
125
     * evict and merge. The fieldGroup is updated with the merged bean.
126
     *
127
     *
128
     * @param bean
129
     * @return The bean merged to the session or original bean in case a merge
130
     *         was not necessary.
131
     */
132
    public T mergedBean(T bean) {
133

    
134
        Session session = getSession();
135

    
136
        // session.clear();
137
        if (session.contains(bean)) {
138
            // evict bean before merge to avoid duplicate beans in same session
139
            logger.trace(this._toString() + ".mergedBean() - evict " + bean.toString());
140
            session.evict(bean);
141
        }
142

    
143
        logger.trace(this._toString() + ".mergedBean() - doing merge of" + bean.toString());
144
        // to avoid merge problems as described in
145
        // https://dev.e-taxonomy.eu/redmine/issues/6687
146
        // we are set the hibernate property
147
        // hibernate.event.merge.entity_copy_observer=allow
148
        @SuppressWarnings("unchecked")
149
        T mergedBean = (T) session.merge(bean);
150
        logger.trace(this._toString() + ".mergedBean() - bean after merge " + bean.toString());
151
        return mergedBean;
152

    
153
    }
154

    
155
    /**
156
     * @return
157
     */
158
    private Session getSession() {
159

    
160
        Session session;
161
//        if(conversationHolder != null){
162
//            session = conversationHolder.getSession();
163
//        } else {
164
            session = repo.getSession();
165
//        }
166
        logger.trace(this._toString() + ".getSession() - session:" + session.hashCode() + ", persistenceContext: "
167
                + ((SessionImplementor) session).getPersistenceContext() + " - " + session.toString());
168

    
169
        return session;
170
    }
171

    
172
    protected String _toString() {
173
        return this.getClass().getSimpleName() + "@" + this.hashCode();
174
    }
175

    
176
    /**
177
     *
178
     * @param bean
179
     * 
180
     * @return the merged bean, this bean is <b>not reloaded</b> from the
181
     *         persistent storage.
182
     */
183
    public EntityChangeEvent saveBean(T bean) {
184

    
185
        Type changeEventType;
186
        if(bean.getId() > 1){
187
            changeEventType = Type.MODIFIED;
188
        } else {
189
            changeEventType = Type.CREATED;
190
        }
191

    
192
        Session session = getSession();
193
        logger.trace(this._toString() + ".onEditorSaveEvent - session: " + session.hashCode());
194

    
195
        if(txStatus == null
196
//                || (conversationHolder != null && !conversationHolder.isTransactionActive())
197
                ){
198
            // no running transaction, start one ...
199
            startTransaction();
200
        }
201

    
202
        logger.trace(this._toString() + ".onEditorSaveEvent - merging bean into session");
203
        // merge the changes into the session, ...
204

    
205
        T mergedBean = mergedBean(bean);
206

    
207
        // NOTE: saveOrUpdate is really needed here even if we to a merge before
208
        repo.getCommonService().saveOrUpdate(mergedBean);
209
        session.flush();
210
        commitTransction();
211

    
212
        return new EntityChangeEvent(mergedBean.getClass(), mergedBean.getId(), changeEventType);
213
    }
214

    
215
    /**
216
     *
217
     * @param bean
218
     * @return a EntityChangeEvent in case the deletion was successful otherwise <code>null</code>.
219
     */
220
    public final EntityChangeEvent deleteBean(T bean) {
221

    
222
        logger.trace(this._toString() + ".onEditorPreSaveEvent - starting transaction");
223

    
224
        startTransaction();
225
        logger.trace(this._toString() + ".deleteBean - deleting" + bean.toString());
226
        DeleteResult result = service.delete(bean);
227
        if (result.isOk()) {
228

    
229
            getSession().flush();
230
            commitTransction();
231
            logger.trace(this._toString() + ".deleteBean - transaction comitted");
232
            return new EntityChangeEvent(bean.getClass(), bean.getId(), Type.REMOVED);
233
        } else {
234
            String notificationTitle;
235
            StringBuffer messageBody = new StringBuffer();
236
            if (result.isAbort()) {
237
                notificationTitle = "The delete operation as abborded by the system.";
238
            } else {
239
                notificationTitle = "An error occured during the delete operation.";
240
            }
241
            if (!result.getExceptions().isEmpty()) {
242
                messageBody.append("<h3>").append("Exceptions:").append("</h3>").append("<ul>");
243
                result.getExceptions().forEach(e -> messageBody.append("<li>").append(e.getMessage()).append("</li>"));
244
                messageBody.append("</ul>");
245
            }
246
            if (!result.getRelatedObjects().isEmpty()) {
247
                messageBody.append("<h3>").append("Related objects exist:").append("</h3>").append("<ul>");
248
                result.getRelatedObjects().forEach(e -> {
249
                    messageBody.append("<li>");
250
                    if (IdentifiableEntity.class.isAssignableFrom(e.getClass())) {
251
                        messageBody.append(((IdentifiableEntity) e).getTitleCache());
252
                    } else {
253
                        messageBody.append(e.toString());
254
                    }
255
                    messageBody.append("</li>");
256
                });
257

    
258
                messageBody.append("</ul>");
259
            }
260
            Notification notification = new Notification(notificationTitle, messageBody.toString(),
261
                    com.vaadin.ui.Notification.Type.ERROR_MESSAGE, true);
262
            notification.show(UI.getCurrent().getPage());
263
            txStatus = null;
264
        }
265
        return null;
266
    }
267

    
268

    
269
    protected void commitTransction() {
270

    
271
//        if(conversationHolder != null){
272
//            conversationHolder.commit();
273
//        } else {
274
            repo.commitTransaction(txStatus);
275
            txStatus = null;
276
//        }
277
    }
278

    
279
    /**
280
     * @param entityId
281
     */
282
    public T loadBean(int entityId) {
283
//        conversationHolder.startTransaction();
284
        return service.find(entityId);
285
    }
286

    
287
    public S getService() {
288
        return service;
289
    }
290

    
291

    
292
}
(2-2/7)