1 package org.kuali.ole.loan.controller;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.loan.LoanProcessor;
6 import org.kuali.ole.loan.bo.OleLoanDocument;
7 import org.kuali.ole.loan.form.OleLoanForm;
8 import org.kuali.rice.krad.service.BusinessObjectService;
9 import org.kuali.rice.krad.web.controller.UifControllerBase;
10 import org.kuali.rice.krad.web.form.UifFormBase;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.validation.BindingResult;
13 import org.springframework.web.bind.annotation.ModelAttribute;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.servlet.ModelAndView;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import java.sql.Timestamp;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
24
25
26
27
28
29
30
31
32 @Controller
33 @RequestMapping(value = "/loancontroller")
34 public class LoanController extends UifControllerBase {
35
36 private static final Logger LOG = Logger.getLogger(LoanController.class);
37
38 private BusinessObjectService boService;
39
40 @Override
41 protected OleLoanForm createInitialForm(HttpServletRequest request) {
42 return new OleLoanForm();
43 }
44
45 @Override
46 @RequestMapping(params = "methodToCall=start")
47 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
48 HttpServletRequest request, HttpServletResponse response) {
49 OleLoanForm oleLoanForm = (OleLoanForm) form;
50 return super.start(oleLoanForm, result, request, response);
51 }
52
53 @RequestMapping(params = "methodToCall=searchPatron")
54 public ModelAndView searchPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
55 HttpServletRequest request, HttpServletResponse response) {
56 OleLoanForm oleLoanForm = (OleLoanForm) form;
57 try{
58 OleLoanDocument oleLoanDocument = getLoanProcessor().getPatronDetails(oleLoanForm.getPatronBarcode());
59 oleLoanForm.setBorrowerType(oleLoanDocument.getBorrowerTypeName());
60 oleLoanForm.setPatronName(oleLoanDocument.getPatronName());
61 oleLoanForm.setPatronId(oleLoanDocument.getPatronId());
62 oleLoanForm.setBorrowerTypeId(oleLoanDocument.getBorrowerTypeId());
63 oleLoanForm.setInformation("");
64 }catch (Exception e){
65 oleLoanForm.setInformation(e.getMessage());
66 }
67 return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
68 }
69
70 @RequestMapping(params = "methodToCall=addItem")
71 public ModelAndView addItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
72 HttpServletRequest request, HttpServletResponse response) {
73 OleLoanForm oleLoanForm = (OleLoanForm) form;
74 try{
75 List<OleLoanDocument> existingItemList = new ArrayList<OleLoanDocument>();
76
77 OleLoanDocument oleLoanDocument = new OleLoanDocument();
78 oleLoanDocument.setPatronId(oleLoanForm.getPatronId());
79 oleLoanDocument.setBorrowerTypeId(oleLoanForm.getBorrowerTypeId());
80 oleLoanDocument.setBorrowerTypeName(oleLoanForm.getBorrowerType());
81 oleLoanDocument = getLoanProcessor().addLoan(oleLoanForm.getPatronBarcode(),oleLoanForm.getItem(),oleLoanDocument);
82 oleLoanForm.setItemUuid(oleLoanDocument.getItemUuid());
83 oleLoanForm.setInstanceUuid(oleLoanDocument.getInstanceUuid());
84 oleLoanForm.setOleItem(oleLoanDocument.getOleItem());
85 oleLoanForm.setDueDateMap(oleLoanDocument.getLoanDueDate());
86 oleLoanForm.setMessage(oleLoanDocument.getErrorMessage());
87
88 if(oleLoanDocument.getErrorMessage() == null){
89 existingItemList.add(oleLoanDocument);
90 oleLoanForm.setItem("");
91 oleLoanForm.setInformation("");
92 } else {
93 oleLoanForm.setDueDateEmpty(oleLoanDocument.isDueDateEmpty());
94 oleLoanForm.setDummyLoan(oleLoanDocument);
95 oleLoanForm.setSuccess(false);
96 oleLoanForm.setInformation("");
97 }
98 if(oleLoanForm.getLoanList()!=null && !oleLoanForm.getLoanList().isEmpty()){
99 existingItemList.addAll(oleLoanForm.getLoanList());
100 }
101 oleLoanForm.setLoanList(existingItemList);
102 }catch (Exception e){
103 oleLoanForm.setInformation(e.getMessage());
104
105 }
106 return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
107 }
108
109 @RequestMapping(params = "methodToCall=loan")
110 public ModelAndView loanPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
111 HttpServletRequest request, HttpServletResponse response) {
112 OleLoanForm oleLoanForm = (OleLoanForm) form;
113 try{
114 List<OleLoanDocument> existingItemList = new ArrayList<OleLoanDocument>();
115 OleLoanDocument oleLoanDocument = oleLoanForm.getDummyLoan();
116 if(!oleLoanDocument.getItemLoanStatus().equalsIgnoreCase(OLEConstants.ITEM_STATUS_LOANED)){
117 oleLoanDocument.setLoanDueDate(new java.sql.Timestamp(oleLoanForm.getDueDateMap().getTime()));
118 getLoanProcessor().saveLoan(oleLoanDocument);
119 existingItemList.add(oleLoanDocument);
120 if(oleLoanForm.getLoanList()!=null && !oleLoanForm.getLoanList().isEmpty()){
121 existingItemList.addAll(oleLoanForm.getLoanList());
122 }
123 oleLoanForm.setLoanList(existingItemList);
124 oleLoanForm.setSuccess(true);
125 oleLoanForm.setMessage(null);
126 oleLoanForm.setItem("");
127 oleLoanForm.setInformation("");
128 }
129 }catch (Exception e){
130 oleLoanForm.setInformation(e.getMessage());
131 }
132 return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
133 }
134
135 @RequestMapping(params = "methodToCall=noLoan")
136 public ModelAndView doNotLoanPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
137 HttpServletRequest request, HttpServletResponse response) {
138 OleLoanForm oleLoanForm = (OleLoanForm) form;
139 oleLoanForm.setItem("");
140 oleLoanForm.setInformation("");
141 oleLoanForm.setMessage(null);
142 oleLoanForm.setSuccess(true);
143 return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
144 }
145
146 @RequestMapping(params = "methodToCall=saveAndClear")
147 public ModelAndView clearPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
148 HttpServletRequest request, HttpServletResponse response) {
149 OleLoanForm oleLoanForm = (OleLoanForm) form;
150 oleLoanForm.setBorrowerType(null);
151 oleLoanForm.setPatronBarcode(null);
152 oleLoanForm.setPatronName(null);
153 oleLoanForm.setLoanList(null);
154 oleLoanForm.setMessage(null);
155 oleLoanForm.setSuccess(true);
156 return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
157 }
158
159 private LoanProcessor getLoanProcessor() {
160 return new LoanProcessor();
161 }
162
163
164
165 }