001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.command.wf;
016    
017    import org.apache.oozie.ErrorCode;
018    import org.apache.oozie.XException;
019    import org.apache.oozie.service.JPAService;
020    import org.apache.oozie.service.Services;
021    import org.apache.oozie.util.ParamChecker;
022    import org.apache.oozie.command.CommandException;
023    import org.apache.oozie.command.PreconditionException;
024    import org.apache.oozie.executor.jpa.WorkflowIdGetForExternalIdJPAExecutor;
025    
026    public class ExternalIdXCommand extends WorkflowXCommand<String> {
027        private String externalId;
028    
029        public ExternalIdXCommand(String externalId) {
030            super("externalId", "externalId", 1);
031            this.externalId = ParamChecker.notEmpty(externalId, "externalId");
032        }
033    
034        @Override
035        protected boolean isLockRequired() {
036            return false;
037        }
038    
039        @Override
040        protected String getEntityKey() {
041            return this.externalId;
042        }
043    
044        @Override
045        protected void loadState() throws CommandException {
046        }
047    
048        @Override
049        protected void verifyPrecondition() throws CommandException, PreconditionException {
050        }
051    
052        @Override
053        protected String execute() throws CommandException {
054            try {
055                JPAService jpaService = Services.get().get(JPAService.class);
056                String wfId = null;
057                if (jpaService != null) {
058                    wfId = jpaService.execute(new WorkflowIdGetForExternalIdJPAExecutor(externalId));
059                }
060                else {
061                    LOG.error(ErrorCode.E0610);
062                }
063                return wfId;
064            }
065            catch (XException ex) {
066                throw new CommandException(ex);
067            }
068        }
069    
070    }