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.BundleJobBean; 025 import org.apache.oozie.CoordinatorJobBean; 026 import org.apache.oozie.ErrorCode; 027 import org.apache.oozie.FaultInjection; 028 import org.apache.oozie.WorkflowJobBean; 029 import org.apache.oozie.client.rest.JsonBean; 030 import org.apache.oozie.util.ParamChecker; 031 032 /** 033 * Delete job, its list of actions and return the number of 034 * actions been deleted. 035 */ 036 public class BulkDeleteForPurgeJPAExecutor implements JPAExecutor<Integer> { 037 038 private Collection<JsonBean> deleteList; 039 040 /** 041 * Initialize the JPAExecutor using the delete list of JSON beans 042 * @param deleteList 043 */ 044 public BulkDeleteForPurgeJPAExecutor(Collection<JsonBean> deleteList) { 045 this.deleteList = deleteList; 046 } 047 048 public BulkDeleteForPurgeJPAExecutor() { 049 } 050 051 /** 052 * Sets the delete list for JSON bean 053 * 054 * @param deleteList 055 */ 056 public void setDeleteList(Collection<JsonBean> deleteList) { 057 this.deleteList = deleteList; 058 } 059 060 /* 061 * (non-Javadoc) 062 * 063 * @see org.apache.oozie.executor.jpa.JPAExecutor#getName() 064 */ 065 @Override 066 public String getName() { 067 return "BulkDeleteForPurgeJPAExecutor"; 068 } 069 070 /* 071 * (non-Javadoc) 072 * 073 * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence. 074 * EntityManager) 075 */ 076 @Override 077 public Integer execute(EntityManager em) throws JPAExecutorException { 078 int actionsDeleted = 0; 079 try { 080 // Only used by test cases to check for rollback of transaction 081 FaultInjection.activate("org.apache.oozie.command.SkipCommitFaultInjection"); 082 if (deleteList != null) { 083 for (JsonBean entity : deleteList) { 084 ParamChecker.notNull(entity, "JsonBean"); 085 // deleting the job (wf/coord/bundle) 086 em.remove(em.merge(entity)); 087 if (entity instanceof WorkflowJobBean) { 088 // deleting the workflow actions for this job 089 Query g = em.createNamedQuery("DELETE_ACTIONS_FOR_WORKFLOW"); 090 g.setParameter("wfId", ((WorkflowJobBean) entity).getId()); 091 actionsDeleted = g.executeUpdate(); 092 } 093 else if (entity instanceof CoordinatorJobBean) { 094 // deleting the coord actions for this job 095 Query g = em.createNamedQuery("DELETE_COMPLETED_ACTIONS_FOR_COORDINATOR"); 096 g.setParameter("jobId", ((CoordinatorJobBean) entity).getId()); 097 actionsDeleted = g.executeUpdate(); 098 } 099 else if (entity instanceof BundleJobBean) { 100 // deleting the bundle actions for this job 101 Query g = em.createNamedQuery("DELETE_COMPLETED_ACTIONS_FOR_BUNDLE"); 102 g.setParameter("bundleId", ((BundleJobBean) entity).getId()); 103 actionsDeleted = g.executeUpdate(); 104 } 105 } 106 } 107 } 108 catch (Exception e) { 109 throw new JPAExecutorException(ErrorCode.E0603, e); 110 } 111 return actionsDeleted; 112 } 113 }