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