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.businessobject.lookup;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.lang.StringUtils;
028import org.kuali.kfs.module.ar.ArPropertyConstants;
029import org.kuali.kfs.module.ar.businessobject.CollectionActivityReport;
030import org.kuali.kfs.module.ar.report.service.CollectionActivityReportService;
031import org.kuali.kfs.sys.KFSConstants;
032import org.kuali.rice.core.web.format.Formatter;
033import org.kuali.rice.kim.api.identity.Person;
034import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
035import org.kuali.rice.kns.lookup.HtmlData;
036import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
037import org.kuali.rice.kns.web.comparator.CellComparatorHelper;
038import org.kuali.rice.kns.web.struts.form.LookupForm;
039import org.kuali.rice.kns.web.ui.Column;
040import org.kuali.rice.kns.web.ui.ResultRow;
041import org.kuali.rice.krad.bo.BusinessObject;
042import org.kuali.rice.krad.bo.PersistableBusinessObject;
043import org.kuali.rice.krad.lookup.CollectionIncomplete;
044import org.kuali.rice.krad.util.GlobalVariables;
045import org.kuali.rice.krad.util.KRADConstants;
046import org.kuali.rice.krad.util.ObjectUtils;
047
048/**
049 * LookupableHelperService class for Collection Activity Report.
050 */
051public class CollectionActivityReportLookupableHelperServiceImpl extends CollectionsReportLookupableHelperServiceImplBase {
052    protected CollectionActivityReportService collectionActivityReportService;
053
054    /**
055     * Get the search results that meet the input search criteria.
056     *
057     * @param fieldValues - Map containing prop name keys and search values
058     * @return a List of found business objects
059     */
060    @Override
061    public List getSearchResultsUnbounded(Map fieldValues) {
062        List<CollectionActivityReport> results = new ArrayList<CollectionActivityReport>();
063        setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
064        setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
065        results = collectionActivityReportService.filterEventsForCollectionActivity(fieldValues);
066        return new CollectionIncomplete<CollectionActivityReport>(results, (long) results.size());
067    }
068
069
070    /**
071     * This method performs the lookup and returns a collection of lookup items
072     *
073     * @param lookupForm
074     * @param kualiLookupable
075     * @param resultTable
076     * @param bounded
077     * @return
078     */
079    @Override
080    public Collection performLookup(LookupForm lookupForm, Collection resultTable, boolean bounded) {
081        Collection displayList = getSearchResultsUnbounded(lookupForm.getFieldsForLookup());
082
083        boolean hasReturnableRow = false;
084
085        Person user = GlobalVariables.getUserSession().getPerson();
086        List pkNames = getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(getBusinessObjectClass());
087
088        // iterate through result list and wrap rows with return url and action urls
089        for (Object aDisplayList : displayList) {
090            BusinessObject element = (BusinessObject) aDisplayList;
091
092            BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
093
094            if (ObjectUtils.isNotNull(getColumns())) {
095                List<Column> columns = getColumns();
096                for (Object column : columns) {
097
098                    Column col = (Column) column;
099                    Formatter formatter = col.getFormatter();
100
101                    // pick off result column from result list, do formatting
102                    Object prop = ObjectUtils.getPropertyValue(element, col.getPropertyName());
103
104                    String propValue = ObjectUtils.getFormattedPropertyValue(element, col.getPropertyName(), col.getFormatter());
105                    Class propClass = getPropertyClass(element, col.getPropertyName());
106
107                    // formatters
108                    if (ObjectUtils.isNotNull(prop)) {
109                        propValue = getContractsGrantsReportHelperService().formatByType(prop, formatter);
110                    }
111
112                    // comparator
113                    col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass));
114                    col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass));
115
116                    propValue = super.maskValueIfNecessary(element.getClass(), col.getPropertyName(), propValue, businessObjectRestrictions);
117                    col.setPropertyValue(propValue);
118
119                    // Add url when property is invoiceNumber
120                    if (col.getPropertyName().equals(ArPropertyConstants.INVOICE_NUMBER)) {
121                        String url = contractsGrantsReportHelperService.getDocSearchUrl(propValue);
122
123                        Map<String, String> fieldList = new HashMap<String, String>();
124                        fieldList.put(ArPropertyConstants.INVOICE_NUMBER, propValue);
125                        AnchorHtmlData a = new AnchorHtmlData(url, KRADConstants.EMPTY_STRING);
126                        a.setTitle(HtmlData.getTitleText(getContractsGrantsReportHelperService().createTitleText(getBusinessObjectClass()), getBusinessObjectClass(), fieldList));
127
128                        col.setColumnAnchor(a);
129                    } else if (StringUtils.isNotBlank(propValue)) {
130                        col.setColumnAnchor(getInquiryUrl(element, col.getPropertyName()));
131                    }
132                }
133
134                ResultRow row = new ResultRow(columns, KFSConstants.EMPTY_STRING, getActionUrls(element, pkNames, businessObjectRestrictions));
135                if (element instanceof PersistableBusinessObject) {
136                    row.setObjectId(((PersistableBusinessObject) element).getObjectId());
137                }
138
139                boolean rowReturnable = isResultReturnable(element);
140                row.setRowReturnable(rowReturnable);
141                if (rowReturnable) {
142                    hasReturnableRow = true;
143                }
144
145                resultTable.add(row);
146            }
147
148            lookupForm.setHasReturnableRow(hasReturnableRow);
149        }
150
151        return displayList;
152    }
153
154    public CollectionActivityReportService getCollectionActivityReportService() {
155        return collectionActivityReportService;
156    }
157
158    public void setCollectionActivityReportService(CollectionActivityReportService collectionActivityReportService) {
159        this.collectionActivityReportService = collectionActivityReportService;
160    }
161
162}