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 */
018package org.apache.oozie.command.wf;
019
020import org.apache.oozie.ErrorCode;
021import org.apache.oozie.WorkflowJobBean;
022import org.apache.oozie.XException;
023import org.apache.oozie.command.CommandException;
024import org.apache.oozie.command.PreconditionException;
025import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor;
026import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor.WorkflowJobQuery;
027import org.apache.oozie.service.JPAService;
028import org.apache.oozie.service.Services;
029import org.apache.oozie.util.LogUtils;
030import org.apache.oozie.util.ParamChecker;
031
032public class DefinitionXCommand extends WorkflowXCommand<String> {
033    private String jobId;
034    private WorkflowJobBean wfJob;
035
036    public DefinitionXCommand(String jobId) {
037        super("definition", "definition", 1);
038        this.jobId = ParamChecker.notEmpty(jobId, "jobId");
039    }
040
041    @Override
042    protected boolean isLockRequired() {
043        return false;
044    }
045
046    @Override
047    public String getEntityKey() {
048        return this.jobId;
049    }
050
051    @Override
052    protected void loadState() throws CommandException {
053        try {
054            JPAService jpaService = Services.get().get(JPAService.class);
055
056            if (jpaService != null) {
057                this.wfJob = WorkflowJobQueryExecutor.getInstance().get(WorkflowJobQuery.GET_WORKFLOW_DEFINITION, jobId);
058                LogUtils.setLogInfo(wfJob, logInfo);
059            }
060            else {
061                LOG.error(ErrorCode.E0610);
062            }
063        }
064        catch (XException ex) {
065            throw new CommandException(ex);
066        }
067    }
068
069    @Override
070    protected void verifyPrecondition() throws CommandException, PreconditionException {
071    }
072
073    @Override
074    protected String execute() throws CommandException {
075        if (wfJob != null) {
076            return wfJob.getWorkflowInstance().getApp().getDefinition();
077        }
078        else {
079            throw new CommandException(ErrorCode.E0604, "null");
080        }
081    }
082
083}