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     * Delete Coord actions of long running coordinators, return the number of actions that were deleted.
029     */
030    public class CoordActionsDeleteJPAExecutor implements JPAExecutor<Integer> {
031        private Collection<String> deleteList;
032    
033        /**
034         * Initialize the JPAExecutor using the delete list of CoordinatorActionBeans
035         * @param deleteList
036         */
037        public CoordActionsDeleteJPAExecutor(Collection<String> deleteList) {
038            this.deleteList = deleteList;
039        }
040    
041        public CoordActionsDeleteJPAExecutor() {
042        }
043    
044        /**
045         * Sets the delete list for CoordinatorActionBeans
046         *
047         * @param deleteList
048         */
049        public void setDeleteList(Collection<String> deleteList) {
050            this.deleteList = deleteList;
051        }
052    
053        /*
054         * (non-Javadoc)
055         *
056         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
057         */
058        @Override
059        public String getName() {
060            return "CoordActionsDeleteJPAExecutor";
061        }
062    
063        /*
064         * (non-Javadoc)
065         *
066         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.
067         * EntityManager)
068         */
069        @Override
070        public Integer execute(EntityManager em) throws JPAExecutorException {
071            int actionsDeleted = 0;
072            try {
073                // Only used by test cases to check for rollback of transaction
074                FaultInjection.activate("org.apache.oozie.command.SkipCommitFaultInjection");
075                if (deleteList != null) {
076                    for (String id : deleteList) {
077                        ParamChecker.notNull(id, "Coordinator Action Id");
078    
079                        // Delete coordAction
080                        Query g = em.createNamedQuery("DELETE_ACTIONS_FOR_LONG_RUNNING_COORDINATOR");
081                        g.setParameter("actionId", id);
082                        g.executeUpdate();
083                        actionsDeleted++;
084                    }
085                }
086            }
087            catch (Exception e) {
088                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
089            }
090            return actionsDeleted;
091        }
092    }