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.TransactionAttribute;
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(
053                                    _transactionAttribute, callable);
054                    }
055                    catch (Throwable t) {
056                            if (t instanceof PortletException) {
057                                    throw (PortletException)t;
058                            }
059    
060                            throw new PortletException(t);
061                    }
062            }
063    
064            protected abstract void doTransactionalCommand(
065                            ActionRequest actionRequest, ActionResponse actionResponse)
066                    throws Exception;
067    
068            private static final TransactionAttribute _transactionAttribute;
069    
070            static {
071                    TransactionAttribute.Builder builder =
072                            new TransactionAttribute.Builder();
073    
074                    builder.setPropagation(Propagation.REQUIRES_NEW);
075                    builder.setRollbackForClasses(Exception.class);
076    
077                    _transactionAttribute = builder.build();
078            }
079    
080    }