View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.fp.document.AdvanceDepositDocument;
26  import org.kuali.ole.sys.OLEConstants;
27  import org.kuali.ole.sys.businessobject.ElectronicPaymentClaim;
28  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.rice.core.api.config.property.ConfigurationService;
31  import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl;
32  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
33  import org.kuali.rice.kns.web.struts.form.LookupForm;
34  import org.kuali.rice.kns.web.ui.Column;
35  import org.kuali.rice.kns.web.ui.ResultRow;
36  import org.kuali.rice.krad.bo.BusinessObject;
37  import org.kuali.rice.krad.bo.PersistableBusinessObject;
38  import org.kuali.rice.krad.dao.LookupDao;
39  import org.kuali.rice.krad.lookup.CollectionIncomplete;
40  import org.springframework.transaction.annotation.Transactional;
41  
42  /**
43   * A helper class that gives us the ability to do special lookups on electronic payment claims.
44   */
45  @Transactional
46  public class ElectronicPaymentClaimLookupableHelperServiceImpl extends AbstractLookupableHelperServiceImpl {
47      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ElectronicPaymentClaimLookupableHelperServiceImpl.class);
48      private LookupDao lookupDao;
49      
50      /**
51       * 
52       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getSearchResults(java.util.Map)
53       */
54      @Override
55      public List<PersistableBusinessObject> getSearchResults(Map<String, String> fieldValues) {
56          boolean unbounded = false;
57          String claimingStatus = fieldValues.remove("paymentClaimStatusCode");
58          if (claimingStatus != null && !claimingStatus.equals("A")) {
59              if (claimingStatus.equals(ElectronicPaymentClaim.ClaimStatusCodes.CLAIMED)) {
60                  fieldValues.put("paymentClaimStatusCode", ElectronicPaymentClaim.ClaimStatusCodes.CLAIMED);
61              } else {
62                  fieldValues.put("paymentClaimStatusCode", ElectronicPaymentClaim.ClaimStatusCodes.UNCLAIMED);
63              }
64          }
65          String organizationReferenceId = fieldValues.remove("generatingAccountingLine.organizationReferenceId");
66          List<PersistableBusinessObject> resultsList = (List)lookupDao.findCollectionBySearchHelper(ElectronicPaymentClaim.class, fieldValues, unbounded, false);
67          if (!StringUtils.isBlank(organizationReferenceId)) {
68              
69              List<PersistableBusinessObject> prunedResults = pruneResults(resultsList, organizationReferenceId);
70              resultsList = new CollectionIncomplete<PersistableBusinessObject>(prunedResults, ((CollectionIncomplete)resultsList).getActualSizeIfTruncated());
71              
72          } 
73          return resultsList;
74      }
75      
76      /**
77       * If organization reference id was present in lookup fields, only returns electronic payment claims which associate with the given organization reference id
78       * @param paymentsToPrune the Collection of electronic payment claims, still unfiltered by organization reference id 
79       * @param organizationReferenceId the organization reference id to use as a filter
80       * @return the filtered results
81       */
82      protected List<PersistableBusinessObject> pruneResults(List<PersistableBusinessObject> paymentsToPrune, String organizationReferenceId) {
83          final String matchingAdvanceDepositDocumentNumbers = getAdvanceDepositsWithOrganizationReferenceId(organizationReferenceId);
84          final List<GeneratingLineHolder> generatingLineHolders = getGeneratingLinesForDocuments(matchingAdvanceDepositDocumentNumbers, organizationReferenceId); 
85          
86          List<PersistableBusinessObject> prunedResults = new ArrayList<PersistableBusinessObject>();
87          for (PersistableBusinessObject epcAsPBO : paymentsToPrune) {
88              final ElectronicPaymentClaim epc = (ElectronicPaymentClaim)epcAsPBO;
89              
90              int count = 0;
91              boolean epcMatches = false;
92              while (count < generatingLineHolders.size() && !epcMatches) {
93                  final GeneratingLineHolder generatingLine = generatingLineHolders.get(count);
94                  if (generatingLine.isMommy(epc)) {
95                      prunedResults.add(epc);
96                      epcMatches = true;
97                  }
98                  
99                  count += 1;
100             }
101         }
102         
103         return prunedResults;
104     }
105     
106     /**
107      * Finds the document ids for all AD documents which have an accounting line with the given organizationReferenceId
108      * @param organizationReferenceId the organization reference id to find advance deposit documents for
109      * @return a lookup String that holds the document numbers of the matching advance deposit documents
110      */
111     protected String getAdvanceDepositsWithOrganizationReferenceId(String organizationReferenceId) {
112         StringBuilder documentNumbers = new StringBuilder();
113         
114         Map fields = new HashMap();
115         fields.put("sourceAccountingLines.organizationReferenceId", organizationReferenceId);
116         Collection ads = getLookupService().findCollectionBySearchUnbounded(AdvanceDepositDocument.class, fields);
117         for (Object adAsObject : ads) {
118             final AdvanceDepositDocument adDoc = (AdvanceDepositDocument)adAsObject;
119             documentNumbers.append("|");
120             documentNumbers.append(adDoc.getDocumentNumber());
121         }
122         
123         return documentNumbers.substring(1);
124     }
125     
126     /**
127      * Looks up all of the generating lines and stores essential information about them on documents given by the matchingAdvanceDepositDocumentNumbers parameter
128      * and matching the given organization reference id
129      * @param matchingAdvanceDepositDocumentNumbers the document numbers of matching advance deposit documents in lookup form 
130      * @param organizationReferenceId the organization reference id the accounting line must match
131      * @return a List of essential information about each of the matching accounting lines
132      */
133     protected List<GeneratingLineHolder> getGeneratingLinesForDocuments(String matchingAdvanceDepositDocumentNumbers, String organizationReferenceId) {
134         List<GeneratingLineHolder> holders = new ArrayList<GeneratingLineHolder>();
135         
136         Map fields = new HashMap();
137         fields.put("documentNumber", matchingAdvanceDepositDocumentNumbers);
138         fields.put("organizationReferenceId", organizationReferenceId);
139         
140         Collection als = getLookupService().findCollectionBySearchUnbounded(SourceAccountingLine.class, fields);
141         for (Object alAsObject : als) {
142             final SourceAccountingLine accountingLine = (SourceAccountingLine)alAsObject;
143             holders.add(new GeneratingLineHolder(accountingLine.getDocumentNumber(), accountingLine.getSequenceNumber()));
144         }
145         
146         return holders;
147     }
148 
149     /**
150      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
151      */
152     @Override
153     public void validateSearchParameters(Map fieldValues) {
154         // grab the backLocation and the docFormKey
155         this.setDocFormKey((String)fieldValues.get("docFormKey"));
156         this.setBackLocation((String)fieldValues.get("backLocation"));
157         super.validateSearchParameters(fieldValues);
158     }
159 
160     /**
161      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#isResultReturnable(org.kuali.rice.krad.bo.BusinessObject)
162      */
163     @Override
164     public boolean isResultReturnable(BusinessObject claimAsBO) {
165         boolean result = super.isResultReturnable(claimAsBO);
166         ElectronicPaymentClaim claim = (ElectronicPaymentClaim)claimAsBO;
167         if (result && ((claim.getPaymentClaimStatusCode() != null && claim.getPaymentClaimStatusCode().equals(ElectronicPaymentClaim.ClaimStatusCodes.CLAIMED)) || (!StringUtils.isBlank(claim.getReferenceFinancialDocumentNumber())))) {
168             result = false;
169         }
170         return result;
171     }
172     
173     /**
174      * Using default results, add columnAnchor link for reference financial document number to open document
175      * 
176      * @param lookupForm
177      * @param kualiLookupable
178      * @param resultTable
179      * @param bounded
180      * @return
181      * 
182      * KRAD Conversion: Lookupable performing customization of columns of the display list.
183      */
184     @Override
185     public Collection performLookup(LookupForm lookupForm, Collection resultTable, boolean bounded) {
186         Collection displayList = super.performLookup(lookupForm, resultTable, bounded);
187         for (ResultRow row : (Collection<ResultRow>)resultTable) {
188             for (Column col : row.getColumns()) {
189                 if (StringUtils.equals("referenceFinancialDocumentNumber", col.getPropertyName()) && StringUtils.isNotBlank(col.getPropertyValue())) {
190                     String propertyURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.WORKFLOW_URL_KEY) + "/DocHandler.do?docId=" + col.getPropertyValue() + "&command=displayDocSearchView";
191                     AnchorHtmlData htmlData = new AnchorHtmlData(propertyURL, "", col.getPropertyValue());
192                     htmlData.setTitle(col.getPropertyValue());
193                     col.setColumnAnchor(htmlData);
194                 }
195             }
196         }
197         return displayList;
198     }    
199 
200     /**
201      * Sets the lookupDao attribute value.
202      * @param lookupDao The lookupDao to set.
203      */
204     public void setLookupDao(LookupDao lookupDao) {
205         this.lookupDao = lookupDao;
206     }
207     
208     /**
209      * Holds information about an accounting line which created an electronic payment claim
210      */
211     class GeneratingLineHolder {
212         private String documentNumber;
213         private Integer lineNumber;
214         
215         /**
216          * Constructs a GeneratingLineHolder
217          * @param documentNumber the document the generating line is on
218          * @param lineNumber the line number of the generating line
219          */
220         public GeneratingLineHolder(String documentNumber, Integer lineNumber) {
221             this.documentNumber = documentNumber;
222             this.lineNumber = lineNumber;
223         }
224         
225         /**
226          * Determines if the given electronic payment claim was generated by the accounting line that this GeneratingLineHolder has information for
227          * @param epc the electronic payment claim to check
228          * @return true if this accounting line did generate the epc, false otherwise
229          */
230         public boolean isMommy(ElectronicPaymentClaim epc) {
231             return epc.getDocumentNumber().equals(documentNumber) && epc.getFinancialDocumentLineNumber().equals(lineNumber);
232         }
233     }
234 }