001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.kfs.module.ar.web.struts;
020
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Properties;
025import java.util.Set;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029
030import org.apache.commons.lang.StringUtils;
031import org.apache.struts.action.ActionForm;
032import org.apache.struts.action.ActionForward;
033import org.apache.struts.action.ActionMapping;
034import org.kuali.kfs.module.ar.ArConstants;
035import org.kuali.kfs.module.ar.web.ui.ContractsGrantsLookupResultRow;
036import org.kuali.kfs.sys.context.SpringContext;
037import org.kuali.rice.core.api.util.RiceConstants;
038import org.kuali.rice.kns.lookup.LookupResultsService;
039import org.kuali.rice.kns.lookup.LookupUtils;
040import org.kuali.rice.kns.web.struts.action.KualiMultipleValueLookupAction;
041import org.kuali.rice.kns.web.struts.form.MultipleValueLookupForm;
042import org.kuali.rice.kns.web.ui.ResultRow;
043import org.kuali.rice.krad.util.GlobalVariables;
044import org.kuali.rice.krad.util.KRADConstants;
045import org.kuali.rice.krad.util.UrlFactory;
046
047public class CustomerInvoiceWriteoffLookupAction extends KualiMultipleValueLookupAction {
048
049    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerInvoiceWriteoffLookupAction.class);
050
051    /**
052     * This method performs the operations necessary for a multiple value lookup to select all of the results and rerender the page
053     *
054     * @param multipleValueLookupForm
055     * @param maxRowsPerPage
056     * @return a list of result rows, used by the UI to render the page
057     */
058    @Override
059    protected List<ResultRow> selectAll(MultipleValueLookupForm multipleValueLookupForm, int maxRowsPerPage) {
060        String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
061
062        List<ResultRow> resultTable = null;
063        try {
064            LookupResultsService lookupResultsService = SpringContext.getBean(LookupResultsService.class);
065            resultTable = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
066        }
067        catch (Exception e) {
068            LOG.error("error occured trying to export multiple lookup results", e);
069            throw new RuntimeException("error occured trying to export multiple lookup results");
070        }
071
072        Map<String, String> selectedObjectIds = new HashMap<String, String>();
073
074        for (ResultRow row : resultTable) {
075
076            // actual object ids are on sub result rows, not on parent rows
077            if (row instanceof ContractsGrantsLookupResultRow) {
078                for (ResultRow subResultRow : ((ContractsGrantsLookupResultRow) row).getSubResultRows()) {
079                    String objId = subResultRow.getObjectId();
080                    selectedObjectIds.put(objId, objId);
081                }
082            }
083            else {
084                String objId = row.getObjectId();
085                selectedObjectIds.put(objId, objId);
086            }
087        }
088
089        multipleValueLookupForm.jumpToPage(multipleValueLookupForm.getViewedPageNumber(), resultTable.size(), maxRowsPerPage);
090        multipleValueLookupForm.setColumnToSortIndex(Integer.parseInt(multipleValueLookupForm.getPreviouslySortedColumnIndex()));
091        multipleValueLookupForm.setCompositeObjectIdMap(selectedObjectIds);
092
093        return resultTable;
094    }
095
096    /**
097     * This method does the processing necessary to return selected results and sends a redirect back to the lookup caller
098     *
099     * @param mapping
100     * @param form must be an instance of MultipleValueLookupForm
101     * @param request
102     * @param response
103     * @return
104     * @throws Exception
105     */
106    @Override
107    public ActionForward prepareToReturnSelectedResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
108        MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
109        if (StringUtils.isBlank(multipleValueLookupForm.getLookupResultsSequenceNumber())) {
110            // no search was executed
111            return prepareToReturnNone(mapping, form, request, response);
112        }
113
114        Map<String, String> compositeObjectIdMap = LookupUtils.generateCompositeSelectedObjectIds(multipleValueLookupForm.getPreviouslySelectedObjectIdSet(), multipleValueLookupForm.getDisplayedObjectIdSet(), multipleValueLookupForm.getSelectedObjectIdSet());
115        Set<String> compositeObjectIds = compositeObjectIdMap.keySet();
116
117        //TODO need to validate the results
118        boolean success = true;
119        if (success) {
120
121            prepareToReturnSelectedResultBOs(multipleValueLookupForm);
122
123            // build the parameters for the refresh url
124            Properties parameters = new Properties();
125            parameters.put(KRADConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, multipleValueLookupForm.getLookupResultsSequenceNumber());
126            parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, ArConstants.CUSTOMER_INVOICE_WRITEOFF_SUMMARY_ACTION);
127
128            String customerInvoiceWriteoffLookupSummaryUrl = UrlFactory.parameterizeUrl("arCustomerInvoiceWriteoffLookupSummary.do", parameters);
129            return new ActionForward(customerInvoiceWriteoffLookupSummaryUrl, true);
130
131        }
132        else {
133            return mapping.findForward(RiceConstants.MAPPING_BASIC);
134        }
135    }
136}
137