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;
021import javax.persistence.EntityManager;
022import javax.persistence.Query;
023
024import org.apache.oozie.CoordinatorActionBean;
025import org.apache.oozie.ErrorCode;
026import org.apache.oozie.FaultInjection;
027import org.apache.oozie.client.rest.JsonBean;
028import org.apache.oozie.sla.SLARegistrationBean;
029import org.apache.oozie.util.ParamChecker;
030
031/**
032 * Class for updating and deleting beans in bulk
033 */
034public class BulkUpdateDeleteJPAExecutor implements JPAExecutor<Void> {
035
036    private Collection<JsonBean> updateList;
037    private Collection<JsonBean> deleteList;
038    private boolean forRerun = true;
039
040    /**
041     * Initialize the JPAExecutor using the update and delete list of JSON beans
042     * @param deleteList
043     * @param updateList
044     */
045    public BulkUpdateDeleteJPAExecutor(Collection<JsonBean> updateList, Collection<JsonBean> deleteList,
046            boolean forRerun) {
047        this.updateList = updateList;
048        this.deleteList = deleteList;
049        this.forRerun = forRerun;
050    }
051
052    public BulkUpdateDeleteJPAExecutor() {
053    }
054
055    /**
056     * Sets the update list for JSON bean
057     *
058     * @param updateList
059     */
060    public void setUpdateList(Collection<JsonBean> updateList) {
061        this.updateList = updateList;
062    }
063
064    /**
065     * Sets the delete list for JSON bean
066     *
067     * @param deleteList
068     */
069    public void setDeleteList(Collection<JsonBean> deleteList) {
070        this.deleteList = deleteList;
071    }
072
073    /**
074     * Sets whether for RerunX command or no. Else it'd be for ChangeX
075     *
076     * @param forRerun
077     */
078    public void setForRerun(boolean forRerun) {
079        this.forRerun = forRerun;
080    }
081
082    /*
083     * (non-Javadoc)
084     *
085     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
086     */
087    @Override
088    public String getName() {
089        return "BulkUpdateDeleteJPAExecutor";
090    }
091
092    /*
093     * (non-Javadoc)
094     *
095     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.
096     * EntityManager)
097     */
098    @Override
099    public Void execute(EntityManager em) throws JPAExecutorException {
100        try {
101            if (updateList != null) {
102                for (JsonBean entity : updateList) {
103                    ParamChecker.notNull(entity, "JsonBean");
104                    em.merge(entity);
105                }
106            }
107            // Only used by test cases to check for rollback of transaction
108            FaultInjection.activate("org.apache.oozie.command.SkipCommitFaultInjection");
109            if (deleteList != null) {
110                for (JsonBean entity : deleteList) {
111                    ParamChecker.notNull(entity, "JsonBean");
112                    if (forRerun) {
113                        em.remove(em.merge(entity));
114                    }
115                    else if (entity instanceof CoordinatorActionBean) {
116                        Query g = em.createNamedQuery("DELETE_UNSCHEDULED_ACTION");
117                        String coordActionId = ((CoordinatorActionBean) entity).getId();
118                        g.setParameter("id", coordActionId);
119                        int actionsDeleted = g.executeUpdate();
120                        if (actionsDeleted == 0)
121                            throw new JPAExecutorException(ErrorCode.E1022, coordActionId);
122                    }
123                    else {
124                        em.remove(em.merge(entity));
125                    }
126                }
127            }
128            return null;
129        }
130        catch (JPAExecutorException je) {
131            throw je;
132        }
133        catch (Exception e) {
134            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
135        }
136    }
137}