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.executor.jpa;
020
021import org.apache.oozie.ErrorCode;
022import org.apache.oozie.FaultInjection;
023
024import javax.persistence.EntityManager;
025import javax.persistence.Query;
026import java.util.Collection;
027/**
028 * Delete Coord actions of long running coordinators, return the number of actions that were deleted.
029 */
030public 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 && !deleteList.isEmpty()) {
076                // Delete coordActions
077                Query g = em.createNamedQuery("DELETE_ACTIONS_FOR_LONG_RUNNING_COORDINATOR");
078                g.setParameter("actionId", deleteList);
079                actionsDeleted = g.executeUpdate();
080            }
081        }
082        catch (Exception e) {
083            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
084        }
085        return actionsDeleted;
086    }
087}