001package org.kuali.ole.deliver.controller;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.deliver.api.OlePatronDefinition;
006import org.kuali.ole.deliver.bo.*;
007import org.kuali.ole.deliver.form.OleMyAccountForm;
008import org.kuali.ole.deliver.service.OleMyAccountProcess;
009import org.kuali.ole.service.OlePatronHelperService;
010import org.kuali.ole.service.OlePatronHelperServiceImpl;
011import org.kuali.ole.service.OlePatronService;
012import org.kuali.ole.service.OlePatronServiceImpl;
013import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
014import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
015import org.kuali.rice.kim.impl.identity.entity.EntityBo;
016import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
017import org.kuali.rice.krad.service.BusinessObjectService;
018import org.kuali.rice.krad.service.KRADServiceLocator;
019import org.kuali.rice.krad.web.controller.UifControllerBase;
020import org.kuali.rice.krad.web.form.UifFormBase;
021import org.springframework.stereotype.Controller;
022import org.springframework.validation.BindingResult;
023import org.springframework.web.bind.annotation.ModelAttribute;
024import org.springframework.web.bind.annotation.RequestMapping;
025import org.springframework.web.servlet.ModelAndView;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033
034/**
035 * The OleMyAccountController is the controller class for processing all the actions that corresponds to the renewal by patron  functionality in OLE.
036 * The request mapping tag takes care of mapping the individual action to the corresponding functions.
037 */
038@Controller
039@RequestMapping(value = "/myaccountcontroller")
040public class OleMyAccountController extends UifControllerBase {
041
042    private static final Logger LOG = Logger.getLogger(OleMyAccountController.class);
043
044    private BusinessObjectService boService;
045    private OleMyAccountProcess oleMyAccountProcess;
046    private OlePatronHelperService olePatronHelperService = new OlePatronHelperServiceImpl();
047
048    /**
049     * This method creates new oleMyAccountForm form
050     *
051     * @param request
052     * @return oleMyAccountForm
053     */
054    @Override
055    protected OleMyAccountForm createInitialForm(HttpServletRequest request) {
056        return new OleMyAccountForm();
057    }
058
059    /**
060     * This method converts UifFormBase to oleMyAccountForm
061     *
062     * @param form
063     * @param result
064     * @param request
065     * @param response
066     * @return ModelAndView
067     */
068    @Override
069    @RequestMapping(params = "methodToCall=start")
070    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
071                              HttpServletRequest request, HttpServletResponse response) {
072        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
073        return super.start(oleMyAccountForm, result, request, response);
074    }
075
076   /* *//**
077     *  This method performs the my account login
078     * @param form
079     * @param result
080     * @param request
081     * @param response
082     * @return ModelAndView
083     *//*
084
085    @RequestMapping(params = "methodToCall=logIn")
086    public ModelAndView logIn(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
087                              HttpServletRequest request, HttpServletResponse response) {
088        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
089        String patrinId=oleMyAccountForm.getPatronId();
090        String password=oleMyAccountForm.getPatronPassword();
091        oleMyAccountForm.setViewId("RenewalItemView");
092        oleMyAccountForm.setMethodToCall("start");
093        oleMyAccountForm.setValidPatronFlag(true);
094        return this.start(oleMyAccountForm, result, request, response);
095
096
097    }*/
098
099
100    /**
101     * This method displays information about a patron in UI.
102     *
103     * @param form
104     * @param result
105     * @param request
106     * @param response
107     * @return ModelAndView
108     */
109    @RequestMapping(params = "methodToCall=searchPatron")
110    public ModelAndView searchPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
111                                     HttpServletRequest request, HttpServletResponse response) {
112        LOG.debug("Inside the searchPatron method");
113        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
114        oleMyAccountForm.setExistingLoanList(new ArrayList<OleRenewalLoanDocument>());
115        oleMyAccountForm.setInformation("");
116        oleMyAccountForm.setMessage("");
117        try {
118            OlePatronDefinition olePatronDefinition = getOleMyAccountProcess().getPatronInfo(oleMyAccountForm.getPatronBarcode());
119            oleMyAccountForm.setBorrowerType(olePatronDefinition.getOleBorrowerType().getBorrowerTypeName());
120            oleMyAccountForm.setPatronName(olePatronDefinition.getEntity().getNames().get(0).getFirstName());
121            oleMyAccountForm.setPatronId(olePatronDefinition.getOlePatronId());
122            oleMyAccountForm.setBorrowerTypeId(olePatronDefinition.getOleBorrowerType().getBorrowerTypeId());
123            oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(olePatronDefinition.getOlePatronId()));
124        } catch (Exception e) {
125            oleMyAccountForm.setInformation(e.getMessage());
126            LOG.error("Exception", e);
127        }
128        if (oleMyAccountForm.getExistingLoanList().size() == 0) {
129            oleMyAccountForm.setInformation("No item currently checked out.");
130            oleMyAccountForm.setPatronName("");
131        }
132        return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
133    }
134
135    /**
136     * This method search Patron document and display in the screen.
137     *
138     * @param form
139     * @param result
140     * @param request
141     * @param response
142     * @return ModelAndView
143     */
144    @RequestMapping(params = "methodToCall=myAccountPatronSearch")
145    public ModelAndView myAccountPatronSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
146                                              HttpServletRequest request, HttpServletResponse response) {
147        LOG.debug("Inside myAccountPatronSearch");
148        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
149        oleMyAccountForm.setExistingLoanList(new ArrayList<OleRenewalLoanDocument>());
150        oleMyAccountForm.setInformation("");
151        try {
152            OlePatronDefinition olePatronDefinition = getOleMyAccountProcess().getPatronInfo(oleMyAccountForm.getPatronBarcode());
153            if (olePatronDefinition == null) {
154                oleMyAccountForm.setMessage("");
155            }
156            OlePatronDocument olePatronDocument = new OlePatronDocument();
157            olePatronDocument = olePatronDocument.from(olePatronDefinition);
158            List<EntityAddressBo> entityAddressBos = olePatronDocument.getAddresses();
159            List<OleEntityAddressBo> oleEntityAddressBos = new ArrayList<OleEntityAddressBo>();
160            OleEntityAddressBo oleEntityAddressBo = new OleEntityAddressBo();
161            Map<String, String> criteriaMap;
162            if (entityAddressBos.size() > 0) {
163                for (EntityAddressBo entityAddressBo : entityAddressBos) {
164                    oleEntityAddressBo = new OleEntityAddressBo();
165                    criteriaMap = new HashMap<String, String>();
166                    criteriaMap.put(OLEConstants.OlePatron.ENTITY_ADDRESS_ID, entityAddressBo.getId());
167                    List<OleAddressBo> oleAddressBos = (List<OleAddressBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class, criteriaMap);
168                    if (oleAddressBos.size() > 0) {
169                        oleEntityAddressBo.setOleAddressBo(oleAddressBos.get(0));
170                    }
171                    oleEntityAddressBo.setEntityAddressBo(entityAddressBo);
172                    oleEntityAddressBos.add(oleEntityAddressBo);
173                }
174            }
175            olePatronDocument.setOleEntityAddressBo(oleEntityAddressBos);
176            String patronId = "";
177            List<OleProxyPatronDocument> oleProxyPatronDocuments = olePatronDocument.getOleProxyPatronDocuments();
178            if (oleProxyPatronDocuments.size() > 0) {
179                for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocuments) {
180                    patronId = oleProxyPatronDocument.getProxyPatronId();
181                    OlePatronDefinition patronDefinition = getOleMyAccountProcess().getPatronInfo(patronId);
182                    oleProxyPatronDocument.setProxyPatronFirstName(patronDefinition.getName().getFirstName());
183                    oleProxyPatronDocument.setProxyPatronLastName(patronDefinition.getName().getLastName());
184                    oleProxyPatronDocument.setProxyPatronBarcode(patronDefinition.getBarcode());
185
186                }
187            }
188            oleMyAccountForm.setOlePatronDocument(olePatronDocument);
189            oleMyAccountForm.setBarcode(olePatronDocument.getBarcode());
190            //setEmails(oleMyAccountForm,olePatronDocument);
191            searchPatron(oleMyAccountForm, result, request, response);
192        } catch (Exception e) {
193            oleMyAccountForm.setInformation(e.getMessage());
194            LOG.error("Exception", e);
195        }
196
197        return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
198    }
199
200    /**
201     * This method performs the renewal item
202     *
203     * @param form
204     * @param result
205     * @param request
206     * @param response
207     * @return ModelAndView
208     */
209    @RequestMapping(params = "methodToCall=renewalItem")
210    public ModelAndView renewalItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
211                                    HttpServletRequest request, HttpServletResponse response) {
212        LOG.debug("Inside renewalItem method");
213        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
214        oleMyAccountForm.setInformation("");
215        String renewalItemId = oleMyAccountForm.getItem();
216        OleRenewalLoanDocument oleRenewalLoanDocument = null;
217        boolean renewalFlag = false;
218        for (int i = 0; i < oleMyAccountForm.getExistingLoanList().size(); i++) {
219            oleRenewalLoanDocument = oleMyAccountForm.getExistingLoanList().get(i);
220            if (oleRenewalLoanDocument.getItemBarcode().equals(renewalItemId)) {
221                renewalFlag = true;
222                break;
223            }
224        }
225        if (renewalFlag) {
226            List<OleRenewalLoanDocument> oleRenewalLoanDocumentList = new ArrayList<OleRenewalLoanDocument>();
227            oleRenewalLoanDocumentList.add(oleRenewalLoanDocument);
228            oleRenewalLoanDocumentList = getOleMyAccountProcess().performRenewalItem(oleRenewalLoanDocumentList);
229            oleRenewalLoanDocument = oleRenewalLoanDocumentList.get(0);
230            oleMyAccountForm.setInformation(oleRenewalLoanDocument.getMessageInfo());
231
232        } else
233            oleMyAccountForm.setInformation("Select an item(s).");
234
235        oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(oleMyAccountForm.getPatronId()));
236        return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
237    }
238
239    /**
240     * This method performs the selected  renewal items operation
241     *
242     * @param form
243     * @param result
244     * @param request
245     * @param response
246     * @return ModelAndView
247     */
248    @RequestMapping(params = "methodToCall=renewalItems")
249    public ModelAndView renewalItems(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
250                                     HttpServletRequest request, HttpServletResponse response) {
251        LOG.debug("Inside renewalItems method");
252        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
253        oleMyAccountForm.setInformation("");
254        boolean renewalFlag = false;
255        List<OleRenewalLoanDocument> oleRenewalLoanDocumentList = new ArrayList<OleRenewalLoanDocument>();
256        for (int i = 0; i < oleMyAccountForm.getExistingLoanList().size(); i++) {
257            OleRenewalLoanDocument oleRenewalLoanDocument = oleMyAccountForm.getExistingLoanList().get(i);
258            if (oleRenewalLoanDocument.isItemCheckFlag()) {
259                renewalFlag = true;
260                oleRenewalLoanDocumentList.add(oleRenewalLoanDocument);
261            }
262        }
263        if (renewalFlag) {
264            oleRenewalLoanDocumentList = getOleMyAccountProcess().performRenewalItem(oleRenewalLoanDocumentList);
265            for (int i = 0; i < oleRenewalLoanDocumentList.size(); i++) {
266                String errMsg = oleRenewalLoanDocumentList.get(i).getMessageInfo();
267                oleMyAccountForm.setInformation(oleMyAccountForm.getInformation() == null ? "" : oleMyAccountForm.getInformation() + "\n" + (i + 1) + ". " + errMsg + "  (" + oleRenewalLoanDocumentList.get(i).getItemBarcode() + ")<br/>");
268
269            }
270
271        } else
272            oleMyAccountForm.setInformation("Select an item(s).");
273        oleMyAccountForm.setExistingLoanList(getOleMyAccountProcess().getPatronLoanedItems(oleMyAccountForm.getPatronId()));
274
275        return getUIFModelAndView(oleMyAccountForm, "RenewalItemViewPage");
276    }
277
278    /**
279     * This method clear UI for next borrower session..
280     *
281     * @param form
282     * @param result
283     * @param request
284     * @param response
285     * @return ModelAndView
286     */
287    @RequestMapping(params = "methodToCall=saveAndClear")
288    public ModelAndView clearPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
289                                    HttpServletRequest request, HttpServletResponse response) {
290        LOG.debug("Inside clearPatron method");
291        OleMyAccountForm oleLoanForm = (OleMyAccountForm) form;
292        oleLoanForm.setInformation("");
293        oleLoanForm.setPatronBarcode(null);
294        oleLoanForm.setPatronName(null);
295        oleLoanForm.setExistingLoanList(null);
296
297        return getUIFModelAndView(oleLoanForm, "RenewalItemViewPage");
298    }
299
300
301    public OleMyAccountProcess getOleMyAccountProcess() {
302
303        if (oleMyAccountProcess == null)
304            oleMyAccountProcess = new OleMyAccountProcess();
305        return oleMyAccountProcess;
306    }
307
308    /**
309     * This method will save or update the patron.document from the myaccount screen.
310     *
311     * @param form
312     * @param result
313     * @param request
314     * @param response
315     * @return ModelAndView
316     */
317    @RequestMapping(params = "methodToCall=savePatron")
318    public ModelAndView savePatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
319                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
320        LOG.debug("Inside savePatron method");
321        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
322        OlePatronService olePatronService = new OlePatronServiceImpl();
323        OlePatronDocument olePatronDocument = oleMyAccountForm.getOlePatronDocument();
324        EntityBo entity = olePatronDocument.getEntity();
325        List<EntityEmailBo> entityEmailBos = entity.getEntityTypeContactInfos().get(0).getEmailAddresses();
326        List<EntityPhoneBo> entityPhoneBos = entity.getEntityTypeContactInfos().get(0).getPhoneNumbers();
327        List<EntityAddressBo> entityAddressBos = new ArrayList<EntityAddressBo>();
328        EntityAddressBo entityAddressBo = new EntityAddressBo();
329        List<OleAddressBo> oleAddressBos = new ArrayList<OleAddressBo>();
330        OleAddressBo oleAddressBo = new OleAddressBo();
331        List<OleEntityAddressBo> oleEntityAddressBos = olePatronDocument.getOleEntityAddressBo();
332        if (oleEntityAddressBos.size() > 0) {
333            for (OleEntityAddressBo oleEntityAddressBo : oleEntityAddressBos) {
334                oleAddressBo = oleEntityAddressBo.getOleAddressBo();
335                oleAddressBos.add(oleAddressBo);
336                entityAddressBo = oleEntityAddressBo.getEntityAddressBo();
337                entityAddressBos.add(entityAddressBo);
338            }
339            olePatronDocument.setOleAddresses(oleAddressBos);
340            olePatronDocument.setAddresses(entityAddressBos);
341        }
342        boolean addressSource = olePatronHelperService.checkAddressSource(olePatronDocument.getOleAddresses());
343        boolean emailDefault = olePatronHelperService.checkEmailMultipleDefault(entityEmailBos);
344        boolean phoneDefault = olePatronHelperService.checkPhoneMultipleDefault(entityPhoneBos);
345        boolean addressDefault = olePatronHelperService.checkAddressMultipleDefault(oleEntityAddressBos);
346        if (addressSource) {
347            if (emailDefault && phoneDefault && addressDefault) {
348                OlePatronDefinition olePatronDefinition = olePatronService.updatePatron(OlePatronDocument.to(oleMyAccountForm.getOlePatronDocument()));
349                cancel(oleMyAccountForm, result, request, response);
350            } else {
351                oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_DEFAULT_MESSAGE);
352                return getUIFModelAndView(oleMyAccountForm);
353            }
354        } else {
355            oleMyAccountForm.setMessage(OLEConstants.OlePatron.ERROR_ADDRESS_SOURCE_REQUIRED);
356            return getUIFModelAndView(oleMyAccountForm);
357        }
358        oleMyAccountForm.setMessage(OLEConstants.OlePatron.SAVE_SUCCESSFUL_MSG);
359        //updatePatron(olePatronForm,olePatronDefinition);
360        //setProxyPatrons(olePatronForm, olePatronDefinition);
361        return getUIFModelAndView(oleMyAccountForm);
362    }
363
364    /**
365     * Just returns as if return with no value was selected.
366     */
367    @Override
368    @RequestMapping(params = "methodToCall=cancel")
369    public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
370                               HttpServletRequest request, HttpServletResponse response) {
371        LOG.debug("Inside the cancel method");
372        OleMyAccountForm oleMyAccountForm = (OleMyAccountForm) form;
373        oleMyAccountForm.setBarcode(null);
374        //oleMyAccountForm.setMessage();
375        return getUIFModelAndView(oleMyAccountForm);
376    }
377
378    /**
379     * Just returns as if return with no value was selected.
380     */
381    @Override
382    @RequestMapping(params = "methodToCall=back")
383    public ModelAndView back(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
384                              HttpServletRequest request, HttpServletResponse response) {
385        return super.back(form, result, request, response);
386    }
387
388}