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