001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.pdp.web.struts;
017    
018    import java.util.ArrayList;
019    import java.util.Date;
020    import java.util.List;
021    import java.util.Properties;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import org.apache.struts.action.ActionForm;
027    import org.apache.struts.action.ActionForward;
028    import org.apache.struts.action.ActionMapping;
029    import org.kuali.kfs.pdp.PdpConstants;
030    import org.kuali.kfs.pdp.PdpKeyConstants;
031    import org.kuali.kfs.pdp.PdpParameterConstants;
032    import org.kuali.kfs.pdp.PdpPropertyConstants;
033    import org.kuali.kfs.pdp.businessobject.CustomerProfile;
034    import org.kuali.kfs.pdp.businessobject.FormatProcessSummary;
035    import org.kuali.kfs.pdp.businessobject.FormatSelection;
036    import org.kuali.kfs.pdp.businessobject.ProcessSummary;
037    import org.kuali.kfs.pdp.service.FormatService;
038    import org.kuali.kfs.pdp.service.PdpAuthorizationService;
039    import org.kuali.kfs.pdp.service.impl.exception.FormatException;
040    import org.kuali.kfs.sys.KFSConstants;
041    import org.kuali.kfs.sys.context.SpringContext;
042    import org.kuali.rice.kim.bo.Person;
043    import org.kuali.rice.kns.exception.AuthorizationException;
044    import org.kuali.rice.kns.service.DateTimeService;
045    import org.kuali.rice.kns.service.KualiConfigurationService;
046    import org.kuali.rice.kns.util.GlobalVariables;
047    import org.kuali.rice.kns.util.KNSConstants;
048    import org.kuali.rice.kns.util.KualiInteger;
049    import org.kuali.rice.kns.util.UrlFactory;
050    import org.kuali.rice.kns.web.struts.action.KualiAction;
051    
052    /**
053     * This class provides actions for the format process
054     */
055    public class FormatAction extends KualiAction {
056    
057        private FormatService formatService;
058    
059        /**
060         * Constructs a FormatAction.java.
061         */
062        public FormatAction() {
063            formatService = SpringContext.getBean(FormatService.class);
064        }
065    
066        /**
067         * @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)
068         */
069        @Override
070        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
071            PdpAuthorizationService authorizationService = SpringContext.getBean(PdpAuthorizationService.class);
072            
073            Person kualiUser = GlobalVariables.getUserSession().getPerson();
074            String methodToCall = findMethodToCall(form, request);
075    
076            if (!authorizationService.hasFormatPermission(kualiUser.getPrincipalId())) {
077                throw new AuthorizationException(kualiUser.getPrincipalName(), methodToCall, kualiUser.getCampusCode());
078            }
079    
080            return super.execute(mapping, form, request, response);
081        }
082    
083        /**
084         * This method prepares the data for the format process
085         * 
086         * @param mapping
087         * @param form
088         * @param request
089         * @param response
090         * @return
091         * @throws Exception
092         */
093        public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
094            FormatForm formatForm = (FormatForm) form;
095            
096            Person kualiUser = GlobalVariables.getUserSession().getPerson();
097            FormatSelection formatSelection = formatService.getDataForFormat(kualiUser);
098            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
099    
100            formatForm.setCampus(kualiUser.getCampusCode());
101    
102            // no data for format because another format process is already running
103            if (formatSelection.getStartDate() != null) {
104                GlobalVariables.getMessageMap().putError(KFSConstants.GLOBAL_ERRORS, PdpKeyConstants.Format.ERROR_PDP_FORMAT_PROCESS_ALREADY_RUNNING, dateTimeService.toDateTimeString(formatSelection.getStartDate()));
105            }
106            else {
107                List<CustomerProfile> customers = formatSelection.getCustomerList();
108    
109                for (CustomerProfile element : customers) {
110    
111                    if (formatSelection.getCampus().equals(element.getDefaultPhysicalCampusProcessingCode())) {
112                        element.setSelectedForFormat(Boolean.TRUE);
113                    }
114                    else {
115                        element.setSelectedForFormat(Boolean.FALSE);
116                    }
117                }
118    
119                formatForm.setPaymentDate(dateTimeService.toDateString(dateTimeService.getCurrentTimestamp()));
120                formatForm.setPaymentTypes(PdpConstants.PaymentTypes.ALL);
121                formatForm.setCustomers(customers);
122                formatForm.setRanges(formatSelection.getRangeList());
123            }
124            
125            return mapping.findForward(PdpConstants.MAPPING_SELECTION);
126        }
127    
128        /**
129         * This method marks the payments for format
130         * 
131         * @param mapping
132         * @param form
133         * @param request
134         * @param response
135         * @return
136         * @throws Exception
137         */
138        public ActionForward prepare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
139            FormatForm formatForm = (FormatForm) form;
140    
141            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
142    
143            if (formatForm.getCampus() == null) {
144                return mapping.findForward(PdpConstants.MAPPING_SELECTION);
145            }
146    
147            // Figure out which ones they have selected
148            List selectedCustomers = new ArrayList();
149    
150            for (CustomerProfile customer : formatForm.getCustomers()) {
151                if (customer.isSelectedForFormat()) {
152                    selectedCustomers.add(customer);
153                }
154            }
155    
156            Date paymentDate = dateTimeService.convertToSqlDate(formatForm.getPaymentDate());
157            Person kualiUser = GlobalVariables.getUserSession().getPerson();
158    
159            FormatProcessSummary formatProcessSummary = formatService.startFormatProcess(kualiUser, formatForm.getCampus(), selectedCustomers, paymentDate, formatForm.getPaymentTypes());
160            if (formatProcessSummary.getProcessSummaryList().size() == 0) {
161                GlobalVariables.getMessageList().add(PdpKeyConstants.Format.ERROR_PDP_NO_MATCHING_PAYMENT_FOR_FORMAT);
162                return mapping.findForward(PdpConstants.MAPPING_SELECTION);
163            }
164    
165            formatForm.setFormatProcessSummary(formatProcessSummary);
166    
167            return mapping.findForward(PdpConstants.MAPPING_CONTINUE);
168        }
169    
170        /**
171         * This method performs the format process.
172         * 
173         * @param mapping
174         * @param form
175         * @param request
176         * @param response
177         * @return
178         * @throws Exception
179         */
180        public ActionForward continueFormat(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
181            FormatForm formatForm = (FormatForm) form;
182            KualiInteger processId = formatForm.getFormatProcessSummary().getProcessId();
183    
184            try {
185                formatService.performFormat(processId.intValue());
186            }
187            catch (FormatException e) {
188                // errors added to global message map
189                return mapping.findForward(PdpConstants.MAPPING_CONTINUE);
190            }
191    
192            String lookupUrl = buildUrl(String.valueOf(processId.intValue()));
193            return new ActionForward(lookupUrl, true);
194        }
195    
196        /**
197         * This method clears all the customer checkboxes.
198         * 
199         * @param mapping
200         * @param form
201         * @param request
202         * @param response
203         * @return
204         * @throws Exception
205         */
206        public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
207            FormatForm formatForm = (FormatForm) form;
208    
209            List<CustomerProfile> customers = formatForm.getCustomers();
210            for (CustomerProfile customerProfile : customers) {
211                customerProfile.setSelectedForFormat(false);
212            }
213            formatForm.setCustomers(customers);
214            
215            return mapping.findForward(PdpConstants.MAPPING_SELECTION);
216    
217        }
218    
219        /**
220         * This method cancels the format process
221         * 
222         * @param mapping
223         * @param form
224         * @param request
225         * @param response
226         * @return
227         * @throws Exception
228         */
229        public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
230            FormatForm formatForm = (FormatForm) form;
231            
232            KualiInteger processId = formatForm.getFormatProcessSummary().getProcessId();
233    
234            if (processId != null) {
235                formatService.clearUnfinishedFormat(processId.intValue());
236            }
237            return mapping.findForward(KNSConstants.MAPPING_PORTAL);
238    
239        }
240    
241        /**
242         * This method clears the unfinished format process and is called from the FormatProcess lookup page.
243         * 
244         * @param mapping
245         * @param form
246         * @param request
247         * @param response
248         * @return
249         * @throws Exception
250         */
251        public ActionForward clearUnfinishedFormat(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
252            
253            String processIdParam = request.getParameter(PdpParameterConstants.FormatProcess.PROCESS_ID_PARAM);
254            Integer processId = Integer.parseInt(processIdParam);
255    
256            if (processId != null) {
257                formatService.resetFormatPayments(processId);
258            }
259    
260            return mapping.findForward(KNSConstants.MAPPING_PORTAL);
261    
262        }
263    
264        /**
265         * This method builds the forward url for the format summary lookup page.
266         * 
267         * @param processId the batch id
268         * @return the built url
269         */
270        private String buildUrl(String processId) {
271            String basePath = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSConstants.APPLICATION_URL_KEY);
272    
273            Properties parameters = new Properties();
274            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.SEARCH_METHOD);
275            parameters.put(KFSConstants.BACK_LOCATION, basePath + "/" + KFSConstants.MAPPING_PORTAL + ".do");
276            parameters.put(KNSConstants.DOC_FORM_KEY, "88888888");
277            parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, ProcessSummary.class.getName());
278            parameters.put(KFSConstants.HIDE_LOOKUP_RETURN_LINK, "true");
279            parameters.put(KFSConstants.SUPPRESS_ACTIONS, "false");
280            parameters.put(PdpPropertyConstants.ProcessSummary.PROCESS_SUMMARY_PROCESS_ID, processId);
281    
282            String lookupUrl = UrlFactory.parameterizeUrl(basePath + "/" + KFSConstants.LOOKUP_ACTION, parameters);
283    
284            return lookupUrl;
285        }
286    }