001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.messaging.web;
017
018import org.apache.struts.action.ActionForm;
019import org.apache.struts.action.ActionForward;
020import org.apache.struts.action.ActionMapping;
021import org.apache.struts.action.ActionMessages;
022import org.kuali.rice.ksb.messaging.PersistedMessageBO;
023import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJob;
024import org.kuali.rice.ksb.service.KSBServiceLocator;
025import org.kuali.rice.ksb.util.KSBConstants;
026import org.quartz.JobDetail;
027import org.quartz.JobKey;
028import org.quartz.Scheduler;
029import org.quartz.Trigger;
030import org.quartz.impl.JobDetailImpl;
031import org.quartz.impl.matchers.GroupMatcher;
032import org.quartz.utils.Key;
033
034import javax.servlet.http.HttpServletRequest;
035import javax.servlet.http.HttpServletResponse;
036import java.util.ArrayList;
037import java.util.List;
038
039
040/**
041 * action to view messages in quartz and push them into the message queue. 
042 * 
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 *
045 */
046public class QuartzQueueAction extends KSBAction {
047    
048    private static final String RENDER_LIST_OVERRIDE = "_renderlistoverride";
049
050    @Override
051    public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm form) throws Exception {
052        if ("moveToRouteQueue".equals(request.getParameter("methodToCall")) && request.getAttribute(RENDER_LIST_OVERRIDE) == null) {
053            return null;
054        }
055
056        Scheduler scheduler = KSBServiceLocator.getScheduler();
057        List<QuartzQueueForm> jobs = new ArrayList<QuartzQueueForm>();
058        List<String> jobGroups = KSBServiceLocator.getScheduler().getJobGroupNames();
059
060        for (int i = 0; i < jobGroups.size(); i++) {
061            String jobGroup = KSBServiceLocator.getScheduler().getJobGroupNames().get(i);
062            for(JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(jobGroup))) {
063                Trigger trigger = scheduler.getTriggersOfJob(jobKey).get(0);
064                JobDetail jobDetail = scheduler.getJobDetail(jobKey);
065                jobs.add(new QuartzQueueForm(jobDetail, trigger) );
066            }
067        }
068
069        request.setAttribute("jobs", jobs);
070        return null;
071    }
072
073    @Override
074    public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request,
075            HttpServletResponse response) throws Exception {
076        return mapping.findForward("joblisting");
077    }
078    
079    public ActionForward moveToRouteQueue(ActionMapping mapping, ActionForm form, HttpServletRequest request,
080            HttpServletResponse response) throws Exception {
081        
082        QuartzQueueForm quartzForm = (QuartzQueueForm)form;
083
084        JobKey jobKey = new JobKey(quartzForm.getJobName(), quartzForm.getJobGroup());
085        JobDetail job = KSBServiceLocator.getScheduler().getJobDetail(jobKey);
086
087        PersistedMessageBO message = (PersistedMessageBO)job.getJobDataMap().get(MessageServiceExecutorJob.MESSAGE_KEY);
088
089        if(message != null){
090            message.setQueueStatus(KSBConstants.ROUTE_QUEUE_EXCEPTION);
091
092            message = KSBServiceLocator.getMessageQueueService().save(message);
093            KSBServiceLocator.getScheduler().deleteJob(jobKey);
094        }
095
096        request.setAttribute(RENDER_LIST_OVERRIDE, new Object());
097        establishRequiredState(request, form);
098
099        return mapping.findForward("joblisting");
100    }
101    
102    
103}