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.coord;
020
021import org.apache.oozie.CoordinatorActionBean;
022import org.apache.oozie.CoordinatorWfActionBean;
023import org.apache.oozie.ErrorCode;
024import org.apache.oozie.WorkflowActionBean;
025import org.apache.oozie.client.CoordinatorWfAction;
026import org.apache.oozie.command.CommandException;
027import org.apache.oozie.command.PreconditionException;
028import org.apache.oozie.executor.jpa.CoordJobGetActionsSubsetJPAExecutor;
029import org.apache.oozie.executor.jpa.JPAExecutorException;
030import org.apache.oozie.executor.jpa.WorkflowActionGetJPAExecutor;
031import org.apache.oozie.service.JPAService;
032import org.apache.oozie.service.Services;
033import org.apache.oozie.util.ParamChecker;
034
035import java.util.ArrayList;
036import java.util.List;
037
038public class CoordWfActionInfoXCommand extends CoordinatorXCommand<List<CoordinatorWfActionBean>>{
039    /**
040     * This class gets the wf action info in coordinator by action name and coordinator job ID.
041     */
042    private static final String ACTION_INFO = "action.info";
043    private static final int DEFAULT_OFFSET = 1;
044    private static final int DEFAULT_LEN = 50;
045
046    private final String jobId;
047    private final String actionName;
048    private final int offset;
049    private final int len;
050    private List<CoordinatorActionBean> coordActions;
051    private JPAService jpaService = null;
052
053    public CoordWfActionInfoXCommand(String jobId, String actionName) {
054        this(jobId, actionName, DEFAULT_OFFSET, DEFAULT_LEN);
055    }
056
057    public CoordWfActionInfoXCommand(String jobId, String actionName, int offset, int len) {
058        super(ACTION_INFO, ACTION_INFO, 1);
059
060        this.jobId = ParamChecker.notEmpty(jobId, "jobId");
061        this.actionName = ParamChecker.notEmpty(actionName, "actionName");
062        this.offset = offset;
063        this.len = len;
064    }
065
066    @Override
067    protected List<CoordinatorWfActionBean> execute() throws CommandException {
068        List<CoordinatorWfActionBean> coordWfActions = new ArrayList<CoordinatorWfActionBean>();
069        for(CoordinatorActionBean coordAction : coordActions) {
070            String wfId = coordAction.getExternalId();
071            String nullReason = null;
072            WorkflowActionBean wfAction = null;
073            if (wfId != null) {
074                String wfActionId = wfId + "@" + actionName;
075                try {
076                    wfAction = jpaService.execute(new WorkflowActionGetJPAExecutor(wfActionId, true));
077                    if (wfAction == null) {
078                        nullReason = CoordinatorWfAction.NullReason.ACTION_NULL.getNullReason(actionName, wfId);
079                    }
080                } catch (JPAExecutorException ex) {
081                    throw new CommandException(ex);
082                }
083            } else {
084                nullReason = CoordinatorWfAction.NullReason.PARENT_NULL.getNullReason();
085                LOG.warn(nullReason);
086                wfAction = null;
087            }
088            int actionNumber = coordAction.getActionNumber();
089            CoordinatorWfActionBean coordWfAction = new CoordinatorWfActionBean(actionNumber, wfAction, nullReason);
090
091            coordWfActions.add(coordWfAction);
092        }
093        return coordWfActions;
094    }
095
096    /**
097     *  (non-Javadoc)
098     *  @see org.apache.oozie.command.XCommand#loadState()
099     **/
100    @Override
101    protected void loadState() throws CommandException {
102        jpaService = Services.get().get(JPAService.class);
103        if (jpaService != null) {
104            try {
105                coordActions = jpaService.execute(
106                        new CoordJobGetActionsSubsetJPAExecutor(jobId, null, offset, len, false));
107            } catch (JPAExecutorException ex) {
108                LOG.error(ErrorCode.E0612);
109                throw new CommandException(ex);
110            }
111        }
112        else {
113            throw new CommandException(ErrorCode.E0610);
114        }
115    }
116
117    /**
118     * (non-Javadoc)
119     * @see org.apache.oozie.command.XCommand#verifyPrecondition()
120     * no-op
121     **/
122    @Override
123    protected void verifyPrecondition() throws CommandException, PreconditionException {
124    }
125
126    /**
127     *  (non-Javadoc)
128     *  @see org.apache.oozie.command.XCommand#isLockRequired()
129     **/
130    @Override
131    protected boolean isLockRequired() {
132        return false;
133    }
134
135    /**
136     *  (non-Javadoc)
137     *  @see org.apache.oozie.command.XCommand#getEntityKey()
138     **/
139    @Override
140    public String getEntityKey() {
141        return null;
142    }
143
144}