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