/*
* The Kuali Financial System, a comprehensive financial management system for higher education.
*
* Copyright 2005-2014 The Kuali Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kuali.kfs.module.ar.web.struts;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.kuali.kfs.module.ar.ArConstants;
import org.kuali.kfs.module.ar.web.ui.ContractsGrantsLookupResultRow;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.rice.core.api.util.RiceConstants;
import org.kuali.rice.kns.lookup.LookupResultsService;
import org.kuali.rice.kns.lookup.LookupUtils;
import org.kuali.rice.kns.web.struts.action.KualiMultipleValueLookupAction;
import org.kuali.rice.kns.web.struts.form.MultipleValueLookupForm;
import org.kuali.rice.kns.web.ui.ResultRow;
import org.kuali.rice.krad.util.GlobalVariables;
import org.kuali.rice.krad.util.KRADConstants;
import org.kuali.rice.krad.util.UrlFactory;
public class CustomerInvoiceWriteoffLookupAction extends KualiMultipleValueLookupAction {
private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerInvoiceWriteoffLookupAction.class);
/**
* This method performs the operations necessary for a multiple value lookup to select all of the results and rerender the page
*
* @param multipleValueLookupForm
* @param maxRowsPerPage
* @return a list of result rows, used by the UI to render the page
*/
@Override
protected List<ResultRow> selectAll(MultipleValueLookupForm multipleValueLookupForm, int maxRowsPerPage) {
String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
List<ResultRow> resultTable = null;
try {
LookupResultsService lookupResultsService = SpringContext.getBean(LookupResultsService.class);
resultTable = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
}
catch (Exception e) {
LOG.error("error occured trying to export multiple lookup results", e);
throw new RuntimeException("error occured trying to export multiple lookup results");
}
Map<String, String> selectedObjectIds = new HashMap<String, String>();
for (ResultRow row : resultTable) {
// actual object ids are on sub result rows, not on parent rows
if (row instanceof ContractsGrantsLookupResultRow) {
for (ResultRow subResultRow : ((ContractsGrantsLookupResultRow) row).getSubResultRows()) {
String objId = subResultRow.getObjectId();
selectedObjectIds.put(objId, objId);
}
}
else {
String objId = row.getObjectId();
selectedObjectIds.put(objId, objId);
}
}
multipleValueLookupForm.jumpToPage(multipleValueLookupForm.getViewedPageNumber(), resultTable.size(), maxRowsPerPage);
multipleValueLookupForm.setColumnToSortIndex(Integer.parseInt(multipleValueLookupForm.getPreviouslySortedColumnIndex()));
multipleValueLookupForm.setCompositeObjectIdMap(selectedObjectIds);
return resultTable;
}
/**
* This method does the processing necessary to return selected results and sends a redirect back to the lookup caller
*
* @param mapping
* @param form must be an instance of MultipleValueLookupForm
* @param request
* @param response
* @return
* @throws Exception
*/
@Override
public ActionForward prepareToReturnSelectedResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
if (StringUtils.isBlank(multipleValueLookupForm.getLookupResultsSequenceNumber())) {
// no search was executed
return prepareToReturnNone(mapping, form, request, response);
}
Map<String, String> compositeObjectIdMap = LookupUtils.generateCompositeSelectedObjectIds(multipleValueLookupForm.getPreviouslySelectedObjectIdSet(), multipleValueLookupForm.getDisplayedObjectIdSet(), multipleValueLookupForm.getSelectedObjectIdSet());
Set<String> compositeObjectIds = compositeObjectIdMap.keySet();
//TODO need to validate the results
boolean success = true;
if (success) {
prepareToReturnSelectedResultBOs(multipleValueLookupForm);
// build the parameters for the refresh url
Properties parameters = new Properties();
parameters.put(KRADConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, multipleValueLookupForm.getLookupResultsSequenceNumber());
parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, ArConstants.CUSTOMER_INVOICE_WRITEOFF_SUMMARY_ACTION);
String customerInvoiceWriteoffLookupSummaryUrl = UrlFactory.parameterizeUrl("arCustomerInvoiceWriteoffLookupSummary.do", parameters);
return new ActionForward(customerInvoiceWriteoffLookupSummaryUrl, true);
}
else {
return mapping.findForward(RiceConstants.MAPPING_BASIC);
}
}
}