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.executor.jpa;
016    
017    import java.util.List;
018    
019    import javax.persistence.EntityManager;
020    import javax.persistence.Query;
021    
022    import org.apache.oozie.util.ParamChecker;
023    
024    /**
025     * Get the Workflow ID with given external ID which will be assigned for the subworkflows.
026     */
027    public class WorkflowIdGetForExternalIdJPAExecutor implements JPAExecutor<String> {
028    
029        private String externalId = null;
030    
031        public WorkflowIdGetForExternalIdJPAExecutor(String externalId) {
032            ParamChecker.notNull(externalId, "externalId");
033            this.externalId = externalId;
034        }
035    
036        /* (non-Javadoc)
037         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
038         */
039        @Override
040        public String getName() {
041            return "WorkflowIdGetForExternalIdJPAExecutor";
042        }
043    
044        /* (non-Javadoc)
045         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
046         */
047        @Override
048        @SuppressWarnings("unchecked")
049        public String execute(EntityManager em) throws JPAExecutorException {
050            String id = "";
051            Query q = em.createNamedQuery("GET_WORKFLOW_ID_FOR_EXTERNAL_ID");
052            q.setParameter("externalId", externalId);
053            List<String> w = q.getResultList();
054            if (w.size() > 0) {
055                int index = w.size() - 1;
056                id = w.get(index);
057            }
058            return id;
059        }
060    }