View Javadoc
1   /*
2    * Copyright 2008 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.pdp.web.struts;
17  
18  import java.util.ArrayList;
19  import java.util.Date;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
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.pdp.PdpConstants;
30  import org.kuali.ole.pdp.PdpKeyConstants;
31  import org.kuali.ole.pdp.PdpParameterConstants;
32  import org.kuali.ole.pdp.PdpPropertyConstants;
33  import org.kuali.ole.pdp.businessobject.CustomerProfile;
34  import org.kuali.ole.pdp.businessobject.FormatProcessSummary;
35  import org.kuali.ole.pdp.businessobject.FormatSelection;
36  import org.kuali.ole.pdp.businessobject.ProcessSummary;
37  import org.kuali.ole.pdp.service.FormatService;
38  import org.kuali.ole.pdp.service.PdpAuthorizationService;
39  import org.kuali.ole.pdp.service.impl.exception.FormatException;
40  import org.kuali.ole.sys.OLEConstants;
41  import org.kuali.ole.sys.context.SpringContext;
42  import org.kuali.rice.core.api.config.property.ConfigurationService;
43  import org.kuali.rice.core.api.datetime.DateTimeService;
44  import org.kuali.rice.core.api.util.type.KualiInteger;
45  import org.kuali.rice.kim.api.identity.Person;
46  import org.kuali.rice.kns.util.KNSGlobalVariables;
47  import org.kuali.rice.kns.web.struts.action.KualiAction;
48  import org.kuali.rice.krad.exception.AuthorizationException;
49  import org.kuali.rice.krad.util.GlobalVariables;
50  import org.kuali.rice.krad.util.KRADConstants;
51  import org.kuali.rice.krad.util.UrlFactory;
52  
53  /**
54   * This class provides actions for the format process
55   */
56  public class FormatAction extends KualiAction {
57  
58      private FormatService formatService;
59  
60      /**
61       * Constructs a FormatAction.java.
62       */
63      public FormatAction() {
64          formatService = SpringContext.getBean(FormatService.class);
65      }
66  
67      /**
68       * @see org.kuali.rice.kns.web.struts.action.KualiAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
69       */
70      @Override
71      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
72          PdpAuthorizationService authorizationService = SpringContext.getBean(PdpAuthorizationService.class);
73          
74          Person kualiUser = GlobalVariables.getUserSession().getPerson();
75          String methodToCall = findMethodToCall(form, request);
76  
77          if (!authorizationService.hasFormatPermission(kualiUser.getPrincipalId())) {
78              throw new AuthorizationException(kualiUser.getPrincipalName(), methodToCall, kualiUser.getCampusCode());
79          }
80  
81          return super.execute(mapping, form, request, response);
82      }
83  
84      /**
85       * This method prepares the data for the format process
86       * 
87       * @param mapping
88       * @param form
89       * @param request
90       * @param response
91       * @return
92       * @throws Exception
93       */
94      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
95          FormatForm formatForm = (FormatForm) form;
96          
97          Person kualiUser = GlobalVariables.getUserSession().getPerson();
98          FormatSelection formatSelection = formatService.getDataForFormat(kualiUser);
99          DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
100 
101         formatForm.setCampus(kualiUser.getCampusCode());
102 
103         // no data for format because another format process is already running
104         if (formatSelection.getStartDate() != null) {
105             GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, PdpKeyConstants.Format.ERROR_PDP_FORMAT_PROCESS_ALREADY_RUNNING, dateTimeService.toDateTimeString(formatSelection.getStartDate()));
106         }
107         else {
108             List<CustomerProfile> customers = formatSelection.getCustomerList();
109 
110             for (CustomerProfile element : customers) {
111 
112                 if (formatSelection.getCampus().equals(element.getDefaultPhysicalCampusProcessingCode())) {
113                     element.setSelectedForFormat(Boolean.TRUE);
114                 }
115                 else {
116                     element.setSelectedForFormat(Boolean.FALSE);
117                 }
118             }
119 
120             formatForm.setPaymentDate(dateTimeService.toDateString(dateTimeService.getCurrentTimestamp()));
121             formatForm.setPaymentTypes(PdpConstants.PaymentTypes.ALL);
122             formatForm.setCustomers(customers);
123             formatForm.setRanges(formatSelection.getRangeList());
124         }
125         
126         return mapping.findForward(PdpConstants.MAPPING_SELECTION);
127     }
128 
129     /**
130      * This method marks the payments for format
131      * 
132      * @param mapping
133      * @param form
134      * @param request
135      * @param response
136      * @return
137      * @throws Exception
138      */
139     public ActionForward prepare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
140         FormatForm formatForm = (FormatForm) form;
141 
142         DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
143 
144         if (formatForm.getCampus() == null) {
145             return mapping.findForward(PdpConstants.MAPPING_SELECTION);
146         }
147 
148         // Figure out which ones they have selected
149         List selectedCustomers = new ArrayList();
150 
151         for (CustomerProfile customer : formatForm.getCustomers()) {
152             if (customer.isSelectedForFormat()) {
153                 selectedCustomers.add(customer);
154             }
155         }
156 
157         Date paymentDate = dateTimeService.convertToSqlDate(formatForm.getPaymentDate());
158         Person kualiUser = GlobalVariables.getUserSession().getPerson();
159 
160         FormatProcessSummary formatProcessSummary = formatService.startFormatProcess(kualiUser, formatForm.getCampus(), selectedCustomers, paymentDate, formatForm.getPaymentTypes());
161         if (formatProcessSummary.getProcessSummaryList().size() == 0) {
162             KNSGlobalVariables.getMessageList().add(PdpKeyConstants.Format.ERROR_PDP_NO_MATCHING_PAYMENT_FOR_FORMAT);
163             return mapping.findForward(PdpConstants.MAPPING_SELECTION);
164         }
165 
166         formatForm.setFormatProcessSummary(formatProcessSummary);
167 
168         return mapping.findForward(PdpConstants.MAPPING_CONTINUE);
169     }
170 
171     /**
172      * This method performs the format process.
173      * 
174      * @param mapping
175      * @param form
176      * @param request
177      * @param response
178      * @return
179      * @throws Exception
180      */
181     public ActionForward continueFormat(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
182         FormatForm formatForm = (FormatForm) form;
183         KualiInteger processId = formatForm.getFormatProcessSummary().getProcessId();
184 
185         try {
186             formatService.performFormat(processId.intValue());
187         }
188         catch (FormatException e) {
189             // errors added to global message map
190             return mapping.findForward(PdpConstants.MAPPING_CONTINUE);
191         }
192 
193         String lookupUrl = buildUrl(String.valueOf(processId.intValue()));
194         return new ActionForward(lookupUrl, true);
195     }
196 
197     /**
198      * This method clears all the customer checkboxes.
199      * 
200      * @param mapping
201      * @param form
202      * @param request
203      * @param response
204      * @return
205      * @throws Exception
206      */
207     public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
208         FormatForm formatForm = (FormatForm) form;
209 
210         List<CustomerProfile> customers = formatForm.getCustomers();
211         for (CustomerProfile customerProfile : customers) {
212             customerProfile.setSelectedForFormat(false);
213         }
214         formatForm.setCustomers(customers);
215         
216         return mapping.findForward(PdpConstants.MAPPING_SELECTION);
217 
218     }
219 
220     /**
221      * This method cancels the format process
222      * 
223      * @param mapping
224      * @param form
225      * @param request
226      * @param response
227      * @return
228      * @throws Exception
229      */
230     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
231         FormatForm formatForm = (FormatForm) form;
232         
233         KualiInteger processId = formatForm.getFormatProcessSummary().getProcessId();
234 
235         if (processId != null) {
236             formatService.clearUnfinishedFormat(processId.intValue());
237         }
238         return mapping.findForward(KRADConstants.MAPPING_PORTAL);
239 
240     }
241 
242     /**
243      * This method clears the unfinished format process and is called from the FormatProcess lookup page.
244      * 
245      * @param mapping
246      * @param form
247      * @param request
248      * @param response
249      * @return
250      * @throws Exception
251      */
252     public ActionForward clearUnfinishedFormat(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
253         
254         String processIdParam = request.getParameter(PdpParameterConstants.FormatProcess.PROCESS_ID_PARAM);
255         Integer processId = Integer.parseInt(processIdParam);
256 
257         if (processId != null) {
258             formatService.resetFormatPayments(processId);
259         }
260 
261         return mapping.findForward(KRADConstants.MAPPING_PORTAL);
262 
263     }
264 
265     /**
266      * This method builds the forward url for the format summary lookup page.
267      * 
268      * @param processId the batch id
269      * @return the built url
270      */
271     private String buildUrl(String processId) {
272         String basePath = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY);
273 
274         Properties parameters = new Properties();
275         parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.SEARCH_METHOD);
276         parameters.put(OLEConstants.BACK_LOCATION, basePath + "/" + OLEConstants.MAPPING_PORTAL + ".do");
277         parameters.put(KRADConstants.DOC_FORM_KEY, "88888888");
278         parameters.put(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, ProcessSummary.class.getName());
279         parameters.put(OLEConstants.HIDE_LOOKUP_RETURN_LINK, "true");
280         parameters.put(OLEConstants.SUPPRESS_ACTIONS, "false");
281         parameters.put(PdpPropertyConstants.ProcessSummary.PROCESS_SUMMARY_PROCESS_ID, processId);
282 
283         String lookupUrl = UrlFactory.parameterizeUrl(basePath + "/" + OLEConstants.LOOKUP_ACTION, parameters);
284 
285         return lookupUrl;
286     }
287 }