001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 * This file is part of the Granite Data Services Platform.
006 *
007 * Granite Data Services is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * Granite Data Services is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 * USA, or see <http://www.gnu.org/licenses/>.
021 */
022
023 package org.granite.tide.spring;
024
025 import java.util.concurrent.Callable;
026
027 import org.granite.gravity.Gravity;
028 import org.granite.tide.data.DataEnabled;
029 import org.granite.tide.data.DataUpdatePostprocessor;
030 import org.springframework.beans.factory.InitializingBean;
031 import org.springframework.beans.factory.annotation.Autowired;
032 import org.springframework.transaction.TransactionException;
033 import org.springframework.transaction.TransactionStatus;
034 import org.springframework.transaction.support.TransactionCallback;
035 import org.springframework.transaction.support.TransactionTemplate;
036
037
038 /**
039 * Extended Spring Transaction template which handles publishing of data changes
040 * This can be used outside of any Granite context and can replace the default Spring TransactionTemplate
041 *
042 * @author William DRAI
043 */
044 public class TideDataPublishingTransactionTemplate extends TransactionTemplate implements InitializingBean {
045
046 private static final long serialVersionUID = 1L;
047
048 //private static final Logger log = Logger.getLogger(TideDataPublishingTransactionTemplate.class);
049
050 private Gravity gravity;
051 private DataUpdatePostprocessor dataUpdatePostprocessor;
052
053 private TideDataPublishingWrapper tideDataPublishingWrapper = null;
054
055 @Autowired
056 public void setGravity(Gravity gravity) {
057 this.gravity = gravity;
058 }
059
060 public void setTideDataPublishingWrapper(TideDataPublishingWrapper tideDataPublishingWrapper) {
061 this.tideDataPublishingWrapper = tideDataPublishingWrapper;
062 }
063
064 @Autowired(required=false)
065 public void setDataUpdatePostprocessor(DataUpdatePostprocessor dataUpdatePostprocessor) {
066 this.dataUpdatePostprocessor = dataUpdatePostprocessor;
067 }
068
069 @Override
070 public void afterPropertiesSet() {
071 if (tideDataPublishingWrapper == null)
072 tideDataPublishingWrapper = new TideDataPublishingWrapper(gravity, dataUpdatePostprocessor);
073 }
074
075 @Override
076 public <T> T execute(TransactionCallback<T> action) throws TransactionException {
077 DataEnabled dataEnabled = null;
078 if (action.getClass().isAnnotationPresent(DataEnabled.class))
079 dataEnabled = action.getClass().getAnnotation(DataEnabled.class);
080 else if ((action.getClass().isMemberClass() || action.getClass().isAnonymousClass()) && action.getClass().getEnclosingClass().isAnnotationPresent(DataEnabled.class))
081 dataEnabled = action.getClass().getEnclosingClass().getAnnotation(DataEnabled.class);
082
083 if (dataEnabled == null || !dataEnabled.useInterceptor())
084 return super.execute(action);
085
086 return super.execute(new DataEnabledTransactionCallback<T>(action, dataEnabled));
087 }
088
089 private class DataEnabledTransactionCallback<T> implements TransactionCallback<T> {
090
091 private final TransactionCallback<T> action;
092 private final DataEnabled dataEnabled;
093
094 public DataEnabledTransactionCallback(TransactionCallback<T> action, DataEnabled dataEnabled) {
095 this.action = action;
096 this.dataEnabled = dataEnabled;
097 }
098
099 @Override
100 public T doInTransaction(final TransactionStatus status) {
101 return tideDataPublishingWrapper.execute(dataEnabled, new Callable<T>() {
102 public T call() throws Exception {
103 return action.doInTransaction(status);
104 }
105 });
106 }
107 }
108 }