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    package org.granite.tide.cdi;
023    
024    import javax.enterprise.event.Observes;
025    import javax.enterprise.event.TransactionPhase;
026    import javax.enterprise.inject.Instance;
027    import javax.transaction.Status;
028    import javax.transaction.SystemException;
029    import javax.transaction.UserTransaction;
030    
031    import org.granite.logging.Logger;
032    import org.granite.tide.data.DataContext;
033    import org.granite.tide.data.DataEnabled.PublishMode;
034    
035    
036    /**
037     * CDI event listener to handle publishing of data changes instead of relying on the default behaviour
038     * This can be used outside of a HTTP Granite context and inside the security/transaction context
039     * @author William DRAI
040     *
041     */
042    public class TideDataPublishingOnSuccessHandler {
043            
044            private static final Logger log = Logger.getLogger(TideDataPublishingOnSuccessHandler.class);
045            
046        public void doPublish(@Observes(during=TransactionPhase.BEFORE_COMPLETION) TideDataPublishingEvent event, Instance<UserTransaction> uts) {
047            if (uts.isUnsatisfied() || uts.isAmbiguous()) {
048                // No JTA UserTransaction, probably a servlet container
049                // Anyway we should not be here
050                return;
051            }
052    
053            UserTransaction ut = uts.get();        
054            try {
055                            if (!(ut.getStatus() == Status.STATUS_MARKED_ROLLBACK || ut.getStatus() == Status.STATUS_ROLLING_BACK || ut.getStatus() == Status.STATUS_ROLLING_BACK))
056                                    DataContext.publish(PublishMode.ON_COMMIT);
057                    } 
058            catch (SystemException e) {
059                    log.warn(e, "Could not get the current status of the transaction ???");
060                    }
061            if (event.getInitContext())
062                    DataContext.remove();
063        }
064    }