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/**
029 * Delete WF job, its list of actions and return the number of actions that were deleted.
030 */
031public class WorkflowJobsDeleteJPAExecutor implements JPAExecutor<Integer> {
032
033    private Collection<String> deleteList;
034
035    /**
036     * Initialize the JPAExecutor using the delete list of WorkflowJobBeans
037     * @param deleteList
038     */
039    public WorkflowJobsDeleteJPAExecutor(Collection<String> deleteList) {
040        this.deleteList = deleteList;
041    }
042
043    public WorkflowJobsDeleteJPAExecutor() {
044    }
045
046    /**
047     * Sets the delete list for WorkflowJobBeans
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 "WorkflowJobsDeleteJPAExecutor";
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 && !deleteList.isEmpty()) {
078                // Delete the WF job
079                Query q = em.createNamedQuery("DELETE_WORKFLOW");
080                q.setParameter("id", deleteList);
081                q.executeUpdate();
082                // Delete the actions for this WF job
083                Query g = em.createNamedQuery("DELETE_ACTIONS_FOR_WORKFLOW");
084                g.setParameter("wfId", deleteList);
085                actionsDeleted = g.executeUpdate();
086            }
087        }
088        catch (Exception e) {
089            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
090        }
091        return actionsDeleted;
092    }
093}