View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.web.struts;
17  
18  import java.util.Date;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.batch.BatchJobStatus;
31  import org.kuali.ole.sys.batch.service.SchedulerService;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.rice.core.api.config.property.ConfigurationService;
34  import org.kuali.rice.core.api.datetime.DateTimeService;
35  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
36  import org.kuali.rice.kim.api.KimConstants;
37  import org.kuali.rice.kim.api.services.IdentityManagementService;
38  import org.kuali.rice.kns.web.struts.action.KualiAction;
39  import org.kuali.rice.krad.exception.AuthorizationException;
40  import org.kuali.rice.krad.util.GlobalVariables;
41  import org.kuali.rice.krad.util.KRADConstants;
42  import org.kuali.rice.krad.util.KRADUtils;
43  import org.kuali.rice.krad.util.UrlFactory;
44  
45  public class KualiBatchJobModifyAction extends KualiAction {
46  
47      private static final String JOB_NAME_PARAMETER = "name";
48      private static final String JOB_GROUP_PARAMETER = "group";
49      private static final String START_STEP_PARAMETER = "startStep";
50      private static final String END_STEP_PARAMETER = "endStep";
51      private static final String START_TIME_PARAMETER = "startTime";
52      private static final String EMAIL_PARAMETER = "emailAddress";
53  
54      private static SchedulerService schedulerService;
55      private static ParameterService parameterService;
56      private static IdentityManagementService identityManagementService;
57      private static DateTimeService dateTimeService;
58      
59      @Override
60      protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
61          if (form instanceof KualiBatchJobModifyForm) {
62              if (!getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, KRADUtils.getNamespaceAndComponentSimpleName(BatchJobStatus.class), new HashMap<String,String>(getRoleQualification(form, "use")))) {
63                  throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), "view", "batch jobs");
64              }
65          }
66          else {
67              super.checkAuthorization(form, methodToCall);
68          }
69      }
70  
71      /**
72       * Performs the actual authorization check for a given job and action against the current user. This method can be overridden by
73       * sub-classes if more granular controls are desired.
74       * 
75       * @param job
76       * @param actionType
77       * @throws AuthorizationException
78       */
79      protected boolean canModifyJob(KualiBatchJobModifyForm form, String actionType) {
80          Map<String,String> permissionDetails = new HashMap<String,String>();
81          permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, form.getJob().getNamespaceCode());
82          permissionDetails.put(KimConstants.AttributeConstants.BEAN_NAME, form.getJob().getName());
83          return getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KNS_NAMESPACE, OLEConstants.PermissionTemplate.MODIFY_BATCH_JOB.name, permissionDetails, new HashMap<String,String>(getRoleQualification(form, actionType)));
84      }
85      
86      protected void checkJobAuthorization(KualiBatchJobModifyForm form, String actionType) throws AuthorizationException {
87          if (!canModifyJob(form, actionType)) {
88              throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), "actionType", form.getJob().getName());
89          }
90      }
91      
92      @Override
93      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
94          // load the given job and map into the form
95          String jobName = request.getParameter(JOB_NAME_PARAMETER);
96          String jobGroup = request.getParameter(JOB_GROUP_PARAMETER);
97          if (form instanceof KualiBatchJobModifyForm) {
98              ((KualiBatchJobModifyForm)form).setJob(getSchedulerService().getJob(jobGroup, jobName));
99          }
100         ActionForward forward = super.execute(mapping, form, request, response);
101         return forward;
102     }
103 
104     private IdentityManagementService getIdentityManagementService() {
105         if (identityManagementService == null) {
106             identityManagementService = SpringContext.getBean(IdentityManagementService.class);
107         }
108         return identityManagementService;
109     }
110 
111     private SchedulerService getSchedulerService() {
112         if (schedulerService == null) {
113             schedulerService = SpringContext.getBean(SchedulerService.class);
114         }
115         return schedulerService;
116     }
117 
118     public static ParameterService getParameterService() {
119         if (parameterService == null) {
120             parameterService = SpringContext.getBean(ParameterService.class);
121         }
122         return parameterService;
123     }
124 
125     public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
126         KualiBatchJobModifyForm batchModifyForm = (KualiBatchJobModifyForm)form;
127 
128         request.setAttribute("job", batchModifyForm.getJob());
129         request.setAttribute("canRunJob", canModifyJob(batchModifyForm, "runJob"));
130         request.setAttribute("canSchedule", canModifyJob(batchModifyForm, "schedule"));
131         request.setAttribute("canUnschedule", canModifyJob(batchModifyForm, "unschedule"));
132         request.setAttribute("canStopJob", canModifyJob(batchModifyForm, "stopJob"));
133         request.setAttribute("userEmailAddress", GlobalVariables.getUserSession().getPerson().getEmailAddressUnmasked());
134 
135         return mapping.findForward(OLEConstants.MAPPING_BASIC);
136     }
137 
138     public ActionForward runJob(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
139         KualiBatchJobModifyForm batchModifyForm = (KualiBatchJobModifyForm)form;
140 
141         checkJobAuthorization(batchModifyForm, "runJob");
142 
143         String startStepStr = request.getParameter(START_STEP_PARAMETER);
144         String endStepStr = request.getParameter(END_STEP_PARAMETER);
145         String startTimeStr = request.getParameter(START_TIME_PARAMETER);
146         String emailAddress = request.getParameter(EMAIL_PARAMETER);
147 
148         int startStep = Integer.parseInt(startStepStr);
149         int endStep = Integer.parseInt(endStepStr);
150         Date startTime;
151         if (!StringUtils.isBlank(startTimeStr)) {
152             startTime = getDateTimeService().convertToDateTime(startTimeStr);
153         } else {
154             startTime = getDateTimeService().getCurrentDate();
155         }
156 
157         batchModifyForm.getJob().runJob(startStep, endStep, startTime, emailAddress);
158 
159         // redirect to display form to prevent re-execution of the job by mistake
160         return getForward(batchModifyForm.getJob());
161     }
162 
163     public ActionForward stopJob(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
164         KualiBatchJobModifyForm batchModifyForm = (KualiBatchJobModifyForm)form;
165 
166         checkJobAuthorization(batchModifyForm, "stopJob");
167 
168         batchModifyForm.getJob().interrupt();
169 
170         return getForward(batchModifyForm.getJob());
171     }
172 
173 
174     public ActionForward schedule(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
175         KualiBatchJobModifyForm batchModifyForm = (KualiBatchJobModifyForm)form;
176 
177         checkJobAuthorization(batchModifyForm, "schedule");
178 
179         batchModifyForm.getJob().schedule();
180 
181         return getForward(batchModifyForm.getJob());
182     }
183 
184     public ActionForward unschedule(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
185         KualiBatchJobModifyForm batchModifyForm = (KualiBatchJobModifyForm)form;
186 
187         checkJobAuthorization(batchModifyForm, "unschedule");
188 
189         batchModifyForm.getJob().unschedule();
190 
191         // move to the unscheduled job object since the scheduled one has been removed
192         batchModifyForm.setJob(getSchedulerService().getJob(SchedulerService.UNSCHEDULED_GROUP, batchModifyForm.getJob().getName()));
193 
194         return getForward(batchModifyForm.getJob());
195     }
196 
197     private ActionForward getForward(BatchJobStatus job) {
198         return new ActionForward(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY) + "/batchModify.do?methodToCall=start&name=" + UrlFactory.encode(job.getName()) + "&group=" + UrlFactory.encode(job.getGroup()), true);
199     }
200 
201     public static DateTimeService getDateTimeService() {
202         if (dateTimeService == null) {
203             dateTimeService = SpringContext.getBean(DateTimeService.class);
204         }
205         return dateTimeService;
206     }
207 }
208