001package org.kuali.ole.krad;
002
003import java.util.ArrayList;
004import java.util.HashMap;
005import java.util.List;
006import java.util.Map;
007
008import javax.servlet.http.HttpServletRequest;
009import javax.servlet.http.HttpServletResponse;
010
011import org.apache.log4j.Logger;
012import org.kuali.ole.OLEConstants;
013import org.kuali.ole.deliver.OleLoanDocumentsFromSolrBuilder;
014import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
015import org.kuali.ole.deliver.bo.OlePatronDocument;
016import org.kuali.ole.deliver.bo.OleProxyPatronDocument;
017import org.kuali.ole.deliver.form.OlePatronDocumentInquiryForm;
018import org.kuali.ole.deliver.processor.LoanProcessor;
019import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
020import org.kuali.rice.krad.service.KRADServiceLocator;
021import org.kuali.rice.krad.web.controller.InquiryController;
022import org.kuali.rice.krad.web.form.InquiryForm;
023import org.kuali.rice.krad.web.form.UifFormBase;
024import org.springframework.stereotype.Controller;
025import org.springframework.validation.BindingResult;
026import org.springframework.web.bind.annotation.ModelAttribute;
027import org.springframework.web.bind.annotation.RequestMapping;
028import org.springframework.web.bind.annotation.RequestMethod;
029import org.springframework.web.servlet.ModelAndView;
030
031@Controller
032@RequestMapping(value = "/inquiry")
033public class OleInquiryController extends InquiryController {
034
035        private static final Logger LOG = Logger
036                        .getLogger(OleInquiryController.class);
037
038        private static String DATA_OBJECT_CN_PARAM = "dataObjectClassName";
039        private static String DATA_OBJECT_CN_ATTRIBUTE = OleInquiryController.class
040                        .getName() + '.' + DATA_OBJECT_CN_PARAM;
041
042        private String getDataObjectClassName(HttpServletRequest request) {
043                String rv = request.getParameter(DATA_OBJECT_CN_PARAM);
044                if (rv == null)
045
046                        rv = (String) request.getSession().getAttribute(
047                                        DATA_OBJECT_CN_ATTRIBUTE);
048
049                else
050                        request.getSession().setAttribute(DATA_OBJECT_CN_ATTRIBUTE, rv);
051
052                return rv;
053        }
054
055        @Override
056        protected InquiryForm createInitialForm(HttpServletRequest request) {
057                String dataObjectClassName = getDataObjectClassName(request);
058
059                if (OlePatronDocument.class.getName().equals(dataObjectClassName))
060                        return new OlePatronDocumentInquiryForm();
061
062                return super.createInitialForm(request);
063        }
064
065        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshLoanSection")
066        public ModelAndView refreshLoanSection(
067                        @ModelAttribute("KualiForm") UifFormBase uifForm,
068                        BindingResult result, HttpServletRequest request,
069                        HttpServletResponse response) throws Exception {
070                if ((uifForm instanceof OlePatronDocumentInquiryForm)
071                                && !((OlePatronDocumentInquiryForm) uifForm)
072                                                .isFilterProxySection()) {
073
074                        OlePatronDocumentInquiryForm form = (OlePatronDocumentInquiryForm) uifForm;
075                        OlePatronDocument patronDocument = (OlePatronDocument) form
076                                        .getDataObject();
077
078                        LoanProcessor loanProcessor = new LoanProcessor();
079                        OleDeliverRequestDocumentHelperServiceImpl requestService = new OleDeliverRequestDocumentHelperServiceImpl();
080                        List<OleDeliverRequestBo> oleDeliverRequestBoList = patronDocument
081                                        .getOleDeliverRequestBos();
082                        if (oleDeliverRequestBoList.size() > 0) {
083                                for (int i = 0; i < oleDeliverRequestBoList.size(); i++) {
084
085                                        // OleItemSearch oleItemSearch = requestService
086                                        // .getItemDetailsForPatron(oleDeliverRequestBoList
087                                        // .get(i).getItemUuid());
088                                        // if (oleItemSearch != null
089                                        // && oleItemSearch.getItemBarCode() != null) {
090                                        // oleDeliverRequestBoList.get(i).setTitle(
091                                        // oleItemSearch.getTitle());
092                                        // oleDeliverRequestBoList.get(i).setCallNumber(
093                                        // oleItemSearch.getCallNumber());
094                                        // }
095
096                                        requestService.processItem(oleDeliverRequestBoList.get(i));
097                                }
098                        }
099
100                        try {
101                                patronDocument.setOleLoanDocuments(new OleLoanDocumentsFromSolrBuilder()
102                                                .getPatronLoanedItemBySolr(patronDocument
103                                                                .getOlePatronId(), null));
104                                patronDocument
105                                                .setOleTemporaryCirculationHistoryRecords(new OleLoanDocumentsFromSolrBuilder()
106                                                                .getPatronTemporaryCirculationHistoryRecords(patronDocument
107                                                                                .getOlePatronId()));
108                                patronDocument.setOleDeliverRequestBos(loanProcessor
109                                                .getPatronRequestRecords(patronDocument
110                                                                .getOlePatronId()));
111                        } catch (Exception e) {
112                                LOG.error("Cannot fetch the patron loaned items.", e);
113
114                        }
115
116                }
117
118                return super.refresh(uifForm, result, request, response);
119        }
120
121        @SuppressWarnings("unchecked")
122        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshProxyPatron")
123        public ModelAndView refreshProxyPatron(
124                        @ModelAttribute("KualiForm") UifFormBase uifForm,
125                        BindingResult result, HttpServletRequest request,
126                        HttpServletResponse response) throws Exception {
127
128                if ((uifForm instanceof OlePatronDocumentInquiryForm)
129                                && !((OlePatronDocumentInquiryForm) uifForm)
130                                                .isFilterProxySection()) {
131
132                        OlePatronDocumentInquiryForm form = (OlePatronDocumentInquiryForm) uifForm;
133                        OlePatronDocument patronDocument = (OlePatronDocument) form
134                                        .getDataObject();
135
136                        List<OleProxyPatronDocument> oleProxyPatronDocuments = patronDocument
137                                        .getOleProxyPatronDocuments();
138                        List<OleProxyPatronDocument> proxyPatronDocumentList = new ArrayList<OleProxyPatronDocument>();
139                        if (oleProxyPatronDocuments.size() > 0) {
140                                for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocuments) {
141                                        Map<String, String> proxyMap = new HashMap<String, String>();
142                                        proxyMap.put(OLEConstants.OlePatron.PATRON_ID,
143                                                        oleProxyPatronDocument.getProxyPatronId());
144                                        OlePatronDocument tempDocument = (OlePatronDocument) KRADServiceLocator
145                                                        .getBusinessObjectService().findByPrimaryKey(
146                                                                        OlePatronDocument.class, proxyMap);
147                                        if (tempDocument != null) {
148                                                oleProxyPatronDocument
149                                                                .setProxyPatronBarcode(tempDocument
150                                                                                .getBarcode());
151                                                oleProxyPatronDocument
152                                                                .setProxyPatronFirstName(tempDocument
153                                                                                .getEntity().getNames().get(0)
154                                                                                .getFirstName());
155                                                oleProxyPatronDocument
156                                                                .setProxyPatronLastName(tempDocument
157                                                                                .getEntity().getNames().get(0)
158                                                                                .getLastName());
159                                                proxyPatronDocumentList.add(oleProxyPatronDocument);
160                                        }
161                                }
162                                patronDocument
163                                                .setOleProxyPatronDocuments(proxyPatronDocumentList);
164                        }
165                        OlePatronDocument olePatronDocument;
166                        OleProxyPatronDocument proxyPatronDocument = null;
167                        List<OleProxyPatronDocument> proxyPatronDocuments = new ArrayList<OleProxyPatronDocument>();
168                        Map proxyMap = new HashMap();
169                        proxyMap.put(OLEConstants.OlePatron.PROXY_PATRON_ID,
170                                        patronDocument.getOlePatronId());
171                        List<OleProxyPatronDocument> oleProxyPatronDocumentList = (List<OleProxyPatronDocument>) KRADServiceLocator
172                                        .getBusinessObjectService().findMatching(
173                                                        OleProxyPatronDocument.class, proxyMap);
174                        if (oleProxyPatronDocumentList.size() > 0) {
175                                proxyMap.remove(OLEConstants.OlePatron.PROXY_PATRON_ID);
176                                for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
177                                        proxyMap.put(OLEConstants.OlePatron.PATRON_ID,
178                                                        oleProxyPatronDocument.getOlePatronId());
179                                        List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) KRADServiceLocator
180                                                        .getBusinessObjectService().findMatching(
181                                                                        OlePatronDocument.class, proxyMap);
182                                        if (olePatronDocumentList.size() > 0) {
183                                                olePatronDocument = olePatronDocumentList.get(0);
184                                                proxyPatronDocument = new OleProxyPatronDocument();
185                                        //      if (olePatronDocument.isActiveIndicator()) {
186                                                        proxyPatronDocument
187                                                                        .setOlePatronId(olePatronDocument
188                                                                                        .getOlePatronId());
189                                                        proxyPatronDocument
190                                                                        .setProxyForPatronFirstName(olePatronDocument
191                                                                                        .getEntity().getNames().get(0)
192                                                                                        .getFirstName());
193                                                        proxyPatronDocument
194                                                                        .setProxyForPatronLastName(olePatronDocument
195                                                                                        .getEntity().getNames().get(0)
196                                                                                        .getLastName());
197                                                        proxyPatronDocument
198                                                                        .setProxyPatronActivationDate(oleProxyPatronDocument
199                                                                                        .getProxyPatronActivationDate());
200                                                        proxyPatronDocument
201                                                                        .setProxyPatronExpirationDate(oleProxyPatronDocument
202                                                                                        .getProxyPatronExpirationDate());
203                                                        /*
204                                                         * List<OleProxyPatronDocument> proxyPatronList =
205                                                         * (List<OleProxyPatronDocument>)
206                                                         * getBusinessObjectService
207                                                         * ().findMatching(OleProxyPatronDocument.class,
208                                                         * proxyMap); if (proxyPatronList.size() > 0) { for
209                                                         * (OleProxyPatronDocument proxyPatron :
210                                                         * proxyPatronList) { if
211                                                         * (proxyPatron.getOlePatronId(
212                                                         * ).equals(olePatronDocument.getOlePatronId())) {
213                                                         * proxyPatronDocument
214                                                         * .setProxyPatronActivationDate(proxyPatron
215                                                         * .getProxyPatronActivationDate());
216                                                         * proxyPatronDocument
217                                                         * .setProxyPatronExpirationDate(proxyPatron
218                                                         * .getProxyPatronExpirationDate()); } } }
219                                                         */
220                                                //}
221                                                proxyPatronDocuments.add(proxyPatronDocument);
222                                        }
223                                }
224                                patronDocument
225                                                .setOleProxyPatronDocumentList(proxyPatronDocuments);
226                        }
227                }
228
229                return super.refresh(uifForm, result, request, response);
230        }
231
232}
233