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