View Javadoc
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.controller.checkout.CheckoutUIController;
6   import org.kuali.ole.deliver.form.CircForm;
7   import org.kuali.ole.deliver.keyvalue.CirculationDeskChangeKeyValue;
8   import org.kuali.ole.deliver.service.CircDeskLocationResolver;
9   import org.kuali.ole.deliver.service.ParameterValueResolver;
10  import org.kuali.ole.deliver.util.ErrorMessage;
11  import org.kuali.rice.core.api.config.property.ConfigContext;
12  import org.kuali.rice.core.api.util.KeyValue;
13  import org.kuali.rice.krad.exception.DocumentAuthorizationException;
14  import org.kuali.rice.krad.util.GlobalVariables;
15  import org.kuali.rice.krad.web.controller.UifControllerBase;
16  import org.kuali.rice.krad.web.form.UifFormBase;
17  import org.springframework.validation.BindingResult;
18  import org.springframework.web.bind.annotation.ModelAttribute;
19  import org.springframework.web.bind.annotation.RequestMapping;
20  import org.springframework.web.servlet.ModelAndView;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  /**
29   * Created by hemalathas on 6/21/15.
30   */
31  public class CircBaseController extends OLEUifControllerBase{
32  
33      private static final Logger LOG = Logger.getLogger(CircBaseController.class);
34  
35      private CircDeskLocationResolver circDeskLocationResolver;
36      public volatile static String fastAddBarcode = "";
37      //TODO: Clean up the map once session timeout is implemented.
38      private Map<String, CheckoutUIController> controllerMap = new HashMap<>();
39  
40      @Override
41      protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
42          return new CircForm();
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  
50          CircForm circForm = null;
51  
52          if (form.getViewId().equalsIgnoreCase("circView")) {
53              String formKey = request.getParameter("formKey");
54  
55              if (null == formKey) {
56                  circForm = (CircForm) form;
57              } else {
58                  circForm = (CircForm) GlobalVariables.getUifFormManager().getSessionForm(formKey);
59                  /* OLE-8186 : Modified for fixing browser backward and forward button functionality.*/
60                  if(circForm.getView() == null) {
61                      circForm = (CircForm) form;
62                  }
63              }
64  
65              setPrincipalInfo(circForm);
66  
67              List<KeyValue> keyValues = new CirculationDeskChangeKeyValue().getKeyValues();
68              if (keyValues.isEmpty()) {
69                  throw new DocumentAuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "not Authorized", form.getViewId());
70              } else {
71                  circForm.setSelectedCirculationDesk(keyValues.get(0).getValue());
72                  circForm.setPreviouslySelectedCirculationDesk(circForm.getSelectedCirculationDesk());
73              }
74          }
75          String maxTimeoutCount = ParameterValueResolver.getInstance().getParameter(OLEConstants
76                  .APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,OLEConstants.MAX_TIME_LOAN);
77          circForm.setMaxSessionTime(maxTimeoutCount);
78          String url = ConfigContext.getCurrentContextConfig().getProperty("ole.fs.url.base");
79          circForm.setUrlBase(url);
80          circForm.setViewBillUrl(OLEConstants.OlePatron.PATRON_VIEW_BILL_URL);
81          circForm.setCreateBillUrl(OLEConstants.OlePatron.PATRON_CREATE_BILL_URL);
82          return super.start(circForm, result, request, response);
83      }
84  
85  
86      @RequestMapping(params = "methodToCall=resetUI")
87      public ModelAndView resetUI(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
88                                  HttpServletRequest request, HttpServletResponse response) {
89          CircForm circForm = (CircForm) form;
90          circForm.reset();
91          return getUIFModelAndView(form, "circViewPage");
92      }
93  
94      @RequestMapping(params = "methodToCall=resetItemForNextTrans")
95      public ModelAndView resetItemForNextTrans(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
96                                                HttpServletRequest request, HttpServletResponse response) {
97          CircForm circForm = (CircForm) form;
98          resetItemInfoForNextTrans(circForm);
99          circForm.setErrorMessage(new ErrorMessage());
100         return getUIFModelAndView(form, "circViewPage");
101     }
102 
103     @RequestMapping(params = "methodToCall=resetUIForAutoCheckout")
104     public ModelAndView resetUIForAutoCheckout(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
105                                 HttpServletRequest request, HttpServletResponse response) {
106         CircForm circForm = (CircForm) form;
107         circForm.resetForAutoCheckout();
108         return getUIFModelAndView(form, "circViewPage");
109     }
110 
111     @RequestMapping(params = "methodToCall=refreshScreen")
112     public ModelAndView refreshScreen(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
113                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
114         return super.refresh(form, result, request, response);
115     }
116 
117     @RequestMapping(params = "methodToCall=clearUI")
118     public ModelAndView clearUI(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
119                                 HttpServletRequest request, HttpServletResponse response) {
120         CircForm circForm = (CircForm) form;
121         circForm.resetAll();
122         circForm.setLightboxScript("jq('#barcodeFieldSection_control').focus();");
123         return getUIFModelAndView(form, "circViewPage");
124     }
125 
126     protected void resetItemInfoForNextTrans(CircForm circForm) {
127         circForm.setItemBarcode("");
128         circForm.setItemRecord(null);
129         circForm.setDroolsExchange(null);
130         circForm.setItemValidationDone(false);
131         circForm.setCustomDueDateMap(null);
132         circForm.setCustomDueDateTime(null);
133     }
134 
135     private void setPrincipalInfo(CircForm circForm) {
136         if (org.apache.commons.lang3.StringUtils.isBlank(circForm.getLoggedInUser()))
137             circForm.setLoggedInUser(GlobalVariables.getUserSession().getPrincipalId());
138     }
139 
140     public String getCircDesk(String principalId) {
141         String circulationDeskId = getCircDeskLocationResolver().getCircDeskForOpertorId(principalId).getCirculationDeskId();
142         return circulationDeskId;
143 
144     }
145 
146     private CircDeskLocationResolver getCircDeskLocationResolver() {
147         if (circDeskLocationResolver == null) {
148             circDeskLocationResolver = new CircDeskLocationResolver();
149         }
150         return circDeskLocationResolver;
151     }
152 
153     public void showErrorMessageDialog(UifFormBase form, HttpServletRequest request, HttpServletResponse response) {
154         showDialog("ptrnValidationErrorMessageDialog", form, request, response);
155     }
156 
157     @RequestMapping(params = "methodToCall=clearSession")
158     public ModelAndView clearSession(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
159                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
160         CircForm circForm = (CircForm) form;
161         circForm.resetAll();
162         controllerMap.remove(circForm.getFormKey());
163         return getUIFModelAndView(circForm);
164     }
165 
166     public CheckoutUIController getCheckoutUIController(String formKey) {
167         if (controllerMap.containsKey(formKey)) {
168             return controllerMap.get(formKey);
169         } else {
170             CheckoutUIController checkoutUIController = new CheckoutUIController();
171             controllerMap.put(formKey, checkoutUIController);
172         }
173         return controllerMap.get(formKey);
174     }
175 }
176