View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.ingest.FileUtil;
7   import org.kuali.ole.ingest.OlePatronRecordHandler;
8   import org.kuali.ole.ingest.OlePatronXMLSchemaValidator;
9   import org.kuali.ole.ingest.pojo.*;
10  import org.kuali.ole.deliver.api.OlePatronDefinition;
11  import org.kuali.ole.deliver.bo.*;
12  import org.kuali.ole.deliver.constant.OLEPatronConstant;
13  import org.kuali.rice.core.api.config.property.ConfigContext;
14  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
15  import org.kuali.rice.kim.impl.identity.address.EntityAddressTypeBo;
16  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationTypeBo;
17  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
18  import org.kuali.rice.kim.impl.identity.email.EntityEmailTypeBo;
19  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
20  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentStatusBo;
21  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentTypeBo;
22  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
23  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
24  import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
25  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
26  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneTypeBo;
27  import org.kuali.rice.krad.service.BusinessObjectService;
28  import org.kuali.rice.krad.service.KRADServiceLocator;
29  import org.kuali.rice.location.impl.campus.CampusBo;
30  import org.kuali.rice.location.impl.country.CountryBo;
31  import org.kuali.rice.location.impl.state.StateBo;
32  import org.xml.sax.SAXException;
33  
34  import java.io.File;
35  import java.io.IOException;
36  import java.net.URISyntaxException;
37  import java.net.URL;
38  import java.sql.*;
39  import java.util.*;
40  
41  /**
42   * OlePatronConverterService generates list of patron to perform ole patron operation.
43   */
44  public class OlePatronConverterService {
45  
46      private static final Logger LOG = Logger.getLogger(OlePatronConverterService.class);
47  
48      private BusinessObjectService businessObjectService;
49      private OlePatronRecordHandler olePatronRecordHandler;
50      private OlePatronService olePatronService;
51      private String patronAddressSource="";
52  
53      public String getPatronAddressSource() {
54          return patronAddressSource;
55      }
56  
57      public void setPatronAddressSource(String patronAddressSource) {
58          this.patronAddressSource = patronAddressSource;
59      }
60  
61      /**
62       * Gets the value of olePatronService which is of type OlePatronService
63       * @return olePatronService(OlePatronService)
64       */
65      public OlePatronService getOlePatronService() {
66          return this.olePatronService;
67      }
68  
69      /**
70       * Sets the value for olePatronService which is of type OlePatronService
71       * @param olePatronService(OlePatronService)
72       */
73      public void setOlePatronService(OlePatronService olePatronService) {
74          this.olePatronService = olePatronService;
75      }
76  
77      /**
78       * This method will get the values from the ingested xml file, check the entity id and addUnmatchedPatron (from screen) .
79       * @param fileContent
80       * @param addUnMatchedPatronFlag
81       * @param fileName
82       * @param olePatronIngestSummaryRecord
83       * @return list of OlePatronDocument
84       * @throws java.io.IOException
85       * @throws java.net.URISyntaxException
86       */
87      public List<OlePatronDocument> persistPatronFromFileContent(String fileContent, boolean addUnMatchedPatronFlag, String fileName, OlePatronIngestSummaryRecord olePatronIngestSummaryRecord,String addressSource,String principalName) throws IOException, URISyntaxException {
88          List<OlePatronDocument> savedOlePatronDocuments = new ArrayList<OlePatronDocument>();
89          Map<String,String> sourceMap = new HashMap<String,String>();
90          sourceMap.put(OLEConstants.OlePatron.OLE_SOURCE_ID,addressSource);
91          List<OleSourceBo> sourceBoList = (List<OleSourceBo>)KRADServiceLocator.getBusinessObjectService().findMatching(OleSourceBo.class,sourceMap);
92          if(sourceBoList.size()>0){
93              this.patronAddressSource= sourceBoList.get(0).getOleSourceCode();
94          }
95          OlePatronGroup olePatronGroup = new OlePatronGroup();
96          OlePatronGroup patron = getOlePatronRecordHandler().buildPatronFromFileContent(fileContent);
97          List<OlePatron> createPatronList = new ArrayList<OlePatron>();
98          List<OlePatron> updatePatronList = new ArrayList<OlePatron>();
99          List<OlePatron> rejectedPatronList = new ArrayList<OlePatron>();
100         List<OlePatron> failedPatronList = new ArrayList<OlePatron>();
101         List<OlePatron> deletePatronList = new ArrayList<OlePatron>();
102         int patronTotCount = patron.getPatronGroup().size();
103 
104         for (int i = 0; i < patron.getPatronGroup().size(); i++) {
105             if (patron.getPatronGroup().get(i).getPatronID() != null && !"".equals(patron.getPatronGroup().get(i).getPatronID())) {
106                 if (isPatronExist(patron.getPatronGroup().get(i).getPatronID())) {
107                     updatePatronList.add(patron.getPatronGroup().get(i));
108                 } else {
109                     if (addUnMatchedPatronFlag) {
110                         createPatronList.add(patron.getPatronGroup().get(i));
111                     } else {
112                         rejectedPatronList.add(patron.getPatronGroup().get(i));
113                     }
114                 }
115             } else {
116                 String patronId = "";
117                 if (addUnMatchedPatronFlag) {
118                     patronId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(OLEConstants.OlePatron.KRIM_ENTITY_ID_S).toString();
119                     patron.getPatronGroup().get(i).setPatronID(patronId);
120                     createPatronList.add(patron.getPatronGroup().get(i));
121                 } else {
122                     rejectedPatronList.add(patron.getPatronGroup().get(i));
123                 }
124             }
125         }
126         if (createPatronList.size() > 0) {
127             savedOlePatronDocuments.addAll(createOlePatronDocument(createPatronList, failedPatronList));
128             olePatronIngestSummaryRecord.setPatronCreateCount(createPatronList.size());
129             createPatronList.clear();
130         }
131         if (updatePatronList.size() > 0) {
132             savedOlePatronDocuments.addAll(updateOlePatronDocument(updatePatronList, failedPatronList));
133             olePatronIngestSummaryRecord.setPatronUpdateCount(updatePatronList.size());
134             updatePatronList.clear();
135         }
136         if (rejectedPatronList.size() > 0) {
137             olePatronIngestSummaryRecord.setPatronRejectCount(rejectedPatronList.size());
138             rejectedPatronList.clear();
139         }
140         if (failedPatronList.size() > 0) {
141             olePatronIngestSummaryRecord.setPatronFailedCount(failedPatronList.size());
142         }
143         olePatronIngestSummaryRecord.setPatronTotCount(patronTotCount);
144 
145         //deletePatronCount = 0;
146         olePatronIngestSummaryRecord.setFileName(fileName);
147         olePatronIngestSummaryRecord.setPrincipalName(principalName);
148         olePatronIngestSummaryRecord.setCreatedDate(new Timestamp(System.currentTimeMillis()));
149         getBusinessObjectService().save(olePatronIngestSummaryRecord);
150         if (failedPatronList.size() > 0) {
151             olePatronGroup.setPatronGroup(failedPatronList);
152             String patronXML = getOlePatronRecordHandler().toXML(failedPatronList);
153             olePatronIngestSummaryRecord.setFailureRecords(patronXML);
154             //saveFailureRecordsForAttachment(patronXML, olePatronIngestSummaryRecord.getOlePatronSummaryId());
155             failedPatronList.clear();
156         }
157         return savedOlePatronDocuments;
158     }
159 
160     /**
161      * This method is for getting the uploaded process message
162      * @param olePatronIngestSummaryRecord
163      * @return message as a string
164      */
165     public String getUploadProcessMessage(OlePatronIngestSummaryRecord olePatronIngestSummaryRecord) {
166 
167         String uploadProcessMessage = OLEConstants.PATRON_RECORD_SUCCESS + OLEConstants.OlePatron.TOTAL_RECORD + olePatronIngestSummaryRecord.getPatronTotCount() +
168                 OLEConstants.OlePatron.CREATED_RECORD + olePatronIngestSummaryRecord.getPatronCreateCount() +
169                 OLEConstants.OlePatron.UPDATED_RECORD + olePatronIngestSummaryRecord.getPatronUpdateCount() +
170                 OLEConstants.OlePatron.REJECTED_RECORD + olePatronIngestSummaryRecord.getPatronRejectCount() +
171                 OLEConstants.OlePatron.FAILED_RECORD + olePatronIngestSummaryRecord.getPatronFailedCount();
172         return uploadProcessMessage;
173     }
174 
175     /**
176      * This method will check the entity id from the database , whether it is existing or not.
177      * @param patronId
178      * @return a boolean flag
179      */
180     public boolean isPatronExist(String patronId) {
181         boolean flag = false;
182         if (getPatron(patronId) != null) {
183             flag = true;
184         } else {
185             flag = false;
186         }
187         return flag;
188     }
189 
190     /**
191      * This method will get the entity id from the EntityBo Object
192      * @param patronId
193      * @return EntityBo object
194      */
195     private EntityBo getPatron(String patronId) {
196         Map<String, Object> criteria = new HashMap<String, Object>(4);
197         criteria.put(OLEConstants.OlePatron.ENTITY_BO_ID, patronId);
198 
199         return getBusinessObjectService().findByPrimaryKey(EntityBo.class, criteria);
200     }
201 
202     /**
203      * This method is for creating a OlePatronDocument
204      * @param createPatronList
205      * @param failedPatronList
206      * @return list of OlePatronDocument
207      */
208     public List<OlePatronDocument> createOlePatronDocument(List<OlePatron> createPatronList, List<OlePatron> failedPatronList) {
209         LOG.debug(" Inside createOlePatronDocument for patron ingest");
210         List<OlePatronDocument> newPatrons = new ArrayList<OlePatronDocument>();
211         OlePatronDocument olePatronDocument = new OlePatronDocument();
212         OlePatron olePatron;
213         int createPatron = createPatronList.size();
214         for (int i = 0; i < createPatronList.size(); i++) {
215             boolean patronCreateFlag = true;
216             olePatron = new OlePatron();
217             olePatron = createPatronList.get(i);
218             EntityBo kimEntity = new EntityBo();
219             kimEntity.setId(olePatron.getPatronID());
220             olePatronDocument.setEntity(kimEntity);
221             if (olePatron.getBarcode() != null) {
222                 if (checkDuplicateBarcode(olePatron.getBarcode())) {
223                     patronCreateFlag=false;
224                     olePatron.setErrorMessage(OLEPatronConstant.DUP_PATRON_BARCODE_ERROR);
225                 } else {
226                     olePatronDocument.setBarcode(olePatron.getBarcode());
227                 }
228             } else {
229                 patronCreateFlag=false;
230                 olePatron.setErrorMessage(OLEPatronConstant.PATRON_BARCODE_EMPTY);
231             }
232             olePatronDocument.setExpirationDate(olePatron.getExpirationDate());
233             olePatronDocument.setActivationDate(olePatron.getActivationDate());
234             olePatronDocument.setCourtesyNotice(olePatron.getPatronLevelPolicies().isReceivesCourtesyNotice());
235             olePatronDocument.setDeliveryPrivilege(olePatron.getPatronLevelPolicies().isHasDeliveryPrivilege());
236             olePatronDocument.setGeneralBlock(olePatron.getPatronLevelPolicies().isGenerallyBlocked());
237             olePatronDocument.setGeneralBlockNotes(olePatron.getPatronLevelPolicies().getGeneralBlockNotes());
238             olePatronDocument.setPagingPrivilege(olePatron.getPatronLevelPolicies().isHasPagingPrivilege());
239             patronCreateFlag &= persistPatronNames(olePatron, olePatronDocument);
240             if (patronCreateFlag)
241                 patronCreateFlag &= persistPatronBorrowerType(olePatron, olePatronDocument);
242             if (patronCreateFlag)
243                 patronCreateFlag &= persistPatronSource(olePatron, olePatronDocument);
244             if (patronCreateFlag)
245                 patronCreateFlag &= persistPatronStatisticalCategory(olePatron, olePatronDocument);
246             if (patronCreateFlag)
247                 patronCreateFlag &= persistPatronPostalAddress(olePatron, olePatronDocument);
248             if (patronCreateFlag)
249                 patronCreateFlag &= persistPatronPhoneNumbers(olePatron, olePatronDocument);
250             if (patronCreateFlag)
251                 patronCreateFlag &= persistPatronEmailAddress(olePatron, olePatronDocument);
252             if (patronCreateFlag)
253                 patronCreateFlag &= persistPatronAffiliations(olePatron, olePatronDocument);
254             if (patronCreateFlag)
255                 patronCreateFlag &= persistPatronEmployments(olePatron, olePatronDocument);
256             if (patronCreateFlag)
257                 patronCreateFlag &= persistPatronNotes(olePatron, olePatronDocument);
258             olePatronDocument.setActiveIndicator(olePatron.isActive());
259             if (patronCreateFlag) {
260                 OlePatronDefinition patron = getOlePatronService().createPatron(OlePatronDocument.to(olePatronDocument));
261                 newPatrons.add(OlePatronDocument.from(patron));
262             } else {
263                 failedPatronList.add(olePatron);
264             }
265         }
266         createPatronList.removeAll(failedPatronList);
267         return newPatrons;
268     }
269 
270     /**
271      * This method is for updating the OlePatronDocument
272      * @param updatePatronList
273      * @param failedPatronList
274      * @return list of OlePatronDocument
275      */
276     public List<OlePatronDocument> updateOlePatronDocument(List<OlePatron> updatePatronList, List<OlePatron> failedPatronList) {
277         LOG.debug(" Inside updateOlePatronDocument for patron ingest");
278         List<OlePatronDocument> updatedPatrons = new ArrayList<OlePatronDocument>();
279         OlePatronDocument olePatronDocument = new OlePatronDocument();
280         OlePatron olePatron;
281         Map criteria = new HashMap<String, String>();
282         for (int i = 0; i < updatePatronList.size(); i++) {
283             olePatron = updatePatronList.get(i);
284             criteria.put(OLEConstants.OlePatron.PATRON_ID, olePatron.getPatronID());
285             List<OlePatronDocument> patronImpls = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, criteria);
286             OlePatronDocument patronDocument;
287             for (Iterator<OlePatronDocument> patronIterator = patronImpls.iterator(); patronIterator.hasNext(); ) {
288                 boolean patronUpdateFlag = true;
289                 patronDocument = patronIterator.next();
290                 olePatronDocument.setOlePatronId(patronDocument.getOlePatronId());
291                 olePatronDocument.setBarcode(olePatron.getBarcode());
292                 olePatronDocument.setExpirationDate(olePatron.getExpirationDate());
293                 olePatronDocument.setActivationDate(olePatron.getActivationDate());
294                 olePatronDocument.setCourtesyNotice(olePatron.getPatronLevelPolicies().isReceivesCourtesyNotice());
295                 olePatronDocument.setDeliveryPrivilege(olePatron.getPatronLevelPolicies().isHasDeliveryPrivilege());
296                 olePatronDocument.setGeneralBlock(olePatron.getPatronLevelPolicies().isGenerallyBlocked());
297                 olePatronDocument.setGeneralBlockNotes(olePatron.getPatronLevelPolicies().getGeneralBlockNotes());
298                 olePatronDocument.setPagingPrivilege(olePatron.getPatronLevelPolicies().isHasPagingPrivilege());
299                 olePatronDocument.setActiveIndicator(olePatron.isActive());
300                 olePatronDocument.setObjectId(patronDocument.getObjectId());
301                 olePatronDocument.setVersionNumber(patronDocument.getVersionNumber());
302                 olePatronDocument.setOleProxyPatronDocuments(patronDocument.getOleProxyPatronDocuments());
303                 olePatronDocument.setOlePatronLocalIds(patronDocument.getOlePatronLocalIds());
304                 olePatronDocument.setLostBarcodes(patronDocument.getLostBarcodes());
305                 patronUpdateFlag &= persistPatronNames(olePatron, olePatronDocument);
306                 if (patronUpdateFlag)
307                     patronUpdateFlag &= persistPatronBorrowerType(olePatron, olePatronDocument);
308                 if (patronUpdateFlag)
309                     patronUpdateFlag &= persistPatronSource(olePatron, olePatronDocument);
310                 if (patronUpdateFlag)
311                     patronUpdateFlag &= persistPatronStatisticalCategory(olePatron, olePatronDocument);
312                 if (patronUpdateFlag)
313                     patronUpdateFlag &= persistPatronPostalAddress(olePatron, olePatronDocument);
314                 if (patronUpdateFlag)
315                     patronUpdateFlag &= persistPatronPhoneNumbers(olePatron, olePatronDocument);
316                 if (patronUpdateFlag)
317                     patronUpdateFlag &= persistPatronEmailAddress(olePatron, olePatronDocument);
318                 if (patronUpdateFlag)
319                     patronUpdateFlag &= persistPatronAffiliations(olePatron, olePatronDocument);
320                 if (patronUpdateFlag)
321                     patronUpdateFlag &= persistPatronEmployments(olePatron, olePatronDocument);
322                 if (patronUpdateFlag)
323                     patronUpdateFlag &= persistPatronNotes(olePatron, olePatronDocument);
324                 if (patronUpdateFlag) {
325                     OlePatronDefinition olePatronDefinition = getOlePatronService().updatePatron(OlePatronDocument.to(olePatronDocument));
326                     updatedPatrons.add(OlePatronDocument.from(olePatronDefinition));
327                 } else {
328                     failedPatronList.add(olePatron);
329                 }
330             }
331         }
332         updatePatronList.removeAll(failedPatronList);
333         return updatedPatrons;
334     }
335 
336    /**
337      * This method is for getting the object of OlePatronGroup from the ingested xml (used for testCase).
338      * @param fileContent
339      * @return OlePatronGroup object
340      * @throws java.net.URISyntaxException
341      * @throws java.io.IOException
342      */
343     public OlePatronGroup buildPatronFromFileContent(String fileContent) throws URISyntaxException, IOException {
344         return getOlePatronRecordHandler().buildPatronFromFileContent(fileContent);
345     }
346 
347     /**
348      * This method will get the resource file(xml file) and convert object from the xml file (used for testCase)
349      * @param fileName
350      * @return OlePatronGroup object (converted from xml)
351      * @throws java.net.URISyntaxException
352      * @throws java.io.IOException
353      * @throws org.xml.sax.SAXException
354      */
355     public OlePatronGroup buildPatron(String fileName) throws URISyntaxException, IOException, SAXException {
356         URL resource = getClass().getResource(fileName);
357         File file = new File(resource.toURI());
358         String fileContent = new FileUtil().readFile(file);
359         if (validProfileXML(fileContent)) {
360             return getOlePatronRecordHandler().buildPatronFromFileContent(fileContent);
361         }
362         return null;
363     }
364 
365     /**
366      * This method will validate the ingested xml against schema.
367      * @param fileContent
368      * @return boolean
369      * @throws java.io.IOException
370      * @throws org.xml.sax.SAXException
371      */
372     private boolean validProfileXML(String fileContent) throws IOException, SAXException {
373         return new OlePatronXMLSchemaValidator().validateContentsAgainstSchema(null);
374     }
375 
376     /**
377      * Persist phone numbers of the patron and set the values from the patron telephone numbers to entity phone bo  object
378      * @param olePatron
379      * @param olePatronDocument
380      * @return boolean flag, to check whether the patron phone number type is valid .
381      */
382     private boolean persistPatronPhoneNumbers(OlePatron olePatron, OlePatronDocument olePatronDocument) {
383         boolean phoneFlag = false;
384         List<EntityPhoneBo> phones = new ArrayList<EntityPhoneBo>();
385         EntityPhoneBo entityPhoneBo;
386         List<OlePatronTelePhoneNumber> olePatronTelePhoneNumbers = olePatron.getTelephoneNumbers();
387         for (Iterator<OlePatronTelePhoneNumber> iterator = olePatronTelePhoneNumbers.iterator(); iterator.hasNext(); ) {
388             OlePatronTelePhoneNumber phoneNumbers = iterator.next();
389             entityPhoneBo = new EntityPhoneBo();
390             entityPhoneBo.setPhoneNumber(phoneNumbers.getTelephoneNumber());
391             Map criteria = new HashMap<String, String>();
392             Map criteriaCountry = new HashMap<String, String>();
393             if (!phoneNumbers.getTelephoneNumberType().equals("")) {
394                 criteria.put(OLEConstants.CODE, phoneNumbers.getTelephoneNumberType());
395                 List<EntityPhoneTypeBo> entityType = (List<EntityPhoneTypeBo>) getBusinessObjectService().findMatching(EntityPhoneTypeBo.class, criteria);
396                 if (entityType.size() > 0) {
397                     entityPhoneBo.setPhoneType(entityType.get(0));
398                     entityPhoneBo.setPhoneTypeCode(entityType.get(0).getCode());
399                     if (phoneNumbers.getExtension() != null && !phoneNumbers.getExtension().equals("")) {
400                         entityPhoneBo.setExtensionNumber(phoneNumbers.getExtension());
401                     }
402                     if (phoneNumbers.getCountry() != null && !phoneNumbers.getCountry().equals("")) {
403                         criteriaCountry.put(OLEConstants.CODE, phoneNumbers.getCountry());
404                         List<CountryBo> countryList = (List<CountryBo>) getBusinessObjectService().findMatching(CountryBo.class, criteriaCountry);
405                         if (countryList.size() > 0) {
406                             entityPhoneBo.setCountryCode(phoneNumbers.getCountry());
407                         } else {
408                             olePatron.setErrorMessage(OLEPatronConstant.COUNTRY_PHONE_ERROR);
409                             return false;
410                         }
411                     }
412                     entityPhoneBo.setActive(phoneNumbers.isActive());
413                     entityPhoneBo.setDefaultValue(phoneNumbers.isDefaults());
414                     phones.add(entityPhoneBo);
415                     boolean defaultValue = checkPhoneMultipleDefault(phones);
416                     if (defaultValue) {
417                         olePatronDocument.setPhones(phones);
418                         phoneFlag = true;
419                     } else {
420                         olePatron.setErrorMessage(OLEPatronConstant.PHONE_DEFAULT_VALUE_ERROR);
421                         phoneFlag = false;
422                     }
423                 } else {
424                     olePatron.setErrorMessage(OLEPatronConstant.PHONETYPE_ERROR);
425                 }
426             } else {
427                 olePatron.setErrorMessage(OLEPatronConstant.PHONETYPE_BLANK_ERROR);
428             }
429         }
430         return phoneFlag;
431     }
432 
433     /**
434      * Persist email Address of the patron and set the values from the patron email address to entity email bo  object
435      * @param olePatron
436      * @param olePatronDocument
437      * @return boolean flag ,to check whether the patron email type is valid .
438      */
439     private boolean persistPatronEmailAddress(OlePatron olePatron, OlePatronDocument olePatronDocument) {
440         boolean emailFlag = false;
441         List<EntityEmailBo> email = new ArrayList<EntityEmailBo>();
442         EntityEmailBo entityEmailBo;
443         List<OlePatronEmailAddress> olePatronEmailAddresses = olePatron.getEmailAddresses();
444         for (Iterator<OlePatronEmailAddress> iterator = olePatronEmailAddresses.iterator(); iterator.hasNext(); ) {
445             OlePatronEmailAddress emailAddresses = iterator.next();
446             entityEmailBo = new EntityEmailBo();
447             entityEmailBo.setEmailAddress(emailAddresses.getEmailAddress());
448             if (!emailAddresses.getEmailAddressType().equals("")) {
449                 Map criteria = new HashMap<String, String>();
450                 criteria.put(OLEConstants.CODE, emailAddresses.getEmailAddressType());
451                 List<EntityEmailTypeBo> entityType = (List<EntityEmailTypeBo>) getBusinessObjectService().findMatching(EntityEmailTypeBo.class, criteria);
452                 if (entityType.size() > 0) {
453                     entityEmailBo.setEmailType(entityType.get(0));
454                     entityEmailBo.setEmailTypeCode(entityType.get(0).getCode());
455                     entityEmailBo.setActive(emailAddresses.isActive());
456                     entityEmailBo.setDefaultValue(emailAddresses.isDefaults());
457                     email.add(entityEmailBo);
458                     boolean defaultValue = checkEmailMultipleDefault(email);
459                     if (defaultValue) {
460                         olePatronDocument.setEmails(email);
461                         emailFlag = true;
462                     } else {
463                         olePatron.setErrorMessage(OLEPatronConstant.EMAIL_DEFAULT_VALUE_ERROR);
464                         emailFlag = false;
465                     }
466                 } else {
467                     olePatron.setErrorMessage(OLEPatronConstant.EMAILTYPE_ERROR);
468                 }
469             } else {
470                 olePatron.setErrorMessage(OLEPatronConstant.EMAILTYPE_BLANK_ERROR);
471             }
472         }
473         return emailFlag;
474     }
475 
476     /**
477      * Persist postal address of the patron and set the values from the patron postal address to entity address bo  object
478      * @param olePatron
479      * @param olePatronDocument
480      * @return boolean flag , to check whether the patron address type is valid .
481      */
482     private boolean persistPatronPostalAddress(OlePatron olePatron, OlePatronDocument olePatronDocument) {
483         boolean addressFlag = false;
484         List<OleEntityAddressBo> addressBo = new ArrayList<OleEntityAddressBo>();
485         OleEntityAddressBo oleEntityAddressBo;
486         List<EntityAddressBo> address = new ArrayList<EntityAddressBo>();
487         List<OleAddressBo> oleAddress = new ArrayList<OleAddressBo>();
488         EntityAddressBo entityAddressBo;
489         OleAddressBo oleAddressBo;
490         List<OlePatronPostalAddress> olePatronPostalAddresses = olePatron.getPostalAddresses();
491         for (Iterator<OlePatronPostalAddress> iterator = olePatronPostalAddresses.iterator(); iterator.hasNext(); ) {
492             OlePatronPostalAddress postalAddresses = iterator.next();
493             oleEntityAddressBo = new OleEntityAddressBo();
494             oleAddressBo = new OleAddressBo();
495             entityAddressBo = new EntityAddressBo();
496             /*if(oleEntityAddressBo.getEntityAddressBo().getId() == null) {
497             String entityAddressSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_addr_id_s").toString();
498             entityAddressBo.setId(entityAddressSeq);
499             }*/
500             List<OleAddressLine> addressLineList = postalAddresses.getAddressLinesList();
501             if (addressLineList.size() > 0) {
502                 for (int i = 0; i < addressLineList.size(); i++) {
503                     if (i == 0) {
504                         entityAddressBo.setLine1(addressLineList.get(i).getAddressLine());
505                     }
506                     if (i == 1) {
507                         entityAddressBo.setLine2(addressLineList.get(i).getAddressLine());
508                     }
509                     if (i == 2) {
510                         entityAddressBo.setLine3(addressLineList.get(i).getAddressLine());
511                     }
512                 }
513             }
514             Map criteria = new HashMap<String, String>();
515             Map criteriaState = new HashMap<String, String>();
516             Map criteriaCountry = new HashMap<String, String>();
517             if (postalAddresses.getPostalAddressType() != null && !postalAddresses.getPostalAddressType().equals("")) {
518                 criteria.put(OLEConstants.CODE, postalAddresses.getPostalAddressType());
519                 List<EntityAddressTypeBo> entityType = (List<EntityAddressTypeBo>) getBusinessObjectService().findMatching(EntityAddressTypeBo.class, criteria);
520                 if (entityType.size() > 0) {
521                     entityAddressBo.setAddressType(entityType.get(0));
522                     entityAddressBo.setAddressTypeCode(entityType.get(0).getCode());
523                 } else {
524                     olePatron.setErrorMessage(OLEPatronConstant.ADDRESSTYPE_ERROR);
525                     return false;
526                 }
527             } else {
528                 olePatron.setErrorMessage(OLEPatronConstant.ADDRESS_TYPE_BLANK_ERROR);
529                 return false;
530             }
531             if (postalAddresses.getStateProvince() != null && !postalAddresses.getStateProvince().equals("")) {
532                 criteriaState.put(OLEConstants.CODE, postalAddresses.getStateProvince());
533                 List<StateBo> stateBoList = (List<StateBo>) getBusinessObjectService().findMatching(StateBo.class, criteriaState);
534                 if (stateBoList.size() > 0) {
535                     entityAddressBo.setStateProvinceCode(postalAddresses.getStateProvince());
536                 } else {
537                     olePatron.setErrorMessage(OLEPatronConstant.STATE_ERROR);
538                     return false;
539                 }
540             }
541             if (postalAddresses.getCountry() != null && !postalAddresses.getCountry().equals("")) {
542                 criteriaCountry.put(OLEConstants.CODE, postalAddresses.getCountry());
543                 List<CountryBo> countryList = (List<CountryBo>) getBusinessObjectService().findMatching(CountryBo.class, criteriaCountry);
544                 if (countryList.size() > 0) {
545                     entityAddressBo.setCountryCode(postalAddresses.getCountry());
546                 } else {
547                     olePatron.setErrorMessage(OLEPatronConstant.COUNTRY_ADDRESS_ERROR);
548                     return false;
549                 }
550             }
551             entityAddressBo.setCity(postalAddresses.getCity());
552             entityAddressBo.setPostalCode(postalAddresses.getPostalCode());
553             entityAddressBo.setActive(postalAddresses.isActive());
554             entityAddressBo.setDefaultValue(postalAddresses.isDefaults());
555             EntityAddressBo entity = getBusinessObjectService().save(entityAddressBo);
556             oleAddressBo.setOleAddressId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(OLEConstants.OlePatron.OLE_DLVR_ADD_S).toString());
557             oleAddressBo.setId(entity.getId());
558             oleAddressBo.setAddressVerified(true);
559             if (postalAddresses.isAddressVerified() || postalAddresses.getAddressSource() != null) {
560                 oleAddressBo.setAddressValidFrom(postalAddresses.getAddressValidFrom());
561                 oleAddressBo.setAddressValidTo(postalAddresses.getAddressValidTo());
562                 oleAddressBo.setAddressVerified(postalAddresses.isAddressVerified());
563                 String addressSource = postalAddresses.getAddressSource();
564                 String addressId;
565                 HashMap<String, String> map = new HashMap<String, String>();
566                 map.put(OLEConstants.OlePatron.OLE_ADD_SRC_CD, addressSource);
567                 List<OleAddressSourceBo> addressSourceList = (List<OleAddressSourceBo>) getBusinessObjectService().findMatching(OleAddressSourceBo.class, map);
568                 if (addressSourceList.size() > 0) {
569                     addressId = addressSourceList.get(0).getOleAddressSourceId();
570                     oleAddressBo.setAddressSource(addressId);
571                     oleAddressBo.setAddressSourceBo(addressSourceList.get(0));
572                 } else {
573                     olePatron.setErrorMessage(OLEPatronConstant.ADDRESS_SOURCE_ERROR);
574                     return false;
575                 }
576             }
577             oleAddress.add(oleAddressBo);
578             oleAddressBo.setEntityAddress(entityAddressBo);
579             oleEntityAddressBo.setEntityAddressBo(entityAddressBo);
580             address.add(entityAddressBo);
581             oleEntityAddressBo.setOleAddressBo(oleAddressBo);
582             addressBo.add(oleEntityAddressBo);
583             boolean defaultValue = checkAddressMultipleDefault(addressBo);
584             if (defaultValue && oleAddressBo.getAddressSource()!=null) {
585                 olePatronDocument.setAddresses(address);
586                 olePatronDocument.setOleAddresses(oleAddress);
587                 olePatronDocument.setOleEntityAddressBo(addressBo);
588                 addressFlag = true;
589             } else {
590                 if (oleAddressBo.getAddressSource() == null) {
591                     olePatron.setErrorMessage(OLEPatronConstant.PATRON_ADDRESS_SOURCE_REQUIRED);
592                 } else {
593                     olePatron.setErrorMessage(OLEPatronConstant.ADDRESS_DEFAULT_VALUE_ERROR);
594                 }
595                 addressFlag = false;
596             }
597         }
598         return addressFlag;
599     }
600 
601     /**
602      * Persist names of the patron and set the values from the patron names to entity name bo  object
603      * @param olePatron
604      * @param olePatronDocument
605      * @return boolean flag , to check whether the patron name type is valid .
606      */
607     private boolean persistPatronNames(OlePatron olePatron, OlePatronDocument olePatronDocument) {
608         boolean nameFlag = false;
609         EntityNameBo names = new EntityNameBo();
610         Map criteria = new HashMap<String, String>();
611         criteria.put(OLEConstants.NAME, OLEConstants.PREFERRED);
612         List<EntityNameTypeBo> entityType = (List<EntityNameTypeBo>) getBusinessObjectService().findMatching(EntityNameTypeBo.class, criteria);
613         if (entityType.size() > 0) {
614             names.setNameType(entityType.get(0));
615             names.setNameCode(entityType.get(0).getCode());
616             if (!olePatron.getName().getFirst().equals("") && !olePatron.getName().getSurname().equals("")) {
617                 names.setFirstName(olePatron.getName().getFirst());
618                 names.setLastName(olePatron.getName().getSurname());
619                 if (olePatron.getName().getTitle() != null && !olePatron.getName().getTitle().equals("")) {
620                     names.setNamePrefix(olePatron.getName().getTitle());
621                 }
622                 if (olePatron.getName().getSuffix() != null && !olePatron.getName().getSuffix().equals("")) {
623                     names.setNameSuffix(olePatron.getName().getSuffix());
624                 }
625                 names.setActive(true);
626                 names.setDefaultValue(true);
627                 olePatronDocument.setName(names);
628                 nameFlag = true;
629             } else {
630                 if (olePatron.getName().getFirst().equals("")) {
631                     olePatron.setErrorMessage(OLEPatronConstant.FIRSTNAME_BLANK_ERROR);
632                 }
633                 if (olePatron.getName().getSurname().equals("")) {
634                     olePatron.setErrorMessage(OLEPatronConstant.SURNAME_BLANK_ERROR);
635                 }
636             }
637         }
638         return nameFlag;
639     }
640 
641     /**
642      * Persist notes of the patron and set the values from the patron notes ( converted object from xml) to Patron notes bo  object
643      * @param olePatron
644      * @param olePatronDocument
645      * @return boolean flag , to check whether the patron note type is valid .
646      */
647     private boolean persistPatronNotes(OlePatron olePatron, OlePatronDocument olePatronDocument) {
648         boolean notesFlag = false;
649         List<OlePatronNotes> olePatronNotesList = new ArrayList<OlePatronNotes>();
650         OlePatronNotes olePatronNotes;
651         List<OlePatronNote> olePatronNoteList = olePatron.getNotes();
652         for (Iterator<OlePatronNote> iterator = olePatronNoteList.iterator(); iterator.hasNext(); ) {
653             OlePatronNote olePatronNote = iterator.next();
654             olePatronNotes = new OlePatronNotes();
655             if (olePatronNote.getNoteType() != null && !"".equals(olePatronNote.getNoteType())) {
656                 Map criteria = new HashMap<String, String>();
657                 criteria.put(OLEConstants.PATRON_NOTE_TYPE_CODE, olePatronNote.getNoteType());
658                 List<OlePatronNoteType> olePatronNoteTypes = (List<OlePatronNoteType>) getBusinessObjectService().findMatching(OlePatronNoteType.class, criteria);
659                 if (olePatronNoteTypes.size() > 0) {
660                     olePatronNotes.setOlePatronNoteType(olePatronNoteTypes.get(0));
661                     olePatronNotes.setPatronNoteTypeId(olePatronNoteTypes.get(0).getPatronNoteTypeId());
662                     olePatronNotes.setPatronNoteText(olePatronNote.getNote());
663                     olePatronNotes.setActive(olePatronNote.isActive());
664                     olePatronNotesList.add(olePatronNotes);
665                     olePatronDocument.setNotes(olePatronNotesList);
666                     notesFlag = true;
667                 } else {
668                     olePatron.setErrorMessage(OLEPatronConstant.NOTETYPE_ERROR);
669                 }
670 
671             } else {
672                 olePatron.setErrorMessage(OLEPatronConstant.NOTETYPE_BLANK_ERROR);
673             }
674         }
675         if (olePatronNoteList.isEmpty()) {
676             notesFlag = true;
677         }
678         return notesFlag;
679     }
680 
681     /**
682      * This method is for persisting the borrower Type of the patron
683      * @param olePatron
684      * @param olePatronDocument
685      * @return boolean borrowerTypeFlag, to check whether the borrower Type is valid.
686      */
687     private boolean persistPatronBorrowerType(OlePatron olePatron, OlePatronDocument olePatronDocument) {
688         boolean borrowerTypeFlag = false;
689         String borroweTypeId;
690         String borrowerTypeName;
691         borrowerTypeName = olePatron.getBorrowerType();
692         if (borrowerTypeName != null && !borrowerTypeName.equals("")) {
693             HashMap<String, String> map = new HashMap<String, String>();
694             map.put(OLEConstants.BORROWER_TYPE_CODE, borrowerTypeName);
695             List<OleBorrowerType> borrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, map);
696             if (borrowerTypes.size() > 0) {
697                 borroweTypeId = borrowerTypes.get(0).getBorrowerTypeId();
698                 olePatronDocument.setBorrowerType(borroweTypeId);
699                 olePatronDocument.setOleBorrowerType(borrowerTypes.get(0));
700                 borrowerTypeFlag = true;
701             } else {
702                 olePatron.setErrorMessage(OLEPatronConstant.BORROWERTYPE_ERROR);
703             }
704         } else {
705             olePatron.setErrorMessage(OLEPatronConstant.BORROWERTYPE_BLANK_ERROR);
706         }
707         return borrowerTypeFlag;
708     }
709 
710     /**
711      * This method is for persisting the Source of the patron
712      * @param olePatron
713      * @param olePatronDocument
714      * @return boolean sourceFlag, to check whether the Source is valid.
715      */
716     private boolean persistPatronSource(OlePatron olePatron, OlePatronDocument olePatronDocument) {
717         boolean sourceFlag = false;
718         String sourceId;
719         String sourceCode;
720         sourceCode = this.patronAddressSource;
721         if (sourceCode != null && !sourceCode.equals("")) {
722             HashMap<String, String> map = new HashMap<String, String>();
723             map.put(OLEConstants.SOURCE_CODE, sourceCode);
724             List<OleSourceBo> sourceList = (List<OleSourceBo>) getBusinessObjectService().findMatching(OleSourceBo.class, map);
725             if (sourceList.size() > 0) {
726                 sourceId = sourceList.get(0).getOleSourceId();
727                 olePatronDocument.setSource(sourceId);
728                 olePatronDocument.setSourceBo(sourceList.get(0));
729                 sourceFlag = true;
730             } else {
731                 olePatron.setErrorMessage(OLEPatronConstant.SOURCE_CODE_ERROR);
732             }
733         } else {
734             sourceFlag = true;
735         }
736         return sourceFlag;
737     }
738 
739 
740     /**
741      * This method is for persisting the Statistical Category of the patron
742      * @param olePatron
743      * @param olePatronDocument
744      * @return boolean statisticalCategoryFlag, to check whether the Statistical Category is valid.
745      */
746     private boolean persistPatronStatisticalCategory(OlePatron olePatron, OlePatronDocument olePatronDocument) {
747         boolean statisticalCategoryFlag = false;
748         String statisticalCategoryId;
749         String statisticalCategoryCode;
750         statisticalCategoryCode = olePatron.getStatisticalCategory();
751         if (statisticalCategoryCode != null && !statisticalCategoryCode.equals("")) {
752             HashMap<String, String> map = new HashMap<String, String>();
753             map.put(OLEConstants.STATISTICAL_CATEGORY_CODE, statisticalCategoryCode);
754             List<OleStatisticalCategoryBo> statisticalCategoryList = (List<OleStatisticalCategoryBo>) getBusinessObjectService().findMatching(OleStatisticalCategoryBo.class, map);
755             if (statisticalCategoryList.size() > 0) {
756                 statisticalCategoryId = statisticalCategoryList.get(0).getOleStatisticalCategoryId();
757                 olePatronDocument.setStatisticalCategory(statisticalCategoryId);
758                 olePatronDocument.setStatisticalCategoryBo(statisticalCategoryList.get(0));
759                 statisticalCategoryFlag = true;
760             } else {
761                 olePatron.setErrorMessage(OLEPatronConstant.STATISTICAL_CATEGORY_CODE_ERROR);
762             }
763         } else {
764             statisticalCategoryFlag = true;
765         }
766         return statisticalCategoryFlag;
767     }
768 
769     /**
770      * This method is for persisting the Affiliations of the patron
771      * @param olePatron
772      * @param olePatronDocument
773      * @return boolean affiliationFlag, to check whether the Affiliations is valid.
774      */
775     private boolean persistPatronAffiliations(OlePatron olePatron, OlePatronDocument olePatronDocument) {
776         boolean affiliationFlag = false;
777         List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
778         OlePatronAffiliation patronAffiliation;
779         if (olePatron.getAffiliations() != null) {
780             List<OlePatronAffiliations> olePatronAffiliationsList = olePatron.getAffiliations();
781             for (Iterator<OlePatronAffiliations> iterator = olePatronAffiliationsList.iterator(); iterator.hasNext(); ) {
782                 OlePatronAffiliations affiliation = iterator.next();
783                 patronAffiliation = new OlePatronAffiliation();
784                 String affiliationSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_afltn_id_s").toString();
785                 if (patronAffiliation.getEntityAffiliationId() == null)
786                     patronAffiliation.setEntityAffiliationId(affiliationSeq);
787                 HashMap<String, String> map = new HashMap<String, String>();
788                 map.put(OLEConstants.CODE, affiliation.getAffiliationType());
789                 List<EntityAffiliationTypeBo> patronAffiliationList = (List<EntityAffiliationTypeBo>) getBusinessObjectService().findMatching(EntityAffiliationTypeBo.class, map);
790                 if (patronAffiliationList.size() > 0) {
791                     patronAffiliation.setAffiliationTypeCode(patronAffiliationList.get(0).getCode());
792                     patronAffiliation.setAffiliationType(patronAffiliationList.get(0));
793                 } else {
794                     olePatron.setErrorMessage(OLEPatronConstant.AFFILIATION_CODE_ERROR);
795                     return false;
796                 }
797                 HashMap<String, String> campusMap = new HashMap<String, String>();
798                 campusMap.put(OLEConstants.CODE, affiliation.getCampusCode());
799                 List<CampusBo> campusBos = (List<CampusBo>) getBusinessObjectService().findMatching(CampusBo.class, campusMap);
800                 if (campusBos.size() > 0) {
801                     patronAffiliation.setCampusCode(affiliation.getCampusCode());
802                 } else {
803                     olePatron.setErrorMessage(OLEPatronConstant.CAMPUS_CODE_ERROR);
804                     return false;
805                 }
806                 patronAffiliation.setCampusCode(affiliation.getCampusCode());
807                 patronAffiliations.add(patronAffiliation);
808                 olePatronDocument.setPatronAffiliations(patronAffiliations);
809                 affiliationFlag = true;
810             }
811         } else {
812             affiliationFlag = true;
813         }
814         return affiliationFlag;
815     }
816 
817 
818     private boolean persistPatronEmployments(OlePatron olePatron, OlePatronDocument olePatronDocument) {
819         boolean employmentFlag = false;
820         List<EntityEmploymentBo> patronEmploymentBoList = new ArrayList<EntityEmploymentBo>();
821         EntityEmploymentBo patronEmploymentBo;
822         if (olePatron.getAffiliations() != null) {
823             for (int i = 0; i < olePatron.getAffiliations().size(); i++) {
824                 if (olePatron.getAffiliations().get(i).getEmployments() != null) {
825                     OlePatronAffiliation olePatronAffiliation = olePatronDocument.getPatronAffiliations().get(i);
826                     List<OlePatronEmployments> patronEmploymentList = olePatron.getAffiliations().get(i).getEmployments();
827                     for (Iterator<OlePatronEmployments> iterator = patronEmploymentList.iterator(); iterator.hasNext(); ) {
828                         OlePatronEmployments employment = iterator.next();
829                         patronEmploymentBo = new EntityEmploymentBo();
830                         patronEmploymentBo.setEntityAffiliationId(olePatronAffiliation.getEntityAffiliationId());
831                         patronEmploymentBo.setEntityAffiliation(olePatronAffiliation.getEntityAffliationBo());
832                         patronEmploymentBo.setEmployeeId(employment.getEmployeeId());
833                         patronEmploymentBo.setPrimary(employment.getPrimary());
834                         patronEmploymentBo.setBaseSalaryAmount((employment.getBaseSalaryAmount()));
835                         patronEmploymentBo.setPrimaryDepartmentCode(employment.getPrimaryDepartmentCode());
836                         patronEmploymentBo.setActive(employment.isActive());
837                         HashMap<String, String> map = new HashMap<String, String>();
838                         map.put(OLEConstants.CODE, employment.getEmployeeStatusCode());
839                         List<EntityEmploymentStatusBo> patronEmploymentStatusList = (List<EntityEmploymentStatusBo>) getBusinessObjectService().findMatching(EntityEmploymentStatusBo.class, map);
840                         if (patronEmploymentStatusList.size() > 0) {
841                             patronEmploymentBo.setEmployeeStatusCode(patronEmploymentStatusList.get(0).getCode());
842                             patronEmploymentBo.setEmployeeStatus(patronEmploymentStatusList.get(0));
843                         } else {
844                             olePatron.setErrorMessage(OLEPatronConstant.EMPLOYMENT_STATUS_ERROR);
845                             return false;
846                         }
847                         HashMap<String, String> empMap = new HashMap<String, String>();
848                         empMap.put(OLEConstants.CODE, employment.getEmployeeTypeCode());
849                         List<EntityEmploymentTypeBo> employmentTypeBoList = (List<EntityEmploymentTypeBo>) getBusinessObjectService().findMatching(EntityEmploymentTypeBo.class, empMap);
850                         if (employmentTypeBoList.size() > 0) {
851                             patronEmploymentBo.setEmployeeTypeCode(employmentTypeBoList.get(0).getCode());
852                             patronEmploymentBo.setEmployeeType(employmentTypeBoList.get(0));
853                         } else {
854                             olePatron.setErrorMessage(OLEPatronConstant.EMPLOYMENT_TYPE_ERROR);
855                             return false;
856                         }
857                         patronEmploymentBo.setEmployeeTypeCode(employment.getEmployeeTypeCode());
858                         patronEmploymentBoList.add(patronEmploymentBo);
859                         olePatronDocument.getPatronAffiliations().get(i).setEmployments(patronEmploymentBoList);
860                         employmentFlag = true;
861                     }
862                 }
863             }
864         } else {
865             employmentFlag = true;
866         }
867         return employmentFlag;
868     }
869 
870     /**
871      * This method is for saving the failed records in a separate location of home directory.
872      * @param patronXML
873      * @param patronReportId
874      */
875     private void saveFailureRecordsForAttachment(String patronXML, String patronReportId) {
876         OlePatronIngestSummaryRecord olePatronIngestSummaryRecord;
877         try {
878             HashMap<String, String> map = new HashMap<String, String>();
879             String directory = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.STAGING_DIRECTORY) +
880                     OLEConstants.PATRON_FILE_DIRECTORY;
881             String homeDirectory = System.getProperty(OLEConstants.USER_HOME_DIRECTORY);
882             int reportId = Integer.parseInt(patronReportId);
883             File file = new File(homeDirectory + directory);
884             if (file.isDirectory()) {
885                 file = new File(homeDirectory + directory + reportId + OLEConstants.FAILED_PATRON_RECORD_NAME);
886                 file.createNewFile();
887                 FileUtils.writeStringToFile(file, patronXML);
888             } else {
889                 file.mkdirs();
890                 if (file.isDirectory()) {
891                     File newFile = new File(file, reportId + OLEConstants.FAILED_PATRON_RECORD_NAME);
892                     newFile.createNewFile();
893                     FileUtils.writeStringToFile(newFile, patronXML);
894                 }
895             }
896         } catch (IOException e) {
897             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
898         }
899     }
900 
901     /**
902      * This method is for checking the entity phone has multiple defaults
903      * @param phoneBoList
904      * @return true , if the entity phone has only one default in a single patron record, else false
905      */
906     protected boolean checkPhoneMultipleDefault(List<EntityPhoneBo> phoneBoList) {
907 
908         boolean valid = true;
909         int defaultCounter = 0;
910         for (EntityPhoneBo entityPhoneBo : phoneBoList) {
911             if (entityPhoneBo.isDefaultValue()) {
912                 defaultCounter++;
913             }
914         }
915         if (defaultCounter != 1 ) {
916             valid = false;
917         }
918         return valid;
919     }
920 
921     /**
922      * This method is for checking the entity address has multiple defaults
923      * @param addrBoList
924      * @return true , if the entity address has only one default in a single patron record, else false
925      */
926     protected boolean checkAddressMultipleDefault(List<OleEntityAddressBo> addrBoList) {
927 
928         boolean valid = true;
929         int defaultCounter = 0;
930         for (OleEntityAddressBo oleEntityAddressBo : addrBoList) {
931             EntityAddressBo entityAddressBo = oleEntityAddressBo.getEntityAddressBo();
932             if (entityAddressBo.isDefaultValue()) {
933                 defaultCounter++;
934             }
935         }
936         if (defaultCounter != 1) {
937             valid = false;
938         }
939         return valid;
940     }
941 
942     /**
943      * This method is for checking the entity email address has multiple defaults
944      * @param emailBoList
945      * @return true , if the entity email address has only one default in a single patron record, else false
946      */
947     protected boolean checkEmailMultipleDefault(List<EntityEmailBo> emailBoList) {
948 
949         boolean valid = true;
950         int defaultCounter = 0;
951         for (EntityEmailBo entityEmailBo : emailBoList) {
952             if (entityEmailBo.isDefaultValue()) {
953                 defaultCounter++;
954             }
955         }
956         if (defaultCounter != 1) {
957             valid = false;
958         }
959         return valid;
960     }
961 
962     /**
963      * Gets the instance of BusinessObjectService
964      * @return businessObjectService(BusinessObjectService)
965      */
966     private BusinessObjectService getBusinessObjectService() {
967         if (null == businessObjectService) {
968             businessObjectService = KRADServiceLocator.getBusinessObjectService();
969         }
970         return businessObjectService;
971     }
972 
973     /**
974      * Gets the instance of OlePatronRecordHandler
975      * @return olePatronRecordHandler(OlePatronRecordHandler)
976      */
977     public OlePatronRecordHandler getOlePatronRecordHandler() {
978         if (null == olePatronRecordHandler) {
979             olePatronRecordHandler = new OlePatronRecordHandler();
980         }
981         return olePatronRecordHandler;
982     }
983 
984     /**
985      * Sets the olePatronRecordHandler which is of type OlePatronRecordHandler
986      * @param olePatronRecordHandler(OlePatronRecordHandler)
987      */
988     public void setOlePatronRecordHandler(OlePatronRecordHandler olePatronRecordHandler) {
989         this.olePatronRecordHandler = olePatronRecordHandler;
990     }
991 
992     protected boolean checkDuplicateBarcode(String barcode) {
993         boolean valid = false;
994         Map barcodeMap = new HashMap();
995         barcodeMap.put(OLEConstants.OlePatron.BARCODE, barcode);
996         List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, barcodeMap);
997         if (olePatronDocuments.size() > 0) {
998             for (OlePatronDocument olePatronDocument : olePatronDocuments) {
999                 if (barcode == null || !(barcode).equals(olePatronDocument.getOlePatronId())) {
1000                     /*this.putFieldError("dataObject.barcode", "error.barcode.duplicate");*/
1001                     valid = true;
1002                 }
1003             }
1004         }
1005         Map<String, String> map = new HashMap<String, String>();
1006         map.put("invalidOrLostBarcodeNumber", barcode);
1007         List<OlePatronLostBarcode> olePatronLostBarcodes = (List<OlePatronLostBarcode>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronLostBarcode.class, map);
1008         if (olePatronLostBarcodes.size() > 0) {
1009             for(OlePatronLostBarcode olePatronLostBarcode:olePatronLostBarcodes){
1010                 if(olePatronLostBarcode.getInvalidOrLostBarcodeNumber()==null || !(olePatronLostBarcode.getInvalidOrLostBarcodeNumber().equalsIgnoreCase(barcode))){
1011                     valid =true;
1012                 }
1013             }
1014         }
1015         return valid;
1016     }
1017 }