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.cg.businessobject.lookup;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import org.kuali.kfs.integration.ar.AccountsReceivableModuleBillingService;
29  import org.kuali.kfs.module.cg.CGPropertyConstants;
30  import org.kuali.kfs.module.cg.businessobject.Award;
31  import org.kuali.kfs.module.cg.service.ContractsAndGrantsLookupService;
32  import org.kuali.kfs.sys.KFSConstants;
33  import org.kuali.kfs.sys.KFSPropertyConstants;
34  import org.kuali.rice.kew.impl.document.search.DocumentSearchCriteriaBo;
35  import org.kuali.rice.kns.lookup.HtmlData;
36  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
37  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
38  import org.kuali.rice.kns.util.FieldUtils;
39  import org.kuali.rice.kns.web.ui.Column;
40  import org.kuali.rice.krad.bo.BusinessObject;
41  import org.kuali.rice.krad.util.KRADConstants;
42  import org.kuali.rice.krad.util.UrlFactory;
43  
44  /**
45   * Allows custom handling of Awards within the lookup framework.
46   */
47  public class AwardLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
48  
49      protected AccountsReceivableModuleBillingService accountsReceivableModuleBillingService;
50      protected ContractsAndGrantsLookupService contractsAndGrantsLookupService;
51  
52  
53      /**
54       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getColumns()
55       */
56      @Override
57      public List<Column> getColumns() {
58          List<Column> columns =  super.getColumns();
59  
60          if (!getAccountsReceivableModuleBillingService().isContractsGrantsBillingEnhancementActive()) {
61              for(Iterator<Column> it = columns.iterator(); it.hasNext(); ) {
62                  Column column = it.next();
63                  if (getFieldsToIgnore().contains(column.getPropertyName())) {
64                      it.remove();
65                  }
66              }
67          }
68  
69          return columns;
70      }
71  
72      /**
73       * Ignore fields that are specific to the Contracts & Grants Billing (CGB) enhancement
74       * if CGB is disabled.
75       *
76       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#setRows()
77       */
78      @Override
79      protected void setRows() {
80          List<String> lookupFieldNames = null;
81          if (getBusinessObjectMetaDataService().isLookupable(getBusinessObjectClass())) {
82              lookupFieldNames = getBusinessObjectMetaDataService().getLookupableFieldNames(
83                      getBusinessObjectClass());
84          }
85          if (lookupFieldNames == null) {
86              throw new RuntimeException("Lookup not defined for business object " + getBusinessObjectClass());
87          }
88  
89          List<String> lookupFieldAttributeList = new ArrayList();
90          for (String lookupFieldName: lookupFieldNames) {
91              if (!getFieldsToIgnore().contains(lookupFieldName)) {
92                  lookupFieldAttributeList.add(lookupFieldName);
93              }
94          }
95  
96          // construct field object for each search attribute
97          List fields = new ArrayList();
98  
99          try {
100             fields = FieldUtils.createAndPopulateFieldsForLookup(lookupFieldAttributeList, getReadOnlyFieldsList(),
101                     getBusinessObjectClass());
102         } catch (InstantiationException | IllegalAccessException e) {
103             throw new RuntimeException("Unable to create instance of business object class" + e.getMessage());
104         }
105 
106         int numCols = getBusinessObjectDictionaryService().getLookupNumberOfColumns(this.getBusinessObjectClass());
107 
108         this.rows = FieldUtils.wrapFields(fields, numCols);
109     }
110 
111     /**
112      * If the Contracts & Grants Billing (CGB) enhancement is disabled, we don't want to
113      * process sections only related to CGB.
114      *
115      * @return list of fields to ignore
116      */
117     protected List<String> getFieldsToIgnore() {
118         List<String> fieldsToIgnore = new ArrayList<String>();
119 
120         if (!getAccountsReceivableModuleBillingService().isContractsGrantsBillingEnhancementActive()) {
121             fieldsToIgnore.add(CGPropertyConstants.LOOKUP_FUND_MGR_USER_ID_FIELD);
122             fieldsToIgnore.add(CGPropertyConstants.AWARD_LOOKUP_PRIMARY_FUND_MGR_FUND_MGR_NAME);
123             fieldsToIgnore.add(CGPropertyConstants.AwardFields.LAST_BILLED_DATE);
124             fieldsToIgnore.add(CGPropertyConstants.AwardFields.BILLING_FREQUENCY_CODE);
125             fieldsToIgnore.add(CGPropertyConstants.AwardFields.EXCLUDED_FROM_INVOICING);
126             fieldsToIgnore.add(CGPropertyConstants.AwardFields.ADDITIONAL_FORMS_DESCRIPTION);
127             fieldsToIgnore.add(CGPropertyConstants.AwardFields.ADDITIONAL_FORMS_REQUIRED_INDICATOR);
128             fieldsToIgnore.add(CGPropertyConstants.AwardFields.MIN_INVOICE_AMOUNT);
129             fieldsToIgnore.add(CGPropertyConstants.AwardFields.FUNDING_EXPIRATION_DATE);
130         }
131 
132         return fieldsToIgnore;
133     }
134 
135     /**
136      * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResultsHelper(java.util.Map, boolean)
137      */
138     @Override
139     protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
140         // perform the lookup on the project director and fund manager objects first
141         if (contractsAndGrantsLookupService.setupSearchFields(fieldValues, CGPropertyConstants.LOOKUP_USER_ID_FIELD, CGPropertyConstants.AWARD_LOOKUP_UNIVERSAL_USER_ID_FIELD) &&
142                 contractsAndGrantsLookupService.setupSearchFields(fieldValues, CGPropertyConstants.LOOKUP_FUND_MGR_USER_ID_FIELD, CGPropertyConstants.AWARD_LOOKUP_FUND_MGR_UNIVERSAL_USER_ID_FIELD)) {
143             return super.getSearchResultsHelper(fieldValues, unbounded);
144         }
145 
146         return Collections.EMPTY_LIST;
147     }
148 
149     /**
150      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject,
151      *      List pkNames)
152      */
153     @Override
154     public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
155         List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
156         anchorHtmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
157         if (allowsMaintenanceNewOrCopyAction()) {
158             anchorHtmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames));
159         }
160 
161         // only display invoice lookup URL if CGB is enabled
162         if (getAccountsReceivableModuleBillingService().isContractsGrantsBillingEnhancementActive()) {
163             AnchorHtmlData invoiceUrl = getInvoicesLookupUrl(businessObject);
164             anchorHtmlDataList.add(invoiceUrl);
165         }
166 
167         return anchorHtmlDataList;
168     }
169 
170     /**
171      * This method adds a link to the look up FOR the invoices associated with a given Award.
172      *
173      * @param bo
174      * @return
175      */
176     protected AnchorHtmlData getInvoicesLookupUrl(BusinessObject bo) {
177         Award award = (Award) bo;
178         Properties params = new Properties();
179         params.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.SEARCH_METHOD);
180         params.put(KFSConstants.DOC_FORM_KEY, "88888888");
181         params.put(KFSConstants.HIDE_LOOKUP_RETURN_LINK, "false");
182         params.put(KFSPropertyConstants.DOCUMENT_TYPE_NAME, getAccountsReceivableModuleBillingService().getContractsGrantsInvoiceDocumentType());
183         params.put(CGPropertyConstants.AWARD_INVOICE_LINK_PROPOSAL_NUMBER_PATH, award.getProposalNumber().toString());
184         params.put(KFSConstants.RETURN_LOCATION_PARAMETER, "portal.do");
185         params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, DocumentSearchCriteriaBo.class.getName());
186         String url = UrlFactory.parameterizeUrl(KRADConstants.LOOKUP_ACTION, params);
187         return new AnchorHtmlData(url, KFSConstants.SEARCH_METHOD, "View Invoices");
188     }
189 
190     public AccountsReceivableModuleBillingService getAccountsReceivableModuleBillingService() {
191         return accountsReceivableModuleBillingService;
192     }
193 
194     public void setAccountsReceivableModuleBillingService(AccountsReceivableModuleBillingService accountsReceivableModuleBillingService) {
195         this.accountsReceivableModuleBillingService = accountsReceivableModuleBillingService;
196     }
197 
198     public ContractsAndGrantsLookupService getContractsAndGrantsLookupService() {
199         return contractsAndGrantsLookupService;
200     }
201 
202     public void setContractsAndGrantsLookupService(ContractsAndGrantsLookupService contractsAndGrantsLookupService) {
203         this.contractsAndGrantsLookupService = contractsAndGrantsLookupService;
204     }
205 
206 }