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 SLAService on restart
032     */
033    public class CoordActionGetForSLAJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
034    
035        private String coordActionId;
036    
037        public CoordActionGetForSLAJPAExecutor(String coordActionId) {
038            ParamChecker.notNull(coordActionId, "coordActionId");
039            this.coordActionId = coordActionId;
040        }
041    
042        @Override
043        public String getName() {
044            return "CoordActionGetForSLAJPAExecutor";
045        }
046    
047        @Override
048        public CoordinatorActionBean execute(EntityManager em) throws JPAExecutorException {
049            try {
050                Query q = em.createNamedQuery("GET_COORD_ACTION_FOR_SLA");
051                q.setParameter("id", coordActionId);
052                Object[] obj = (Object[]) q.getSingleResult();
053                CoordinatorActionBean caBean = getBeanForRunningCoordAction(obj);
054                return caBean;
055            }
056            catch (Exception e) {
057                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
058            }
059    
060        }
061    
062        private CoordinatorActionBean getBeanForRunningCoordAction(Object[] arr) {
063            CoordinatorActionBean bean = new CoordinatorActionBean();
064            if (arr[0] != null) {
065                bean.setId((String) arr[0]);
066            }
067            if (arr[1] != null) {
068                bean.setJobId((String) arr[1]);
069            }
070            if (arr[2] != null) {
071                bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
072            }
073            if (arr[3] != null) {
074                bean.setExternalId((String) arr[3]);
075            }
076            if (arr[4] != null) {
077                bean.setLastModifiedTime(DateUtils.toDate((Timestamp)arr[4]));
078            }
079            return bean;
080        }
081    }