/*
 * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution for a full
 * listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.zanata.seam;

import javax.persistence.EntityManager;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import org.apache.deltaspike.core.api.exclude.Exclude;
import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import lombok.Getter;

/**
 * Simulates a seam transaction for use with {@link SeamAutowire}. Since
 * transactions are not created/injected with {@link SeamAutowire}, this class
 * provides support for methods that use transactions explicitly.
 *
 * This class will always represent an active, non-committed, non-rollbacked
 * transaction.
 *
 * @author Carlos Munoz <a
 *         href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
 */
@Exclude(ifProjectStage = ProjectStage.IntegrationTest.class)
public class AutowireTransaction implements UserTransaction {

    private static final AutowireTransaction instance =
            new AutowireTransaction();
    @Getter
    private boolean active;

    public static UserTransaction instance() {
        return instance;
    }

    @Override
    public void begin() throws NotSupportedException, SystemException {
        active = true;
    }

    private EntityManager getEntityManager() {
        return (EntityManager) SeamAutowire.instance().getComponent(
                "entityManager");
    }

    @Override
    public void commit() throws RollbackException, HeuristicMixedException,
            HeuristicRollbackException, SecurityException,
            IllegalStateException, SystemException {
        EntityManager entityManager = getEntityManager();
        if (entityManager != null) {
            entityManager.flush();
        }
        active = false;
    }

    @Override
    public void rollback() throws IllegalStateException, SecurityException,
            SystemException {
        active = false;
    }

    @Override
    public void setRollbackOnly() throws IllegalStateException, SystemException {
    }

    @Override
    public int getStatus() throws SystemException {
        if (active) {
            return Status.STATUS_ACTIVE;
        } else {
            return Status.STATUS_NO_TRANSACTION;
        }
    }

    @Override
    public void setTransactionTimeout(int seconds) throws SystemException {
    }
}

@@ -1132,32 +1132,86 @@
 lbackException;%0A
+import javax.transaction.InvalidTransactionException;%0A
 import javax.tra
@@ -1234,32 +1234,32 @@
 ortedException;%0A
-
 import javax.tra
@@ -1395,32 +1395,115 @@
 ystemException;%0A
+import javax.transaction.Transaction;%0Aimport javax.transaction.TransactionManager;%0A
 import javax.tra
@@ -2212,16 +2212,44 @@
 nsaction
+,%0A        TransactionManager
  %7B%0A%0A    
@@ -3348,24 +3348,62 @@
 Exception %7B%0A
+        throw new RuntimeException();%0A
     %7D%0A%0A    @
@@ -3615,32 +3615,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -3695,20 +3695,500 @@
 Exception %7B%0A
+        throw new RuntimeException();%0A    %7D%0A%0A    @Override%0A    public Transaction getTransaction() throws SystemException %7B%0A        throw new RuntimeException();%0A    %7D%0A%0A    @Override%0A    public Transaction suspend() throws SystemException %7B%0A        throw new RuntimeException();%0A    %7D%0A%0A    @Override%0A    public void resume(Transaction tobj)%0A            throws InvalidTransactionException, IllegalStateException,%0A            SystemException %7B%0A        throw new RuntimeException();%0A
     %7D%0A%7D%0A

Make AutowireTransaction implement TransactionManager