1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
78
79
80
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
108
109
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
128
129
130
131
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
151
152 @Override
153 public void validateSearchParameters(Map fieldValues) {
154
155 this.setDocFormKey((String)fieldValues.get("docFormKey"));
156 this.setBackLocation((String)fieldValues.get("backLocation"));
157 super.validateSearchParameters(fieldValues);
158 }
159
160
161
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
175
176
177
178
179
180
181
182
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
202
203
204 public void setLookupDao(LookupDao lookupDao) {
205 this.lookupDao = lookupDao;
206 }
207
208
209
210
211 class GeneratingLineHolder {
212 private String documentNumber;
213 private Integer lineNumber;
214
215
216
217
218
219
220 public GeneratingLineHolder(String documentNumber, Integer lineNumber) {
221 this.documentNumber = documentNumber;
222 this.lineNumber = lineNumber;
223 }
224
225
226
227
228
229
230 public boolean isMommy(ElectronicPaymentClaim epc) {
231 return epc.getDocumentNumber().equals(documentNumber) && epc.getFinancialDocumentLineNumber().equals(lineNumber);
232 }
233 }
234 }