001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.store;
020
021//import javax.persistence.EntityManagerFactory;
022
023import javax.persistence.EntityManager;
024import javax.persistence.FlushModeType;
025import javax.persistence.PersistenceUnit;
026/*
027 import javax.persistence.Persistence;
028 import org.apache.oozie.CoordinatorActionBean;
029 import org.apache.oozie.CoordinatorJobBean;
030 import org.apache.oozie.WorkflowActionBean;
031 import org.apache.oozie.WorkflowJobBean;
032 import org.apache.oozie.SLAEventBean;
033 import org.apache.oozie.client.rest.JsonCoordinatorAction;
034 import org.apache.oozie.client.rest.JsonCoordinatorJob;
035 import org.apache.oozie.client.rest.JsonWorkflowAction;
036 import org.apache.oozie.client.rest.JsonWorkflowJob;
037 import org.apache.oozie.client.rest.JsonSLAEvent;
038 */
039import org.apache.oozie.service.Services;
040import org.apache.oozie.service.StoreService;
041import org.apache.oozie.util.XLog;
042import org.apache.openjpa.persistence.OpenJPAEntityManager;
043import org.apache.openjpa.persistence.OpenJPAPersistence;
044
045import java.sql.Connection;
046import java.sql.SQLException;
047
048@PersistenceUnit(unitName = "oozie")
049/**
050 * <code>Store</code> Abstract class to separate Entities from Actual store implementation
051 */
052public abstract class Store {
053
054    private EntityManager entityManager;
055
056    /**
057     * create a fresh transaction
058     */
059    public Store() {
060        entityManager = Services.get().get(StoreService.class).getEntityManager();
061    }
062
063    /**
064     * Use an existing transaction for cross store operations
065     */
066    public Store(Store store) {
067        entityManager = store.getEntityManager();
068    }
069
070    /**
071     * Return EntityManager
072     */
073    public EntityManager getEntityManager() {
074        return entityManager;
075    }
076
077    /**
078     * Invoke transaction on the EntityManager
079     */
080    public void beginTrx() {
081        entityManager.setFlushMode(FlushModeType.COMMIT);
082        entityManager.getTransaction().begin();
083    }
084
085    /**
086     * Commit current transaction
087     */
088    public void commitTrx() {
089        entityManager.getTransaction().commit();
090    }
091
092    /**
093     * Close current transaction <p> Before close transaction, it needs to be committed.
094     */
095    public void closeTrx() {
096        entityManager.close();
097    }
098
099    /**
100     * Rollback transaction
101     */
102    public void rollbackTrx() {
103        entityManager.getTransaction().rollback();
104    }
105
106    /**
107     * Check if transaction is active
108     *
109     * @return boolean
110     */
111    public boolean isActive() {
112        return entityManager.getTransaction().isActive();
113    }
114
115    public String getConnection() {
116        OpenJPAEntityManager kem = OpenJPAPersistence.cast(entityManager);
117        Connection conn = (Connection) kem.getConnection();
118        return conn.toString();
119    }
120
121    public boolean isDetached(Object o) {
122        OpenJPAEntityManager kem = OpenJPAPersistence.cast(entityManager);
123        return kem.isDetached(o);
124    }
125
126    public boolean isClosed() {
127        OpenJPAEntityManager kem = OpenJPAPersistence.cast(entityManager);
128        Connection conn = (Connection) kem.getConnection();
129        try {
130            return conn.isClosed();
131        }
132        catch (SQLException e) {
133            XLog.getLog(getClass()).info(XLog.STD, e.getMessage(), e);
134        }
135        return true;
136    }
137
138    public boolean contains(Object entity) {
139        return entityManager.contains(entity);
140    }
141
142    public String getFlushMode() {
143        return entityManager.getFlushMode().toString();
144    }
145}