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.command.sla;
020
021import org.apache.oozie.CoordinatorActionBean;
022import org.apache.oozie.WorkflowJobBean;
023import org.apache.oozie.command.CommandException;
024import org.apache.oozie.executor.jpa.CoordActionQueryExecutor;
025import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor;
026import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
027import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor.WorkflowJobQuery;
028import org.apache.oozie.executor.jpa.JPAExecutorException;
029import org.apache.oozie.util.LogUtils;
030
031public class SLACoordActionJobHistoryXCommand extends SLAJobHistoryXCommand {
032
033    CoordinatorActionBean cAction = null;
034
035    public SLACoordActionJobHistoryXCommand(String jobId) {
036        super(jobId);
037    }
038
039
040    protected void loadState() throws CommandException {
041        try {
042            cAction = CoordActionQueryExecutor.getInstance().get(CoordActionQuery.GET_COORD_ACTION_FOR_SLA, jobId);
043        }
044        catch (JPAExecutorException e) {
045            throw new CommandException(e);
046        }
047        LogUtils.setLogInfo(cAction);
048    }
049
050    protected void updateSLASummary() throws CommandException {
051        try {
052            updateSLASummaryForCoordAction(cAction);
053        }
054        catch (JPAExecutorException e) {
055            throw new CommandException(e);
056        }
057
058    }
059
060    protected void updateSLASummaryForCoordAction(CoordinatorActionBean bean) throws JPAExecutorException {
061        String wrkflowId = bean.getExternalId();
062        if (wrkflowId != null) {
063            WorkflowJobBean wrkflow = WorkflowJobQueryExecutor.getInstance().get(
064                    WorkflowJobQuery.GET_WORKFLOW_START_END_TIME, wrkflowId);
065            if (wrkflow != null) {
066                updateSLASummary(bean.getId(), wrkflow.getStartTime(), wrkflow.getEndTime(), bean.getStatusStr());
067            }
068        }
069        else{
070            updateSLASummary(bean.getId(), null, bean.getLastModifiedTime(), bean.getStatusStr());
071        }
072    }
073
074    @Override
075    protected boolean isJobEnded() {
076        return cAction.isTerminalStatus();
077    }
078}