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