View Javadoc
1   package org.kuali.ole.deliver.lookup;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.OleLookupableImpl;
7   import org.kuali.ole.deliver.bo.*;
8   import org.kuali.ole.deliver.service.OLEDeliverService;
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.entity.EntityBo;
14  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
15  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
16  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
17  import org.kuali.rice.krad.lookup.LookupUtils;
18  import org.kuali.rice.krad.lookup.LookupableImpl;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krad.uif.UifConstants;
21  import org.kuali.rice.krad.uif.UifParameters;
22  import org.kuali.rice.krad.uif.field.InputField;
23  import org.kuali.rice.krad.uif.util.ComponentUtils;
24  import org.kuali.rice.krad.uif.view.LookupView;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.util.KRADConstants;
27  import org.kuali.rice.krad.util.KRADUtils;
28  import org.kuali.rice.krad.util.UrlFactory;
29  import org.kuali.rice.krad.web.form.LookupForm;
30  
31  import java.text.SimpleDateFormat;
32  import java.util.*;
33  
34  /**
35   * OlePatronLookupableImpl makes validation  and populate the search criteria and return the search results
36   */
37  public class OlePatronLookupableImpl extends OleLookupableImpl {
38      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronLookupableImpl.class);
39      List<?> searchResults;
40      OlePatronHelperService olePatronHelperService = new OlePatronHelperServiceImpl();
41      public static int count = 0;
42  
43      @Override
44      public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
45          long startTime = System.currentTimeMillis();
46          List<OlePatronDocument> finalResult=new ArrayList<OlePatronDocument>();
47          LookupUtils.preprocessDateFields(searchCriteria);
48          String borrowerType = searchCriteria.get(OLEConstants.OlePatron.BORROWER_TYPE);
49          String barcode = searchCriteria.get(OLEConstants.OlePatron.BARCODE);
50          String firstName = searchCriteria.get(OLEConstants.OlePatron.PATRON_FIRST_NAME);
51          String middleName = searchCriteria.get(OLEConstants.OlePatron.PATRON_MIDDLE_NAME);
52          String lastName = searchCriteria.get(OLEConstants.OlePatron.PATRON_LAST_NAME);
53          String email = searchCriteria.get(OLEConstants.OlePatron.PATRON_EMAIL_ADDRESS);
54          String phoneNumber = searchCriteria.get(OLEConstants.OlePatron.PATRON_PHONE_NUMNER);
55          if(StringUtils.isNotEmpty(barcode)){
56              Map<String, String> map = new HashMap<String, String>();
57              map.put(OLEConstants.OlePatron.PATRON_LOST_BARCODE_FLD, barcode);
58              List<OlePatronLostBarcode> olePatronLostBarcodes = (List<OlePatronLostBarcode>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronLostBarcode.class, map);
59              if(olePatronLostBarcodes!=null && olePatronLostBarcodes.size()>0){
60                  for(Map.Entry<String,String> entry:searchCriteria.entrySet()){
61                      if(entry.getKey().equalsIgnoreCase(OLEConstants.OlePatron.BARCODE)){
62                          String patronId=olePatronLostBarcodes.get(0).getOlePatronId();
63                          Map patronMap = new HashMap();
64                          patronMap.put(OLEConstants.OleDeliverRequest.PATRON_ID, patronId);
65                          List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, patronMap);
66                          if (olePatronDocumentList != null && olePatronDocumentList.size() > 0) {
67                              entry.setValue(olePatronDocumentList.get(0).getBarcode());
68                          }
69                      }
70                  }
71              }
72              /*searchCriteria.remove(OLEConstants.OlePatron.PATRON_ACTIVE_IND);*/
73          }
74          searchCriteria.remove(OLEConstants.OlePatron.PATRON_EMAIL_ADDRESS);
75          searchCriteria.remove(OLEConstants.OlePatron.PATRON_FIRST_NAME);
76          searchCriteria.remove(OLEConstants.OlePatron.PATRON_MIDDLE_NAME);
77          searchCriteria.remove(OLEConstants.OlePatron.PATRON_LAST_NAME);
78          searchCriteria.remove(OLEConstants.OlePatron.PATRON_PHONE_NUMNER);
79  
80  
81          if(StringUtils.isNotEmpty(firstName)){
82              searchCriteria.put(OLEConstants.OLEPatronEntityViewBo.PATRON_FIRST_NAME,firstName);
83          }
84          if(StringUtils.isNotEmpty(borrowerType)){
85              searchCriteria.remove(OLEConstants.OlePatron.BORROWER_TYPE);
86              searchCriteria.put(OLEConstants.OLEPatronEntityViewBo.PATRON_TYPE_ID,borrowerType);
87          }
88          if(StringUtils.isNotEmpty(lastName)){
89              searchCriteria.put(OLEConstants.OLEPatronEntityViewBo.PATRON_LAST_NAME,lastName);
90          }
91          if(StringUtils.isNotEmpty(middleName)){
92              searchCriteria.put(OLEConstants.OLEPatronEntityViewBo.PATRON_MIDDLE_NAME,middleName);
93          }
94          if(StringUtils.isNotEmpty(phoneNumber)){
95              searchCriteria.put(OLEConstants.OLEPatronEntityViewBo.PATRON_PHONE_NUMBER,phoneNumber);
96          }
97          if(StringUtils.isNotEmpty(email)){
98              searchCriteria.put(OLEConstants.OLEPatronEntityViewBo.PATRON_EMAIL_ADDRESS,email);
99          }
100 
101         if (StringUtils.isNotBlank(phoneNumber)) {
102             if (!validatePhoneNumber(phoneNumber)) {
103                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.INVALID_PHONE_NUMBER_FORMAT);
104                 return new ArrayList<Object>();
105             }
106             phoneNumber = buildPhoneNumber(phoneNumber);
107         }
108 
109         Collection<?> displayList;
110 
111         // TODO: force uppercase will be done in binding at some point
112         displayList = getSearchResults(form, LookupUtils.forceUppercase(getDataObjectClass(), searchCriteria),
113                 !bounded);
114         // TODO delyea - is this the best way to set that the entire set has a returnable row?
115         for (Object object : displayList) {
116             if (object instanceof OlePatronDocument) {
117                 String patronId="";
118                 try {
119                     OlePatronDocument patronBo = (OlePatronDocument) object;
120                     patronId=patronBo.getOlePatronId();
121                     patronBo = OLEDeliverService.populatePatronName(patronBo);
122                     patronBo = OLEDeliverService.populatePatronEmailAndPhone(patronBo);
123                     patronBo.setCreateBillUrl(getPatronBillUrl(patronBo.getOlePatronId(), patronBo.getFirstName(), patronBo.getLastName()));
124                     List<PatronBillPayment> patronBillPaymentList = patronBo.getPatronBillPayments();
125                     if (CollectionUtils.isNotEmpty(patronBillPaymentList)) {
126                         patronBo.setPatronBillFileName(OLEConstants.OlePatron.PATRON_BILL);
127                         patronBo.setViewBillUrl(OLEConstants.OlePatron.PATRON_VIEW_BILL_URL + patronBo.getOlePatronId());
128                     }
129                 } catch (Exception e) {
130                     LOG.error("Error occurred while patron Lookup (patron Id -"+patronId+"):"+e);  //To change body of catch statement use File | Settings | File Templates.
131                 }
132             }
133             if (isResultReturnable(object)) {
134                 form.setAtLeastOneRowReturnable(true);
135             }
136         }
137         finalResult = (List<OlePatronDocument>) displayList;
138         searchResults = finalResult;
139         long endTime = System.currentTimeMillis();
140         System.out.println("Time taken for Patron lookup: " + (endTime-startTime) + " ms");
141         return finalResult;
142     }
143 
144 
145 
146     /**
147      * This method will validate the criteria fields
148      *
149      * @param lookupView
150      * @param form
151      * @return criteriaFieldMap(Map)
152      */
153     @Override
154     protected Map<String, InputField> getCriteriaFieldsForValidation(LookupView lookupView, LookupForm form) {
155         LOG.debug("Inside getCriteriaFieldsForValidation()");
156         Map<String, InputField> criteriaFieldMap = new HashMap<String, InputField>();
157 
158         List<InputField> fields = ComponentUtils.getComponentsOfTypeDeep(lookupView.getCriteriaFields(),
159                 InputField.class);
160 
161         for (InputField field : fields) {
162             criteriaFieldMap.put(field.getPropertyName(), field);
163         }
164 
165         return criteriaFieldMap;
166     }
167 
168     /**
169      * This method is to override the maintenance mapping
170      *
171      * @param lookupForm
172      * @param dataObject
173      * @param methodToCall
174      * @param pkNames
175      * @return mapping Url
176      */
177     @Override
178     protected String getActionUrlHref(LookupForm lookupForm, Object dataObject, String methodToCall,
179                                       List<String> pkNames) {
180         LOG.debug("Inside getActionUrlHref()");
181         LookupView lookupView = (LookupView) lookupForm.getView();
182 
183         Properties props = new Properties();
184         props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
185         Map<String, String> primaryKeyValues = super.getPropertyKeyValuesFromDataObject(pkNames, dataObject);
186         for (String primaryKey : primaryKeyValues.keySet()) {
187             String primaryKeyValue = primaryKeyValues.get(primaryKey);
188 
189             props.put(primaryKey, primaryKeyValue);
190         }
191 
192         if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) {
193             props.put(KRADConstants.RETURN_LOCATION_PARAMETER, lookupForm.getReturnLocation());
194         }
195 
196         props.put(UifParameters.DATA_OBJECT_CLASS_NAME, OlePatronDocument.class.getName());
197         props.put(UifParameters.VIEW_TYPE_NAME, UifConstants.ViewType.MAINTENANCE.name());
198 
199         String maintenanceMapping = OLEConstants.OlePatron.PATRON_MAINTENANCE_ACTION_LINK;
200 
201         return UrlFactory.parameterizeUrl(maintenanceMapping, props);
202     }
203 
204     /**
205      * This method will return url for view link in patron record.
206      *
207      * @return Url
208      */
209 
210     public String getUrl() {
211         if (searchResults != null && searchResults.size() > 0) {
212             List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) searchResults;
213             int pos = 0;
214             OlePatronDocument document = (OlePatronDocument) searchResults.get(0);
215             if (!document.isStartingIndexExecuted()) {
216                 document.setPointing(true);
217                 document.setStartingIndexExecuted(true);
218             }
219             OlePatronDocument patronDocument = null;
220             for (OlePatronDocument olePatronDocument : olePatronDocuments) {
221                 if (olePatronDocument.isPointing()) {
222                     if (olePatronDocument.isPatronBillFlag()) {
223                         patronDocument = olePatronDocument;
224                         break;
225                     } else {
226                         break;
227                     }
228                 }
229                 pos++;
230             }
231             if (patronDocument != null) {
232                 return "patronbill?viewId=BillView&amp;methodToCall=start&amp;patronId=" + patronDocument.getOlePatronId();
233             } else {
234                 return null;
235             }
236 
237         }
238         return null;
239     }
240 
241     /**
242      * This method will return view bill  link name for create bill.
243      *
244      * @return link name
245      */
246     public String getFileName() {
247         if (searchResults != null && searchResults.size() > 0) {
248             List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) searchResults;
249             int pos = 0;
250             OlePatronDocument patronDocument = null;
251             for (OlePatronDocument olePatronDocument : olePatronDocuments) {
252                 if (olePatronDocument.isPointing()) {
253                     olePatronDocument.setPointing(false);
254                     if (searchResults.size() - 1 > pos) {
255                         OlePatronDocument nextOlePatronDocument = (OlePatronDocument) searchResults.get(pos + 1);
256                         nextOlePatronDocument.setPointing(true);
257                     }
258                     if (olePatronDocument.isPatronBillFlag()) {
259                         patronDocument = olePatronDocument;
260                         break;
261                     } else {
262                         break;
263                     }
264 
265                 }
266                 pos++;
267             }
268             if (patronDocument != null) {
269                 return "patronbill";
270             } else {
271                 return null;
272             }
273         }
274         return null;
275 
276     }
277 
278     /**
279      * This method will return the url for create bill link in patron record.
280      *
281      * @return link url
282      */
283 
284     public String getPatronBillUrl(String patronId, String firstName, String lastName) {
285         String url =  OLEConstants.OlePatron.PATRON_CREATE_BILL_URL+ patronId + "&firstName=" + firstName + "&lastName=" + lastName;
286         return url;
287     }
288 
289     public boolean isWildCardMatches(String word, String wildCardString) {
290         if (LOG.isDebugEnabled()) {
291             LOG.debug("Applying WildCard Search");
292         }
293         boolean isSuccess = true;
294         if (wildCardString != null && (!wildCardString.equalsIgnoreCase("")) && wildCardString.contains("*")) {
295             if (wildCardString.equalsIgnoreCase("*")) {
296                 isSuccess = true;
297             } else {
298                 wildCardString = wildCardString.replace('*', ',');
299                 String[] wCardString = wildCardString.split(",");
300                 if (wCardString != null && wCardString.length > 0) {
301                     for (String str : wCardString) {
302                         if (word.toLowerCase().contains(str.toLowerCase())) {
303                             isSuccess = isSuccess && true;
304                         } else {
305                             isSuccess = isSuccess && false;
306                         }
307                     }
308                 } else {
309                     isSuccess = false;
310                     if (word.equalsIgnoreCase(wildCardString)) {
311                         isSuccess = true;
312                     }
313                 }
314             }
315             return isSuccess;
316         } else {
317             if (wildCardString.equalsIgnoreCase(word)) {
318                 return true;
319             } else {
320                 return false;
321             }
322         }
323     }
324 
325     private boolean validatePhoneNumber(String phoneNo) {
326         if (LOG.isDebugEnabled()) {
327             LOG.debug("Validating the Phone Number  Format - ##########, (###)###-#### , ###-###-#### , ### ###-#### , ### ### ####");
328         }
329         if (phoneNo.matches("\\d{10}")) return true;
330         else if (phoneNo.matches("\\d{3}[-]\\d{3}[-]\\d{4}")) return true;
331         else if (phoneNo.matches("\\d{3}[\\s]\\d{3}[-]\\d{4}")) return true;
332         else if (phoneNo.matches("\\d{3}[\\s]\\d{3}[\\s]\\d{4}")) return true;
333         else if (phoneNo.matches("\\(\\d{3}\\)[\\s]\\d{3}[-]\\d{4}")) return true;
334         else return false;
335 
336     }
337     private String buildPhoneNumber(String phoneNumber){
338         StringBuilder userPhoneNumber = new StringBuilder();
339         for (int i = 0; i < phoneNumber.length(); i++) {
340             if (Character.isDigit(phoneNumber.charAt(i))) {
341                 userPhoneNumber.append(phoneNumber.charAt(i));
342             }
343         }
344         return userPhoneNumber.toString();
345     }
346 
347 }