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.command.coord;
019    
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.oozie.ErrorCode;
024    import org.apache.oozie.SLAEventBean;
025    import org.apache.oozie.XException;
026    import org.apache.oozie.command.CommandException;
027    import org.apache.oozie.command.PreconditionException;
028    import org.apache.oozie.command.XCommand;
029    import org.apache.oozie.executor.jpa.SLAEventsGetForFilterJPAExecutor;
030    import org.apache.oozie.service.JPAService;
031    import org.apache.oozie.service.Service;
032    import org.apache.oozie.service.Services;
033    
034    /**
035     * The command to get a list of SLAEvents which are greater than given seqId.
036     *
037     */
038    public class SLAEventsXCommand extends XCommand<List<SLAEventBean>> {
039    
040        private long seqId = 0;
041        private int maxNoEvents = 100; // Default
042        private long lastSeqId = -1;
043        private final Map<String, List<String>> filter;
044    
045        public static final String SLA_DEFAULT_MAXEVENTS = Service.CONF_PREFIX + "sla.default.maxevents";
046    
047        public SLAEventsXCommand(long seqId, int maxNoEvnts, Map<String, List<String>> filter) {
048            super("SLAEventsXCommand", "SLAEventsXCommand", 1);
049            this.seqId = seqId;
050            int sysMax = Services.get().getConf().getInt(SLA_DEFAULT_MAXEVENTS, 1000);
051            this.maxNoEvents = maxNoEvnts > sysMax ? sysMax : maxNoEvnts;
052            this.filter = filter;
053        }
054    
055        @Override
056        protected boolean isLockRequired() {
057            return false;
058        }
059    
060        @Override
061        public String getEntityKey() {
062            return Long.toString(seqId);
063        }
064    
065        @Override
066        protected void loadState() throws CommandException {
067        }
068    
069        @Override
070        protected void verifyPrecondition() throws CommandException, PreconditionException {
071        }
072    
073        @Override
074        protected List<SLAEventBean> execute() throws CommandException {
075            try {
076                JPAService jpaService = Services.get().get(JPAService.class);
077                List<SLAEventBean> slaEventList = null;
078                long lastSeqId[] = new long[1];
079                if (jpaService != null) {
080                    slaEventList = jpaService.execute(new SLAEventsGetForFilterJPAExecutor(seqId, maxNoEvents, filter, lastSeqId));
081                }
082                else {
083                    LOG.error(ErrorCode.E0610);
084                }
085                setLastSeqId(lastSeqId[0]);
086                return slaEventList;
087            }
088            catch (XException ex) {
089                throw new CommandException(ex);
090            }
091        }
092    
093        /**
094         * Set lastSeqId
095         *
096         * @param lastSeqId
097         */
098        public void setLastSeqId(long lastSeqId) {
099            this.lastSeqId = lastSeqId;
100        }
101    
102        /**
103         * Get lastSeqId
104         *
105         * @return lastSeqId
106         */
107        public long getLastSeqId() {
108            return lastSeqId;
109        }
110    
111    }