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.bundle;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.oozie.CoordinatorJobBean;
023import org.apache.oozie.ErrorCode;
024import org.apache.oozie.XException;
025import org.apache.oozie.command.CommandException;
026import org.apache.oozie.command.coord.CoordSubmitXCommand;
027import org.apache.oozie.executor.jpa.CoordJobQueryExecutor;
028import org.apache.oozie.executor.jpa.CoordJobQueryExecutor.CoordJobQuery;
029
030public class BundleCoordSubmitXCommand extends CoordSubmitXCommand {
031
032    private String coordId;
033
034    public BundleCoordSubmitXCommand(Configuration conf, String bundleId, String coordName) {
035        super(conf, bundleId, coordName);
036    }
037
038    @Override
039    public String getEntityKey() {
040        return bundleId + "_" + coordName;
041    }
042
043    @Override
044    protected boolean isLockRequired() {
045        return true;
046    }
047
048    @Override
049    protected void verifyPrecondition() throws CommandException {
050        super.verifyPrecondition();
051        if (coordId != null) {
052            LOG.warn("Coord [{0}] is already submitted for bundle [{1}]", coordId, bundleId);
053            throw new CommandException(ErrorCode.E1304, coordName);
054        }
055    }
056
057    protected void loadState() throws CommandException {
058        super.loadState();
059        try {
060            CoordinatorJobBean coordJobs = CoordJobQueryExecutor.getInstance().getIfExist(
061                    CoordJobQuery.GET_COORD_JOBS_FOR_BUNDLE_BY_APPNAME_ID, coordName, bundleId);
062
063            if (coordJobs != null) {
064                coordId = coordJobs.getId();
065            }
066        }
067        catch (XException ex) {
068            throw new CommandException(ex);
069        }
070    }
071
072    @Override
073    public String getKey() {
074        return getName() + "_" + getEntityKey();
075    }
076
077}