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