View Javadoc
1   package org.kuali.ole.deliver.controller;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.api.OlePatronDefinition;
7   import org.kuali.ole.deliver.bo.*;
8   import org.kuali.ole.deliver.form.OleMyAccountForm;
9   import org.kuali.ole.deliver.service.OleMyAccountProcess;
10  import org.kuali.ole.service.OlePatronHelperService;
11  import org.kuali.ole.service.OlePatronHelperServiceImpl;
12  import org.kuali.ole.service.OlePatronService;
13  import org.kuali.ole.service.OlePatronServiceImpl;
14  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
15  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
16  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
17  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
18  import org.kuali.rice.krad.service.BusinessObjectService;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krad.web.controller.UifControllerBase;
21  import org.kuali.rice.krad.web.form.UifFormBase;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.validation.BindingResult;
24  import org.springframework.web.bind.annotation.ModelAttribute;
25  import org.springframework.web.bind.annotation.RequestMapping;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * The OleMyAccountController is the controller class for processing all the actions that corresponds to the renewal by patron  functionality in OLE.
37   * The request mapping tag takes care of mapping the individual action to the corresponding functions.
38   */
39  @Controller
40  @RequestMapping(value = "/myaccountcontroller")
41  public class OleMyAccountController extends UifControllerBase {
42  
43      private static final Logger LOG = Logger.getLogger(OleMyAccountController.class);
44  
45      private BusinessObjectService boService;
46      private OleMyAccountProcess oleMyAccountProcess;
47      private OlePatronHelperService olePatronHelperService = new OlePatronHelperServiceImpl();
48  
49      /**
50       * This method creates new oleMyAccountForm form
51       *
52       * @param request
53       * @return oleMyAccountForm
54       */
55      @Override
56      protected OleMyAccountForm createInitialForm(HttpServletRequest request) {
57          return new OleMyAccountForm();
58      }
59  
60      /**
61       * This method converts UifFormBase to oleMyAccountForm
62       *
63       * @param form
64       * @param result
65       * @param request
66       * @param response
67       * @return ModelAndView
68       */
69      @Override
70      @RequestMapping(params = "methodToCall=start")
71      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
72                                HttpServletRequest request, HttpServletResponse response) {
73          OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
74          return super.start(oleMyAccountForm, result, request, response);
75      }
76  
77     /* *//**
78       *  This method performs the my account login
79       * @param form
80       * @param result
81       * @param request
82       * @param response
83       * @return ModelAndView
84       *//*
85  
86      @RequestMapping(params = "methodToCall=logIn")
87      public ModelAndView logIn(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
88                                HttpServletRequest request, HttpServletResponse response) {
89          OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
90          String patrinId=oleMyAccountForm.getPatronId();
91          String password=oleMyAccountForm.getPatronPassword();
92          oleMyAccountForm.setViewId("RenewalItemView");
93          oleMyAccountForm.setMethodToCall("start");
94          oleMyAccountForm.setValidPatronFlag(true);
95          return this.start(oleMyAccountForm, result, request, response);
96  
97  
98      }*/
99  
100 
101     /**
102      * This method displays information about a patron in UI.
103      *
104      * @param form
105      * @param result
106      * @param request
107      * @param response
108      * @return ModelAndView
109      */
110     @RequestMapping(params = "methodToCall=searchPatron")
111     public ModelAndView searchPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
112                                      HttpServletRequest request, HttpServletResponse response) {
113         LOG.debug("Inside the searchPatron method");
114         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
115         oleMyAccountForm.setExistingLoanList(new ArrayList<OleRenewalLoanDocument>());
116         oleMyAccountForm.setInformation("");
117         oleMyAccountForm.setMessage("");
118         try {
119             OlePatronDefinition olePatronDefinition = getOleMyAccountProcess().getPatronInfo(oleMyAccountForm.getPatronBarcode());
120             oleMyAccountForm.setBorrowerType(olePatronDefinition.getOleBorrowerType().getBorrowerTypeName());
121             oleMyAccountForm.setPatronName(olePatronDefinition.getEntity().getNames().get(0).getFirstName());
122             oleMyAccountForm.setPatronId(olePatronDefinition.getOlePatronId());
123             oleMyAccountForm.setBorrowerTypeId(olePatronDefinition.getOleBorrowerType().getBorrowerTypeId());
124             oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(olePatronDefinition.getOlePatronId()));
125         } catch (Exception e) {
126             oleMyAccountForm.setInformation(e.getMessage());
127             LOG.error("Exception", e);
128         }
129         if (oleMyAccountForm.getExistingLoanList().size() == 0) {
130             oleMyAccountForm.setInformation("No item currently checked out.");
131             oleMyAccountForm.setPatronName("");
132         }
133         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
134     }
135 
136     /**
137      * This method search Patron document and display in the screen.
138      *
139      * @param form
140      * @param result
141      * @param request
142      * @param response
143      * @return ModelAndView
144      */
145     @RequestMapping(params = "methodToCall=myAccountPatronSearch")
146     public ModelAndView myAccountPatronSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
147                                               HttpServletRequest request, HttpServletResponse response) {
148         LOG.debug("Inside myAccountPatronSearch");
149         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
150         oleMyAccountForm.setExistingLoanList(new ArrayList<OleRenewalLoanDocument>());
151         oleMyAccountForm.setInformation("");
152         try {
153             OlePatronDefinition olePatronDefinition = getOleMyAccountProcess().getPatronInfo(oleMyAccountForm.getPatronBarcode());
154             if (olePatronDefinition == null) {
155                 oleMyAccountForm.setMessage("");
156             }
157             OlePatronDocument olePatronDocument = new OlePatronDocument();
158             olePatronDocument = olePatronDocument.from(olePatronDefinition);
159             List<EntityAddressBo> entityAddressBos = olePatronDocument.getAddresses();
160             List<OleEntityAddressBo> oleEntityAddressBos = new ArrayList<OleEntityAddressBo>();
161             OleEntityAddressBo oleEntityAddressBo = new OleEntityAddressBo();
162             Map<String, String> criteriaMap;
163             if (entityAddressBos.size() > 0) {
164                 for (EntityAddressBo entityAddressBo : entityAddressBos) {
165                     oleEntityAddressBo = new OleEntityAddressBo();
166                     criteriaMap = new HashMap<String, String>();
167                     criteriaMap.put(OLEConstants.OlePatron.ENTITY_ADDRESS_ID, entityAddressBo.getId());
168                     List<OleAddressBo> oleAddressBos = (List<OleAddressBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class, criteriaMap);
169                     if (oleAddressBos.size() > 0) {
170                         oleEntityAddressBo.setOleAddressBo(oleAddressBos.get(0));
171                     }
172                     oleEntityAddressBo.setEntityAddressBo(entityAddressBo);
173                     oleEntityAddressBos.add(oleEntityAddressBo);
174                 }
175             }
176             olePatronDocument.setOleEntityAddressBo(oleEntityAddressBos);
177             String patronId = "";
178             List<OleProxyPatronDocument> oleProxyPatronDocuments = olePatronDocument.getOleProxyPatronDocuments();
179             if (oleProxyPatronDocuments.size() > 0) {
180                 for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocuments) {
181                     patronId = oleProxyPatronDocument.getProxyPatronId();
182                     OlePatronDefinition patronDefinition = getOleMyAccountProcess().getPatronInfo(patronId);
183                     oleProxyPatronDocument.setProxyPatronFirstName(patronDefinition.getName().getFirstName());
184                     oleProxyPatronDocument.setProxyPatronLastName(patronDefinition.getName().getLastName());
185                     oleProxyPatronDocument.setProxyPatronBarcode(patronDefinition.getBarcode());
186 
187                 }
188             }
189             oleMyAccountForm.setOlePatronDocument(olePatronDocument);
190             oleMyAccountForm.setBarcode(olePatronDocument.getBarcode());
191             //setEmails(oleMyAccountForm,olePatronDocument);
192             searchPatron(oleMyAccountForm, result, request, response);
193         } catch (Exception e) {
194             oleMyAccountForm.setInformation(e.getMessage());
195             LOG.error("Exception", e);
196         }
197 
198         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
199     }
200 
201     /**
202      * This method performs the renewal item
203      *
204      * @param form
205      * @param result
206      * @param request
207      * @param response
208      * @return ModelAndView
209      */
210     @RequestMapping(params = "methodToCall=renewalItem")
211     public ModelAndView renewalItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
212                                     HttpServletRequest request, HttpServletResponse response) {
213         LOG.debug("Inside renewalItem method");
214         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
215         oleMyAccountForm.setInformation("");
216         String renewalItemId = oleMyAccountForm.getItem();
217         OleRenewalLoanDocument oleRenewalLoanDocument = null;
218         boolean renewalFlag = false;
219         for (int i = 0; i < oleMyAccountForm.getExistingLoanList().size(); i++) {
220             oleRenewalLoanDocument = oleMyAccountForm.getExistingLoanList().get(i);
221             if (oleRenewalLoanDocument.getItemBarcode().equals(renewalItemId)) {
222                 renewalFlag = true;
223                 break;
224             }
225         }
226         if (renewalFlag) {
227             List<OleRenewalLoanDocument> oleRenewalLoanDocumentList = new ArrayList<OleRenewalLoanDocument>();
228             oleRenewalLoanDocumentList.add(oleRenewalLoanDocument);
229             oleRenewalLoanDocumentList = getOleMyAccountProcess().performRenewalItem(oleRenewalLoanDocumentList);
230             oleRenewalLoanDocument = oleRenewalLoanDocumentList.get(0);
231             oleMyAccountForm.setInformation(oleRenewalLoanDocument.getMessageInfo());
232 
233         } else
234             oleMyAccountForm.setInformation("Select an item(s).");
235 
236         oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(oleMyAccountForm.getPatronId()));
237         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
238     }
239 
240     /**
241      * This method performs the selected  renewal items operation
242      *
243      * @param form
244      * @param result
245      * @param request
246      * @param response
247      * @return ModelAndView
248      */
249     @RequestMapping(params = "methodToCall=renewalItems")
250     public ModelAndView renewalItems(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
251                                      HttpServletRequest request, HttpServletResponse response) {
252         LOG.debug("Inside renewalItems method");
253         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
254         oleMyAccountForm.setInformation("");
255         boolean renewalFlag = false;
256         List<OleRenewalLoanDocument> oleRenewalLoanDocumentList = new ArrayList<OleRenewalLoanDocument>();
257         for (int i = 0; i < oleMyAccountForm.getExistingLoanList().size(); i++) {
258             OleRenewalLoanDocument oleRenewalLoanDocument = oleMyAccountForm.getExistingLoanList().get(i);
259             if (oleRenewalLoanDocument.isItemCheckFlag()) {
260                 renewalFlag = true;
261                 oleRenewalLoanDocumentList.add(oleRenewalLoanDocument);
262             }
263         }
264         if (renewalFlag) {
265             oleRenewalLoanDocumentList = getOleMyAccountProcess().performRenewalItem(oleRenewalLoanDocumentList);
266             for (int i = 0; i < oleRenewalLoanDocumentList.size(); i++) {
267                 String errMsg = oleRenewalLoanDocumentList.get(i).getMessageInfo();
268                 oleMyAccountForm.setInformation(oleMyAccountForm.getInformation() == null ? "" : oleMyAccountForm.getInformation() + "\n" + (i + 1) + ". " + errMsg + "  (" + oleRenewalLoanDocumentList.get(i).getItemBarcode() + ")<br/>");
269 
270             }
271 
272         } else
273             oleMyAccountForm.setInformation("Select an item(s).");
274         oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(oleMyAccountForm.getPatronId()));
275 
276         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
277     }
278 
279     /**
280      * This method clear UI for next borrower session..
281      *
282      * @param form
283      * @param result
284      * @param request
285      * @param response
286      * @return ModelAndView
287      */
288     @RequestMapping(params = "methodToCall=saveAndClear")
289     public ModelAndView clearPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
290                                     HttpServletRequest request, HttpServletResponse response) {
291         LOG.debug("Inside clearPatron method");
292         OleMyAccountForm oleLoanForm = (OleMyAccountForm) form;
293         oleLoanForm.setInformation("");
294         oleLoanForm.setPatronBarcode(null);
295         oleLoanForm.setPatronName(null);
296         oleLoanForm.setExistingLoanList(null);
297 
298         return getUIFModelAndView(oleLoanForm, "RenewalItemViewPage");
299     }
300 
301 
302     public OleMyAccountProcess getOleMyAccountProcess() {
303 
304         if (oleMyAccountProcess == null)
305             oleMyAccountProcess = new OleMyAccountProcess();
306         return oleMyAccountProcess;
307     }
308 
309     /**
310      * This method will save or update the patron.document from the myaccount screen.
311      *
312      * @param form
313      * @param result
314      * @param request
315      * @param response
316      * @return ModelAndView
317      */
318     @RequestMapping(params = "methodToCall=savePatron")
319     public ModelAndView savePatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
320                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
321         LOG.debug("Inside savePatron method");
322         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
323         OlePatronService olePatronService = new OlePatronServiceImpl();
324         OlePatronDocument olePatronDocument = oleMyAccountForm.getOlePatronDocument();
325         EntityBo entity = olePatronDocument.getEntity();
326         List<EntityAddressBo> entityAddressBos = new ArrayList<EntityAddressBo>();
327         EntityAddressBo entityAddressBo = new EntityAddressBo();
328         List<OleAddressBo> oleAddressBos = new ArrayList<OleAddressBo>();
329         OleAddressBo oleAddressBo = new OleAddressBo();
330         List<OleEntityAddressBo> oleEntityAddressBos = olePatronDocument.getOleEntityAddressBo();
331         if (oleEntityAddressBos.size() > 0) {
332             for (OleEntityAddressBo oleEntityAddressBo : oleEntityAddressBos) {
333                 oleAddressBo = oleEntityAddressBo.getOleAddressBo();
334                 oleAddressBos.add(oleAddressBo);
335                 entityAddressBo = oleEntityAddressBo.getEntityAddressBo();
336                 entityAddressBos.add(entityAddressBo);
337             }
338             olePatronDocument.setOleAddresses(oleAddressBos);
339             olePatronDocument.setAddresses(entityAddressBos);
340         }
341         List<EntityPhoneBo> entityPhoneBos = new ArrayList<>();
342         EntityPhoneBo entityPhoneBo = new EntityPhoneBo();
343         List<OlePhoneBo> olePhoneBos = new ArrayList<>();
344         OlePhoneBo olePhoneBo = new OlePhoneBo();
345         List<OleEntityPhoneBo> oleEntityPhoneBos = olePatronDocument.getOleEntityPhoneBo();
346         if(CollectionUtils.isNotEmpty(oleEntityPhoneBos)) {
347             for(OleEntityPhoneBo oleEntityPhoneBo : oleEntityPhoneBos) {
348                 olePhoneBo = oleEntityPhoneBo.getOlePhoneBo();
349                 olePhoneBos.add(olePhoneBo);
350                 entityPhoneBo = oleEntityPhoneBo.getEntityPhoneBo();
351                 entityPhoneBos.add(entityPhoneBo);
352             }
353             olePatronDocument.setOlePhones(olePhoneBos);
354             olePatronDocument.setPhones(entityPhoneBos);
355         }
356         List<EntityEmailBo> entityEmailBos = new ArrayList<>();
357         EntityEmailBo entityEmailBo = new EntityEmailBo();
358         List<OleEmailBo> oleEmailBos = new ArrayList<>();
359         OleEmailBo oleEmailBo = new OleEmailBo();
360         List<OleEntityEmailBo> oleEntityEmailBos = olePatronDocument.getOleEntityEmailBo();
361         if(CollectionUtils.isNotEmpty(oleEntityEmailBos)) {
362             for(OleEntityEmailBo oleEntityEmailBo : oleEntityEmailBos) {
363                 oleEmailBo = oleEntityEmailBo.getOleEmailBo();
364                 oleEmailBos.add(oleEmailBo);
365                 entityEmailBo = oleEntityEmailBo.getEntityEmailBo();
366                 entityEmailBos.add(entityEmailBo);
367             }
368             olePatronDocument.setOleEmails(oleEmailBos);
369             olePatronDocument.setEmails(entityEmailBos);
370         }
371         boolean addressSource = olePatronHelperService.checkAddressSource(olePatronDocument.getOleAddresses());
372         boolean phoneSource = olePatronHelperService.checkPhoneSource(olePatronDocument.getOlePhones());
373         boolean emailSource = olePatronHelperService.checkEmailSource(olePatronDocument.getOleEmails());
374         boolean emailDefault = olePatronHelperService.checkEmailMultipleDefault(oleEntityEmailBos);
375         boolean phoneDefault = olePatronHelperService.checkPhoneMultipleDefault(oleEntityPhoneBos);
376         boolean addressDefault = olePatronHelperService.checkAddressMultipleDefault(oleEntityAddressBos);
377         if (addressSource && phoneSource && emailSource) {
378             if (emailDefault && phoneDefault && addressDefault) {
379                 OlePatronDefinition olePatronDefinition = olePatronService.updatePatron(OlePatronDocument.to(oleMyAccountForm.getOlePatronDocument()));
380                 cancel(oleMyAccountForm, result, request, response);
381             } else {
382                 oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_DEFAULT_MESSAGE);
383                 return getUIFModelAndView(oleMyAccountForm);
384             }
385         } else {
386             if(phoneSource) {
387                 oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_PHONE_SOURCE_REQUIRED);
388                 return getUIFModelAndView(oleMyAccountForm);
389             } else if(emailSource){
390                 oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_EMAIL_SOURCE_REQUIRED);
391                 return getUIFModelAndView(oleMyAccountForm);
392             } else {
393                 oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_ADDRESS_SOURCE_REQUIRED);
394                 return getUIFModelAndView(oleMyAccountForm);
395             }
396         }
397         oleMyAccountForm.setMessage(OLEConstants.OlePatron.SAVE_SUCCESSFUL_MSG);
398         //updatePatron(olePatronForm,olePatronDefinition);
399         //setProxyPatrons(olePatronForm, olePatronDefinition);
400         return getUIFModelAndView(oleMyAccountForm);
401     }
402 
403     /**
404      * Just returns as if return with no value was selected.
405      */
406     @Override
407     @RequestMapping(params = "methodToCall=cancel")
408     public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
409                                HttpServletRequest request, HttpServletResponse response) {
410         LOG.debug("Inside the cancel method");
411         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
412         oleMyAccountForm.setBarcode(null);
413         //oleMyAccountForm.setMessage();
414         return getUIFModelAndView(oleMyAccountForm);
415     }
416 
417     /**
418      * Just returns as if return with no value was selected.
419      */
420     @Override
421     @RequestMapping(params = "methodToCall=back")
422     public ModelAndView back(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
423                               HttpServletRequest request, HttpServletResponse response) {
424         return super.back(form, result, request, response);
425     }
426 
427 }