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
37
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
50
51
52
53 @Override
54 protected OleMyAccountForm createInitialForm(HttpServletRequest request) {
55 return new OleMyAccountForm();
56 }
57
58
59
60
61
62
63
64
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
76
77
78
79
80
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
101
102
103
104
105
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
133
134
135
136
137
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
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
195
196
197
198
199
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
233
234
235
236
237
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
271
272
273
274
275
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
298
299
300
301
302
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
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
343
344 return getUIFModelAndView(oleMyAccountForm);
345 }
346
347
348
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
357 return getUIFModelAndView(oleMyAccountForm);
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
383
384
385
386
387
388
389
390
391
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
412
413
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
435
436
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
457
458
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 }