View Javadoc

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   * The LoanController is the controller class for processing all the actions that corresponds to the Loan functionality in OLE.
27   * The request mapping tag takes care of mapping the individual action to the corresponding functions.
28   */
29  @Controller
30  @RequestMapping(value = "/loancontroller")
31  public class LoanController extends UifControllerBase {
32  
33      private static final Logger LOG = Logger.getLogger(LoanController.class);
34  
35      private BusinessObjectService boService;
36  
37      /**
38       *  This method creates new OleLoan form
39       * @param request
40       * @return  OleLoanForm
41       */
42      @Override
43      protected OleLoanForm createInitialForm(HttpServletRequest request) {
44          return new OleLoanForm();
45      }
46  
47      /**
48       *  This method converts UifFormBase to OleLoanForm
49       * @param form
50       * @param result
51       * @param request
52       * @param response
53       * @return ModelAndView
54       */
55      @Override
56      @RequestMapping(params = "methodToCall=start")
57      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
58                                HttpServletRequest request, HttpServletResponse response) {
59          OleLoanForm oleLoanForm = (OleLoanForm) form;
60          return super.start(oleLoanForm, result, request, response);
61      }
62  
63      /**
64       *   This method displays information about a patron in UI.
65       * @param form
66       * @param result
67       * @param request
68       * @param response
69       * @return ModelAndView
70       */
71      @RequestMapping(params = "methodToCall=searchPatron")
72      public ModelAndView searchPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
73                                             HttpServletRequest request, HttpServletResponse response) {
74          OleLoanForm oleLoanForm = (OleLoanForm) form;
75          try{
76          OleLoanDocument oleLoanDocument = getLoanProcessor().getLoanDocument(oleLoanForm.getPatronBarcode());
77          oleLoanForm.setBorrowerType(oleLoanDocument.getBorrowerTypeName());
78          oleLoanForm.setPatronName(oleLoanDocument.getPatronName());
79          oleLoanForm.setPatronId(oleLoanDocument.getPatronId());
80          oleLoanForm.setBorrowerTypeId(oleLoanDocument.getBorrowerTypeId());
81          oleLoanForm.setInformation("");
82          oleLoanForm.setDummyLoan(oleLoanDocument);
83              if(oleLoanDocument.getErrorMessage()!=null){
84                  oleLoanForm.setSuccess(false);
85                  oleLoanForm.setInformation("");
86                  oleLoanForm.setMessage(oleLoanDocument.getErrorMessage());
87                  oleLoanForm.setPatronName(null);
88              }
89          }catch (Exception e){
90              oleLoanForm.setInformation(e.getMessage());
91          }
92          return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
93      }
94  
95      /**
96       * This method creates new loan for a patron..
97       * @param form
98       * @param result
99       * @param request
100      * @param response
101      * @return ModelAndView
102      */
103     @RequestMapping(params = "methodToCall=addItem")
104     public ModelAndView addItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
105                                 HttpServletRequest request, HttpServletResponse response) {
106          OleLoanForm oleLoanForm = (OleLoanForm) form;
107         try{
108          List<OleLoanDocument> existingItemList = new ArrayList<OleLoanDocument>();
109 
110          OleLoanDocument oleLoanDocument = new OleLoanDocument();
111          oleLoanDocument.setPatronId(oleLoanForm.getPatronId());
112          oleLoanDocument.setBorrowerTypeId(oleLoanForm.getBorrowerTypeId());
113          oleLoanDocument.setBorrowerTypeName(oleLoanForm.getBorrowerType());
114          oleLoanDocument = getLoanProcessor().addLoan(oleLoanForm.getPatronBarcode(),oleLoanForm.getItem(),oleLoanDocument);
115          oleLoanForm.setItemUuid(oleLoanDocument.getItemUuid());
116          oleLoanForm.setInstanceUuid(oleLoanDocument.getInstanceUuid());
117          oleLoanForm.setOleItem(oleLoanDocument.getOleItem());
118          oleLoanForm.setDueDateMap(oleLoanDocument.getLoanDueDate());
119          oleLoanForm.setMessage(oleLoanDocument.getErrorMessage());
120          if(oleLoanForm.getPatronName()==null){
121             oleLoanForm.setPatronName(oleLoanDocument.getPatronName());
122          }
123         if(oleLoanDocument.getErrorMessage() == null){
124           existingItemList.add(oleLoanDocument);
125           oleLoanForm.setItem("");
126           oleLoanForm.setInformation("");
127         } else {
128             oleLoanForm.setDueDateEmpty(oleLoanDocument.isDueDateEmpty());
129             oleLoanForm.setDummyLoan(oleLoanDocument);
130             oleLoanForm.setSuccess(false);
131             oleLoanForm.setInformation("");
132         }
133         if(oleLoanForm.getLoanList()!=null && !oleLoanForm.getLoanList().isEmpty()){
134             existingItemList.addAll(oleLoanForm.getLoanList());
135         }
136         oleLoanForm.setLoanList(existingItemList);
137         }catch (Exception e){
138             oleLoanForm.setInformation(e.getMessage());
139 
140         }
141        return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
142     }
143 
144     /**
145      * This method  creates loan for a patron who is not able to borrow.
146      * @param form
147      * @param result
148      * @param request
149      * @param response
150      * @return  ModelAndView
151      */
152     @RequestMapping(params = "methodToCall=loan")
153     public ModelAndView loanPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
154                                     HttpServletRequest request, HttpServletResponse response) {
155         OleLoanForm oleLoanForm = (OleLoanForm) form;
156         try{
157         List<OleLoanDocument> existingItemList = new ArrayList<OleLoanDocument>();
158         OleLoanDocument oleLoanDocument = oleLoanForm.getDummyLoan();
159         if(oleLoanDocument!=null){
160             if(oleLoanForm.getDueDateMap()!=null){
161                 oleLoanDocument.setLoanDueDate(new java.sql.Timestamp(oleLoanForm.getDueDateMap().getTime()));
162                 getLoanProcessor().saveLoan(oleLoanDocument);
163                 existingItemList.add(oleLoanDocument);
164                 if(oleLoanForm.getLoanList()!=null && !oleLoanForm.getLoanList().isEmpty()){
165                     existingItemList.addAll(oleLoanForm.getLoanList());
166                 }
167                 oleLoanForm.setLoanList(existingItemList);
168             }
169             if(oleLoanForm.getPatronName()==null){
170                 oleLoanForm.setPatronName(oleLoanDocument.getPatronName());
171             }
172             oleLoanForm.setSuccess(true);
173             oleLoanForm.setMessage(null);
174             oleLoanForm.setItem("");
175             oleLoanForm.setInformation("");
176         }
177         }catch (Exception e){
178             oleLoanForm.setInformation(e.getMessage());
179         }
180         return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
181     }
182 
183     /**
184      * This method doesn't allow a patron to be loaned.
185      *  @param form
186      * @param result
187      * @param request
188      * @param response
189      * @return ModelAndView
190      */
191     @RequestMapping(params = "methodToCall=noLoan")
192     public ModelAndView doNotLoanPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
193                                    HttpServletRequest request, HttpServletResponse response) {
194         OleLoanForm oleLoanForm = (OleLoanForm) form;
195         oleLoanForm.setItem("");
196         oleLoanForm.setInformation("");
197         oleLoanForm.setMessage(null);
198         oleLoanForm.setSuccess(true);
199         return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
200     }
201 
202     /**
203      *  This method clear UI for next borrower session..
204      * @param form
205      * @param result
206      * @param request
207      * @param response
208      * @return  ModelAndView
209      */
210     @RequestMapping(params = "methodToCall=saveAndClear")
211     public ModelAndView clearPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
212                                      HttpServletRequest request, HttpServletResponse response) {
213         OleLoanForm oleLoanForm = (OleLoanForm) form;
214         oleLoanForm.setBorrowerType(null);
215         oleLoanForm.setPatronBarcode(null);
216         oleLoanForm.setPatronName(null);
217         oleLoanForm.setLoanList(null);
218         oleLoanForm.setMessage(null);
219         oleLoanForm.setSuccess(true);
220         return getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
221     }
222 
223     /**
224      * This method initiate LoanProcessor.
225      * @return LoanProcessor
226      */
227     private LoanProcessor getLoanProcessor() {
228         return new LoanProcessor();
229     }
230 
231 
232 
233 }