View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    *
4    * Copyright 2005-2014 The Kuali Foundation
5    *
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   *
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ar.businessobject.lookup;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.commons.collections.CollectionUtils;
28  import org.kuali.kfs.module.ar.businessobject.CollectionActivityInvoiceLookup;
29  import org.kuali.kfs.module.ar.document.ContractsGrantsInvoiceDocument;
30  import org.kuali.kfs.module.ar.document.service.ContractsGrantsCollectionActivityDocumentService;
31  import org.kuali.kfs.module.ar.report.service.ContractsGrantsReportHelperService;
32  import org.kuali.kfs.sys.KFSConstants;
33  import org.kuali.kfs.sys.KFSPropertyConstants;
34  import org.kuali.rice.core.web.format.Formatter;
35  import org.kuali.rice.kim.api.identity.Person;
36  import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
37  import org.kuali.rice.kns.lookup.HtmlData;
38  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
39  import org.kuali.rice.kns.web.comparator.CellComparatorHelper;
40  import org.kuali.rice.kns.web.struts.form.LookupForm;
41  import org.kuali.rice.kns.web.ui.Column;
42  import org.kuali.rice.kns.web.ui.ResultRow;
43  import org.kuali.rice.krad.bo.BusinessObject;
44  import org.kuali.rice.krad.lookup.CollectionIncomplete;
45  import org.kuali.rice.krad.util.GlobalVariables;
46  import org.kuali.rice.krad.util.ObjectUtils;
47  
48  /**
49   * Defines a lookupable helper service class for the Collection Activity Report.
50   */
51  public class CollectionActivityInvoiceLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
52      private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CollectionActivityInvoiceLookupableHelperServiceImpl.class);
53      protected ContractsGrantsCollectionActivityDocumentService contractsGrantsCollectionActivityDocumentService;
54      //protected ContractsGrantsInvoiceDocumentService contractsGrantsInvoiceDocumentService;
55      protected ContractsGrantsReportHelperService contractsGrantsReportHelperService;
56  
57      /**
58       * Get the search results that meet the input search criteria.
59       *
60       * @param fieldValues - Map containing prop name keys and search values
61       * @return a List of found business objects
62       */
63      @Override
64      public List getSearchResults(Map fieldValues) {
65          List<CollectionActivityInvoiceLookup> results = new ArrayList<CollectionActivityInvoiceLookup>();
66          setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
67          setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
68          Long proposalNumber = new Long((String) fieldValues.get(KFSPropertyConstants.PROPOSAL_NUMBER));
69          Collection<ContractsGrantsInvoiceDocument> cgInvoices = getContractsGrantsCollectionActivityDocumentService().retrieveCollectionActivityEligibleContractsGrantsInvoicesByProposalNumber(proposalNumber);
70  
71          for (ContractsGrantsInvoiceDocument invoiceDocument : cgInvoices) {
72              results.add(convert(invoiceDocument));
73          }
74  
75          return new CollectionIncomplete<CollectionActivityInvoiceLookup>(results, (long) results.size());
76      }
77  
78      protected CollectionActivityInvoiceLookup convert(ContractsGrantsInvoiceDocument contractsGrantsInvoiceDocument) {
79          CollectionActivityInvoiceLookup cl = new CollectionActivityInvoiceLookup();
80          cl.setProposalNumber(contractsGrantsInvoiceDocument.getInvoiceGeneralDetail().getProposalNumber());
81          cl.setInvoiceNumber(contractsGrantsInvoiceDocument.getDocumentNumber());
82  
83          if (CollectionUtils.isNotEmpty(contractsGrantsInvoiceDocument.getAccountDetails())) {
84              cl.setAccountNumber(contractsGrantsInvoiceDocument.getAccountDetails().get(0).getAccountNumber());
85          }
86  
87          cl.setInvoiceDate(contractsGrantsInvoiceDocument.getBillingDate());
88          cl.setInvoiceAmount(contractsGrantsInvoiceDocument.getSourceTotal());
89          cl.setBillingPeriod(contractsGrantsInvoiceDocument.getInvoiceGeneralDetail().getBillingPeriod());
90          if (ObjectUtils.isNotNull(contractsGrantsInvoiceDocument.getInvoiceGeneralDetail())) {
91              cl.setBillingFrequency(contractsGrantsInvoiceDocument.getInvoiceGeneralDetail().getBillingFrequencyCode());
92          }
93          cl.setPaymentAmount(getContractsGrantsCollectionActivityDocumentService().retrievePaymentAmountByDocumentNumber(contractsGrantsInvoiceDocument.getDocumentNumber()));
94          cl.setBalanceDue(cl.getInvoiceAmount().subtract(cl.getPaymentAmount()));
95          cl.setAge(contractsGrantsInvoiceDocument.getAge());
96          return cl;
97      }
98  
99      /**
100      * This method performs the lookup and returns a collection of lookup items
101      *
102      * @param lookupForm
103      * @param kualiLookupable
104      * @param resultTable
105      * @param bounded
106      * @return
107      */
108     @Override
109     public Collection performLookup(LookupForm lookupForm, Collection resultTable, boolean bounded) {
110         if (LOG.isDebugEnabled()) {
111             for (Object key : lookupForm.getFieldsForLookup().keySet()) {
112                 LOG.debug("Key : " + key.toString() + " Value : " + lookupForm.getFieldsForLookup().get(key));
113             }
114         }
115 
116         Collection displayList = getSearchResults(lookupForm.getFieldsForLookup());
117 
118         // MJM get resultTable populated here
119         HashMap<String, Class> propertyTypes = new HashMap<String, Class>();
120 
121         boolean hasReturnableRow = false;
122 
123         List pkNames = getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(getBusinessObjectClass());
124         List returnKeys = getReturnKeys();
125 
126         Person user = GlobalVariables.getUserSession().getPerson();
127 
128         // iterate through result list and wrap rows with return url and action urls
129         for (Object aDisplayList : displayList) {
130             BusinessObject element = (BusinessObject) aDisplayList;
131 
132             BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
133 
134             if (ObjectUtils.isNotNull(getColumns())) {
135                 List<Column> columns = getColumns();
136                 for (Object column : columns) {
137 
138                     Column col = (Column) column;
139                     Formatter formatter = col.getFormatter();
140 
141                     // pick off result column from result list, do formatting
142                     Object prop = ObjectUtils.getPropertyValue(element, col.getPropertyName());
143 
144                     String propValue = ObjectUtils.getFormattedPropertyValue(element, col.getPropertyName(), col.getFormatter());
145                     Class propClass = getPropertyClass(element, col.getPropertyName());
146 
147                     // formatters
148                     if (ObjectUtils.isNotNull(prop)) {
149                         propValue = getContractsGrantsReportHelperService().formatByType(prop, formatter);
150                     }
151 
152                     // comparator
153                     col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass));
154                     col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass));
155 
156                     propValue = super.maskValueIfNecessary(element.getClass(), col.getPropertyName(), propValue, businessObjectRestrictions);
157                     col.setPropertyValue(propValue);
158 
159                 }
160                 lookupForm.setLookupObjectId(((CollectionActivityInvoiceLookup) element).getInvoiceNumber());
161                 HtmlData returnUrl = getReturnUrl(element, lookupForm, returnKeys, businessObjectRestrictions);
162                 ResultRow row = new ResultRow(columns, returnUrl.constructCompleteHtmlTag(), getActionUrls(element, pkNames, businessObjectRestrictions));
163                     row.setObjectId(((CollectionActivityInvoiceLookup) element).getInvoiceNumber());
164                     row.setRowId(returnUrl.getName());
165                     row.setReturnUrlHtmlData(returnUrl);
166                 boolean isRowReturnable = isResultReturnable(element);
167                 row.setRowReturnable(isRowReturnable);
168                 if (isRowReturnable) {
169                     hasReturnableRow = true;
170                 }
171 
172                 resultTable.add(row);
173             }
174             lookupForm.setHasReturnableRow(hasReturnableRow);
175         }
176 
177         return displayList;
178     }
179 
180     public ContractsGrantsReportHelperService getContractsGrantsReportHelperService() {
181         return contractsGrantsReportHelperService;
182     }
183 
184     public void setContractsGrantsReportHelperService(ContractsGrantsReportHelperService contractsGrantsReportHelperService) {
185         this.contractsGrantsReportHelperService = contractsGrantsReportHelperService;
186     }
187 
188     public ContractsGrantsCollectionActivityDocumentService getContractsGrantsCollectionActivityDocumentService() {
189         return contractsGrantsCollectionActivityDocumentService;
190     }
191 
192     public void setContractsGrantsCollectionActivityDocumentService(ContractsGrantsCollectionActivityDocumentService contractsGrantsCollectionActivityDocumentService) {
193         this.contractsGrantsCollectionActivityDocumentService = contractsGrantsCollectionActivityDocumentService;
194     }
195 }