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
36
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
50
51
52
53
54 @Override
55 protected OleMyAccountForm createInitialForm(HttpServletRequest request) {
56 return new OleMyAccountForm();
57 }
58
59
60
61
62
63
64
65
66
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
137
138
139
140
141
142
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
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
202
203
204
205
206
207
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
241
242
243
244
245
246
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
280
281
282
283
284
285
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
310
311
312
313
314
315
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
360
361 return getUIFModelAndView(oleMyAccountForm);
362 }
363
364
365
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
375 return getUIFModelAndView(oleMyAccountForm);
376 }
377
378
379
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 }