View Javadoc
1   package org.kuali.ole.krad;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  
11  import org.apache.log4j.Logger;
12  import org.kuali.ole.OLEConstants;
13  import org.kuali.ole.deliver.OleLoanDocumentsFromSolrBuilder;
14  import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
15  import org.kuali.ole.deliver.bo.OlePatronDocument;
16  import org.kuali.ole.deliver.bo.OleProxyPatronDocument;
17  import org.kuali.ole.deliver.form.OlePatronDocumentInquiryForm;
18  import org.kuali.ole.deliver.processor.LoanProcessor;
19  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
20  import org.kuali.ole.deliver.util.OlePatronRecordUtil;
21  import org.kuali.rice.krad.service.KRADServiceLocator;
22  import org.kuali.rice.krad.web.controller.InquiryController;
23  import org.kuali.rice.krad.web.form.InquiryForm;
24  import org.kuali.rice.krad.web.form.UifFormBase;
25  import org.springframework.stereotype.Controller;
26  import org.springframework.validation.BindingResult;
27  import org.springframework.web.bind.annotation.ModelAttribute;
28  import org.springframework.web.bind.annotation.RequestMapping;
29  import org.springframework.web.bind.annotation.RequestMethod;
30  import org.springframework.web.servlet.ModelAndView;
31  
32  @Controller
33  @RequestMapping(value = "/inquiry")
34  public class OleInquiryController extends InquiryController {
35      private OlePatronRecordUtil olePatronRecordUtil ;
36  
37      public OlePatronRecordUtil getOlePatronRecordUtil() {
38          if(olePatronRecordUtil == null){
39              olePatronRecordUtil = new OlePatronRecordUtil();
40          }
41          return olePatronRecordUtil;
42      }
43  
44  	private static final Logger LOG = Logger
45  			.getLogger(OleInquiryController.class);
46  
47  	private static String DATA_OBJECT_CN_PARAM = "dataObjectClassName";
48  	private static String DATA_OBJECT_CN_ATTRIBUTE = OleInquiryController.class
49  			.getName() + '.' + DATA_OBJECT_CN_PARAM;
50  
51  	private String getDataObjectClassName(HttpServletRequest request) {
52  		String rv = request.getParameter(DATA_OBJECT_CN_PARAM);
53  		if (rv == null)
54  
55  			rv = (String) request.getSession().getAttribute(
56  					DATA_OBJECT_CN_ATTRIBUTE);
57  
58  		else
59  			request.getSession().setAttribute(DATA_OBJECT_CN_ATTRIBUTE, rv);
60  
61  		return rv;
62  	}
63  
64  	@Override
65  	protected InquiryForm createInitialForm(HttpServletRequest request) {
66  		String dataObjectClassName = getDataObjectClassName(request);
67  
68  		if (OlePatronDocument.class.getName().equals(dataObjectClassName))
69  			return new OlePatronDocumentInquiryForm();
70  
71  		return super.createInitialForm(request);
72  	}
73  
74  	@RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshLoanSection")
75  	public ModelAndView refreshLoanSection(
76  			@ModelAttribute("KualiForm") UifFormBase uifForm,
77  			BindingResult result, HttpServletRequest request,
78  			HttpServletResponse response) throws Exception {
79  		if ((uifForm instanceof OlePatronDocumentInquiryForm)
80  				&& !((OlePatronDocumentInquiryForm) uifForm)
81  						.isFilterProxySection()) {
82  
83  			OlePatronDocumentInquiryForm form = (OlePatronDocumentInquiryForm) uifForm;
84  			OlePatronDocument patronDocument = (OlePatronDocument) form
85  					.getDataObject();
86  
87  
88  			OleDeliverRequestDocumentHelperServiceImpl requestService = new OleDeliverRequestDocumentHelperServiceImpl();
89  			List<OleDeliverRequestBo> oleDeliverRequestBoList = patronDocument
90  					.getOleDeliverRequestBos();
91  			if (oleDeliverRequestBoList.size() > 0) {
92  				for (int i = 0; i < oleDeliverRequestBoList.size(); i++) {
93  
94  					// OleItemSearch oleItemSearch = requestService
95  					// .getItemDetailsForPatron(oleDeliverRequestBoList
96  					// .get(i).getItemUuid());
97  					// if (oleItemSearch != null
98  					// && oleItemSearch.getItemBarCode() != null) {
99  					// oleDeliverRequestBoList.get(i).setTitle(
100 					// oleItemSearch.getTitle());
101 					// oleDeliverRequestBoList.get(i).setCallNumber(
102 					// oleItemSearch.getCallNumber());
103 					// }
104 
105 					requestService.processItem(oleDeliverRequestBoList.get(i));
106 				}
107 			}
108 
109 			try {
110 				patronDocument.setOleLoanDocuments(new OleLoanDocumentsFromSolrBuilder()
111 						.getPatronLoanedItemBySolr(patronDocument
112 								.getOlePatronId(), null));
113 				patronDocument
114 						.setOleTemporaryCirculationHistoryRecords(new OleLoanDocumentsFromSolrBuilder()
115 								.getPatronTemporaryCirculationHistoryRecords(patronDocument
116 										.getOlePatronId()));
117 				patronDocument.setOleDeliverRequestBos(getOlePatronRecordUtil()
118 						.getPatronRequestRecords(patronDocument));
119 			} catch (Exception e) {
120 				LOG.error("Cannot fetch the patron loaned items.", e);
121 
122 			}
123 
124 		}
125 
126 		return super.refresh(uifForm, result, request, response);
127 	}
128 
129 	@SuppressWarnings("unchecked")
130 	@RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshProxyPatron")
131 	public ModelAndView refreshProxyPatron(
132 			@ModelAttribute("KualiForm") UifFormBase uifForm,
133 			BindingResult result, HttpServletRequest request,
134 			HttpServletResponse response) throws Exception {
135 
136 		if ((uifForm instanceof OlePatronDocumentInquiryForm)
137 				&& !((OlePatronDocumentInquiryForm) uifForm)
138 						.isFilterProxySection()) {
139 
140 			OlePatronDocumentInquiryForm form = (OlePatronDocumentInquiryForm) uifForm;
141 			OlePatronDocument patronDocument = (OlePatronDocument) form
142 					.getDataObject();
143 
144 			List<OleProxyPatronDocument> oleProxyPatronDocuments = patronDocument
145 					.getOleProxyPatronDocuments();
146 			List<OleProxyPatronDocument> proxyPatronDocumentList = new ArrayList<OleProxyPatronDocument>();
147 			if (oleProxyPatronDocuments.size() > 0) {
148 				for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocuments) {
149 					Map<String, String> proxyMap = new HashMap<String, String>();
150 					proxyMap.put(OLEConstants.OlePatron.PATRON_ID,
151 							oleProxyPatronDocument.getProxyPatronId());
152 					OlePatronDocument tempDocument = (OlePatronDocument) KRADServiceLocator
153 							.getBusinessObjectService().findByPrimaryKey(
154 									OlePatronDocument.class, proxyMap);
155 					if (tempDocument != null) {
156 						oleProxyPatronDocument
157 								.setProxyPatronBarcode(tempDocument
158 										.getBarcode());
159 						oleProxyPatronDocument
160 								.setProxyPatronFirstName(tempDocument
161 										.getEntity().getNames().get(0)
162 										.getFirstName());
163 						oleProxyPatronDocument
164 								.setProxyPatronLastName(tempDocument
165 										.getEntity().getNames().get(0)
166 										.getLastName());
167 						proxyPatronDocumentList.add(oleProxyPatronDocument);
168 					}
169 				}
170 				patronDocument
171 						.setOleProxyPatronDocuments(proxyPatronDocumentList);
172 			}
173 			OlePatronDocument olePatronDocument;
174 			OleProxyPatronDocument proxyPatronDocument = null;
175 			List<OleProxyPatronDocument> proxyPatronDocuments = new ArrayList<OleProxyPatronDocument>();
176 			Map proxyMap = new HashMap();
177 			proxyMap.put(OLEConstants.OlePatron.PROXY_PATRON_ID,
178 					patronDocument.getOlePatronId());
179 			List<OleProxyPatronDocument> oleProxyPatronDocumentList = (List<OleProxyPatronDocument>) KRADServiceLocator
180 					.getBusinessObjectService().findMatching(
181 							OleProxyPatronDocument.class, proxyMap);
182 			if (oleProxyPatronDocumentList.size() > 0) {
183 				proxyMap.remove(OLEConstants.OlePatron.PROXY_PATRON_ID);
184 				for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
185 					proxyMap.put(OLEConstants.OlePatron.PATRON_ID,
186 							oleProxyPatronDocument.getOlePatronId());
187 					List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) KRADServiceLocator
188 							.getBusinessObjectService().findMatching(
189 									OlePatronDocument.class, proxyMap);
190 					if (olePatronDocumentList.size() > 0) {
191 						olePatronDocument = olePatronDocumentList.get(0);
192 						proxyPatronDocument = new OleProxyPatronDocument();
193 					//	if (olePatronDocument.isActiveIndicator()) {
194 							proxyPatronDocument
195 									.setOlePatronId(olePatronDocument
196 											.getOlePatronId());
197 							proxyPatronDocument
198 									.setProxyForPatronFirstName(olePatronDocument
199 											.getEntity().getNames().get(0)
200 											.getFirstName());
201 							proxyPatronDocument
202 									.setProxyForPatronLastName(olePatronDocument
203 											.getEntity().getNames().get(0)
204 											.getLastName());
205 							proxyPatronDocument
206 									.setProxyPatronActivationDate(oleProxyPatronDocument
207 											.getProxyPatronActivationDate());
208 							proxyPatronDocument
209 									.setProxyPatronExpirationDate(oleProxyPatronDocument
210 											.getProxyPatronExpirationDate());
211 							/*
212 							 * List<OleProxyPatronDocument> proxyPatronList =
213 							 * (List<OleProxyPatronDocument>)
214 							 * getBusinessObjectService
215 							 * ().findMatching(OleProxyPatronDocument.class,
216 							 * proxyMap); if (proxyPatronList.size() > 0) { for
217 							 * (OleProxyPatronDocument proxyPatron :
218 							 * proxyPatronList) { if
219 							 * (proxyPatron.getOlePatronId(
220 							 * ).equals(olePatronDocument.getOlePatronId())) {
221 							 * proxyPatronDocument
222 							 * .setProxyPatronActivationDate(proxyPatron
223 							 * .getProxyPatronActivationDate());
224 							 * proxyPatronDocument
225 							 * .setProxyPatronExpirationDate(proxyPatron
226 							 * .getProxyPatronExpirationDate()); } } }
227 							 */
228 						//}
229 						proxyPatronDocuments.add(proxyPatronDocument);
230 					}
231 				}
232 				patronDocument
233 						.setOleProxyPatronDocumentList(proxyPatronDocuments);
234 			}
235 		}
236 
237 		return super.refresh(uifForm, result, request, response);
238 	}
239 
240 }
241