View Javadoc
1   package org.kuali.ole.deliver.controller;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.OleLoanDocumentsFromSolrBuilder;
7   import org.kuali.ole.deliver.bo.OlePatronDocument;
8   import org.kuali.ole.deliver.bo.OleTemporaryCirculationHistory;
9   import org.kuali.ole.deliver.form.OleTemporaryCirculationRecordsForm;
10  import org.kuali.ole.deliver.processor.LoanProcessor;
11  import org.kuali.ole.deliver.service.OLEDeliverService;
12  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
13  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  import org.kuali.rice.krad.util.GlobalVariables;
16  import org.kuali.rice.krad.util.KRADConstants;
17  import org.kuali.rice.krad.web.controller.UifControllerBase;
18  import org.kuali.rice.krad.web.form.UifFormBase;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.validation.BindingResult;
21  import org.springframework.web.bind.annotation.ModelAttribute;
22  import org.springframework.web.bind.annotation.RequestMapping;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * .OlePatronMaintenanceDocumentController invokes Transaction document Controller and returns instance of Transaction document service.
33   */
34  @Controller
35  @RequestMapping(value = "/temporaryCirculationRecord")
36  public class OleTemporaryCirculationRecordsController extends UifControllerBase {
37  
38      /**
39       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
40       */
41      private static final Logger LOG = Logger.getLogger(OleTemporaryCirculationRecordsController.class);
42      private OleLoanDocumentsFromSolrBuilder oleLoanDocumentsFromSolrBuilder;
43  
44      @Override
45      protected OleTemporaryCirculationRecordsForm createInitialForm(HttpServletRequest request) {
46          return new OleTemporaryCirculationRecordsForm();
47      }
48  
49      /**
50       * This method takes the initial request when click on temporaryCirculationRecord Screen.
51       *
52       * @param form
53       * @param result
54       * @param request
55       * @param response
56       * @return ModelAndView
57       */
58      @Override
59      @RequestMapping(params = "methodToCall=start")
60      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
61                                HttpServletRequest request, HttpServletResponse response) {
62          LOG.debug("Start -- Start Method of OleTemporaryCirculationRecordsForm");
63          OleTemporaryCirculationRecordsForm oleTemporaryCirculationRecordsForm = (OleTemporaryCirculationRecordsForm) form;
64          return super.start(oleTemporaryCirculationRecordsForm, result, request, response);
65      }
66  
67      /**
68       * This method persist to viewing all the temporary circulation history records.
69       *
70       * @param form
71       * @param result
72       * @param request
73       * @param response
74       * @return ModelAndView
75       * @throws Exception
76       */
77      @RequestMapping(params = "methodToCall=viewTempCircRecords")
78      public ModelAndView viewTempCircRecords(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
79                                              HttpServletRequest request, HttpServletResponse response) throws Exception {
80  
81          OleTemporaryCirculationRecordsForm oleTemporaryCirculationRecordsForm = (OleTemporaryCirculationRecordsForm) form;
82          String patronId = request.getParameter("patronId");
83          Map patron = new HashMap();
84          patron.put(OLEConstants.OlePatron.PATRON_ID, patronId);
85          OlePatronDocument olePatronDocument = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patron);
86          if (olePatronDocument != null) {
87              EntityBo entity = olePatronDocument.getEntity();
88              EntityNameBo entityName = entity.getNames().get(0);
89              String firstName = entityName.getFirstName();
90              String lastName = entityName.getLastName();
91              String name = firstName + " " + lastName;
92              oleTemporaryCirculationRecordsForm.setPatronName(name);
93              oleTemporaryCirculationRecordsForm.setOlePatronDocument(olePatronDocument);
94              LoanProcessor loanProcessor = new LoanProcessor();
95              try {
96                  olePatronDocument.setOleTemporaryCirculationHistoryRecords(getOleLoanDocumentsFromSolrBuilder()
97                          .getPatronTemporaryCirculationHistoryRecords(olePatronDocument.getOlePatronId()));
98                  if(CollectionUtils.isNotEmpty(olePatronDocument.getOleTemporaryCirculationHistoryRecords())) {
99                      for(OleTemporaryCirculationHistory oleTemporaryCirculationHistory : olePatronDocument.getOleTemporaryCirculationHistoryRecords()) {
100                         if(oleTemporaryCirculationHistory.getOleProxyPatronId() != null){
101                             Map<String, String> criteria = new HashMap<>();
102                             criteria.put(OLEConstants.OlePatron.PATRON_ID,oleTemporaryCirculationHistory.getOleProxyPatronId());
103                             OlePatronDocument patronDocument = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, criteria);
104                             if(olePatronDocument != null){
105                                 patronDocument = OLEDeliverService.populatePatronName(patronDocument);
106                                 oleTemporaryCirculationHistory.setProxyPatronBarcode(patronDocument.getBarcode());
107                                 oleTemporaryCirculationHistory.setProxyPatronName(patronDocument.getPatronName());
108                                 oleTemporaryCirculationHistory.setProxyPatronBarcodeUrl(OLEConstants.ASSIGN_INQUIRY_PATRON_ID + patronDocument.getOlePatronId() + OLEConstants.ASSIGN_PATRON_INQUIRY);
109                             }
110                         }
111                     }
112                 }
113             } catch (Exception e) {
114                 LOG.error("Exception while setting temporary circulation history records", e);
115             }
116             List<OleTemporaryCirculationHistory> oleTemporaryCirculationHistoryRecords = olePatronDocument.getOleTemporaryCirculationHistoryRecords();
117             oleTemporaryCirculationRecordsForm.setOleTemporaryCirculationHistoryRecords(oleTemporaryCirculationHistoryRecords);
118         } else {
119             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_NOT_FOUND);
120         }
121         return getUIFModelAndView(oleTemporaryCirculationRecordsForm, "OleTemporaryCirculationHistoryRecordPage");
122     }
123 
124     private OleLoanDocumentsFromSolrBuilder getOleLoanDocumentsFromSolrBuilder() {
125         if (null == oleLoanDocumentsFromSolrBuilder) {
126             oleLoanDocumentsFromSolrBuilder = new OleLoanDocumentsFromSolrBuilder();
127         }
128         return oleLoanDocumentsFromSolrBuilder;
129     }
130 
131     public void setOleLoanDocumentsFromSolrBuilder(OleLoanDocumentsFromSolrBuilder oleLoanDocumentsFromSolrBuilder) {
132         this.oleLoanDocumentsFromSolrBuilder = oleLoanDocumentsFromSolrBuilder;
133     }
134 }