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.executor.jpa;
020
021import java.sql.Timestamp;
022import java.util.ArrayList;
023import java.util.List;
024
025import javax.persistence.EntityManager;
026import javax.persistence.Query;
027
028import org.apache.oozie.CoordinatorActionBean;
029import org.apache.oozie.ErrorCode;
030import org.apache.oozie.StringBlob;
031import org.apache.oozie.client.CoordinatorAction;
032import org.apache.oozie.util.ParamChecker;
033
034public class CoordActionsGetForRecoveryJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
035
036    private long checkAgeSecs = 0;
037
038    public CoordActionsGetForRecoveryJPAExecutor(final long checkAgeSecs) {
039        ParamChecker.notNull(checkAgeSecs, "checkAgeSecs");
040        this.checkAgeSecs = checkAgeSecs;
041    }
042
043    /* (non-Javadoc)
044     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
045     */
046    @Override
047    public String getName() {
048        return "CoordActionsGetForRecoveryJPAExecutor";
049    }
050
051    /* (non-Javadoc)
052     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
053     */
054    @SuppressWarnings("unchecked")
055    @Override
056    public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
057        List<CoordinatorActionBean> allActions = new ArrayList<CoordinatorActionBean>();
058
059        try {
060            Query q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_RECOVERY_OLDER_THAN");
061            Timestamp ts = new Timestamp(System.currentTimeMillis() - this.checkAgeSecs * 1000);
062            q.setParameter("lastModifiedTime", ts);
063            List<Object[]> objectArrList = q.getResultList();
064            for (Object[] arr : objectArrList) {
065                CoordinatorActionBean caa = getBeanForCoordinatorActionFromArrayForRecovery(arr);
066                allActions.add(caa);
067            }
068
069            q = em.createNamedQuery("GET_COORD_ACTIONS_WAITING_SUBMITTED_OLDER_THAN");
070            q.setParameter("lastModifiedTime", ts);
071            objectArrList = q.getResultList();
072            for (Object[] arr : objectArrList) {
073                CoordinatorActionBean caa = getBeanForCoordinatorActionFromArrayForWaiting(arr);
074                allActions.add(caa);
075            }
076
077            return allActions;
078        }
079        catch (IllegalStateException e) {
080            throw new JPAExecutorException(ErrorCode.E0601, e.getMessage(), e);
081        }
082    }
083
084    private CoordinatorActionBean getBeanForCoordinatorActionFromArrayForRecovery(Object[] arr) {
085        CoordinatorActionBean bean = new CoordinatorActionBean();
086        if (arr[0] != null) {
087            bean.setId((String) arr[0]);
088        }
089        if (arr[1] != null){
090            bean.setJobId((String) arr[1]);
091        }
092        if (arr[2] != null) {
093            bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
094        }
095        if (arr[3] != null) {
096            bean.setExternalId((String) arr[3]);
097        }
098        if (arr[4] != null) {
099            bean.setPending((Integer) arr[4]);
100        }
101        return bean;
102    }
103
104
105    private CoordinatorActionBean getBeanForCoordinatorActionFromArrayForWaiting(Object[] arr){
106        CoordinatorActionBean bean = new CoordinatorActionBean();
107        if (arr[0] != null) {
108            bean.setId((String) arr[0]);
109        }
110        if (arr[1] != null){
111            bean.setJobId((String) arr[1]);
112        }
113        if (arr[2] != null) {
114            bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
115        }
116        if (arr[3] != null) {
117            bean.setExternalId((String) arr[3]);
118        }
119        if (arr[4] != null) {
120            bean.setPushMissingDependenciesBlob((StringBlob) arr[4]);
121        }
122        return bean;
123    }
124
125}