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 java.util.Date; 022 023 import javax.persistence.EntityManager; 024 import javax.persistence.Query; 025 026 import org.apache.oozie.CoordinatorActionBean; 027 import org.apache.oozie.ErrorCode; 028 import org.apache.oozie.FaultInjection; 029 import org.apache.oozie.client.rest.JsonBean; 030 import org.apache.oozie.util.ParamChecker; 031 032 /** 033 * Class for inserting and updating beans in bulk 034 * 035 */ 036 public class BulkUpdateInsertForCoordActionStartJPAExecutor implements JPAExecutor<Void> { 037 038 private Collection<JsonBean> updateList; 039 private Collection<JsonBean> insertList; 040 041 /** 042 * Initialize the JPAExecutor using the update and insert list of JSON beans 043 * 044 * @param updateList 045 * @param insertList 046 */ 047 public BulkUpdateInsertForCoordActionStartJPAExecutor(Collection<JsonBean> updateList, 048 Collection<JsonBean> insertList) { 049 this.updateList = updateList; 050 this.insertList = insertList; 051 } 052 053 public BulkUpdateInsertForCoordActionStartJPAExecutor() { 054 } 055 056 /** 057 * Sets the update list for JSON bean 058 * 059 * @param updateList 060 */ 061 public void setUpdateList(Collection<JsonBean> updateList) { 062 this.updateList = updateList; 063 } 064 065 /** 066 * Sets the insert list for JSON bean 067 * 068 * @param insertList 069 */ 070 public void setInsertList(Collection<JsonBean> insertList) { 071 this.insertList = insertList; 072 } 073 074 /* 075 * (non-Javadoc) 076 * 077 * @see org.apache.oozie.executor.jpa.JPAExecutor#getName() 078 */ 079 @Override 080 public String getName() { 081 return "BulkUpdateInsertForCoordActionStartJPAExecutor"; 082 } 083 084 /* 085 * (non-Javadoc) 086 * 087 * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence. 088 * EntityManager) 089 */ 090 @Override 091 public Void execute(EntityManager em) throws JPAExecutorException { 092 try { 093 if (insertList != null) { 094 for (JsonBean entity : insertList) { 095 ParamChecker.notNull(entity, "JsonBean"); 096 em.persist(entity); 097 } 098 } 099 // Only used by test cases to check for rollback of transaction 100 FaultInjection.activate("org.apache.oozie.command.SkipCommitFaultInjection"); 101 if (updateList != null) { 102 for (JsonBean entity : updateList) { 103 ParamChecker.notNull(entity, "JsonBean"); 104 if (entity instanceof CoordinatorActionBean) { 105 CoordinatorActionBean action = (CoordinatorActionBean) entity; 106 Query q = em.createNamedQuery("UPDATE_COORD_ACTION_FOR_START"); 107 q.setParameter("id", action.getId()); 108 q.setParameter("status", action.getStatus().toString()); 109 q.setParameter("lastModifiedTime", new Date()); 110 q.setParameter("runConf", action.getRunConf()); 111 q.setParameter("externalId", action.getExternalId()); 112 q.setParameter("pending", action.getPending()); 113 q.setParameter("errorCode", action.getErrorCode()); 114 q.setParameter("errorMessage", action.getErrorMessage()); 115 q.executeUpdate(); 116 } 117 else { 118 em.merge(entity); 119 } 120 } 121 } 122 // Since the return type is Void, we have to return null 123 return null; 124 } 125 catch (Exception e) { 126 throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e); 127 } 128 } 129 }