1 package org.kuali.ole.deliver.lookup;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.deliver.bo.OlePatronDocument;
6 import org.kuali.ole.deliver.bo.PatronBillPayment;
7 import org.kuali.ole.service.OlePatronHelperService;
8 import org.kuali.ole.service.OlePatronHelperServiceImpl;
9 import org.kuali.rice.core.api.util.RiceKeyConstants;
10 import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
11 import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
12 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
13 import org.kuali.rice.krad.lookup.LookupUtils;
14 import org.kuali.rice.krad.lookup.LookupableImpl;
15 import org.kuali.rice.krad.service.KRADServiceLocator;
16 import org.kuali.rice.krad.uif.UifConstants;
17 import org.kuali.rice.krad.uif.UifParameters;
18 import org.kuali.rice.krad.uif.view.LookupView;
19 import org.kuali.rice.krad.util.GlobalVariables;
20 import org.kuali.rice.krad.util.KRADConstants;
21 import org.kuali.rice.krad.util.KRADUtils;
22 import org.kuali.rice.krad.util.UrlFactory;
23 import org.kuali.rice.krad.web.form.LookupForm;
24
25 import java.util.*;
26
27
28
29
30 public class OlePatronBillLookupableImpl extends LookupableImpl {
31 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronBillLookupableImpl.class);
32 OlePatronHelperService olePatronHelperService = new OlePatronHelperServiceImpl();
33 private String firstName;
34 private String middleName;
35 private String lastName;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 protected String getActionUrlHref(LookupForm lookupForm, Object dataObject, String methodToCall,
53 List<String> pkNames) {
54 LookupView lookupView = (LookupView) lookupForm.getView();
55 Properties props = new Properties();
56 props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
57
58 Map<String, String> primaryKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(pkNames, dataObject);
59 for (String primaryKey : primaryKeyValues.keySet()) {
60 String primaryKeyValue = primaryKeyValues.get(primaryKey);
61
62 props.put(primaryKey, primaryKeyValue);
63 }
64
65 if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) {
66 props.put(KRADConstants.RETURN_LOCATION_PARAMETER, lookupForm.getReturnLocation());
67 }
68
69 props.put(UifParameters.DATA_OBJECT_CLASS_NAME, lookupForm.getDataObjectClassName());
70 props.put(UifParameters.VIEW_TYPE_NAME, UifConstants.ViewType.MAINTENANCE.name());
71
72 String maintenanceMapping = "patronBillMaintenance";
73
74 return UrlFactory.parameterizeUrl(maintenanceMapping, props);
75 }
76
77
78
79
80
81
82
83
84
85 @Override
86 public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
87 LOG.debug("Inside performSearch()");
88 List<PatronBillPayment> patronBillPayments = new ArrayList<PatronBillPayment>();
89 List<PatronBillPayment> billPayments = new ArrayList<PatronBillPayment>();
90 List<PatronBillPayment> finalResult = new ArrayList<PatronBillPayment>();
91 List<String> olePatronIdList = new ArrayList<String>();
92 Map<String, String> searchEntityMap = new HashMap<String, String>();
93 LookupUtils.preprocessDateFields(searchCriteria);
94 firstName = searchCriteria.get(OLEConstants.OlePatron.PATRON_FIRST_NAME);
95 lastName = searchCriteria.get(OLEConstants.OlePatron.PATRON_LAST_NAME);
96 searchCriteria.remove("firstName");
97 searchCriteria.remove("lastName");
98 if (StringUtils.isNotEmpty(firstName)) {
99 searchEntityMap.put("firstName", firstName);
100 }
101 if (StringUtils.isNotEmpty(lastName)) {
102 searchEntityMap.put("lastName", lastName);
103 }
104 Integer searchResultsLimit = null;
105 try {
106
107 if (searchEntityMap.size() > 0) {
108 try {
109 List<?> searchResults;
110 Map<String, String> searchEntityCriteria = new HashMap<String, String>();
111 for (Map.Entry<String, String> entry : searchEntityMap.entrySet()) {
112 int counter=0;
113 for( int i=0; i<entry.getValue().length(); i++ ) {
114 if( entry.getValue().charAt(i) == '*' ) {
115 counter++;
116 }
117 }
118 if (counter==0 || (counter!=entry.getValue().length())) {
119 searchEntityCriteria.put(entry.getKey(), entry.getValue());
120 }
121 }
122 if (searchEntityCriteria.size() > 0) {
123 Class entityNameBoClass = Class.forName("org.kuali.rice.kim.impl.identity.name.EntityNameBo");
124 if (LookupUtils.hasExternalBusinessObjectProperty(entityNameBoClass, searchEntityCriteria)) {
125 Map<String, String> eboSearchCriteria = adjustCriteriaForNestedEBOs(searchEntityCriteria, bounded);
126 searchResults = (List<EntityNameBo>) getLookupService().findCollectionBySearchUnbounded(entityNameBoClass, eboSearchCriteria);
127 } else {
128 searchResults = (List<EntityNameBo>) getLookupService().findCollectionBySearchUnbounded(entityNameBoClass, searchEntityCriteria);
129 }
130 } else {
131 searchResults = (List<EntityNameBo>) KRADServiceLocator.getBusinessObjectService().findAll(EntityNameBo.class);
132 }
133 Iterator iterator = searchResults.iterator();
134 while (iterator.hasNext()) {
135 EntityNameBo entityNameBo = (EntityNameBo) iterator.next();
136 if (!olePatronIdList.contains(entityNameBo.getEntityId()))
137 olePatronIdList.add(entityNameBo.getEntityId());
138 }
139 if (searchCriteria.size() > 0) {
140 for (String patronId : olePatronIdList) {
141 Map<String, String> map = new HashMap<String, String>();
142 map.put("patronId", patronId);
143 PatronBillPayment patronBillPayment = (PatronBillPayment) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PatronBillPayment.class, map);
144 if (patronBillPayment != null) {
145 patronBillPayments.add(patronBillPayment);
146 }
147 }
148 }
149 Class entityPatronBoClass = Class.forName("org.kuali.ole.deliver.bo.PatronBillPayment");
150 if (bounded) {
151 searchResultsLimit = LookupUtils.getSearchResultsLimit(entityPatronBoClass, form);
152 }
153 int count=0;
154 if (searchResultsLimit != null) {
155 iterator = patronBillPayments.iterator();
156 while (iterator.hasNext()) {
157 PatronBillPayment billPayment = (PatronBillPayment) iterator.next();
158 if (count < searchResultsLimit.intValue()) {
159 billPayments.add(billPayment);
160 count++;
161 } else {
162 break;
163 }
164 }
165 } else {
166 billPayments.addAll(patronBillPayments);
167 }
168 String resultsPropertyName = "LookupResultMessages";
169 GlobalVariables.getMessageMap().clearErrorMessages();
170 GlobalVariables.getMessageMap().removeAllInfoMessagesForProperty(resultsPropertyName);
171 GlobalVariables.getMessageMap().removeAllWarningMessagesForProperty(resultsPropertyName);
172 if (billPayments.size() == 0) {
173 GlobalVariables.getMessageMap().putInfoForSectionId(resultsPropertyName,
174 RiceKeyConstants.INFO_LOOKUP_RESULTS_NONE_FOUND);
175 } else if (billPayments.size() == 1) {
176 GlobalVariables.getMessageMap().putInfoForSectionId(resultsPropertyName,
177 RiceKeyConstants.INFO_LOOKUP_RESULTS_DISPLAY_ONE);
178 } else if (billPayments.size() > 1) {
179 Boolean resultsExceedsLimit = bounded && searchResultsLimit != null && billPayments.size() > 0 && billPayments.size() > searchResultsLimit ? true : false;
180 if (resultsExceedsLimit) {
181 GlobalVariables.getMessageMap().putInfoForSectionId(resultsPropertyName,
182 RiceKeyConstants.INFO_LOOKUP_RESULTS_EXCEEDS_LIMIT, billPayments.size() + "",
183 searchResultsLimit.toString());
184 } else {
185
186 GlobalVariables.getMessageMap().putInfoForSectionId(resultsPropertyName,
187 RiceKeyConstants.INFO_LOOKUP_RESULTS_DISPLAY_ALL, billPayments.size() + "");
188 }
189 }
190 patronBillPayments.clear();
191 patronBillPayments.addAll(billPayments);
192 } catch (IllegalAccessException e) {
193 throw new RuntimeException("Error trying to perform search", e);
194 } catch (InstantiationException e1) {
195 throw new RuntimeException("Error trying to perform search", e1);
196 }
197 } else {
198 patronBillPayments =(List<PatronBillPayment>) super.performSearch(form, searchCriteria, bounded);
199 }
200
201 } catch (Exception e) {
202 LOG.error("Exception Occurred In Searching of Patrons------>Patron Bill Lookup");
203 }
204 Iterator iterator=patronBillPayments.iterator();
205 while (iterator.hasNext()){
206 PatronBillPayment patronBillPayment=(PatronBillPayment)iterator.next();
207 Map<String, String> map = new HashMap<String, String>();
208 map.put("entityId", patronBillPayment.getPatronId());
209 EntityNameBo entityNameBo=(EntityNameBo)KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(EntityNameBo.class,map);
210 if(entityNameBo!=null){
211 patronBillPayment.setFirstName(entityNameBo.getFirstName());
212 patronBillPayment.setLastName(entityNameBo.getLastName());
213 }
214 finalResult.add(patronBillPayment);
215 }
216
217 return finalResult;
218 }
219
220 public boolean isWildCardMatches(String word, String wildCardString) {
221 LOG.debug("Applying WildCard Search");
222 boolean isSuccess = true;
223 if (wildCardString.contains("*")) {
224 if (wildCardString.equalsIgnoreCase("*")) {
225 isSuccess = true;
226 } else {
227 wildCardString = wildCardString.replace('*', ',');
228 String[] wCardString = wildCardString.split(",");
229 if (wCardString != null && wCardString.length > 0) {
230 for (String str : wCardString) {
231 if (word.toLowerCase().contains(str.toLowerCase())) {
232 isSuccess = isSuccess && true;
233 } else {
234 isSuccess = isSuccess && false;
235 }
236 }
237 } else {
238 isSuccess = false;
239 if (word.equalsIgnoreCase(wildCardString)) {
240 isSuccess = true;
241 }
242 }
243 }
244 return isSuccess;
245 } else {
246 if (wildCardString.equalsIgnoreCase(word)) {
247 return true;
248 } else {
249 return false;
250 }
251 }
252 }
253
254 }