001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.ken.util;
017    
018    import java.util.concurrent.Callable;
019    
020    import org.springframework.transaction.TransactionStatus;
021    import org.springframework.transaction.support.TransactionCallback;
022    import org.springframework.transaction.support.TransactionTemplate;
023    
024    /**
025     * A Callable that performs work within a transaction and returns the
026     * TransactionCallback's result
027     * @author Kuali Rice Team (rice.collab@kuali.org)
028     */
029    public class TransactionedCallable implements Callable, TransactionCallback {
030        private TransactionTemplate txTemplate;
031        private TransactionCallback callback;
032        
033        /**
034         * Constructs a TransactionedCallable.java.
035         * @param txTemplate
036         */
037        public TransactionedCallable(TransactionTemplate txTemplate) {
038            this.txTemplate = txTemplate;
039        }
040    
041        /**
042         * Constructs a TransactionedCallable.java.
043         * @param txTemplate
044         * @param callback
045         */
046        public TransactionedCallable(TransactionTemplate txTemplate, TransactionCallback callback) {
047            this.txTemplate = txTemplate;
048            this.callback = callback;
049        }
050    
051        /**
052         * @see java.util.concurrent.Callable#call()
053         */
054        public Object call() throws Exception {
055            TransactionCallback cb;
056            if (callback != null) {
057                cb = callback;
058            } else {
059                cb = this;
060            }
061            return txTemplate.execute(cb);
062        }
063    
064        /**
065         * @see org.springframework.transaction.support.TransactionCallback#doInTransaction(org.springframework.transaction.TransactionStatus)
066         */
067        public Object doInTransaction(TransactionStatus txStatus) {
068            return null;
069        }
070    }