1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
55  
56  public class FormatAction extends KualiAction {
57  
58      private FormatService formatService;
59  
60      
61  
62  
63      public FormatAction() {
64          formatService = SpringContext.getBean(FormatService.class);
65      }
66  
67      
68  
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  
86  
87  
88  
89  
90  
91  
92  
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         
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 
131 
132 
133 
134 
135 
136 
137 
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         
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 
173 
174 
175 
176 
177 
178 
179 
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             
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 
199 
200 
201 
202 
203 
204 
205 
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 
222 
223 
224 
225 
226 
227 
228 
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 
244 
245 
246 
247 
248 
249 
250 
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 
267 
268 
269 
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 }