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