001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.kfs.web.struts.action;
017:
018: import java.util.Date;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.struts.action.ActionForm;
025: import org.apache.struts.action.ActionForward;
026: import org.apache.struts.action.ActionMapping;
027: import org.kuali.core.exceptions.AuthorizationException;
028: import org.kuali.core.service.DateTimeService;
029: import org.kuali.core.service.KualiConfigurationService;
030: import org.kuali.core.util.GlobalVariables;
031: import org.kuali.core.util.UrlFactory;
032: import org.kuali.core.web.struts.action.KualiAction;
033: import org.kuali.kfs.KFSConstants;
034: import org.kuali.kfs.batch.BatchJobStatus;
035: import org.kuali.kfs.context.SpringContext;
036: import org.kuali.kfs.service.ParameterService;
037: import org.kuali.kfs.service.SchedulerService;
038: import org.kuali.kfs.service.impl.ParameterConstants;
039:
040: public class KualiBatchJobModifyAction extends KualiAction {
041:
042: private static final String JOB_NAME_PARAMETER = "name";
043: private static final String JOB_GROUP_PARAMETER = "group";
044: private static final String START_STEP_PARAMETER = "startStep";
045: private static final String END_STEP_PARAMETER = "endStep";
046: private static final String START_TIME_PARAMETER = "startTime";
047: private static final String EMAIL_PARAMETER = "emailAddress";
048:
049: private static SchedulerService schedulerService;
050: private static ParameterService parameterService;
051:
052: private SchedulerService getSchedulerService() {
053: if (schedulerService == null) {
054: schedulerService = SpringContext
055: .getBean(SchedulerService.class);
056: }
057: return schedulerService;
058: }
059:
060: public static ParameterService getParameterService() {
061: if (parameterService == null) {
062: parameterService = SpringContext
063: .getBean(ParameterService.class);
064: }
065: return parameterService;
066: }
067:
068: private BatchJobStatus getJob(HttpServletRequest request) {
069: // load the given job and map into the form
070: String jobName = request.getParameter(JOB_NAME_PARAMETER);
071: String jobGroup = request.getParameter(JOB_GROUP_PARAMETER);
072:
073: return getSchedulerService().getJob(jobGroup, jobName);
074: }
075:
076: private boolean canModifyJob(BatchJobStatus job, String actionType) {
077: try {
078: checkJobAuthorization(job, actionType);
079: } catch (AuthorizationException ex) {
080: return false;
081: }
082: return true;
083: }
084:
085: /**
086: * Performs the actual authorization check for a given job and action against the current user. This method can be overridden by
087: * sub-classes if more granular controls are desired.
088: *
089: * @param job
090: * @param actionType
091: * @throws AuthorizationException
092: */
093: protected void checkJobAuthorization(BatchJobStatus job,
094: String actionType) throws AuthorizationException {
095: String adminWorkgroup = getParameterService()
096: .getParameterValue(
097: ParameterConstants.FINANCIAL_SYSTEM_BATCH.class,
098: KFSConstants.SystemGroupParameterNames.JOB_ADMIN_WORKGROUP);
099: if (getParameterService()
100: .parameterExists(
101: ParameterConstants.FINANCIAL_SYSTEM_BATCH.class,
102: job.getFullName()
103: + KFSConstants.SystemGroupParameterNames.JOB_WORKGROUP_SUFFIX)) {
104: String jobSpecificAdminWorkgroup = getParameterService()
105: .getParameterValue(
106: ParameterConstants.FINANCIAL_SYSTEM_BATCH.class,
107: job.getFullName()
108: + KFSConstants.SystemGroupParameterNames.JOB_WORKGROUP_SUFFIX);
109: if (!(GlobalVariables.getUserSession().getUniversalUser()
110: .isMember(adminWorkgroup) || GlobalVariables
111: .getUserSession().getUniversalUser().isMember(
112: jobSpecificAdminWorkgroup))) {
113: throw new AuthorizationException(GlobalVariables
114: .getUserSession().getUniversalUser()
115: .getPersonUserIdentifier(), actionType, job
116: .getFullName());
117: }
118: }
119: }
120:
121: public ActionForward start(ActionMapping mapping, ActionForm form,
122: HttpServletRequest request, HttpServletResponse response)
123: throws Exception {
124: BatchJobStatus job = getJob(request);
125:
126: request.setAttribute("job", job);
127: request.setAttribute("canRunJob", canModifyJob(job, "runJob"));
128: request.setAttribute("canSchedule", canModifyJob(job,
129: "schedule"));
130: request.setAttribute("canUnschedule", canModifyJob(job,
131: "unschedule"));
132: request
133: .setAttribute("canStopJob",
134: canModifyJob(job, "stopJob"));
135: request.setAttribute("userEmailAddress", GlobalVariables
136: .getUserSession().getUniversalUser()
137: .getPersonEmailAddress());
138:
139: return mapping.findForward(KFSConstants.MAPPING_BASIC);
140: }
141:
142: public ActionForward runJob(ActionMapping mapping, ActionForm form,
143: HttpServletRequest request, HttpServletResponse response)
144: throws Exception {
145: BatchJobStatus job = getJob(request);
146:
147: checkJobAuthorization(job, "runJob");
148:
149: String startStepStr = request
150: .getParameter(START_STEP_PARAMETER);
151: String endStepStr = request.getParameter(END_STEP_PARAMETER);
152: String startTimeStr = request
153: .getParameter(START_TIME_PARAMETER);
154: String emailAddress = request.getParameter(EMAIL_PARAMETER);
155:
156: int startStep = Integer.parseInt(startStepStr);
157: int endStep = Integer.parseInt(endStepStr);
158: Date startTime = SpringContext.getBean(DateTimeService.class)
159: .getCurrentDate();
160: if (!StringUtils.isBlank(startTimeStr)) {
161: startTime = SpringContext.getBean(DateTimeService.class)
162: .convertToDateTime(startTimeStr);
163: }
164:
165: job.runJob(startStep, endStep, startTime, emailAddress);
166:
167: // redirect to display form to prevent re-execution of the job by mistake
168: return getForward(job);
169: }
170:
171: public ActionForward stopJob(ActionMapping mapping,
172: ActionForm form, HttpServletRequest request,
173: HttpServletResponse response) throws Exception {
174: BatchJobStatus job = getJob(request);
175:
176: checkJobAuthorization(job, "stopJob");
177:
178: job.interrupt();
179:
180: return getForward(job);
181: }
182:
183: public ActionForward schedule(ActionMapping mapping,
184: ActionForm form, HttpServletRequest request,
185: HttpServletResponse response) throws Exception {
186: BatchJobStatus job = getJob(request);
187:
188: checkJobAuthorization(job, "schedule");
189:
190: job.schedule();
191:
192: return getForward(job);
193: }
194:
195: public ActionForward unschedule(ActionMapping mapping,
196: ActionForm form, HttpServletRequest request,
197: HttpServletResponse response) throws Exception {
198: BatchJobStatus job = getJob(request);
199:
200: checkJobAuthorization(job, "unschedule");
201:
202: job.unschedule();
203:
204: // move to the unscheduled job object since the scheduled one has been removed
205: job = getSchedulerService().getJob(
206: SchedulerService.UNSCHEDULED_GROUP, job.getName());
207:
208: return getForward(job);
209: }
210:
211: private ActionForward getForward(BatchJobStatus job) {
212: return new ActionForward(SpringContext.getBean(
213: KualiConfigurationService.class).getPropertyString(
214: KFSConstants.APPLICATION_URL_KEY)
215: + "/batchModify.do?methodToCall=start&name="
216: + UrlFactory.encode(job.getName())
217: + "&group="
218: + UrlFactory.encode(job.getGroup()), true);
219: }
220: }
|