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