View Javadoc

1   package org.kuali.ole.myaccount.renewal.controller;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.myaccount.renewal.bo.OleRenewalLoanDocument;
6   import org.kuali.ole.myaccount.renewal.form.OleMyAccountForm;
7   import org.kuali.ole.myaccount.renewal.service.OleMyAccountProcess;
8   import org.kuali.ole.patron.api.OlePatronDefinition;
9   import org.kuali.ole.patron.bo.OleAddressBo;
10  import org.kuali.ole.patron.bo.OleEntityAddressBo;
11  import org.kuali.ole.patron.bo.OlePatronDocument;
12  import org.kuali.ole.patron.bo.OleProxyPatronDocument;
13  import org.kuali.ole.service.OlePatronService;
14  import org.kuali.ole.service.OlePatronServiceImpl;
15  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
16  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
17  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
18  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
19  import org.kuali.rice.krad.service.BusinessObjectService;
20  import org.kuali.rice.krad.service.KRADServiceLocator;
21  import org.kuali.rice.krad.web.controller.UifControllerBase;
22  import org.kuali.rice.krad.web.form.UifFormBase;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.validation.BindingResult;
25  import org.springframework.web.bind.annotation.ModelAttribute;
26  import org.springframework.web.bind.annotation.RequestMapping;
27  import org.springframework.web.servlet.ModelAndView;
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  
48      /**
49       *  This method creates new oleMyAccountForm form
50       * @param request
51       * @return  oleMyAccountForm
52       */
53      @Override
54      protected OleMyAccountForm createInitialForm(HttpServletRequest request) {
55          return new OleMyAccountForm();
56      }
57  
58      /**
59       *  This method converts UifFormBase to oleMyAccountForm
60       * @param form
61       * @param result
62       * @param request
63       * @param response
64       * @return ModelAndView
65       */
66      @Override
67      @RequestMapping(params = "methodToCall=start")
68      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
69                                HttpServletRequest request, HttpServletResponse response) {
70          OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
71          return super.start(oleMyAccountForm, result, request, response);
72      }
73  
74     /* *//**
75       *  This method performs the my account login
76       * @param form
77       * @param result
78       * @param request
79       * @param response
80       * @return ModelAndView
81       *//*
82  
83      @RequestMapping(params = "methodToCall=logIn")
84      public ModelAndView logIn(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
85                                HttpServletRequest request, HttpServletResponse response) {
86          OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
87          String patrinId=oleMyAccountForm.getPatronId();
88          String password=oleMyAccountForm.getPatronPassword();
89          oleMyAccountForm.setViewId("RenewalItemView");
90          oleMyAccountForm.setMethodToCall("start");
91          oleMyAccountForm.setValidPatronFlag(true);
92          return this.start(oleMyAccountForm, result, request, response);
93  
94  
95      }*/
96  
97  
98  
99      /**
100      *   This method displays information about a patron in UI.
101      * @param form
102      * @param result
103      * @param request
104      * @param response
105      * @return ModelAndView
106      */
107     @RequestMapping(params = "methodToCall=searchPatron")
108     public ModelAndView searchPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
109                                      HttpServletRequest request, HttpServletResponse response) {
110         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
111         oleMyAccountForm.setExistingLoanList(new ArrayList<OleRenewalLoanDocument>());
112         oleMyAccountForm.setInformation("");
113         oleMyAccountForm.setMessage("");
114         try{
115             OlePatronDefinition olePatronDefinition = getOleMyAccountProcess().getPatronInfo(oleMyAccountForm.getPatronBarcode());
116             oleMyAccountForm.setBorrowerType(olePatronDefinition.getOleBorrowerType().getBorrowerTypeName());
117             oleMyAccountForm.setPatronName(olePatronDefinition.getEntity().getNames().get(0).getFirstName());
118             oleMyAccountForm.setPatronId(olePatronDefinition.getOlePatronId());
119             oleMyAccountForm.setBorrowerTypeId(olePatronDefinition.getOleBorrowerType().getBorrowerTypeId());
120             oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(olePatronDefinition.getOlePatronId()));
121         }catch (Exception e){
122             oleMyAccountForm.setInformation(e.getMessage());
123         }
124         if(oleMyAccountForm.getExistingLoanList().size()==0)  {
125             oleMyAccountForm.setInformation("No items in the Previous Session Item(s).");
126             oleMyAccountForm.setPatronName("");
127         }
128         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
129     }
130 
131     /**
132      *   This method search Patron document and display in the screen.
133      * @param form
134      * @param result
135      * @param request
136      * @param response
137      * @return ModelAndView
138      */
139     @RequestMapping(params = "methodToCall=myAccountPatronSearch")
140     public ModelAndView myAccountPatronSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
141                                      HttpServletRequest request, HttpServletResponse response) {
142         LOG.info("Inside myAccountPatronSearch");
143         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
144         oleMyAccountForm.setExistingLoanList(new ArrayList<OleRenewalLoanDocument>());
145         oleMyAccountForm.setInformation("");
146         try{
147             OlePatronDefinition olePatronDefinition = getOleMyAccountProcess().getPatronInfo(oleMyAccountForm.getPatronBarcode());
148             if ( olePatronDefinition == null ) {
149                 oleMyAccountForm.setMessage("");
150             }
151             OlePatronDocument olePatronDocument = new OlePatronDocument();
152             olePatronDocument = olePatronDocument.from(olePatronDefinition);
153             List<EntityAddressBo> entityAddressBos = olePatronDocument.getAddresses();
154             List<OleEntityAddressBo> oleEntityAddressBos = new ArrayList<OleEntityAddressBo>();
155             OleEntityAddressBo oleEntityAddressBo = new OleEntityAddressBo();
156             Map<String,String> criteriaMap;
157             if(entityAddressBos.size() > 0) {
158                 for(EntityAddressBo entityAddressBo : entityAddressBos) {
159                     oleEntityAddressBo = new OleEntityAddressBo();
160                 criteriaMap = new HashMap<String, String>();
161                 criteriaMap.put(OLEConstants.OlePatron.ENTITY_ADDRESS_ID,entityAddressBo.getId());
162                 List<OleAddressBo> oleAddressBos = (List<OleAddressBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class,criteriaMap);
163                 if(oleAddressBos.size() > 0) {
164                     oleEntityAddressBo.setOleAddressBo(oleAddressBos.get(0));
165                 }
166                     oleEntityAddressBo.setEntityAddressBo(entityAddressBo);
167                     oleEntityAddressBos.add(oleEntityAddressBo);
168                 }
169             }
170             olePatronDocument.setOleEntityAddressBo(oleEntityAddressBos);
171             String patronId = "";
172             List<OleProxyPatronDocument> oleProxyPatronDocuments = olePatronDocument.getOleProxyPatronDocuments();
173             if(oleProxyPatronDocuments.size() > 0) {
174                for(OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocuments) {
175                    patronId = oleProxyPatronDocument.getProxyPatronId();
176                    OlePatronDefinition patronDefinition = getOleMyAccountProcess().getPatronInfo(patronId);
177                    oleProxyPatronDocument.setProxyFirstName(patronDefinition.getName().getFirstName());
178                    oleProxyPatronDocument.setProxyLastName(patronDefinition.getName().getLastName());
179                    oleProxyPatronDocument.setProxyBarcode(patronDefinition.getBarcode());
180 
181                }
182             }
183             oleMyAccountForm.setOlePatronDocument(olePatronDocument);
184             oleMyAccountForm.setBarcode(olePatronDocument.getBarcode());
185             //setEmails(oleMyAccountForm,olePatronDocument);
186             searchPatron(oleMyAccountForm,result,request,response);
187         }catch (Exception e){
188             oleMyAccountForm.setInformation(e.getMessage());
189         }
190 
191         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
192     }
193     /**
194      *   This method performs the renewal item
195      * @param form
196      * @param result
197      * @param request
198      * @param response
199      * @return ModelAndView
200      */
201     @RequestMapping(params = "methodToCall=renewalItem")
202     public ModelAndView renewalItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
203                                      HttpServletRequest request, HttpServletResponse response) {
204         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
205         oleMyAccountForm.setInformation("");
206         String renewalItemId=oleMyAccountForm.getItem();
207         OleRenewalLoanDocument oleRenewalLoanDocument=null;
208         boolean renewalFlag=false;
209         for(int i=0;i<oleMyAccountForm.getExistingLoanList().size();i++){
210             oleRenewalLoanDocument= oleMyAccountForm.getExistingLoanList().get(i);
211             if(oleRenewalLoanDocument.getItemBarcode().equals(renewalItemId)){
212                 renewalFlag=true;
213                 break;
214             }
215         }
216         if(renewalFlag){
217             List<OleRenewalLoanDocument> oleRenewalLoanDocumentList=new ArrayList<OleRenewalLoanDocument>();
218             oleRenewalLoanDocumentList.add(oleRenewalLoanDocument);
219             oleRenewalLoanDocumentList=getOleMyAccountProcess().performRenewalItem(oleRenewalLoanDocumentList);
220             oleRenewalLoanDocument=oleRenewalLoanDocumentList.get(0);
221             oleMyAccountForm.setInformation("1. "+oleRenewalLoanDocument.getMessageInfo());
222 
223         }
224         else
225            oleMyAccountForm.setInformation("No selected items from previous session.");
226 
227         oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(oleMyAccountForm.getPatronId()));
228         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
229     }
230 
231     /**
232      *   This method performs the selected  renewal items operation
233      * @param form
234      * @param result
235      * @param request
236      * @param response
237      * @return ModelAndView
238      */
239     @RequestMapping(params = "methodToCall=renewalItems")
240     public ModelAndView renewalItems(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
241                                     HttpServletRequest request, HttpServletResponse response) {
242         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
243         oleMyAccountForm.setInformation("");
244         boolean renewalFlag=false;
245         List<OleRenewalLoanDocument> oleRenewalLoanDocumentList=new ArrayList<OleRenewalLoanDocument>();
246         for(int i=0;i<oleMyAccountForm.getExistingLoanList().size();i++){
247             OleRenewalLoanDocument oleRenewalLoanDocument= oleMyAccountForm.getExistingLoanList().get(i);
248             if(oleRenewalLoanDocument.isItemCheckFlag()){
249                 renewalFlag=true;
250                 oleRenewalLoanDocumentList.add(oleRenewalLoanDocument);
251             }
252         }
253         if(renewalFlag){
254             oleRenewalLoanDocumentList=getOleMyAccountProcess().performRenewalItem(oleRenewalLoanDocumentList);
255             for(int i=0;i<oleRenewalLoanDocumentList.size();i++){
256                String errMsg=oleRenewalLoanDocumentList.get(i).getMessageInfo();
257                oleMyAccountForm.setInformation(oleMyAccountForm.getInformation()==null?"":oleMyAccountForm.getInformation()+"\n"+(i+1)+". "+errMsg+"  ("+oleRenewalLoanDocumentList.get(i).getItemBarcode()+")<br/>");
258 
259             }
260 
261         }
262         else
263             oleMyAccountForm.setInformation("No selected items from previous session.");
264         oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(oleMyAccountForm.getPatronId()));
265 
266         return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
267     }
268 
269     /**
270      *  This method clear UI for next borrower session..
271      * @param form
272      * @param result
273      * @param request
274      * @param response
275      * @return  ModelAndView
276      */
277     @RequestMapping(params = "methodToCall=saveAndClear")
278     public ModelAndView clearPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
279                                     HttpServletRequest request, HttpServletResponse response) {
280         OleMyAccountForm oleLoanForm = (OleMyAccountForm) form;
281         oleLoanForm.setInformation("");
282         oleLoanForm.setPatronBarcode(null);
283         oleLoanForm.setPatronName(null);
284         oleLoanForm.setExistingLoanList(null);
285 
286         return getUIFModelAndView(oleLoanForm, "RenewalItemViewPage");
287     }
288 
289 
290     public OleMyAccountProcess getOleMyAccountProcess(){
291 
292         if(oleMyAccountProcess==null)
293             oleMyAccountProcess=new OleMyAccountProcess();
294         return oleMyAccountProcess;
295     }
296     /**
297      *  This method will save or update the patron.document from the myaccount screen.
298      * @param form
299      * @param result
300      * @param request
301      * @param response
302      * @return  ModelAndView
303      */
304     @RequestMapping(params = "methodToCall=savePatron")
305     public ModelAndView savePatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
306                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
307         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
308         OlePatronService olePatronService = new OlePatronServiceImpl();
309         //OlePatronAccountService olePatronAccountService = new OlePatronAccountServiceImpl();
310         OlePatronDocument olePatronDocument = oleMyAccountForm.getOlePatronDocument();
311         EntityBo entity = olePatronDocument.getEntity();
312         List<EntityEmailBo> entityEmailBos = entity.getEntityTypeContactInfos().get(0).getEmailAddresses();
313         List<EntityPhoneBo> entityPhoneBos = entity.getEntityTypeContactInfos().get(0).getPhoneNumbers();
314         List<EntityAddressBo> entityAddressBos = new ArrayList<EntityAddressBo>();
315         EntityAddressBo entityAddressBo = new EntityAddressBo();
316         List<OleAddressBo> oleAddressBos = new ArrayList<OleAddressBo>();
317         OleAddressBo oleAddressBo = new OleAddressBo();
318         List<OleEntityAddressBo> oleEntityAddressBos = olePatronDocument.getOleEntityAddressBo();
319         if(oleEntityAddressBos.size() > 0) {
320             for(OleEntityAddressBo oleEntityAddressBo : oleEntityAddressBos) {
321                 oleAddressBo = oleEntityAddressBo.getOleAddressBo();
322                 oleAddressBos.add(oleAddressBo);
323                 entityAddressBo = oleEntityAddressBo.getEntityAddressBo();
324                 entityAddressBos.add(entityAddressBo);
325             }
326             olePatronDocument.setOleAddresses(oleAddressBos);
327             olePatronDocument.setAddresses(entityAddressBos);
328         }
329         if((checkAddressSource(olePatronDocument.getOleAddresses()))) {
330             if(checkEmailMultipleDefault(entityEmailBos) && checkPhoneMultipleDefault(entityPhoneBos) && checkAddressMultipleDefault(oleEntityAddressBos)) {
331                 OlePatronDefinition olePatronDefinition = olePatronService.updatePatron(OlePatronDocument.to(oleMyAccountForm.getOlePatronDocument()));
332                 cancel(oleMyAccountForm, result, request , response);
333             } else {
334                 oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_DEFAULT_MESSAGE);
335                 return getUIFModelAndView(oleMyAccountForm);
336             }
337         } else {
338                 oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_ADDRESS_SOURCE_REQUIRED);
339                 return getUIFModelAndView(oleMyAccountForm);
340         }
341         oleMyAccountForm.setMessage(OLEConstants.OlePatron.SAVE_SUCCESSFUL_MSG);
342         //updatePatron(olePatronForm,olePatronDefinition);
343         //setProxyPatrons(olePatronForm, olePatronDefinition);
344         return getUIFModelAndView(oleMyAccountForm);
345     }
346 
347     /**
348      * Just returns as if return with no value was selected.
349      */
350     @Override
351     @RequestMapping(params = "methodToCall=cancel")
352     public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
353                                HttpServletRequest request, HttpServletResponse response) {
354         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
355         oleMyAccountForm.setBarcode(null);
356         //oleMyAccountForm.setMessage();
357         return getUIFModelAndView(oleMyAccountForm);
358     }
359    /* @RequestMapping(params = "methodToCall=submit")
360     public ModelAndView submit(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
361                                HttpServletRequest request, HttpServletResponse response) throws Exception {
362         OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
363         OlePatronService olePatronService = new OlePatronServiceImpl();
364         //OlePatronAccountService olePatronAccountService = new OlePatronAccountServiceImpl();
365         OlePatronDefinition olePatronDefinition = olePatronService.getPatron(oleMyAccountForm.getPatronBarcode());
366         OlePatronDocument olePatronDocument = new OlePatronDocument();
367         updatePatron(oleMyAccountForm,olePatronDefinition);
368         //setProxyPatrons(olePatronForm, olePatronDefinition);
369         return getUIFModelAndView(oleMyAccountForm);
370     }
371 */
372     /**
373      * Just returns as if return with no value was selected.
374      */
375     @Override
376     @RequestMapping(params = "methodToCall=close")
377     public ModelAndView close(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
378                                HttpServletRequest request, HttpServletResponse response) {
379         return super.close(form, result, request, response);
380     }
381 
382     /*public void updatePatron(OleMyAccountForm oleMyAccountForm ,OlePatronDefinition olePatronDefinition) {
383 
384         OlePatronDocument olePatronDocument = new OlePatronDocument();
385         olePatronDefinition = olePatronDocument.to(olePatronDocument);
386     }
387 */
388     /**
389      * This method is for checking the entity phone has multiple defaults
390      * @param phoneBoList
391      * @return true , if the entity phone has only one default in a single patron record, else false
392      */
393     protected boolean checkPhoneMultipleDefault (List<EntityPhoneBo> phoneBoList) {
394 
395         boolean valid = true;
396         int defaultCounter = 0;
397         for(EntityPhoneBo entityPhoneBo:phoneBoList){
398             if(entityPhoneBo.isDefaultValue()){
399                 defaultCounter++;
400             } else {
401                 defaultCounter--;
402             }
403         }
404         if(defaultCounter > 1 || defaultCounter < 0){
405             valid = false;
406         }
407         return valid;
408     }
409 
410     /**
411      * This method is for checking the entity address has multiple defaults
412      * @param addrBoList
413      * @return true , if the entity address has only one default in a single patron record, else false
414      */
415     protected boolean checkAddressMultipleDefault (List<OleEntityAddressBo> addrBoList) {
416 
417         boolean valid = true;
418         int defaultCounter = 0;
419         for(OleEntityAddressBo oleEntityAddressBo:addrBoList){
420             EntityAddressBo entityAddressBo = oleEntityAddressBo.getEntityAddressBo();
421             if(entityAddressBo.isDefaultValue()){
422                 defaultCounter++;
423             } else {
424                 defaultCounter--;
425             }
426         }
427         if(defaultCounter > 1 || defaultCounter < 0){
428             valid = false;
429         }
430         return valid;
431     }
432 
433     /**
434      * This method is for checking the entity email address has multiple defaults
435      * @param emailBoList
436      * @return true , if the entity email address has only one default in a single patron record, else false
437      */
438     protected boolean checkEmailMultipleDefault (List<EntityEmailBo> emailBoList) {
439 
440         boolean valid = true;
441         int defaultCounter = 0;
442         for(EntityEmailBo entityEmailBo:emailBoList){
443             if(entityEmailBo.isDefaultValue()){
444                 defaultCounter++;
445             }else {
446                 defaultCounter--;
447             }
448         }
449         if(defaultCounter > 1 || defaultCounter < 0){
450             valid = false;
451         }
452         return valid;
453     }
454 
455     /**
456      * This method is for checking the added address source or not
457      * @param oleAddresses
458      * @return true , if the address source has value in a single patron record, else false
459      */
460     protected boolean checkAddressSource (List<OleAddressBo> oleAddresses) {
461 
462         boolean valid = true;
463         for(OleAddressBo oleAddress : oleAddresses){
464             if((oleAddress.getAddressSource() != null) && (oleAddress.getAddressSource().equals(""))) {
465                valid = false;
466             }
467         }
468         return valid;
469     }
470 }