001/**
002 * Copyright 2005-2016 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.Trigger;
028
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031import java.util.ArrayList;
032import java.util.List;
033
034
035/**
036 * action to view messages in quartz and push them into the message queue. 
037 * 
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041public class QuartzQueueAction extends KSBAction {
042    
043    private static final String RENDER_LIST_OVERRIDE = "_renderlistoverride";
044
045    @Override
046    public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm form) throws Exception {
047        if ("moveToRouteQueue".equals(request.getParameter("methodToCall")) && request.getAttribute(RENDER_LIST_OVERRIDE) == null) {
048            return null;
049        }
050        
051        List<QuartzQueueForm> jobs = new ArrayList<QuartzQueueForm>();
052        String[] jobGroups = KSBServiceLocator.getScheduler().getJobGroupNames(); 
053        for (int i = 0; i < jobGroups.length; i++) {
054            String jobGroup = KSBServiceLocator.getScheduler().getJobGroupNames()[i];
055            String[] jobNames = KSBServiceLocator.getScheduler().getJobNames(jobGroup);
056            for (int j = 0; j < jobNames.length; j++) {
057                JobDetail job = KSBServiceLocator.getScheduler().getJobDetail(jobNames[j], jobGroup);
058                Trigger trigger = KSBServiceLocator.getScheduler().getTriggersOfJob(job.getName(), job.getGroup())[0];
059                jobs.add(new QuartzQueueForm(job, trigger));
060            }
061        }
062        request.setAttribute("jobs", jobs);
063        return null;
064    }
065
066    @Override
067    public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request,
068            HttpServletResponse response) throws Exception {
069        return mapping.findForward("joblisting");
070    }
071    
072    public ActionForward moveToRouteQueue(ActionMapping mapping, ActionForm form, HttpServletRequest request,
073            HttpServletResponse response) throws Exception {
074        
075        QuartzQueueForm quartzForm = (QuartzQueueForm)form;
076
077        JobDetail job = KSBServiceLocator.getScheduler().getJobDetail(quartzForm.getJobName(), quartzForm.getJobGroup());
078        PersistedMessageBO message = (PersistedMessageBO)job.getJobDataMap().get(MessageServiceExecutorJob.MESSAGE_KEY);
079     if(message != null){
080            message.setQueueStatus(KSBConstants.ROUTE_QUEUE_EXCEPTION);
081
082        message = KSBServiceLocator.getMessageQueueService().save(message);
083        KSBServiceLocator.getScheduler().deleteJob(quartzForm.getJobName(), quartzForm.getJobGroup());
084     }
085    request.setAttribute(RENDER_LIST_OVERRIDE, new Object());
086    establishRequiredState(request, form);
087        return mapping.findForward("joblisting");
088    }
089    
090    
091}