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 */
018package org.apache.oozie.executor.jpa;
019
020import java.util.Collection;
021
022import javax.persistence.EntityManager;
023import org.apache.oozie.ErrorCode;
024import org.apache.oozie.FaultInjection;
025import org.apache.oozie.client.rest.JsonBean;
026import org.apache.oozie.util.ParamChecker;
027
028/**
029 * Class for inserting and updating beans in bulk
030 * @param <T>
031*/
032public class BulkUpdateInsertJPAExecutor implements JPAExecutor<Void> {
033
034    private Collection<JsonBean> updateList;
035    private Collection<JsonBean> insertList;
036
037    /**
038     * Initialize the JPAExecutor using the insert and update list of JSON beans
039     * @param updateList
040     * @param insertList
041     */
042    public BulkUpdateInsertJPAExecutor(Collection<JsonBean> updateList, Collection<JsonBean> insertList) {
043        this.updateList = updateList;
044        this.insertList = insertList;
045    }
046
047    public BulkUpdateInsertJPAExecutor() {
048    }
049
050    /**
051     * Sets the update list for JSON bean
052     * @param updateList
053     */
054    public void setUpdateList(Collection<JsonBean> updateList) {
055        this.updateList = updateList;
056    }
057
058    /**
059     * Sets the insert list for JSON bean
060     * @param insertList
061     */
062    public void setInsertList(Collection<JsonBean> insertList) {
063        this.insertList = insertList;
064    }
065
066
067    /* (non-Javadoc)
068     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
069     */
070    @Override
071    public String getName() {
072        return "BulkUpdateInsertJPAExecutor";
073    }
074
075    /* (non-Javadoc)
076     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
077     */
078    @Override
079    public Void execute(EntityManager em) throws JPAExecutorException {
080        try {
081            if (insertList!= null){
082                for (JsonBean entity: insertList){
083                    ParamChecker.notNull(entity, "JsonBean");
084                    em.persist(entity);
085                }
086            }
087            // Only used by test cases to check for rollback of transaction
088            FaultInjection.activate("org.apache.oozie.command.SkipCommitFaultInjection");
089            if (updateList!= null){
090                for (JsonBean entity: updateList){
091                    ParamChecker.notNull(entity, "JsonBean");
092                    em.merge(entity);
093                }
094            }
095            return null;
096        }
097        catch (Exception e) {
098            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
099        }
100    }
101
102
103}