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