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.OleLookupableImpl;
6 import org.kuali.ole.deliver.bo.OLEPatronEntityViewBo;
7 import org.kuali.ole.deliver.bo.OlePatronDocument;
8 import org.kuali.ole.deliver.bo.PatronBillPayment;
9 import org.kuali.ole.service.OlePatronHelperService;
10 import org.kuali.ole.service.OlePatronHelperServiceImpl;
11 import org.kuali.rice.core.api.util.RiceKeyConstants;
12 import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
13 import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
14 import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
15 import org.kuali.rice.krad.lookup.LookupUtils;
16 import org.kuali.rice.krad.lookup.LookupableImpl;
17 import org.kuali.rice.krad.service.KRADServiceLocator;
18 import org.kuali.rice.krad.uif.UifConstants;
19 import org.kuali.rice.krad.uif.UifParameters;
20 import org.kuali.rice.krad.uif.field.InputField;
21 import org.kuali.rice.krad.uif.util.ComponentUtils;
22 import org.kuali.rice.krad.uif.view.LookupView;
23 import org.kuali.rice.krad.util.GlobalVariables;
24 import org.kuali.rice.krad.util.KRADConstants;
25 import org.kuali.rice.krad.util.KRADUtils;
26 import org.kuali.rice.krad.util.UrlFactory;
27 import org.kuali.rice.krad.web.form.LookupForm;
28
29 import java.util.*;
30
31
32
33
34 public class OlePatronEnitityLookupableImpl extends OleLookupableImpl {
35 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronEnitityLookupableImpl.class);
36 List<?> searchResults;
37 OlePatronHelperService olePatronHelperService = new OlePatronHelperServiceImpl();
38 public static int count = 0;
39
40
41
42
43
44
45
46
47
48 @Override
49 public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
50 LOG.debug("Inside performSearch()");
51 List<OLEPatronEntityViewBo> olePatronDocuments=new ArrayList<OLEPatronEntityViewBo>();
52 List<OLEPatronEntityViewBo> finalResult=new ArrayList<OLEPatronEntityViewBo>();
53 List<String> olePatronIdList=new ArrayList<String>();
54 Map<String,String> searchEntityMap=new HashMap<String,String>();
55 Map<String,String> searchEntityPhoneMap=new HashMap<String,String>();
56 Map<String,String> searchEntityEmailMap=new HashMap<String,String>();
57 LookupUtils.preprocessDateFields(searchCriteria);
58 String firstName = searchCriteria.get(OLEConstants.OlePatron.PATRON_FIRST_NAME);
59 String middleName = searchCriteria.get("middleName");
60 String lastName = searchCriteria.get(OLEConstants.OlePatron.PATRON_LAST_NAME);
61 String email = searchCriteria.get("emailAddress");
62 String phoneNumber = searchCriteria.get("phoneNumber");
63 String patronType = searchCriteria.get("patronType");
64 if(StringUtils.isNotEmpty(searchCriteria.get("patronBarcode"))){
65 searchCriteria.remove("active");
66 }
67 if(StringUtils.isNotEmpty(firstName)){
68 searchEntityMap.put("firstName",firstName);
69 }
70 if(StringUtils.isNotEmpty(lastName)){
71 searchEntityMap.put("lastName",lastName);
72 }
73 if(StringUtils.isNotEmpty(middleName)){
74 searchEntityMap.put("middleName",middleName);
75 }
76 if(StringUtils.isNotEmpty(phoneNumber)){
77 searchEntityPhoneMap.put("phoneNumber",phoneNumber);
78 }
79 if(StringUtils.isNotEmpty(email)){
80 searchEntityEmailMap.put("emailAddress",email);
81 }
82
83 if (StringUtils.isNotBlank(phoneNumber)) {
84 if (!validatePhoneNumber(phoneNumber)) {
85 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.INVALID_PHONE_NUMBER_FORMAT);
86 return new ArrayList<Object>();
87 }
88 phoneNumber = buildPhoneNumber(phoneNumber);
89 }
90
91 Collection<?> displayList;
92
93
94 displayList = getSearchResults(form, LookupUtils.forceUppercase(getDataObjectClass(), searchCriteria),
95 !bounded);
96
97
98 for (Object object : displayList) {
99
100
101
102
103
104
105
106
107 if (isResultReturnable(object)) {
108 form.setAtLeastOneRowReturnable(true);
109 }
110 }
111
112 finalResult = (List<OLEPatronEntityViewBo>) displayList;
113 searchResults=finalResult;
114 return finalResult;
115 }
116
117
118
119
120
121
122
123
124
125
126 @Override
127 protected Map<String, InputField> getCriteriaFieldsForValidation(LookupView lookupView, LookupForm form) {
128 LOG.debug("Inside getCriteriaFieldsForValidation()");
129 Map<String, InputField> criteriaFieldMap = new HashMap<String, InputField>();
130
131 List<InputField> fields = ComponentUtils.getComponentsOfTypeDeep(lookupView.getCriteriaFields(),
132 InputField.class);
133
134 for (InputField field : fields) {
135 criteriaFieldMap.put(field.getPropertyName(), field);
136 }
137
138 return criteriaFieldMap;
139 }
140
141
142
143
144
145
146
147
148
149
150 @Override
151 protected String getActionUrlHref(LookupForm lookupForm, Object dataObject, String methodToCall,
152 List<String> pkNames) {
153 LOG.debug("Inside getActionUrlHref()");
154 LookupView lookupView = (LookupView) lookupForm.getView();
155
156 Properties props = new Properties();
157 props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
158 Map<String, String> primaryKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(pkNames, dataObject);
159 for (String primaryKey : primaryKeyValues.keySet()) {
160 String primaryKeyValue = primaryKeyValues.get(primaryKey);
161
162 props.put(primaryKey, primaryKeyValue);
163 }
164
165 if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) {
166 props.put(KRADConstants.RETURN_LOCATION_PARAMETER, lookupForm.getReturnLocation());
167 }
168
169 props.put(UifParameters.DATA_OBJECT_CLASS_NAME, OlePatronDocument.class.getName());
170 props.put(UifParameters.VIEW_TYPE_NAME, UifConstants.ViewType.MAINTENANCE.name());
171
172 String maintenanceMapping = OLEConstants.OlePatron.PATRON_MAINTENANCE_ACTION_LINK;
173
174 return UrlFactory.parameterizeUrl(maintenanceMapping, props);
175 }
176
177
178
179
180
181
182
183 public String getPatronBillUrl(String patronId, String firstName, String lastName) {
184 String url = "patronBillMaintenance?viewTypeName=MAINTENANCE&returnLocation=%2Fportal.do&methodToCall=start&dataObjectClassName=org.kuali.ole.deliver.bo.PatronBillPayment&patronId=" + patronId + "&firstName=" + firstName + "&lastName=" + lastName;
185 return url;
186 }
187
188 public boolean isWildCardMatches(String word, String wildCardString) {
189 if (LOG.isInfoEnabled()) {
190 LOG.info("Applying WildCard Search");
191 }
192 boolean isSuccess = true;
193 if (wildCardString != null && (!wildCardString.equalsIgnoreCase("")) && wildCardString.contains("*")) {
194 if (wildCardString.equalsIgnoreCase("*")) {
195 isSuccess = true;
196 } else {
197 wildCardString = wildCardString.replace('*', ',');
198 String[] wCardString = wildCardString.split(",");
199 if (wCardString != null && wCardString.length > 0) {
200 for (String str : wCardString) {
201 if (word.toLowerCase().contains(str.toLowerCase())) {
202 isSuccess = isSuccess && true;
203 } else {
204 isSuccess = isSuccess && false;
205 }
206 }
207 } else {
208 isSuccess = false;
209 if (word.equalsIgnoreCase(wildCardString)) {
210 isSuccess = true;
211 }
212 }
213 }
214 return isSuccess;
215 } else {
216 if (wildCardString.equalsIgnoreCase(word)) {
217 return true;
218 } else {
219 return false;
220 }
221 }
222 }
223
224 private boolean validatePhoneNumber(String phoneNo) {
225 if (LOG.isInfoEnabled()) {
226 LOG.info("Validating the Phone Number Format - ##########, (###)###-#### , ###-###-#### , ### ###-#### , ### ### ####");
227 }
228 if (phoneNo.matches("\\d{10}")) return true;
229 else if (phoneNo.matches("\\d{3}[-]\\d{3}[-]\\d{4}")) return true;
230 else if (phoneNo.matches("\\d{3}[\\s]\\d{3}[-]\\d{4}")) return true;
231 else if (phoneNo.matches("\\d{3}[\\s]\\d{3}[\\s]\\d{4}")) return true;
232 else if (phoneNo.matches("\\(\\d{3}\\)[\\s]\\d{3}[-]\\d{4}")) return true;
233 else return false;
234
235 }
236 private String buildPhoneNumber(String phoneNumber){
237 StringBuilder userPhoneNumber = new StringBuilder();
238 for (int i = 0; i < phoneNumber.length(); i++) {
239 if (Character.isDigit(phoneNumber.charAt(i))) {
240 userPhoneNumber.append(phoneNumber.charAt(i));
241 }
242 }
243 return userPhoneNumber.toString();
244 }
245
246 }