001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.portlet.bridges.mvc;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.transaction.Propagation;
019    import com.liferay.portal.kernel.transaction.TransactionConfig;
020    import com.liferay.portal.kernel.transaction.TransactionInvokerUtil;
021    
022    import java.util.concurrent.Callable;
023    
024    import javax.portlet.ActionRequest;
025    import javax.portlet.ActionResponse;
026    import javax.portlet.PortletException;
027    
028    /**
029     * @author Bruno Basto
030     */
031    public abstract class BaseTransactionalMVCActionCommand
032            implements MVCActionCommand {
033    
034            @Override
035            public boolean processAction(
036                            final ActionRequest actionRequest,
037                            final ActionResponse actionResponse)
038                    throws PortletException {
039    
040                    try {
041                            Callable<Boolean> callable = new Callable<Boolean>() {
042    
043                                    @Override
044                                    public Boolean call() throws Exception {
045                                            doTransactionalCommand(actionRequest, actionResponse);
046    
047                                            return SessionErrors.isEmpty(actionRequest);
048                                    }
049    
050                            };
051    
052                            return TransactionInvokerUtil.invoke(_transactionConfig, callable);
053                    }
054                    catch (Throwable t) {
055                            if (t instanceof PortletException) {
056                                    throw (PortletException)t;
057                            }
058    
059                            throw new PortletException(t);
060                    }
061            }
062    
063            protected abstract void doTransactionalCommand(
064                            ActionRequest actionRequest, ActionResponse actionResponse)
065                    throws Exception;
066    
067            private static final TransactionConfig _transactionConfig;
068    
069            static {
070                    TransactionConfig.Builder builder = new TransactionConfig.Builder();
071    
072                    builder.setPropagation(Propagation.REQUIRES_NEW);
073                    builder.setRollbackForClasses(Exception.class);
074    
075                    _transactionConfig = builder.build();
076            }
077    
078    }