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.module.ar.web.struts;
017    
018    import java.io.FileInputStream;
019    import java.io.IOException;
020    import java.io.ObjectInputStream;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.HashMap;
024    import java.util.Iterator;
025    import java.util.List;
026    import java.util.Map;
027    
028    import javax.servlet.ServletException;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    import org.kuali.kfs.module.ar.businessobject.lookup.CustomerAgingReportLookupableHelperServiceImpl;
036    import org.kuali.kfs.sys.KFSConstants;
037    import org.kuali.kfs.sys.KFSKeyConstants;
038    import org.kuali.kfs.sys.KFSPropertyConstants;
039    import org.kuali.rice.kns.lookup.CollectionIncomplete;
040    import org.kuali.rice.kns.lookup.Lookupable;
041    import org.kuali.rice.kns.lookup.LookupableHelperService;
042    import org.kuali.rice.kns.util.GlobalVariables;
043    import org.kuali.rice.kns.web.struts.action.KualiAction;
044    import org.kuali.rice.kns.web.struts.form.LookupForm;
045    import org.kuali.rice.kns.web.ui.Field;
046    import org.kuali.rice.kns.web.ui.ResultRow;
047    import org.kuali.rice.kns.web.ui.Row;
048    
049    
050    /**
051     * This class handles Actions for lookup flow
052     */
053    
054    public class CustomerAgingReportAction extends KualiAction {
055        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerAgingReportAction.class);
056    
057        private static final String TOTALS_TABLE_KEY = "totalsTable";
058    
059    
060        public CustomerAgingReportAction() {
061            super();
062        }
063    
064    
065    
066        public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
067            return mapping.findForward(KFSConstants.MAPPING_BASIC);
068        }
069    
070        /**
071         * Search - sets the values of the data entered on the form on the jsp into a map and then searches for the results.
072         *
073         * @param mapping
074         * @param form
075         * @param request
076         * @param response
077         * @return
078         * @throws Exception
079         */
080        public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
081            CustomerAgingReportForm lookupForm = (CustomerAgingReportForm) form;
082    
083            Lookupable lookupable = lookupForm.getLookupable();    
084    
085            if (lookupable == null) {
086                LOG.error("Lookupable is null.");
087                throw new RuntimeException("Lookupable is null.");
088            }
089            
090            LookupableHelperService lookupablehelper =  lookupable.getLookupableHelperService();
091            Collection displayList = new ArrayList();
092            List<ResultRow> resultTable = new ArrayList<ResultRow>();
093    
094            try {
095                displayList = lookupable.performLookup(lookupForm, resultTable, true);
096    
097                Object[] resultTableAsArray = resultTable.toArray();
098    
099    
100                CollectionIncomplete incompleteDisplayList = (CollectionIncomplete) displayList;
101                Long totalSize = ((CollectionIncomplete) displayList).getActualSizeIfTruncated();
102    
103                request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS_SIZE, totalSize);
104    
105                request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS, resultTable);
106               
107                if (request.getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY) != null) {
108                    GlobalVariables.getUserSession().removeObject(request.getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY));
109                }
110    
111                request.setAttribute(KFSConstants.SEARCH_LIST_REQUEST_KEY, GlobalVariables.getUserSession().addObject(resultTable));
112    
113            }
114            catch (NumberFormatException e) {
115                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, KFSKeyConstants.ERROR_CUSTOM, new String[] { "Fiscal Year must be a four-digit number" });
116            }
117            catch (Exception e) {
118                GlobalVariables.getMessageMap().putError(KFSConstants.DOCUMENT_ERRORS, KFSKeyConstants.ERROR_CUSTOM, new String[] { "Please report the server error." });
119                LOG.error("Application Errors", e);
120            }
121            return mapping.findForward(KFSConstants.MAPPING_BASIC);
122        }
123    
124        /**
125         * Refresh - is called when one quickFinder returns to the previous one. Sets all the values and performs the new search.
126         * 
127         * @see org.kuali.rice.kns.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
128         */
129        @Override
130        public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
131            LookupForm lookupForm = (LookupForm) form;
132            Lookupable lookupable = lookupForm.getLookupable();
133            if (lookupable == null) {
134                LOG.error("Lookupable is null.");
135                throw new RuntimeException("Lookupable is null.");
136            }
137    
138            Map fieldValues = new HashMap();
139            Map values = lookupForm.getFields();
140    
141            for (Iterator iter = lookupable.getRows().iterator(); iter.hasNext();) {
142                Row row = (Row) iter.next();
143    
144                for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
145                    Field field = (Field) iterator.next();
146    
147                    if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
148                        if (request.getParameter(field.getPropertyName()) != null) {
149                            field.setPropertyValue(request.getParameter(field.getPropertyName()));
150                        }
151                        else if (values.get(field.getPropertyName()) != null) {
152                            field.setPropertyValue(values.get(field.getPropertyName()));
153                        }
154                    }
155                    fieldValues.put(field.getPropertyName(), field.getPropertyValue());
156                }
157            }
158            fieldValues.put(KFSConstants.DOC_FORM_KEY, lookupForm.getFormKey());
159            fieldValues.put(KFSConstants.BACK_LOCATION, lookupForm.getBackLocation());
160    
161            if (lookupable.checkForAdditionalFields(fieldValues)) {
162                for (Iterator iter = lookupable.getRows().iterator(); iter.hasNext();) {
163                    Row row = (Row) iter.next();
164                    for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
165                        Field field = (Field) iterator.next();
166                        if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
167                            if (request.getParameter(field.getPropertyName()) != null) {
168                                field.setPropertyValue(request.getParameter(field.getPropertyName()));
169                                fieldValues.put(field.getPropertyName(), request.getParameter(field.getPropertyName()));
170                            }
171                            else if (values.get(field.getPropertyName()) != null) {
172                                field.setPropertyValue(values.get(field.getPropertyName()));
173                            }
174                        }
175                    }
176                }
177            }
178    
179            return mapping.findForward(KFSConstants.MAPPING_BASIC);
180        }
181    
182        /**
183         * Returns as if return with no value was selected.
184         * 
185         * @param mapping
186         * @param form
187         * @param request
188         * @param response
189         * @return
190         * @throws Exception
191         */
192        public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
193            CustomerAgingReportForm lookupForm = (CustomerAgingReportForm) form;
194    
195            //String backUrl = lookupForm.getBackLocation() + "?methodToCall=refresh&docFormKey=" + lookupForm.getFormKey();
196            String backUrl = getBasePath(request)+"/portal.do?selectedTab=maintenance";
197            return new ActionForward(backUrl, true);
198        }
199    
200    
201        /**
202         * Clears the values of all the fields on the jsp.
203         * 
204         * @param mapping
205         * @param form
206         * @param request
207         * @param response
208         * @return
209         * @throws IOException
210         * @throws ServletException
211         */
212        public ActionForward clearValues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
213            LookupForm lookupForm = (LookupForm) form;
214            Lookupable lookupable = lookupForm.getLookupable();
215            if (lookupable == null) {
216                LOG.error("Lookupable is null.");
217                throw new RuntimeException("Lookupable is null.");
218            }
219    
220            for (Iterator iter = lookupable.getRows().iterator(); iter.hasNext();) {
221                Row row = (Row) iter.next();
222                for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
223                    Field field = (Field) iterator.next();
224                    if (!field.getFieldType().equals(Field.RADIO)) {
225                        field.setPropertyValue(field.getDefaultValue());
226                    }
227                }
228            }
229    
230            return mapping.findForward(KFSConstants.MAPPING_BASIC);
231        }
232    
233        /**
234         * View results from balance inquiry action
235         * 
236         * @param mapping
237         * @param form
238         * @param request
239         * @param response
240         * @return
241         * @throws Exception
242         */
243        public ActionForward viewResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
244            request.setAttribute(KFSConstants.SEARCH_LIST_REQUEST_KEY, request.getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY));
245            request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS, GlobalVariables.getUserSession().retrieveObject(request.getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY)));
246            request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS_SIZE, request.getParameter(KFSConstants.REQUEST_SEARCH_RESULTS_SIZE));
247    
248            // TODO: use inheritance instead of this if statement
249            if (((CustomerAgingReportForm) form).getLookupable().getLookupableHelperService() instanceof CustomerAgingReportLookupableHelperServiceImpl) {
250                Object totalsTable = GlobalVariables.getUserSession().retrieveObject(TOTALS_TABLE_KEY);
251                request.setAttribute(TOTALS_TABLE_KEY, totalsTable);
252            }
253    
254            return mapping.findForward(KFSConstants.MAPPING_BASIC);
255        }
256    
257    
258    }