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