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    package org.apache.oozie.executor.jpa;
019    
020    import java.util.Collection;
021    import javax.persistence.EntityManager;
022    import javax.persistence.Query;
023    
024    import org.apache.oozie.ErrorCode;
025    import org.apache.oozie.FaultInjection;
026    import org.apache.oozie.util.ParamChecker;
027    
028    /**
029     * Delete Bundle job, its list of actions and return the number of actions that were deleted.
030     */
031    public class BundleJobsDeleteJPAExecutor implements JPAExecutor<Integer> {
032    
033        private Collection<String> deleteList;
034    
035        /**
036         * Initialize the JPAExecutor using the delete list of BundleJobBeans
037         * @param deleteList
038         */
039        public BundleJobsDeleteJPAExecutor(Collection<String> deleteList) {
040            this.deleteList = deleteList;
041        }
042    
043        public BundleJobsDeleteJPAExecutor() {
044        }
045    
046        /**
047         * Sets the delete list for BundleJobBeans
048         *
049         * @param deleteList
050         */
051        public void setDeleteList(Collection<String> deleteList) {
052            this.deleteList = deleteList;
053        }
054    
055        /*
056         * (non-Javadoc)
057         *
058         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
059         */
060        @Override
061        public String getName() {
062            return "CoordJobsDeleteJPAExecutor";
063        }
064    
065        /*
066         * (non-Javadoc)
067         *
068         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.
069         * EntityManager)
070         */
071        @Override
072        public Integer execute(EntityManager em) throws JPAExecutorException {
073            int actionsDeleted = 0;
074            try {
075                // Only used by test cases to check for rollback of transaction
076                FaultInjection.activate("org.apache.oozie.command.SkipCommitFaultInjection");
077                if (deleteList != null) {
078                    for (String id : deleteList) {
079                        ParamChecker.notNull(id, "Bundle Job Id");
080                        // Delete the coord job
081                        Query q = em.createNamedQuery("DELETE_BUNDLE_JOB");
082                        q.setParameter("id", id);
083                        q.executeUpdate();
084                        // Delete the actions for this coord job
085                        Query g = em.createNamedQuery("DELETE_ACTIONS_FOR_BUNDLE");
086                        g.setParameter("bundleId", id);
087                        actionsDeleted = g.executeUpdate();
088                    }
089                }
090            }
091            catch (Exception e) {
092                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
093            }
094            return actionsDeleted;
095        }
096    }