View Javadoc
1   package org.kuali.ole.deliver.controller;
2   
3   import java.awt.Graphics2D;
4   import java.awt.image.BufferedImage;
5   import java.io.ByteArrayInputStream;
6   import java.io.ByteArrayOutputStream;
7   import java.text.DateFormat;
8   import java.text.ParseException;
9   import java.text.SimpleDateFormat;
10  import java.util.ArrayList;
11  import java.util.Date;
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Map;
16  
17  import javax.imageio.ImageIO;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.kuali.ole.OLEConstants;
24  import org.kuali.ole.deliver.bo.OleAddressBo;
25  import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
26  import org.kuali.ole.deliver.bo.OleEntityAddressBo;
27  import org.kuali.ole.deliver.bo.OleItemSearch;
28  import org.kuali.ole.deliver.bo.OleLoanDocument;
29  import org.kuali.ole.deliver.bo.OlePatronAffiliation;
30  import org.kuali.ole.deliver.bo.OlePatronDocument;
31  import org.kuali.ole.deliver.bo.OlePatronLocalIdentificationBo;
32  import org.kuali.ole.deliver.bo.OlePatronLostBarcode;
33  import org.kuali.ole.deliver.bo.OleProxyPatronDocument;
34  import org.kuali.ole.deliver.bo.OleTemporaryCirculationHistory;
35  import org.kuali.ole.deliver.form.OlePatronMaintenanceDocumentForm;
36  import org.kuali.ole.deliver.processor.LoanProcessor;
37  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
38  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
39  import org.kuali.ole.docstore.common.document.ItemOleml;
40  import org.kuali.ole.docstore.common.document.content.enums.DocType;
41  import org.kuali.ole.docstore.common.document.content.instance.Item;
42  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
43  import org.kuali.ole.service.OlePatronHelperService;
44  import org.kuali.ole.service.OlePatronHelperServiceImpl;
45  import org.kuali.ole.service.OlePatronMaintenanceDocumentServiceImpl;
46  import org.kuali.ole.service.OlePatronService;
47  import org.kuali.ole.sys.context.SpringContext;
48  import org.kuali.rice.core.api.datetime.DateTimeService;
49  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
50  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
51  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
52  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
53  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
54  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
55  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
56  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
57  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
58  import org.kuali.rice.krad.maintenance.MaintenanceUtils;
59  import org.kuali.rice.krad.service.KRADServiceLocator;
60  import org.kuali.rice.krad.service.MaintenanceDocumentService;
61  import org.kuali.rice.krad.uif.UifParameters;
62  import org.kuali.rice.krad.uif.container.CollectionGroup;
63  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
64  import org.kuali.rice.krad.uif.util.ProcessLogger;
65  import org.kuali.rice.krad.util.GlobalVariables;
66  import org.kuali.rice.krad.util.KRADConstants;
67  import org.kuali.rice.krad.util.ObjectUtils;
68  import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
69  import org.kuali.rice.krad.web.form.DocumentFormBase;
70  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
71  import org.kuali.rice.krad.web.form.UifFormBase;
72  import org.springframework.stereotype.Controller;
73  import org.springframework.validation.BindingResult;
74  import org.springframework.web.bind.annotation.ModelAttribute;
75  import org.springframework.web.bind.annotation.RequestMapping;
76  import org.springframework.web.bind.annotation.RequestMethod;
77  import org.springframework.web.multipart.MultipartFile;
78  import org.springframework.web.servlet.ModelAndView;
79  
80  /**
81   * .OlePatronMaintenanceDocumentController invokes MaintenanceDocumentController and returns instance of MaintenanceDocumentService.
82   */
83  @Controller
84  @RequestMapping(value = "/patronMaintenance")
85  public class OlePatronMaintenanceDocumentController extends MaintenanceDocumentController {
86  
87      private static final Logger LOG = Logger.getLogger(OlePatronMaintenanceDocumentController.class);
88      private OlePatronService olePatronService;
89      private OlePatronHelperService olePatronHelperService = new OlePatronHelperServiceImpl();
90      private byte[] imageInByte;
91      private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService;
92      private DateTimeService dateTimeService;
93  
94      @Override
95  	protected MaintenanceDocumentForm createInitialForm(
96  			HttpServletRequest request) {
97  		return new OlePatronMaintenanceDocumentForm();
98  	}
99  
100 	public DateTimeService getDateTimeService() {
101         return (DateTimeService)SpringContext.getService("dateTimeService");
102     }
103 
104     public void setDateTimeService(DateTimeService dateTimeService) {
105         this.dateTimeService = dateTimeService;
106     }
107 
108     public OlePatronService getOlePatronService() {
109         if (olePatronService != null) {
110             olePatronService = GlobalResourceLoader.getService(OLEConstants.OlePatron.OLE_PATRON_SERVICE);
111         }
112         return olePatronService;
113     }
114 
115     private LoanProcessor loanProcessor;
116 
117     private OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
118         if (oleDeliverRequestDocumentHelperService == null) {
119             oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
120         }
121         return oleDeliverRequestDocumentHelperService;
122     }
123 
124     private DocstoreClientLocator docstoreClientLocator;
125 
126     public DocstoreClientLocator getDocstoreClientLocator() {
127 
128         if (docstoreClientLocator == null) {
129             docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
130 
131         }
132         return docstoreClientLocator;
133     }
134 
135     /**
136      * This method returns the instance of olePatronMaintenanceDocumentService
137      *
138      * @return olePatronMaintenanceDocumentService(MaintenanceDocumentService)
139      */
140     @Override
141     protected MaintenanceDocumentService getMaintenanceDocumentService() {
142         return GlobalResourceLoader.getService(OLEConstants.OlePatron.OLE_PATRON_MAINTENANCE_DOC_SERVICE);
143     }
144 
145 
146     @Override
147     @RequestMapping(params = "methodToCall=maintenanceEdit")
148     public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
149                                         HttpServletRequest request, HttpServletResponse response) throws Exception {
150 
151         MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
152         setupMaintenance(form, request, KRADConstants.MAINTENANCE_EDIT_ACTION);
153         MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
154         super.maintenanceEdit(form, result, request, response);
155         OlePatronDocument olePatronDocument = (OlePatronDocument) document.getOldMaintainableObject().getDataObject();
156         olePatronDocument.getAddresses().clear();
157         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
158         if (olePatronDocument.getActivationDate() == null)
159             olePatronDocument.setActivationDate(getDateTimeService().getCurrentDate());
160         OlePatronDocument patronDocument = (OlePatronDocument)document.getNewMaintainableObject().getDataObject();
161         patronDocument.setBarcodeEditable(false);
162         if((patronDocument.getExpirationDate() != null && patronDocument.getActivationDate() != null && fmt.format(patronDocument.getActivationDate()).compareTo(fmt.format(patronDocument.getExpirationDate())) >= 0) || (patronDocument.getExpirationDate() != null && fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(patronDocument.getExpirationDate())) > 0)){
163 
164             patronDocument.setExpirationFlag(false);
165         }
166         olePatronDocument.setShowLoanedRecords(false);
167         olePatronDocument.setShowRequestedItems(false);
168         olePatronDocument.setShowTemporaryCirculationHistoryRecords(false);
169         return getUIFModelAndView(form);
170     }
171 
172     @Override
173     @RequestMapping(params = "methodToCall=maintenanceCopy")
174     public ModelAndView maintenanceCopy(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
175         MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
176         setupMaintenance(form, request, KRADConstants.MAINTENANCE_COPY_ACTION);
177         super.maintenanceCopy(form, result, request, response);
178         MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
179         OlePatronDocument patronDocument=(OlePatronDocument)document.getNewMaintainableObject().getDataObject();
180         if(patronDocument.getOlePatronLocalIds()!=null && patronDocument.getOlePatronLocalIds().size()>0){
181            for(OlePatronLocalIdentificationBo  olePatronLocalIdentificationBo:patronDocument.getOlePatronLocalIds()){
182                 olePatronLocalIdentificationBo.setPatronLocalSeqId(null);
183                 olePatronLocalIdentificationBo.setOlePatronId(null);
184                 olePatronLocalIdentificationBo.setOlePatronDocument(null);
185            }
186         }
187         patronDocument.setShowLoanedRecords(false);
188         patronDocument.setShowRequestedItems(false);
189         patronDocument.setShowTemporaryCirculationHistoryRecords(false);
190         return getUIFModelAndView(form);
191     }
192 
193     /**
194      * Use to create a link delete in the lookup result action field which will use to delete patron record..
195      *
196      * @param form
197      * @param result
198      * @param request
199      * @param response
200      * @return ModelAndView
201      * @throws Exception
202      */
203     @RequestMapping(params = "methodToCall=" + "maintenanceDelete")
204     public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
205                                           HttpServletRequest request, HttpServletResponse response) throws Exception {
206         LOG.debug(" Inside maintenanceDelete ");
207         setupMaintenanceForDelete(form, request, OLEConstants.OlePatron.OLE_PATRON_DELETE);
208         MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
209         MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
210         OlePatronDocument olePatronDocument = (OlePatronDocument) document.getOldMaintainableObject().getDataObject();
211         OlePatronDocument patronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
212         olePatronDocument.setBarcodeEditable(false);
213         patronDocument.setBarcodeEditable(false);
214         olePatronDocument.setShowLoanedRecords(false);
215         olePatronDocument.setShowRequestedItems(false);
216         olePatronDocument.setShowTemporaryCirculationHistoryRecords(false);
217         return getUIFModelAndView(form);
218     }
219 
220     /**
221      * To delete the whole patron document.
222      *
223      * @param form
224      * @param result
225      * @param request
226      * @param response
227      * @return Close the document
228      * @throws Exception
229      */
230     @RequestMapping(params = "methodToCall=" + "deleteDocument")
231     public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
232                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
233 
234         LOG.debug(" Inside deleteDocument ");
235         MaintenanceDocument document = form.getDocument();
236         OlePatronDocument olePatronDocument = new OlePatronDocument();
237         try {
238             if (document.getDocumentDataObject() != null) {
239                 olePatronDocument = (OlePatronDocument) document.getDocumentDataObject();
240                 if (olePatronDocument != null && olePatronDocument.getOlePatronId() != null) {
241                     boolean deletePatronDetail = olePatronHelperService.deletePatron(olePatronDocument);
242                     if (deletePatronDetail) {
243                         return back(form, result, request, response);
244                     } else {
245                         GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OlePatron.ERROR_PATRON_HAS_LOAN);
246                         return getUIFModelAndView(form);
247                     }
248                 } else {
249                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OlePatron.ERROR_PATRON_NOT_FOUND);
250                     return getUIFModelAndView(form);
251                 }
252             }
253         } catch (Exception ex) {
254             LOG.error("Exception while delete document", ex);
255         }
256         return back(form, result, request, response);
257     }
258 
259 
260     /**
261      * This method populates confirmation to delete the document.
262      *
263      * @param form
264      * @param request
265      * @param maintenanceAction
266      */
267     protected void setupMaintenanceForDelete(MaintenanceDocumentForm form, HttpServletRequest request, String maintenanceAction) {
268         LOG.debug(" Inside setupMaintenanceForDelete ");
269         MaintenanceDocument document = form.getDocument();
270         if (document == null) {
271             document = getMaintenanceDocumentService()
272                     .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
273                             maintenanceAction);
274 
275             form.setDocument(document);
276             form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
277         }
278 
279         form.setMaintenanceAction(maintenanceAction);
280         OlePatronMaintenanceDocumentServiceImpl olePatronMaintenanceDocumentServicec = (OlePatronMaintenanceDocumentServiceImpl) getMaintenanceDocumentService();
281         olePatronMaintenanceDocumentServicec.setupMaintenanceObjectForDelete(document, maintenanceAction, request.getParameterMap());
282         MaintenanceUtils.checkForLockingDocument(document, false);
283     }
284 
285 
286     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=searchAddLine")
287     public ModelAndView searchAddLine(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
288                                       HttpServletRequest request, HttpServletResponse response) {
289         MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
290         MaintenanceDocument maintenanceDocument = (MaintenanceDocument) maintenanceForm.getDocument();
291         OlePatronDocument olePatronDocument = (OlePatronDocument) maintenanceDocument.getNewMaintainableObject().getDataObject();
292         String selectedCollectionPath = maintenanceForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
293         CollectionGroup collectionGroup = maintenanceForm.getPostedView().getViewIndex().getCollectionGroupByPath(
294                 selectedCollectionPath);
295         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
296         Object eventObject = ObjectPropertyUtils.getPropertyValue(maintenanceForm, addLinePath);
297         OleProxyPatronDocument oleProxyPatronDocument = (OleProxyPatronDocument) eventObject;
298         Map<String, String> proxyMap = new HashMap<String, String>();
299         proxyMap.put(OLEConstants.OlePatron.BARCODE, oleProxyPatronDocument.getProxyPatronBarcode());
300         OlePatronDocument tempDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, proxyMap);
301         if (tempDocument != null) {
302             oleProxyPatronDocument.setProxyPatronId(tempDocument.getOlePatronId());
303             oleProxyPatronDocument.setProxyPatronBarcode(tempDocument.getBarcode());
304             oleProxyPatronDocument.setProxyPatronFirstName(tempDocument.getEntity().getNames().get(0).getFirstName());
305             oleProxyPatronDocument.setProxyPatronLastName(tempDocument.getEntity().getNames().get(0).getLastName());
306         } else {
307             GlobalVariables.getMessageMap().putError(OLEConstants.OleDeliverRequest.BORROWER_ID, OLEConstants.OleDeliverRequest.INVALID_PATRON);
308         }
309         ModelAndView modelAndView = super.addLine(form, result, request, response);
310         return modelAndView;
311     }
312 
313 
314 
315 
316     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addPhoneNumber")
317     public ModelAndView addPhoneNumber(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
318                                    HttpServletRequest request, HttpServletResponse response) {
319         MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
320         MaintenanceDocument maintenanceDocument = (MaintenanceDocument) maintenanceForm.getDocument();
321         OlePatronDocument olePatronDocument = (OlePatronDocument) maintenanceDocument.getNewMaintainableObject().getDataObject();
322         String selectedCollectionPath = maintenanceForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
323         CollectionGroup collectionGroup = maintenanceForm.getPostedView().getViewIndex().getCollectionGroupByPath(
324                 selectedCollectionPath);
325         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
326         Object eventObject = ObjectPropertyUtils.getPropertyValue(maintenanceForm, addLinePath);
327         EntityPhoneBo entityPhoneBo = (EntityPhoneBo) eventObject;
328         boolean isValidPhoneNumber=false;
329         if (LOG.isDebugEnabled()) {
330             LOG.debug("Validating the Phone Number  Format - ##########, (###)###-#### , ###-###-#### , ### ###-#### , ### ### ####");
331         }
332         if (entityPhoneBo.getPhoneNumber().matches("\\d{10}")) isValidPhoneNumber=true;
333         else if (entityPhoneBo.getPhoneNumber().matches("\\d{3}[-]\\d{3}[-]\\d{4}")) isValidPhoneNumber=true;
334         else if (entityPhoneBo.getPhoneNumber().matches("\\d{3}[\\s]\\d{3}[-]\\d{4}")) isValidPhoneNumber=true;
335         else if (entityPhoneBo.getPhoneNumber().matches("\\d{3}[\\s]\\d{3}[\\s]\\d{4}")) isValidPhoneNumber=true;
336         else if (entityPhoneBo.getPhoneNumber().matches("\\(\\d{3}\\)[\\s]\\d{3}[-]\\d{4}")) isValidPhoneNumber=true;
337         else isValidPhoneNumber=false;
338         if(!isValidPhoneNumber){
339             GlobalVariables.getMessageMap().putErrorForSectionId("OlePatronDocument-Phone", OLEConstants.INVALID_PHONE_NUMBER_FORMAT);
340         }
341         if (isValidPhoneNumber) {
342             ModelAndView modelAndView = super.addLine(form, result, request, response);
343             return modelAndView;
344         } else {
345             return getUIFModelAndView(form);
346         }
347     }
348 
349     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addEmailAddress")
350     public ModelAndView addEmailAddress(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
351                                    HttpServletRequest request, HttpServletResponse response) {
352         MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
353         MaintenanceDocument maintenanceDocument = (MaintenanceDocument) maintenanceForm.getDocument();
354         OlePatronDocument olePatronDocument = (OlePatronDocument) maintenanceDocument.getNewMaintainableObject().getDataObject();
355         String selectedCollectionPath = maintenanceForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
356         CollectionGroup collectionGroup = maintenanceForm.getPostedView().getViewIndex().getCollectionGroupByPath(
357                 selectedCollectionPath);
358         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
359         Object eventObject = ObjectPropertyUtils.getPropertyValue(maintenanceForm, addLinePath);
360         EntityEmailBo  emailBo = (EntityEmailBo) eventObject;
361         return super.addLine(form, result, request, response);
362     }
363 
364     @Override
365     public ModelAndView save(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
366         DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
367         LOG.debug(" Inside route method of patron maintenance controller ");
368         ModelAndView modelAndView;
369         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
370         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
371         String action = mainForm.getMaintenanceAction();
372         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
373         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
374         List<OleLoanDocument> oleLoanDocumentList = newOlePatronDocument.getOleLoanDocuments();
375         for (OleLoanDocument oleLoanDocument : oleLoanDocumentList) {
376             if (oleLoanDocument.isMissingPieceFlag()) {
377                 if (oleLoanDocument.getMissingPiecesCount() == null || oleLoanDocument.getMissingPiecesCount() != null && oleLoanDocument.getMissingPiecesCount().equalsIgnoreCase("")) {
378                     oleLoanDocument.setNoOfMissingPiecesEditable(true);
379                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_INFO);
380                     return getUIFModelAndView(form);
381                 }
382                 if (oleLoanDocument.getMissingPiecesCount() != null && !oleLoanDocument.getMissingPiecesCount().equalsIgnoreCase("")) {
383                     int count = Integer.parseInt(oleLoanDocument.getMissingPiecesCount());
384                     if (count < 1) {
385                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_COUNT_GREATER, new String[]{"Missing Piece Count", "1"});
386                         return getUIFModelAndView(form);
387                     }
388                 }
389                 if (oleLoanDocument.getItemNumberOfPieces() == null || oleLoanDocument.getItemNumberOfPieces() != null && oleLoanDocument.getItemNumberOfPieces().toString().equalsIgnoreCase("")) {
390                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_ITEM_INFO);
391                     oleLoanDocument.setNoOfPiecesEditable(true);
392                     return getUIFModelAndView(form);
393                 }
394                 if(oleLoanDocument.getItemNumberOfPieces().intValue()<1){
395                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_COUNT_GREATER, new String[]{"no of piece", "1"});
396                     return getUIFModelAndView(form);
397                 }
398                 int missingPieceCount=Integer.parseInt(oleLoanDocument.getMissingPiecesCount());
399                 int numberOfPieces=oleLoanDocument.getItemNumberOfPieces();
400                 if(missingPieceCount>numberOfPieces){
401                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_COUNT_LESSER, new String[]{"Missing Piece Count", "no of pieces"});
402                     return getUIFModelAndView(form);
403                 }
404             }
405 
406         }
407         for (OleLoanDocument oleLoanDocument : oleLoanDocumentList) {
408             try {
409                 String itemXmlContent = getLoanProcessor().getItemXML(oleLoanDocument.getItemUuid());
410                 Item oleItem = getLoanProcessor().getItemPojo(itemXmlContent);
411 
412 
413                 oleItem.setClaimsReturnedFlag(oleLoanDocument.isClaimsReturnedIndicator());
414                 if (oleItem.isClaimsReturnedFlag()) {
415                     getOleDeliverRequestDocumentHelperService().cancelPendingRequestForClaimsReturnedItem(oleItem.getItemIdentifier());
416                     oleItem.setClaimsReturnedNote(oleLoanDocument.getClaimsReturnNote());
417                     oleItem.setClaimsReturnedFlagCreateDate(df.format(getDateTimeService().getCurrentDate()));
418                 } else {
419                     oleItem.setClaimsReturnedNote(null);
420                     oleItem.setClaimsReturnedFlagCreateDate(null);
421                 }
422                 oleItem.setMissingPieceFlag(oleLoanDocument.isMissingPieceFlag());
423                 oleItem.setMissingPieceFlagNote(oleLoanDocument.getMissingPieceNote());
424                 oleItem.setItemDamagedStatus(oleLoanDocument.isItemDamagedStatus());
425                 oleItem.setDamagedItemNote(oleLoanDocument.getItemDamagedNote());
426                 oleItem.setMissingPiecesCount(oleLoanDocument.getMissingPiecesCount());
427                 if(oleLoanDocument.getItemNumberOfPieces()!=null&& !oleLoanDocument.getItemNumberOfPieces().toString().equalsIgnoreCase("")){
428                     oleItem.setNumberOfPieces(oleLoanDocument.getItemNumberOfPieces().toString());
429                 }
430                 org.kuali.ole.docstore.common.document.Item itemXML = new ItemOleml();
431                 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
432                 itemXML.setContent(itemOlemlRecordProcessor.toXML(oleItem));
433                 itemXML.setCategory(OLEConstants.WORK_CATEGORY);
434                 itemXML.setType(DocType.ITEM.getCode());
435                 itemXML.setFormat(OLEConstants.OLEML_FORMAT);
436                 itemXML.setId(oleLoanDocument.getItemUuid());
437                 getDocstoreClientLocator().getDocstoreClient().updateItem(itemXML);
438             } catch (Exception e) {
439                 throw new RuntimeException(e);
440             }
441         }
442         if(!olePatronHelperService.validatePatron(newOlePatronDocument)) {
443             return getUIFModelAndView(form);
444         }
445         if (!KRADConstants.MAINTENANCE_EDIT_ACTION.equals(action)) {
446             if (newOlePatronDocument.getOlePatronId() == null || newOlePatronDocument.getOlePatronId().isEmpty()) {
447                 if (newOlePatronDocument.getActivationDate() != null && fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(newOlePatronDocument.getActivationDate())) > 0) {
448                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_ACTIVATION_DATE);
449 
450                     return getUIFModelAndView(mainForm);
451                 }
452             }
453         } else {
454             Map<String, String> patronMap = new HashMap<String, String>();
455             patronMap.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
456             OlePatronDocument patronDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
457             SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
458             Date todayDate=getDateTimeService().getCurrentDate();
459             String format=simpleDateFormat.format(getDateTimeService().getCurrentDate());
460             try {
461                 todayDate=simpleDateFormat.parse(format);
462             } catch (ParseException e) {
463                 e.printStackTrace();
464             }
465             if(newOlePatronDocument.getActivationDate()!=null && newOlePatronDocument.getActivationDate().compareTo(todayDate)<0 && patronDocument!=null && patronDocument.getActivationDate()!=null && patronDocument.getActivationDate().compareTo(newOlePatronDocument.getActivationDate())>0){
466                 if (!newOlePatronDocument.isPopupDialog()) {
467                     newOlePatronDocument.setPopupDialog(true);
468                     newOlePatronDocument.setUiMessageType("patron-message-info");
469                     newOlePatronDocument.setPatronMessage(OLEConstants.OlePatron.ERROR_PATRON_NEW_PAST_DATE+"<br/>"+OLEConstants.PROCEED_MESSAGE);
470                     return getUIFModelAndView(mainForm);
471                 }
472             }
473         }
474 
475         newOlePatronDocument.setPopupDialog(false);
476         newOlePatronDocument.setOleLoanDocuments(oleLoanDocumentList);
477         List<OleProxyPatronDocument> oleProxyPatronDocumentList = newOlePatronDocument.getOleProxyPatronDocuments();
478         List<OleProxyPatronDocument> proxyPatronDocumentList = new ArrayList<OleProxyPatronDocument>();
479         if (oleProxyPatronDocumentList.size() > 0) {
480             for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
481                 Map<String, String> proxyMap = new HashMap<String, String>();
482                 proxyMap.put(OLEConstants.OlePatron.PATRON_ID, oleProxyPatronDocument.getProxyPatronId());
483                 OlePatronDocument tempDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, proxyMap);
484                 if (tempDocument != null) {
485                     oleProxyPatronDocument.setProxyPatronBarcode(tempDocument.getBarcode());
486                     oleProxyPatronDocument.setProxyPatronFirstName(tempDocument.getEntity().getNames().get(0).getFirstName());
487                     oleProxyPatronDocument.setProxyPatronLastName(tempDocument.getEntity().getNames().get(0).getLastName());
488                     proxyPatronDocumentList.add(oleProxyPatronDocument);
489                 }
490             }
491             newOlePatronDocument.setOleProxyPatronDocuments(proxyPatronDocumentList);
492         }
493         // Lost and reinstate barcode section
494         if (newOlePatronDocument.getOlePatronId() != null && !newOlePatronDocument.getOlePatronId().equalsIgnoreCase("")) {
495             Map<String, String> map = new HashMap<String, String>();
496             map.put(OLEConstants.OlePatron.PATRON_LOST_BARCODE_FLD, newOlePatronDocument.getBarcode());
497             map.put("active", "N");
498             List<OlePatronLostBarcode> olePatronLostBarcodes = (List<OlePatronLostBarcode>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronLostBarcode.class, map);
499             if (olePatronLostBarcodes.size() > 0 && (!newOlePatronDocument.isSkipBarcodeValidation())) {
500                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_DUPLICATE_BARCODE);
501                 return getUIFModelAndView(mainForm);
502             }
503             // LOST Barcode Validation for current lost section
504             boolean isBarcodeContainsInLostSection = false;
505             if(newOlePatronDocument.getLostBarcodes()!=null&& newOlePatronDocument.getBarcode()!=null){
506                 // if barcode contains in lost section submitting the document will reinstate the barcode
507                 for(OlePatronLostBarcode olePatronLostBarcode:olePatronLostBarcodes){
508                     if(olePatronLostBarcode.getInvalidOrLostBarcodeNumber()!=null && olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(newOlePatronDocument.getBarcode())){
509                         isBarcodeContainsInLostSection=true;
510                     }
511                 }
512             }
513             // if barcode is editable mode and shouldn't be reinstated
514             if (newOlePatronDocument.isBarcodeEditable()&& !newOlePatronDocument.isReinstated()) {
515                 for (OlePatronLostBarcode olePatronLostBarcode : newOlePatronDocument.getLostBarcodes()) {
516                     if (olePatronLostBarcode.getInvalidOrLostBarcodeNumber() != null && olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(newOlePatronDocument.getBarcode())) {
517                         GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_BARCODE_BLOCK_MANUALLY,new String[]{newOlePatronDocument.getBarcode()});
518                         newOlePatronDocument.setBarcode(null);
519                         newOlePatronDocument.setBarcodeEditable(true);
520                         newOlePatronDocument.setReinstated(false);
521                         return getUIFModelAndView(mainForm);
522                     }
523                 }
524             }
525             if (!isBarcodeContainsInLostSection) {
526                 //JIRA OLE-5707
527                 Map<String, String> patronMap = new HashMap<String, String>();
528                 patronMap.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
529                 OlePatronDocument patronDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
530                 if (patronDocument != null && patronDocument.getBarcode() != null && !(patronDocument.getBarcode().equalsIgnoreCase(""))) {
531                     if (newOlePatronDocument.getLostBarcodes() == null) {
532                         newOlePatronDocument.setLostBarcodes(new ArrayList<OlePatronLostBarcode>());
533                     }
534                     if (!patronDocument.getBarcode().equalsIgnoreCase(newOlePatronDocument.getBarcode()) && (!newOlePatronDocument.isBarcodeChanged())) {
535                         if (patronDocument != null && patronDocument.getBarcode() != null) {
536                             boolean isNeedToUpdateBarcode=false;
537                             String originalBarcode=patronDocument.getBarcode();
538                             for(OlePatronLostBarcode olePatronLostBarcode:newOlePatronDocument.getLostBarcodes()){
539                                 if(olePatronLostBarcode.getInvalidOrLostBarcodeNumber()!=null && olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(originalBarcode)){
540                                     isNeedToUpdateBarcode=true;
541                                 }
542                             }
543                             if (!isNeedToUpdateBarcode) {
544                                 OlePatronLostBarcode olePatronLostBarcode = new OlePatronLostBarcode();
545                                 olePatronLostBarcode.setInvalidOrLostBarcodeNumber(patronDocument.getBarcode());
546                                 olePatronLostBarcode.setInvalidOrLostBarcodeEffDate(new java.sql.Date(getDateTimeService().getCurrentDate().getTime()));
547                                 olePatronLostBarcode.setRevertBarcode(true);
548                                 olePatronLostBarcode.setDescription("");
549                                 olePatronLostBarcode.setStatus("Lost");
550                                 newOlePatronDocument.getLostBarcodes().add(olePatronLostBarcode);
551                             }
552                         }
553 
554                     }
555                 }
556             }
557         }
558         // proxy patron
559         List<OleProxyPatronDocument> proxyPatron = newOlePatronDocument.getOleProxyPatronDocuments();
560         String patronId = "";
561         Date proxyActDate = null;
562         Date proxyExpDate = null;
563         if (proxyPatron.size() > 0) {
564             for (OleProxyPatronDocument proxy : proxyPatron) {
565                 patronId = proxy.getProxyPatronId();
566                 proxyActDate = proxy.getProxyPatronActivationDate();
567                 proxyExpDate = proxy.getProxyPatronExpirationDate();
568                 Map<String, String> patronMap = new HashMap<String, String>();
569                 patronMap.put(OLEConstants.OlePatron.PATRON_ID, patronId);
570                 OlePatronDocument patronDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
571                 if (patronDocument != null) {
572                     Date patronExpDate = patronDocument.getExpirationDate();
573                     Date patronActDate = patronDocument.getActivationDate();
574                     if (proxyActDate != null) {
575                         if (patronActDate != null && fmt.format(patronActDate).compareTo(fmt.format(proxyActDate)) > 0) {
576                             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_REAL_PATRON_ACTIVATION_DATE);
577                             return getUIFModelAndView(mainForm);
578                         }
579                     }
580                     if (proxyExpDate != null) {
581                         if (patronExpDate != null) {
582                             if (fmt.format(proxyExpDate).compareTo(fmt.format(patronExpDate)) > 0) {
583                                 if(newOlePatronDocument.isExpirationFlag()){
584                                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_REAL_PATRON_EXPIRATION_DATE);
585                                     return getUIFModelAndView(mainForm);
586                                 }
587                             }
588                         }
589                     }
590                 }
591                 boolean isBorrowerTypeActive = olePatronHelperService.isBorrowerTypeActive(newOlePatronDocument);
592                 if (!isBorrowerTypeActive) {
593                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_BORROWER_TYPE_INACTIVE);
594                     return getUIFModelAndView(mainForm);
595                 }
596                 if (patronId != null && newOlePatronDocument.getOlePatronId() != null && newOlePatronDocument.getOlePatronId().equals(patronId)) {
597                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PROXY_PATRON_ID);
598                     return getUIFModelAndView(mainForm);
599                 } else {
600                     if (proxyExpDate != null) {
601                         if (proxyActDate != null) {
602                             if ((fmt.format(proxyActDate).compareTo(fmt.format(proxyExpDate)) >= 0)) {
603                                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PROXY_PATRON_EXPIRATION_DATE);
604                                 return getUIFModelAndView(mainForm);
605                             }
606                         }
607                     }
608                 }
609             }
610         }
611         return super.save(form, result, request, response);
612     }
613 
614     /**
615      * To submit or route the patron maintenance document
616      *
617      * @param form document form base containing the document instance that will be routed
618      * @return ModelAndView
619      */
620     @Override
621     @RequestMapping(params = "methodToCall=route")
622     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
623                               HttpServletRequest request, HttpServletResponse response) {
624         DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
625         LOG.debug(" Inside route method of patron maintenance controller ");
626         ModelAndView modelAndView;
627         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
628         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
629         String action = mainForm.getMaintenanceAction();
630         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
631         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
632         List<OleLoanDocument> oleLoanDocumentList = newOlePatronDocument.getOleLoanDocuments();
633         for (OleLoanDocument oleLoanDocument : oleLoanDocumentList) {
634             if (oleLoanDocument.isLoanModified()) {
635                 if (oleLoanDocument.isMissingPieceFlag()) {
636                     if (oleLoanDocument.getMissingPiecesCount() == null || oleLoanDocument.getMissingPiecesCount() != null && oleLoanDocument.getMissingPiecesCount().equalsIgnoreCase("")) {
637                         oleLoanDocument.setNoOfMissingPiecesEditable(true);
638                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_INFO);
639                         return getUIFModelAndView(form);
640                     }
641                     if (oleLoanDocument.getMissingPiecesCount() != null && !oleLoanDocument.getMissingPiecesCount().equalsIgnoreCase("")) {
642                         int count = Integer.parseInt(oleLoanDocument.getMissingPiecesCount());
643                         if (count < 1) {
644                             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_COUNT_GREATER, new String[]{"Missing Piece Count", "1"});
645                             return getUIFModelAndView(form);
646                         }
647                     }
648                     if (oleLoanDocument.getItemNumberOfPieces() == null || oleLoanDocument.getItemNumberOfPieces() != null && oleLoanDocument.getItemNumberOfPieces().toString().equalsIgnoreCase("")) {
649                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_ITEM_INFO);
650                         oleLoanDocument.setNoOfPiecesEditable(true);
651                         return getUIFModelAndView(form);
652                     }
653                     if (oleLoanDocument.getItemNumberOfPieces().intValue() < 1) {
654                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_COUNT_GREATER, new String[]{"no of piece", "1"});
655                         oleLoanDocument.setNoOfPiecesEditable(true);
656                         oleLoanDocument.setNoOfMissingPiecesEditable(true);
657                         return getUIFModelAndView(form);
658                     }
659                     int missingPieceCount = Integer.parseInt(oleLoanDocument.getMissingPiecesCount());
660                     int numberOfPieces = oleLoanDocument.getItemNumberOfPieces();
661                     if (missingPieceCount > numberOfPieces) {
662                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PATRON_LOANED_ITEM_SECTION, OLEConstants.OlePatron.ERROR_PATRON_MISSING_PIECE_ITEM_COUNT_LESSER, new String[]{"Missing Piece Count", "no of pieces"});
663                         oleLoanDocument.setNoOfPiecesEditable(true);
664                         oleLoanDocument.setNoOfMissingPiecesEditable(true);
665                         return getUIFModelAndView(form);
666                     }
667                 }
668                 try {
669                     String itemXmlContent = getLoanProcessor().getItemXML(oleLoanDocument.getItemUuid());
670                     Item oleItem = getLoanProcessor().getItemPojo(itemXmlContent);
671 
672 
673                     oleItem.setClaimsReturnedFlag(oleLoanDocument.isClaimsReturnedIndicator());
674                     if (oleItem.isClaimsReturnedFlag()) {
675                         getOleDeliverRequestDocumentHelperService().cancelPendingRequestForClaimsReturnedItem(oleItem.getItemIdentifier());
676                         oleItem.setClaimsReturnedNote(oleLoanDocument.getClaimsReturnNote());
677                         oleItem.setClaimsReturnedFlagCreateDate(df.format(getDateTimeService().getCurrentDate()));
678                     } else {
679                         oleItem.setClaimsReturnedNote(null);
680                         oleItem.setClaimsReturnedFlagCreateDate(null);
681                     }
682                     oleItem.setMissingPieceFlag(oleLoanDocument.isMissingPieceFlag());
683                     oleItem.setMissingPieceFlagNote(oleLoanDocument.getMissingPieceNote());
684                     oleItem.setItemDamagedStatus(oleLoanDocument.isItemDamagedStatus());
685                     oleItem.setDamagedItemNote(oleLoanDocument.getItemDamagedNote());
686                     oleItem.setMissingPiecesCount(oleLoanDocument.getMissingPiecesCount());
687                     if (oleLoanDocument.getItemNumberOfPieces() != null && !oleLoanDocument.getItemNumberOfPieces().toString().equalsIgnoreCase("")) {
688                         oleItem.setNumberOfPieces(oleLoanDocument.getItemNumberOfPieces().toString());
689                     }
690                     org.kuali.ole.docstore.common.document.Item itemXML = new ItemOleml();
691                     ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
692                     itemXML.setContent(itemOlemlRecordProcessor.toXML(oleItem));
693                     itemXML.setCategory(OLEConstants.WORK_CATEGORY);
694                     itemXML.setType(DocType.ITEM.getCode());
695                     itemXML.setFormat(OLEConstants.OLEML_FORMAT);
696                     itemXML.setId(oleLoanDocument.getItemUuid());
697                     getDocstoreClientLocator().getDocstoreClient().updateItem(itemXML);
698                 } catch (Exception e) {
699                     throw new RuntimeException(e);
700                 }
701             }
702 
703         }
704         if(!olePatronHelperService.validatePatron(newOlePatronDocument)) {
705             return getUIFModelAndView(form);
706         }
707         if (!KRADConstants.MAINTENANCE_EDIT_ACTION.equals(action)) {
708             if (newOlePatronDocument.getOlePatronId() == null || newOlePatronDocument.getOlePatronId().isEmpty()) {
709                 if (newOlePatronDocument.getActivationDate() != null && fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(newOlePatronDocument.getActivationDate())) > 0) {
710                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_ACTIVATION_DATE);
711 
712                     return getUIFModelAndView(mainForm); // JIRA OLE-5107
713                 }
714             }
715         } else {
716             Map<String, String> patronMap = new HashMap<String, String>();
717             patronMap.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
718             OlePatronDocument patronDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
719             SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
720             Date todayDate=getDateTimeService().getCurrentDate();
721             String format=simpleDateFormat.format(getDateTimeService().getCurrentDate());
722             try {
723                  todayDate=simpleDateFormat.parse(format);
724             } catch (ParseException e) {
725                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
726             }
727             if(newOlePatronDocument.getActivationDate()!=null && newOlePatronDocument.getActivationDate().compareTo(todayDate)<0 && patronDocument!=null && patronDocument.getActivationDate()!=null && patronDocument.getActivationDate().compareTo(newOlePatronDocument.getActivationDate())>0){
728                 if (!newOlePatronDocument.isPopupDialog()) {
729                     newOlePatronDocument.setPopupDialog(true);
730                     newOlePatronDocument.setUiMessageType("patron-message-info");
731                     newOlePatronDocument.setPatronMessage(OLEConstants.OlePatron.ERROR_PATRON_NEW_PAST_DATE+"<br/>"+OLEConstants.PROCEED_MESSAGE);
732                     return getUIFModelAndView(mainForm);
733                 }
734             }
735         }
736 
737         newOlePatronDocument.setPopupDialog(false);
738         newOlePatronDocument.setOleLoanDocuments(oleLoanDocumentList);
739         List<OleProxyPatronDocument> oleProxyPatronDocumentList = newOlePatronDocument.getOleProxyPatronDocuments();
740         List<OleProxyPatronDocument> proxyPatronDocumentList = new ArrayList<OleProxyPatronDocument>();
741         if (oleProxyPatronDocumentList.size() > 0) {
742             for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
743                 Map<String, String> proxyMap = new HashMap<String, String>();
744                 proxyMap.put(OLEConstants.OlePatron.PATRON_ID, oleProxyPatronDocument.getProxyPatronId());
745                 OlePatronDocument tempDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, proxyMap);
746                 if (tempDocument != null) {
747                     oleProxyPatronDocument.setProxyPatronBarcode(tempDocument.getBarcode());
748                     oleProxyPatronDocument.setProxyPatronFirstName(tempDocument.getEntity().getNames().get(0).getFirstName());
749                     oleProxyPatronDocument.setProxyPatronLastName(tempDocument.getEntity().getNames().get(0).getLastName());
750                     proxyPatronDocumentList.add(oleProxyPatronDocument);
751                 }
752             }
753             newOlePatronDocument.setOleProxyPatronDocuments(proxyPatronDocumentList);
754         }
755         // Lost and reinstate barcode section
756         if (newOlePatronDocument.getOlePatronId() != null && !newOlePatronDocument.getOlePatronId().equalsIgnoreCase("")) {
757             // if barcode is editable mode and shouldn't be reinstated
758             if (newOlePatronDocument.isBarcodeEditable()&& !newOlePatronDocument.isReinstated()) {
759                 for (OlePatronLostBarcode olePatronLostBarcode : newOlePatronDocument.getLostBarcodes()) {
760                     if (olePatronLostBarcode.getInvalidOrLostBarcodeNumber() != null && olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(newOlePatronDocument.getBarcode())) {
761                         GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_BARCODE_BLOCK_MANUALLY,new String[]{newOlePatronDocument.getBarcode()});
762                         newOlePatronDocument.setBarcode(null);
763                         newOlePatronDocument.setBarcodeEditable(true);
764                         newOlePatronDocument.setReinstated(false);
765                         olePatronLostBarcode.setStatus("LOST");
766                         olePatronLostBarcode.setDescription("LOST");
767                         return getUIFModelAndView(mainForm);
768                     }
769                 }
770             }
771             Map<String, String> map = new HashMap<String, String>();
772             map.put(OLEConstants.OlePatron.PATRON_LOST_BARCODE_FLD, newOlePatronDocument.getBarcode());
773             map.put("active", "N");
774             List<OlePatronLostBarcode> olePatronLostBarcodes = (List<OlePatronLostBarcode>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronLostBarcode.class, map);
775             if (olePatronLostBarcodes.size() > 0 && (!newOlePatronDocument.isSkipBarcodeValidation())) {
776                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_DUPLICATE_BARCODE);
777                 return getUIFModelAndView(mainForm);
778             }
779             // LOST Barcode Validation for current lost section
780             boolean isBarcodeContainsInLostSection = false;
781             if(newOlePatronDocument.getLostBarcodes()!=null&& newOlePatronDocument.getBarcode()!=null){
782                 // if barcode contains in lost section submitting the document will reinstate the barcode
783                 for(OlePatronLostBarcode olePatronLostBarcode:olePatronLostBarcodes){
784                      if(olePatronLostBarcode.getInvalidOrLostBarcodeNumber()!=null && olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(newOlePatronDocument.getBarcode())){
785                          isBarcodeContainsInLostSection=true;
786                      }
787                 }
788             }
789             if (!isBarcodeContainsInLostSection) {
790                 //JIRA OLE-5707
791                 Map<String, String> patronMap = new HashMap<String, String>();
792                 patronMap.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
793                 OlePatronDocument patronDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
794                 if (patronDocument != null && patronDocument.getBarcode() != null && !(patronDocument.getBarcode().equalsIgnoreCase(""))) {
795                     if (newOlePatronDocument.getLostBarcodes() == null) {
796                         newOlePatronDocument.setLostBarcodes(new ArrayList<OlePatronLostBarcode>());
797                     }
798                     if (!patronDocument.getBarcode().equalsIgnoreCase(newOlePatronDocument.getBarcode()) && (!newOlePatronDocument.isBarcodeChanged())) {
799                         if (patronDocument != null && patronDocument.getBarcode() != null) {
800                             boolean isNeedToUpdateBarcode=false;
801                             String originalBarcode=patronDocument.getBarcode();
802                             for(OlePatronLostBarcode olePatronLostBarcode:newOlePatronDocument.getLostBarcodes()){
803                                 if(olePatronLostBarcode.getInvalidOrLostBarcodeNumber()!=null && olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(originalBarcode)){
804                                     isNeedToUpdateBarcode=true;
805                                 }
806                             }
807                             if (!isNeedToUpdateBarcode) {
808                                 OlePatronLostBarcode olePatronLostBarcode = new OlePatronLostBarcode();
809                                 olePatronLostBarcode.setInvalidOrLostBarcodeNumber(patronDocument.getBarcode());
810                                 olePatronLostBarcode.setInvalidOrLostBarcodeEffDate(new java.sql.Date(getDateTimeService().getCurrentDate().getTime()));
811                                 olePatronLostBarcode.setRevertBarcode(true);
812                                 olePatronLostBarcode.setDescription("");
813                                 olePatronLostBarcode.setStatus("Lost");
814                                 newOlePatronDocument.getLostBarcodes().add(olePatronLostBarcode);
815                             }
816                         }
817 
818                     }
819                 }
820             }
821         }
822         // proxy patron
823         List<OleProxyPatronDocument> proxyPatron = newOlePatronDocument.getOleProxyPatronDocuments();
824         String patronId = "";
825         Date proxyActDate = null;
826         Date proxyExpDate = null;
827         if (proxyPatron.size() > 0) {
828             for (OleProxyPatronDocument proxy : proxyPatron) {
829                 patronId = proxy.getProxyPatronId();
830                 proxyActDate = proxy.getProxyPatronActivationDate();
831                 proxyExpDate = proxy.getProxyPatronExpirationDate();
832                 Map<String, String> patronMap = new HashMap<String, String>();
833                 patronMap.put(OLEConstants.OlePatron.PATRON_ID, patronId);
834                 OlePatronDocument patronDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
835                 if (patronDocument != null) {
836                     Date patronExpDate = patronDocument.getExpirationDate();
837                     Date patronActDate = patronDocument.getActivationDate();
838                     if (proxyActDate != null) {
839                         if (patronActDate != null && fmt.format(patronActDate).compareTo(fmt.format(proxyActDate)) > 0) {
840                             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_REAL_PATRON_ACTIVATION_DATE);
841                             return getUIFModelAndView(mainForm);
842                         }
843                     }
844                     if (proxyExpDate != null) {
845                         if (patronExpDate != null) {
846                             if (fmt.format(proxyExpDate).compareTo(fmt.format(patronExpDate)) > 0) {
847                                 if(newOlePatronDocument.isExpirationFlag()){
848                                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_REAL_PATRON_EXPIRATION_DATE);
849                                     return getUIFModelAndView(mainForm);
850                                 }
851                             }
852                         }
853                     }
854                 }
855                /* boolean lostBarcodeCheck = olePatronHelperService.CheckBarcodeAndLostBarcode(newOlePatronDocument);
856                 if (lostBarcodeCheck) {
857                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_BARCODE_INVALID);
858                     return getUIFModelAndView(mainForm);
859                 }*/
860                 boolean isBorrowerTypeActive = olePatronHelperService.isBorrowerTypeActive(newOlePatronDocument);
861                 if (!isBorrowerTypeActive) {
862                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_BORROWER_TYPE_INACTIVE);
863                     return getUIFModelAndView(mainForm);
864                 }
865                 if (patronId != null && newOlePatronDocument.getOlePatronId() != null && newOlePatronDocument.getOlePatronId().equals(patronId)) {
866                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PROXY_PATRON_ID);
867                     return getUIFModelAndView(mainForm);
868                 } /*else if (proxyActDate != null && fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(proxyActDate)) > 0) {
869                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PROXY_PATRON_ACTIVATION_DATE);
870                     return getUIFModelAndView(mainForm);
871                 } */else {
872                     if (proxyExpDate != null) {
873                         if (proxyActDate != null) {
874                             if ((fmt.format(proxyActDate).compareTo(fmt.format(proxyExpDate)) >= 0)) {
875                                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PROXY_PATRON_EXPIRATION_DATE);
876                                 return getUIFModelAndView(mainForm);
877                             }
878                         } /*else if (fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(proxyExpDate)) > 0) {
879                             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PROXY_PATRON_EXPIRATION_DATE);
880                             return getUIFModelAndView(mainForm);
881                         }*/
882                     }
883                 }
884             }
885         }
886         if (newOlePatronDocument.isGeneralBlock() && (newOlePatronDocument.getGeneralBlockNotes() == null || newOlePatronDocument.getGeneralBlockNotes().equals(""))) {
887             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_GENERAL_BLOCK_NOTES, OLEConstants.OlePatron.PATRON_GENERAL_BLOCK_NOTES);
888             return getUIFModelAndView(mainForm);
889         } /*else if (newOlePatronDocument.getActivationDate() != null && fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(newOlePatronDocument.getActivationDate())) > 0) {
890             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_ACTIVATION_DATE);
891             return getUIFModelAndView(mainForm); // JIRA OLE-5107
892         }*/ else if ((newOlePatronDocument.getExpirationDate() != null && newOlePatronDocument.getActivationDate() != null && fmt.format(newOlePatronDocument.getActivationDate()).compareTo(fmt.format(newOlePatronDocument.getExpirationDate())) >= 0) || (newOlePatronDocument.getExpirationDate() != null && fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(newOlePatronDocument.getExpirationDate())) > 0)) {
893             if(newOlePatronDocument.isExpirationFlag()){
894             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_EXPIRATION_DATE);
895             return getUIFModelAndView(mainForm);
896             }
897         } else if (newOlePatronDocument.getOlePatronId() != null) {
898             Map<String, String> tempId = new HashMap<String, String>();
899             tempId.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
900             OlePatronDocument tempDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, tempId);
901             if (tempDocument != null) {
902                 if (tempDocument.getEntity() != null) {
903                     if (newOlePatronDocument.getDeletedOleEntityAddressBo().size() > 0) {
904                         for(OleEntityAddressBo oleEntityAddressBo:newOlePatronDocument.getDeletedOleEntityAddressBo()){
905                             KRADServiceLocator.getBusinessObjectService().delete(oleEntityAddressBo.getOleAddressBo());
906                             KRADServiceLocator.getBusinessObjectService().delete(oleEntityAddressBo.getEntityAddressBo());
907                         }
908                     }
909                     Map<String, String> mapEntityId = new HashMap<String, String>();
910                     mapEntityId.put("entityId", newOlePatronDocument.getOlePatronId());
911                     List<EntityEmailBo> emailBoList = tempDocument.getEntity().getEntityTypeContactInfos().get(0).getEmailAddresses();
912                     if(newOlePatronDocument.getDeletedEmails().size()>0){
913                         KRADServiceLocator.getBusinessObjectService().delete(newOlePatronDocument.getDeletedEmails());
914                     }
915                     List<EntityNameBo> entityNameBos = tempDocument.getEntity().getNames();
916                     if (entityNameBos.size() > 0) {
917                         KRADServiceLocator.getBusinessObjectService().deleteMatching(EntityNameBo.class,mapEntityId);
918                     }
919                     if(newOlePatronDocument.getDeletedPhones().size()>0){
920                         KRADServiceLocator.getBusinessObjectService().delete(newOlePatronDocument.getDeletedPhones());
921                     }
922                     if(newOlePatronDocument.getDeletedNotes().size()>0){
923                         KRADServiceLocator.getBusinessObjectService().delete(newOlePatronDocument.getDeletedNotes());
924                     }
925 
926                     List<OlePatronLostBarcode> lostBarcodeList = tempDocument.getLostBarcodes();
927                     if (lostBarcodeList.size() > 0) {
928                         KRADServiceLocator.getBusinessObjectService().delete(lostBarcodeList);
929                     }
930                     if(newOlePatronDocument.getDeletedEmployments().size()>0){
931                         KRADServiceLocator.getBusinessObjectService().delete(newOlePatronDocument.getDeletedEmployments());
932                     }
933                     if(newOlePatronDocument.getDeletedPatronAffiliations().size()>0){
934                         for(OlePatronAffiliation olePatronAffiliation:newOlePatronDocument.getDeletedPatronAffiliations()){
935                            if(olePatronAffiliation.getEmployments()!=null && olePatronAffiliation.getEmployments().size()>0){
936                                KRADServiceLocator.getBusinessObjectService().delete(newOlePatronDocument.getDeletedOleProxyPatronDocuments());
937                            }
938                            Map<String,String> affiliationsMap=new HashMap<String,String>();
939                            affiliationsMap.put("id",olePatronAffiliation.getEntityAffiliationId());
940                             KRADServiceLocator.getBusinessObjectService().deleteMatching(EntityAffiliationBo.class,affiliationsMap);
941 
942                         }
943                     }
944                     if(newOlePatronDocument.getDeletedOleProxyPatronDocuments().size()>0){
945                        KRADServiceLocator.getBusinessObjectService().delete(newOlePatronDocument.getDeletedOleProxyPatronDocuments());
946                     }
947                     if(newOlePatronDocument.getDeletedOlePatronLocalIds().size()>0){
948                         KRADServiceLocator.getBusinessObjectService().delete(newOlePatronDocument.getDeletedOlePatronLocalIds());
949                     }
950                 }
951             }
952         }
953         if (newOlePatronDocument.getLostBarcodes() != null) {
954             for (OlePatronLostBarcode lostBarcodes : newOlePatronDocument.getLostBarcodes()) {
955                 if (lostBarcodes.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(newOlePatronDocument.getBarcode())) {
956                     lostBarcodes.setActive(true);
957                 }
958             }
959         }
960         if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(action)) {
961             newOlePatronDocument.setEntity(olePatronHelperService.editAndSaveEntityBo(newOlePatronDocument));
962             Map<String, Object> criteria = new HashMap<String, Object>();
963             criteria.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
964             KRADServiceLocator.getBusinessObjectService().deleteMatching(OleAddressBo.class, criteria);
965             prepareOleAddressForSave(newOlePatronDocument);
966         }
967         if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(action) || KRADConstants.MAINTENANCE_NEW_ACTION.equals(action)) {
968             newOlePatronDocument.setEntity(olePatronHelperService.copyAndSaveEntityBo(newOlePatronDocument));
969             newOlePatronDocument.setOlePatronId(newOlePatronDocument.getEntity().getId());
970             for (OleEntityAddressBo oleEntityAddressBo : newOlePatronDocument.getOleEntityAddressBo()) {
971                if(oleEntityAddressBo.getOleAddressBo()!=null){
972                    oleEntityAddressBo.getOleAddressBo().setOleAddressId(null);
973                }
974             }
975             prepareOleAddressForSave(newOlePatronDocument);
976         }
977         ModelAndView model = null;
978         try {
979             model = super.route(mainForm, result, request, response);
980         } catch (Exception e) {
981             return model;
982         }
983         return model;
984     }
985 
986 
987     @RequestMapping(params = "methodToCall=lostBarcode")
988     public ModelAndView lostBarcode(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
989                                     HttpServletRequest request, HttpServletResponse response) {
990 
991         LOG.debug(" Inside route method of patron maintenance controller ");
992         ModelAndView modelAndView;
993         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
994         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
995         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
996         if (StringUtils.isBlank(newOlePatronDocument.getBarcode()) || StringUtils.isEmpty(newOlePatronDocument.getBarcode())) {
997             newOlePatronDocument.setBarcodeEditable(true);
998             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_REQUIRED, new String[]{"Barcode"});
999             return getUIFModelAndView(mainForm);
1000         }
1001         newOlePatronDocument.setReinstated(false);
1002         boolean isBarcodeExistInOLE = false;
1003         Map<String, String> patronMap = new HashMap<String, String>();
1004         patronMap.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
1005         OlePatronDocument patronDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
1006         if (newOlePatronDocument.getBarcode() != null && (!newOlePatronDocument.getBarcode().equalsIgnoreCase(""))) {
1007             Map<String, String> map = new HashMap<String, String>();
1008             map.put(OLEConstants.OlePatron.PATRON_ID, newOlePatronDocument.getOlePatronId());
1009             map.put("active", "N");
1010             List<OlePatronLostBarcode> olePatronLostBarcodes = (List<OlePatronLostBarcode>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronLostBarcode.class, map);
1011             if (olePatronLostBarcodes.size() > 0 && (!newOlePatronDocument.isSkipBarcodeValidation())) {
1012                 isBarcodeExistInOLE = true;
1013             }
1014             if (patronDocument != null && patronDocument.getBarcode() != null && patronDocument.getBarcode().equalsIgnoreCase(newOlePatronDocument.getBarcode())) {
1015                 isBarcodeExistInOLE = true;
1016             }
1017         }
1018         if (LOG.isDebugEnabled()){
1019             LOG.debug("Allow the patron to add barcode "+isBarcodeExistInOLE);
1020         }
1021         if (!isBarcodeExistInOLE) {
1022             // Added validation for if barcode exist in OLE system
1023             newOlePatronDocument.setBarcodeEditable(true);
1024             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_BARCODE_EXIST_LOST_SECTION);
1025             return getUIFModelAndView(mainForm);
1026         }
1027         for(OlePatronLostBarcode olePatronLostBarcode:newOlePatronDocument.getLostBarcodes()){
1028             // Added validation if barcode exist in current section to restrict duplicacy
1029             if(olePatronLostBarcode.getInvalidOrLostBarcodeNumber()!=null && olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(newOlePatronDocument.getBarcode())){
1030                 if (!(patronDocument != null && patronDocument.getBarcode().equalsIgnoreCase(newOlePatronDocument.getBarcode()))) {
1031                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_REQUIRED, new String[]{"Barcode"});
1032                     return getUIFModelAndView(mainForm);
1033                 }
1034             }
1035         }
1036         if (isBarcodeExistInOLE) {
1037             if (!newOlePatronDocument.isInvalidateBarcode()) {
1038                 if (StringUtils.isBlank(newOlePatronDocument.getBarcode()) || StringUtils.isEmpty(newOlePatronDocument.getBarcode())) {
1039                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_REQUIRED, new String[]{"Barcode"});
1040                     return getUIFModelAndView(mainForm);
1041                 }
1042                 newOlePatronDocument.setBarcodeEditable(true);
1043                 newOlePatronDocument.setLostDescription("");
1044                 newOlePatronDocument.setInvalidateBarcode(true);
1045                 return getUIFModelAndView(mainForm);
1046             }
1047             boolean isBarcodeExist = false;
1048             newOlePatronDocument.setBarcodeChanged(true);
1049             newOlePatronDocument.setInvalidateBarcode(false);
1050             String lostBarcode = newOlePatronDocument.getBarcode();
1051             OlePatronLostBarcode olePatronLostBarcode = new OlePatronLostBarcode();
1052             olePatronLostBarcode.setInvalidOrLostBarcodeNumber(lostBarcode);
1053             olePatronLostBarcode.setInvalidOrLostBarcodeEffDate(new java.sql.Date(getDateTimeService().getCurrentDate().getTime()));
1054             olePatronLostBarcode.setRevertBarcode(true);
1055             olePatronLostBarcode.setDescription(newOlePatronDocument.getLostDescription());
1056             olePatronLostBarcode.setStatus(newOlePatronDocument.getLostStatus());
1057             for (OlePatronLostBarcode lostBarcodes : newOlePatronDocument.getLostBarcodes()) {
1058                 if (lostBarcodes.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(lostBarcode)) {
1059                     lostBarcodes.setDescription(newOlePatronDocument.getLostDescription());
1060                     lostBarcodes.setStatus(newOlePatronDocument.getLostStatus());
1061                     isBarcodeExist = true;
1062                 }
1063             }
1064             List<OlePatronLostBarcode> lostBarcodes = newOlePatronDocument.getLostBarcodes();
1065             List<OlePatronLostBarcode> lostBarcodeList = new ArrayList<OlePatronLostBarcode>();
1066             if (!isBarcodeExist) {
1067                 lostBarcodeList.add(olePatronLostBarcode);
1068                 if (lostBarcodes.size() > 0) {
1069                     for (OlePatronLostBarcode lostPatronBarcode : lostBarcodes) {
1070                         lostPatronBarcode.setRevertBarcode(false);
1071                         lostBarcodeList.add(lostPatronBarcode);
1072                     }
1073                 }
1074                 newOlePatronDocument.setLostBarcodes(lostBarcodeList);
1075             }
1076             newOlePatronDocument.setReinstated(false);
1077             newOlePatronDocument.setBarcode(null);
1078             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ENTER_PATRON_BARCODE);
1079         }
1080         return getUIFModelAndView(mainForm);
1081     }
1082 
1083     @RequestMapping(params = "methodToCall=reinstateBarcode")
1084     public ModelAndView reinstateBarcode(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
1085                                          HttpServletRequest request, HttpServletResponse response) {
1086 
1087         LOG.debug(" Inside route method of patron maintenance controller ");
1088         ModelAndView modelAndView;
1089         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
1090         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1091         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1092         String oldBarcode=newOlePatronDocument.getBarcode();
1093         if (!newOlePatronDocument.isReinstateBarcode()) {
1094             newOlePatronDocument.setReinstateBarcode(true);
1095             if (oldBarcode != null && !oldBarcode.equalsIgnoreCase("")) {
1096                 newOlePatronDocument.setBarcodeEditable(false);
1097             } else {
1098                 newOlePatronDocument.setBarcodeEditable(true);
1099             }
1100             return getUIFModelAndView(mainForm);
1101         }
1102         //adding or updating  older barcode to lost barcode table
1103 
1104         boolean isBarcodeExistInOLE=false;
1105 
1106         if (oldBarcode != null && !oldBarcode.equalsIgnoreCase("")) {
1107             Map<String, String> map = new HashMap<String, String>();
1108             map.put(OLEConstants.OlePatron.PATRON_LOST_BARCODE_FLD, newOlePatronDocument.getBarcode());
1109             List<OlePatronLostBarcode> olePatronLostBarcodes = (List<OlePatronLostBarcode>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronLostBarcode.class, map);
1110             map.clear();
1111             map.put("barcode",oldBarcode);
1112             List<OlePatronDocument> patronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, map);
1113             if (olePatronLostBarcodes.size() > 0 || patronDocuments.size()>0) {
1114                  isBarcodeExistInOLE=true;
1115             }
1116             if(!isBarcodeExistInOLE){
1117                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,OLEConstants.OlePatron.PATRON_BARCODE_DOES_NOT_EXIST_REINSTATE,new String[]{oldBarcode});
1118             }
1119         }
1120 
1121         for (OlePatronLostBarcode olePatronLostBarcode : newOlePatronDocument.getLostBarcodes()) {
1122             if (olePatronLostBarcode.isActive()) {
1123                 newOlePatronDocument.setBarcode(olePatronLostBarcode.getInvalidOrLostBarcodeNumber());
1124                 olePatronLostBarcode.setDescription(newOlePatronDocument.getLostDescription());
1125                 olePatronLostBarcode.setStatus(newOlePatronDocument.getLostStatus());
1126             }
1127             if(olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(oldBarcode)){
1128                 olePatronLostBarcode.setDescription("LOST");
1129                 olePatronLostBarcode.setStatus("LOST");
1130                 olePatronLostBarcode.setActive(false);
1131             }
1132         }
1133         boolean isOldBarcodeExist=false;
1134         for (OlePatronLostBarcode olePatronLostBarcode : newOlePatronDocument.getLostBarcodes()) {
1135             if (olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(oldBarcode)) {
1136                 isOldBarcodeExist=true;
1137             }
1138         }
1139         if (oldBarcode != null && !oldBarcode.equalsIgnoreCase("")) {
1140             if (!isOldBarcodeExist && isBarcodeExistInOLE) {
1141                 OlePatronLostBarcode olePatronLostBarcode = new OlePatronLostBarcode();
1142                 olePatronLostBarcode.setInvalidOrLostBarcodeNumber(oldBarcode);
1143                 olePatronLostBarcode.setInvalidOrLostBarcodeEffDate(new java.sql.Date(getDateTimeService().getCurrentDate().getTime()));
1144                 olePatronLostBarcode.setRevertBarcode(true);
1145                 olePatronLostBarcode.setDescription("LOST");
1146                 olePatronLostBarcode.setStatus("LOST");
1147                 newOlePatronDocument.getLostBarcodes().add(olePatronLostBarcode);
1148             }
1149         }
1150         newOlePatronDocument.setBarcodeEditable(false);
1151         newOlePatronDocument.setBarcodeChanged(true);
1152         newOlePatronDocument.setReinstated(true);
1153         newOlePatronDocument.setSkipBarcodeValidation(true);
1154         newOlePatronDocument.setReinstateBarcode(false);
1155         return getUIFModelAndView(mainForm);
1156     }
1157 
1158     @RequestMapping(params = "methodToCall=refreshInvalidOrLostBarcodeSection")
1159     public ModelAndView refreshInvalidOrLostBarcodeSection(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
1160                                                            HttpServletRequest request, HttpServletResponse response) {
1161 
1162         LOG.debug(" Inside route method of patron maintenance controller ");
1163         ModelAndView modelAndView;
1164         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
1165         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1166         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1167         for (OlePatronLostBarcode lostBarcode : newOlePatronDocument.getLostBarcodes()) {
1168             if (lostBarcode.getId() != null) {
1169                 Map<String, String> map = new HashMap<String, String>();
1170                 map.put(OLEConstants.OlePatron.PATRON_ID, lostBarcode.getId());
1171                 OlePatronLostBarcode olePatronLostBarcode = (OlePatronLostBarcode) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronLostBarcode.class, map);
1172                 if (olePatronLostBarcode != null) {
1173                     lostBarcode.setActive(olePatronLostBarcode.isActive());
1174                 } else {
1175                     lostBarcode.setActive(false);
1176                 }
1177             }
1178             if(lostBarcode.getId() == null) {
1179                 lostBarcode.setActive(false);
1180             }
1181         }
1182         return navigate(form, result, request, response);
1183     }
1184 
1185     @RequestMapping(params = "methodToCall=uploadImage")
1186     public ModelAndView uploadImage(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
1187                                     HttpServletRequest request, HttpServletResponse response) {
1188         ModelAndView modelAndView;
1189         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
1190         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1191         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1192         MultipartFile multipartFile = mainForm.getAttachmentFile();
1193         if (multipartFile == null) {
1194             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_PHOTOGRAPH_WITHOUT_FILE);
1195             return getUIFModelAndView(mainForm);
1196         }
1197         String fileName = multipartFile.getOriginalFilename();
1198         if (validateFile(multipartFile.getOriginalFilename())) {
1199             try {
1200                 byte[] fileContent = multipartFile.getBytes();
1201                 BufferedImage patronImage = ImageIO.read(new ByteArrayInputStream(fileContent));
1202                 if (patronImage.getWidth() >= 100 || patronImage.getHeight() >= 100) {
1203                     BufferedImage resizedImage = new BufferedImage(100, 100, 1);
1204                     Graphics2D g = resizedImage.createGraphics();
1205                     g.drawImage(patronImage, 0, 0, 100, 100, null);
1206                     g.dispose();
1207                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
1208                     ImageIO.write(resizedImage, "jpg", baos);
1209                     byte[] res = baos.toByteArray();
1210                     imageInByte = baos.toByteArray();
1211                     newOlePatronDocument.setPatronPhotograph(res);
1212                 } else {
1213                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_PHOTOGRAPH_SIZE);
1214                     return getUIFModelAndView(mainForm);
1215                 }
1216             } catch (Exception ex) {
1217                 LOG.error("Exception while uploading image", ex);
1218             }
1219         } else if (fileName != null && fileName.isEmpty()) {
1220             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_PHOTOGRAPH_WITHOUT_FILE);
1221             return getUIFModelAndView(mainForm);
1222         } else {
1223             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_PHOTOGRAPH_FORMAT);
1224             return getUIFModelAndView(mainForm);
1225         }
1226         return getUIFModelAndView(mainForm);
1227     }
1228 
1229     @RequestMapping(params = "methodToCall=deleteImage")
1230     public ModelAndView deleteImage(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
1231                                     HttpServletRequest request, HttpServletResponse response) {
1232         ModelAndView modelAndView;
1233         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
1234         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1235         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1236         newOlePatronDocument.setPatronPhotograph(null);
1237         return getUIFModelAndView(mainForm);
1238     }
1239 
1240     /**
1241      * This method validate the image file type.
1242      *
1243      * @param inputFile
1244      * @return boolean
1245      */
1246     public boolean validateFile(String inputFile) {
1247         return (inputFile.contains(".jpg") || inputFile.contains(".png") || inputFile.contains(".jpeg") || inputFile.contains(".gif") ? true : false);
1248     }
1249 
1250     @RequestMapping(params = "methodToCall=getImage")
1251     public ModelAndView getImage(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
1252                                  HttpServletRequest request, HttpServletResponse response) {
1253         try {
1254             String inquiry = "false";
1255             if (request.getParameter("patronInquiryFlag") != null) {
1256                 inquiry = (String) request.getParameter("patronInquiryFlag");
1257             }
1258 
1259             if (inquiry.equalsIgnoreCase("false")) {
1260 
1261 
1262                 String patronId = request.getParameter(OLEConstants.OlePatron.PATRON_ID);
1263                 if (patronId != null && !patronId.equals("")) {
1264                     Map patronMap = new HashMap();
1265                     patronMap.put(OLEConstants.OlePatron.PATRON_ID, patronId);
1266                     OlePatronDocument olePatronDocument = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
1267                     byte[] patronPhoto = olePatronDocument.getPatronPhotograph();
1268                     if (patronPhoto != null) {
1269                         if (imageInByte == null || !patronPhoto.equals(imageInByte)) {
1270                             response.setContentType("image/jpg");
1271                             response.getOutputStream().write(patronPhoto);
1272                         } else {
1273                             response.setContentType("image/jpg");
1274                             response.getOutputStream().write(imageInByte);
1275                         }
1276                     } else {
1277                         response.setContentType("image/jpg");
1278                         response.getOutputStream().write(imageInByte);
1279                     }
1280                 } else {
1281                     if (imageInByte != null) {
1282                         response.setContentType("image/jpg");
1283                         response.getOutputStream().write(imageInByte);
1284                     }
1285                 }
1286             } else {
1287                 Map patronMap = new HashMap();
1288                 String patronId = request.getParameter(OLEConstants.OlePatron.PATRON_ID);
1289                 if (patronId != null && !patronId.equals("")) {
1290                     patronMap.put(OLEConstants.OlePatron.PATRON_ID, patronId);
1291                     OlePatronDocument olePatronDocument = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
1292                     byte[] patronPhoto = olePatronDocument.getPatronPhotograph();
1293                     if (patronPhoto != null) {
1294 
1295                         response.setContentType("image/jpg");
1296                         response.getOutputStream().write(patronPhoto);
1297 
1298                     }
1299                 }
1300             }
1301 
1302         } catch (Exception ex) {
1303             LOG.error("Exception while getting image", ex);
1304         }
1305         return null;
1306     }
1307 
1308     @RequestMapping(params = "methodToCall=getImageForLoan")
1309     public ModelAndView getImageForLoan(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
1310                                         HttpServletRequest request, HttpServletResponse response) {
1311         try {
1312             String patronId = request.getParameter(OLEConstants.OlePatron.PATRON_ID);
1313             if (patronId != null && !patronId.equals("")) {
1314                 Map patronMap = new HashMap();
1315                 patronMap.put(OLEConstants.OlePatron.PATRON_ID, patronId);
1316                 OlePatronDocument olePatronDocument = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
1317                 byte[] patronPhoto = olePatronDocument.getPatronPhotograph();
1318                 if (patronPhoto != null) {
1319                     response.setContentType("image/jpg");
1320                     response.getOutputStream().write(patronPhoto);
1321                 }
1322             }
1323         } catch (Exception ex) {
1324             LOG.error("Exception while getting image for loan", ex);
1325         }
1326         return null;
1327     }
1328 
1329     @RequestMapping(params = "methodToCall=refreshProgGroup")
1330     public ModelAndView refreshProgGroup(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
1331                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
1332         ModelAndView modelAndView;
1333         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
1334         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1335         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1336         if (newOlePatronDocument.getOleLoanDocuments() != null) {
1337             for (OleLoanDocument loanDocument : newOlePatronDocument.getOleLoanDocuments()) {
1338                 if(loanDocument.isMissingPieceFlag() && loanDocument.getMissingPiecesCount()==null || (loanDocument.getMissingPiecesCount()!=null && loanDocument.getMissingPiecesCount().equalsIgnoreCase(""))){
1339                     loanDocument.setNoOfMissingPiecesEditable(true);
1340                 } else {
1341                     loanDocument.setNoOfMissingPiecesEditable(false);
1342                 }
1343 
1344                 if(loanDocument.isMissingPieceFlag() && loanDocument.getItemNumberOfPieces()==null || (loanDocument.getItemNumberOfPieces()!=null && loanDocument.getItemNumberOfPieces().toString().equalsIgnoreCase(""))){
1345                     loanDocument.setNoOfPiecesEditable(true);
1346                 } else {
1347                     loanDocument.setNoOfPiecesEditable(false);
1348                 }
1349             }
1350         }
1351 
1352         return getUIFModelAndView(form);
1353     }
1354 
1355     /**
1356      * This method initiate LoanProcessor.
1357      *
1358      * @return LoanProcessor
1359      */
1360     private LoanProcessor getLoanProcessor() {
1361         if (loanProcessor == null) {
1362             loanProcessor = new LoanProcessor();
1363         }
1364         return loanProcessor;
1365     }
1366 
1367     private void prepareOleAddressForSave(OlePatronDocument newOlePatronDocument) {
1368         if (newOlePatronDocument.getOleEntityAddressBo() != null) {
1369             newOlePatronDocument.getOleAddresses().clear();
1370             int i=0;
1371             for (OleEntityAddressBo oleEntityAddressBo : newOlePatronDocument.getOleEntityAddressBo()) {
1372                 if (oleEntityAddressBo.getEntityAddressBo().getId() == null) {
1373                     if (newOlePatronDocument.getEntity() != null && newOlePatronDocument.getEntity().getEntityTypeContactInfos() != null && newOlePatronDocument.getEntity().getEntityTypeContactInfos().size() > 0) {
1374                         EntityTypeContactInfoBo entityTypeContactInfo = (EntityTypeContactInfoBo) newOlePatronDocument.getEntity().getEntityTypeContactInfos().get(0);
1375                         if (entityTypeContactInfo.getAddresses() != null && entityTypeContactInfo.getAddresses().size() > 0 && entityTypeContactInfo.getAddresses().size()>i) {
1376                             if(oleEntityAddressBo.getEntityAddressBo()!=null){
1377                                entityTypeContactInfo.getAddresses().get(i).setDefaultValue(oleEntityAddressBo.getEntityAddressBo().isDefaultValue());
1378                                 entityTypeContactInfo.getAddresses().get(i).setActive(oleEntityAddressBo.getEntityAddressBo().isActive());
1379                             }
1380                             oleEntityAddressBo.setEntityAddressBo(entityTypeContactInfo.getAddresses().get(i));
1381                         }
1382                     }
1383                 }
1384                 i++;
1385             }
1386             for (OleEntityAddressBo oleEntityAddressBo : newOlePatronDocument.getOleEntityAddressBo()) {
1387                 if (oleEntityAddressBo.getEntityAddressBo() != null && oleEntityAddressBo.getEntityAddressBo().getId() != null) {
1388 
1389                     oleEntityAddressBo.getOleAddressBo().setOleAddressId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("OLE_DLVR_ADD_S").toString());
1390 
1391                     oleEntityAddressBo.getOleAddressBo().setId(oleEntityAddressBo.getEntityAddressBo().getId());
1392                     oleEntityAddressBo.getOleAddressBo().setOlePatronId(newOlePatronDocument.getOlePatronId());
1393                     oleEntityAddressBo.getOleAddressBo().setVersionNumber(null);
1394                     oleEntityAddressBo.getOleAddressBo().setObjectId(null);
1395                     oleEntityAddressBo.getOleAddressBo().setAddressSource(oleEntityAddressBo.getOleAddressBo().getAddressSource());
1396                     oleEntityAddressBo.getOleAddressBo().setAddressValidFrom(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom());
1397                     oleEntityAddressBo.getOleAddressBo().setAddressValidTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo());
1398                     oleEntityAddressBo.getOleAddressBo().setAddressVerified(oleEntityAddressBo.getOleAddressBo().isAddressVerified());
1399                 }
1400                 OleAddressBo oleAddressBo = (OleAddressBo) ObjectUtils.deepCopy(oleEntityAddressBo.getOleAddressBo());
1401                 EntityAddressBo entityAddressBo = (EntityAddressBo) ObjectUtils.deepCopy(oleEntityAddressBo.getEntityAddressBo());
1402                 oleEntityAddressBo.getOleAddressBo().setEntityAddress(entityAddressBo);
1403                 newOlePatronDocument.getOleAddresses().add(oleAddressBo);
1404             }
1405         }
1406     }
1407 
1408     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteAddress")
1409     public ModelAndView deleteAddress(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1410                                         HttpServletRequest request, HttpServletResponse response) {
1411         LOG.debug("Initialized addLine method");
1412         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1413         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
1414         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1415         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1416         newOlePatronDocument.getDeletedOleEntityAddressBo().add(newOlePatronDocument.getOleEntityAddressBo().get(Integer.parseInt(selectedLineIndex)));
1417         return deleteLine(uifForm, result, request, response);
1418     }
1419 
1420     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deletePhoneNumber")
1421     public ModelAndView deletePhoneNumber(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1422                                         HttpServletRequest request, HttpServletResponse response) {
1423         LOG.debug("Initialized addLine method");
1424         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1425         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
1426         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1427         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1428         newOlePatronDocument.getDeletedPhones().add(newOlePatronDocument.getPhones().get(Integer.parseInt(selectedLineIndex)));
1429         return deleteLine(uifForm, result, request, response);
1430 
1431     }
1432 
1433     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteEmailAddress")
1434     public ModelAndView deleteEmailAddress(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1435                                         HttpServletRequest request, HttpServletResponse response) {
1436         LOG.debug("Initialized addLine method");
1437         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1438         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
1439         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1440         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1441         newOlePatronDocument.getDeletedEmails().add(newOlePatronDocument.getEmails().get(Integer.parseInt(selectedLineIndex)));
1442         return deleteLine(uifForm, result, request, response);
1443 
1444     }
1445 
1446     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteProxyPatron")
1447     public ModelAndView deleteProxyPatron(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1448                                           HttpServletRequest request, HttpServletResponse response) {
1449         LOG.debug("Initialized addLine method");
1450         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1451         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
1452         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1453         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1454         newOlePatronDocument.getDeletedOleProxyPatronDocuments().add(newOlePatronDocument.getOleProxyPatronDocuments().get(Integer.parseInt(selectedLineIndex)));
1455         return deleteLine(uifForm, result, request, response);
1456 
1457     }
1458 
1459     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteNotes")
1460     public ModelAndView deleteNotes(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1461                                            HttpServletRequest request, HttpServletResponse response) {
1462         LOG.debug("Initialized addLine method");
1463         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1464         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
1465         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1466         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1467         newOlePatronDocument.getDeletedNotes().add(newOlePatronDocument.getNotes().get(Integer.parseInt(selectedLineIndex)));
1468         return deleteLine(uifForm, result, request, response);
1469 
1470     }
1471 
1472     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteOlePatronLocalIds")
1473     public ModelAndView deleteOlePatronLocalIds(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1474                                           HttpServletRequest request, HttpServletResponse response) {
1475         LOG.debug("Initialized addLine method");
1476         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1477         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
1478         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1479         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1480         newOlePatronDocument.getDeletedOlePatronLocalIds().add(newOlePatronDocument.getOlePatronLocalIds().get(Integer.parseInt(selectedLineIndex)));
1481         return deleteLine(uifForm, result, request, response);
1482 
1483     }
1484 
1485 
1486 
1487     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deletePatronAffiliations")
1488     public ModelAndView deletePatronAffiliations(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1489                                           HttpServletRequest request, HttpServletResponse response) {
1490         LOG.debug("Initialized addLine method");
1491         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1492         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
1493         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1494         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1495         newOlePatronDocument.getDeletedPatronAffiliations().add(newOlePatronDocument.getPatronAffiliations().get(Integer.parseInt(selectedLineIndex)));
1496         newOlePatronDocument.getDeletedEmployments().addAll(newOlePatronDocument.getPatronAffiliations().get(Integer.parseInt(selectedLineIndex)).getEmployments());
1497 
1498         return deleteLine(uifForm, result, request, response);
1499     }
1500 
1501     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteEmploymentBo")
1502     public ModelAndView deleteEmploymentBo(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1503                                                  HttpServletRequest request, HttpServletResponse response) {
1504         LOG.debug("Initialized addLine method");
1505         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1506         Map<String,String> actionParameters = form.getActionParameters();
1507         String subCollectionIndex = actionParameters.get(UifParameters.SELECTED_LINE_INDEX);
1508         String mainCollectionIndex= StringUtils.substringBefore(StringUtils.substringAfter(actionParameters.get(UifParameters.SELLECTED_COLLECTION_PATH),"["),"]");
1509         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
1510         OlePatronDocument newOlePatronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
1511         EntityEmploymentBo entityEmploymentBo=newOlePatronDocument.getPatronAffiliations().get(Integer.parseInt(mainCollectionIndex)).getEmployments().get(Integer.parseInt(subCollectionIndex));
1512         if(entityEmploymentBo!=null){
1513             newOlePatronDocument.getDeletedEmployments().add(entityEmploymentBo);
1514             newOlePatronDocument.getPatronAffiliations().get(Integer.parseInt(mainCollectionIndex)).getEmployments().remove(Integer.parseInt(subCollectionIndex));
1515             return getUIFModelAndView(uifForm);
1516         }else{
1517             newOlePatronDocument.getDeletedEmployments().add(entityEmploymentBo);
1518             return deleteLine(uifForm, result, request, response);
1519         }
1520     }
1521 
1522     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=showPatronLoanedItem")
1523     public ModelAndView showPatronLoanedItem(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1524                                            HttpServletRequest request, HttpServletResponse response) {
1525         LOG.debug("Patron View : showing Patron Loaned Records");
1526         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1527         OlePatronDocument olePatronDocument=(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1528         try {
1529             olePatronDocument.setOleLoanDocuments(getLoanProcessor().getPatronLoanedItemBySolr(olePatronDocument.getOlePatronId()));
1530         } catch (Exception e) {
1531             LOG.error("While fetching loan records error occured" + e);
1532         }
1533         olePatronDocument.setShowLoanedRecords(true);
1534         return getUIFModelAndView(form);
1535     }
1536     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=hidePatronLoanedItem")
1537     public ModelAndView hidePatronLoanedItem(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1538                                            HttpServletRequest request, HttpServletResponse response) {
1539         LOG.debug("Patron View : Hiding Patron Loaned Records");
1540         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1541         OlePatronDocument olePatronDocument=(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1542         olePatronDocument.setOleLoanDocuments(new ArrayList<OleLoanDocument>());
1543         olePatronDocument.setShowLoanedRecords(false);
1544         return getUIFModelAndView(form);
1545     }
1546 
1547     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=showPatronRequestedRecords")
1548     public ModelAndView showPatronRequestedRecords(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1549                                              HttpServletRequest request, HttpServletResponse response) {
1550         LOG.debug("Patron View : showing Patron Requested Records");
1551         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1552         OlePatronDocument olePatronDocument=(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1553         try {
1554             OleDeliverRequestDocumentHelperServiceImpl requestService = new OleDeliverRequestDocumentHelperServiceImpl();
1555             List<OleDeliverRequestBo> oleDeliverRequestBoList = olePatronDocument.getOleDeliverRequestBos();
1556             if (oleDeliverRequestBoList.size() > 0) {
1557                 for (int i = 0; i < oleDeliverRequestBoList.size(); i++) {
1558                     OleItemSearch oleItemSearch = requestService.getItemDetailsForPatron(oleDeliverRequestBoList.get(i).getItemUuid());
1559                     if (oleItemSearch != null && oleItemSearch.getItemBarCode() != null) {
1560                         oleDeliverRequestBoList.get(i).setTitle(oleItemSearch.getTitle());
1561                         oleDeliverRequestBoList.get(i).setCallNumber(oleItemSearch.getCallNumber());
1562                     }
1563                 }
1564             }
1565             olePatronDocument.setOleDeliverRequestBos(loanProcessor.getPatronRequestRecords(olePatronDocument.getOlePatronId()));
1566         } catch (Exception e) {
1567             LOG.error("While fetching Patron Requested Records error occured" + e);
1568         }
1569         olePatronDocument.setShowRequestedItems(true);
1570         return getUIFModelAndView(form);
1571     }
1572 
1573     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=hidePatronRequestedRecords")
1574     public ModelAndView hidePatronRequestedRecords(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1575                                              HttpServletRequest request, HttpServletResponse response) {
1576         LOG.debug("Patron View : Hiding Patron Loaned Records");
1577         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1578         OlePatronDocument olePatronDocument=(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1579         olePatronDocument.setOleDeliverRequestBos(new ArrayList<OleDeliverRequestBo>());
1580         olePatronDocument.setShowRequestedItems(false);
1581         return getUIFModelAndView(form);
1582     }
1583 
1584     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=showTemporaryCirculationHistoryRecords")
1585     public ModelAndView showTemporaryCirculationHistoryRecords(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1586                                              HttpServletRequest request, HttpServletResponse response) {
1587         LOG.debug("Patron View : showing Patron TemporaryCirculationHistory Records");
1588         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1589         OlePatronDocument olePatronDocument=(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1590         try {
1591             olePatronDocument.setOleTemporaryCirculationHistoryRecords(loanProcessor.getPatronTemporaryCirculationHistoryRecords(olePatronDocument.getOlePatronId()));
1592         } catch (Exception e) {
1593             LOG.error("While fetching Patron TemporaryCirculationHistory Records error occured" + e);
1594         }
1595 
1596         olePatronDocument.setShowTemporaryCirculationHistoryRecords(true);
1597         return getUIFModelAndView(form);
1598     }
1599     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=hideTemporaryCirculationHistoryRecords")
1600     public ModelAndView hideTemporaryCirculationHistoryRecords(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1601                                              HttpServletRequest request, HttpServletResponse response) {
1602         LOG.debug("Patron View : Hiding Patron Loaned Records");
1603         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1604         OlePatronDocument olePatronDocument=(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1605         olePatronDocument.setOleTemporaryCirculationHistoryRecords(new ArrayList<OleTemporaryCirculationHistory>());
1606         olePatronDocument.setShowTemporaryCirculationHistoryRecords(false);
1607         return getUIFModelAndView(form);
1608     }
1609 
1610     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshLoanedItemSection")
1611     public ModelAndView refreshLoanedItemSection(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1612                                            HttpServletRequest request, HttpServletResponse response) {
1613         LOG.debug("Patron View : Hiding Patron Loaned Records");
1614         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
1615         OlePatronDocument olePatronDocument=(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1616         return getUIFModelAndView(form);
1617     }
1618 
1619     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshProxyPatron")
1620     public ModelAndView refreshProxyPatron(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1621                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
1622 
1623         if ((uifForm instanceof OlePatronMaintenanceDocumentForm)
1624         		&& !((OlePatronMaintenanceDocumentForm) uifForm).isFilterProxySection()) {
1625         	
1626         	OlePatronMaintenanceDocumentForm form = (OlePatronMaintenanceDocumentForm) uifForm;
1627             OlePatronDocument olePatron =
1628             		(OlePatronDocument)form.getDocument().getNewMaintainableObject().getDataObject();
1629         	
1630             List<OleProxyPatronDocument> oleProxyPatronDocuments = olePatron.getOleProxyPatronDocuments();
1631             List<OleProxyPatronDocument> proxyPatronDocumentList = new ArrayList<OleProxyPatronDocument>();
1632             List<OlePatronDocument> olePatronDocuments = new ArrayList<OlePatronDocument>();
1633 			ProcessLogger.trace("patron:proxy:begin:"
1634 					+ oleProxyPatronDocuments.size());
1635             if (oleProxyPatronDocuments.size() > 0) {
1636                 for (Iterator<OleProxyPatronDocument> proxyPatronIterator = oleProxyPatronDocuments.iterator(); proxyPatronIterator.hasNext(); ) {
1637                     OleProxyPatronDocument oleProxyPatronDocument = (OleProxyPatronDocument) proxyPatronIterator.next();
1638                     Map map = new HashMap();
1639                     map.put(OLEConstants.OlePatron.PATRON_ID, oleProxyPatronDocument.getProxyPatronId());
1640         			ProcessLogger.trace("patron:proxy:"
1641         					+ oleProxyPatronDocument.getProxyPatronId());
1642                     OlePatronDocument olePatronDocument = (OlePatronDocument) getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, map);
1643                     if (olePatronDocument.isActiveIndicator()) {
1644                         oleProxyPatronDocument.setProxyPatronBarcode(olePatronDocument.getBarcode());
1645                         oleProxyPatronDocument.setProxyPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
1646                         oleProxyPatronDocument.setProxyPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
1647                         proxyPatronDocumentList.add(oleProxyPatronDocument);
1648                     }
1649                 }
1650                 olePatron.setOleProxyPatronDocuments(proxyPatronDocumentList);
1651                 ProcessLogger.trace("patron:proxy:end");
1652             }
1653         }
1654         
1655         return super.refresh(uifForm, result, request, response);
1656     }
1657 
1658 }