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.wf;
020
021import org.apache.oozie.ErrorCode;
022import org.apache.oozie.XException;
023import org.apache.oozie.service.JPAService;
024import org.apache.oozie.service.Services;
025import org.apache.oozie.util.ParamChecker;
026import org.apache.oozie.command.CommandException;
027import org.apache.oozie.command.PreconditionException;
028import org.apache.oozie.executor.jpa.WorkflowIdGetForExternalIdJPAExecutor;
029
030public class ExternalIdXCommand extends WorkflowXCommand<String> {
031    private String externalId;
032
033    public ExternalIdXCommand(String externalId) {
034        super("externalId", "externalId", 1);
035        this.externalId = ParamChecker.notEmpty(externalId, "externalId");
036    }
037
038    @Override
039    protected boolean isLockRequired() {
040        return false;
041    }
042
043    @Override
044    public String getEntityKey() {
045        return this.externalId;
046    }
047
048    @Override
049    protected void loadState() throws CommandException {
050    }
051
052    @Override
053    protected void verifyPrecondition() throws CommandException, PreconditionException {
054    }
055
056    @Override
057    protected String execute() throws CommandException {
058        try {
059            JPAService jpaService = Services.get().get(JPAService.class);
060            String wfId = null;
061            if (jpaService != null) {
062                wfId = jpaService.execute(new WorkflowIdGetForExternalIdJPAExecutor(externalId));
063            }
064            else {
065                LOG.error(ErrorCode.E0610);
066            }
067            return wfId;
068        }
069        catch (XException ex) {
070            throw new CommandException(ex);
071        }
072    }
073
074}