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