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.sql.Timestamp;
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.client.CoordinatorAction;
027    import org.apache.oozie.util.DateUtils;
028    import org.apache.oozie.util.ParamChecker;
029    
030    /**
031     * JPAExecutor to get attributes of CoordinatorActionBean required by CoordActionInputCheckXCommand
032     */
033    public class CoordActionGetForInputCheckJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
034    
035        private String coordActionId = null;
036    
037        public CoordActionGetForInputCheckJPAExecutor(String coordActionId) {
038            ParamChecker.notNull(coordActionId, "coordActionId");
039            this.coordActionId = coordActionId;
040        }
041    
042        /* (non-Javadoc)
043         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
044         */
045        @Override
046        public String getName() {
047            return "CoordActionGetForInputCheckJPAExecutor";
048        }
049    
050        /* (non-Javadoc)
051         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
052         */
053        @Override
054        public CoordinatorActionBean execute(EntityManager em) throws JPAExecutorException {
055            try {
056                Query q = em.createNamedQuery("GET_COORD_ACTION_FOR_INPUTCHECK");
057                q.setParameter("id", coordActionId);
058                Object[] obj = (Object[]) q.getSingleResult();
059                CoordinatorActionBean caBean = getBeanForRunningCoordAction(obj);
060                return caBean;
061            }
062            catch (Exception e) {
063                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
064            }
065    
066        }
067    
068        private CoordinatorActionBean getBeanForRunningCoordAction(Object[] arr) {
069            CoordinatorActionBean bean = new CoordinatorActionBean();
070            if (arr[0] != null) {
071                bean.setId((String) arr[0]);
072            }
073            if (arr[1] != null) {
074                bean.setJobId((String) arr[1]);
075            }
076            if (arr[2] != null) {
077                bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
078            }
079            if (arr[3] != null) {
080                bean.setRunConf((String) arr[3]);
081            }
082            if (arr[4] != null) {
083                bean.setNominalTime(DateUtils.toDate((Timestamp) arr[4]));
084            }
085            if (arr[5] != null) {
086                bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[5]));
087            }
088            if (arr[6] != null) {
089                bean.setActionXml((String) arr[6]);
090            }
091            if (arr[7] != null) {
092                bean.setMissingDependencies((String) arr[7]);
093            }
094            if (arr[8] != null) {
095                bean.setTimeOut((Integer) arr[8]);
096            }
097            return bean;
098        }
099    }