001 package org.kuali.ole.service;
002
003 import org.apache.commons.io.FileUtils;
004 import org.apache.log4j.Logger;
005 import org.kuali.ole.OLEConstants;
006 import org.kuali.ole.PropertyUtil;
007 import org.kuali.ole.deliver.loan.bo.OleLoanDocument;
008 import org.kuali.ole.ingest.FileUtil;
009 import org.kuali.ole.ingest.OlePatronRecordHandler;
010 import org.kuali.ole.ingest.OlePatronXMLSchemaValidator;
011 import org.kuali.ole.ingest.pojo.*;
012 import org.kuali.ole.patron.api.OlePatronDefinition;
013 import org.kuali.ole.patron.bo.*;
014 import org.kuali.ole.patron.constant.OLEPatronConstant;
015 import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
016 import org.kuali.rice.kim.impl.identity.address.EntityAddressTypeBo;
017 import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
018 import org.kuali.rice.kim.impl.identity.email.EntityEmailTypeBo;
019 import org.kuali.rice.kim.impl.identity.entity.EntityBo;
020 import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
021 import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
022 import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
023 import org.kuali.rice.kim.impl.identity.phone.EntityPhoneTypeBo;
024 import org.kuali.rice.krad.service.BusinessObjectService;
025 import org.kuali.rice.krad.service.KRADServiceLocator;
026 import org.kuali.rice.location.impl.country.CountryBo;
027 import org.kuali.rice.location.impl.state.StateBo;
028 import org.xml.sax.SAXException;
029
030 import java.io.File;
031 import java.io.IOException;
032 import java.net.URISyntaxException;
033 import java.net.URL;
034 import java.util.*;
035
036 /**
037 * OlePatronConverterService generates list of patron to perform ole patron operation.
038 */
039 public class OlePatronConverterService {
040
041 private static final Logger LOG = Logger.getLogger(OlePatronConverterService.class);
042
043 private BusinessObjectService businessObjectService;
044 private OlePatronRecordHandler olePatronRecordHandler;
045 private OlePatronService olePatronService;
046 int deletePatronCount = 0;
047
048 /**
049 * Gets the value of olePatronService which is of type OlePatronService
050 * @return olePatronService(OlePatronService)
051 */
052 public OlePatronService getOlePatronService() {
053 return this.olePatronService;
054 }
055
056 /**
057 * Sets the value for olePatronService which is of type OlePatronService
058 * @param olePatronService(OlePatronService)
059 */
060 public void setOlePatronService(OlePatronService olePatronService) {
061 this.olePatronService = olePatronService;
062 }
063
064 /**
065 * This method will get the values from the ingested xml file, check the entity id and addUnmatchedPatron (from screen) .
066 * @param fileContent
067 * @param addUnMatchedPatronFlag
068 * @param fileName
069 * @param olePatronIngestSummaryRecord
070 * @return list of OlePatronDocument
071 * @throws java.io.IOException
072 * @throws java.net.URISyntaxException
073 */
074 public List<OlePatronDocument> persistPatronFromFileContent(String fileContent, boolean addUnMatchedPatronFlag, boolean deletePatron, String fileName,OlePatronIngestSummaryRecord olePatronIngestSummaryRecord) throws IOException, URISyntaxException {
075 List<OlePatronDocument> savedOlePatronDocuments = new ArrayList<OlePatronDocument>();
076 OlePatronGroup olePatronGroup = new OlePatronGroup();
077 OlePatronGroup patron = getOlePatronRecordHandler().buildPatronFromFileContent(fileContent);
078 List<OlePatron> createPatronList = new ArrayList<OlePatron>();
079 List<OlePatron> updatePatronList = new ArrayList<OlePatron>();
080 List<OlePatron> rejectedPatronList = new ArrayList<OlePatron>();
081 List<OlePatron> failedPatronList = new ArrayList<OlePatron>();
082 List<OlePatron> deletePatronList = new ArrayList<OlePatron>();
083 int patronTotCount = patron.getPatronGroup().size();
084
085 for (int i = 0; i < patron.getPatronGroup().size(); i++) {
086 if (patron.getPatronGroup().get(i).getPatronID() != null && !"".equals(patron.getPatronGroup().get(i).getPatronID())) {
087 if (isPatronExist(patron.getPatronGroup().get(i).getPatronID())) {
088
089 if(deletePatron) {
090
091 deletePatronList.add(patron.getPatronGroup().get(i));
092 }
093 updatePatronList.add(patron.getPatronGroup().get(i));
094
095 } else {
096 if (addUnMatchedPatronFlag) {
097 createPatronList.add(patron.getPatronGroup().get(i));
098 } else {
099 rejectedPatronList.add(patron.getPatronGroup().get(i));
100 }
101 }
102 } else {
103 String patronId = "";
104 if (addUnMatchedPatronFlag) {
105 patronId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S").toString();
106 patron.getPatronGroup().get(i).setPatronID(patronId);
107 createPatronList.add(patron.getPatronGroup().get(i));
108 } else {
109 rejectedPatronList.add(patron.getPatronGroup().get(i));
110 }
111 }
112 }
113 if(deletePatronList.size() > 0) {
114 deletePatronDocument(deletePatronList);
115 }
116 if (createPatronList.size() > 0) {
117 savedOlePatronDocuments.addAll(createOlePatronDocument(createPatronList,failedPatronList));
118 olePatronIngestSummaryRecord.setPatronCreateCount(createPatronList.size());
119 createPatronList.clear();
120 }
121 if (updatePatronList.size() > 0) {
122 savedOlePatronDocuments.addAll(updateOlePatronDocument(updatePatronList,failedPatronList));
123 olePatronIngestSummaryRecord.setPatronUpdateCount(updatePatronList.size()-deletePatronCount);
124 updatePatronList.clear();
125 }
126 if (rejectedPatronList.size() > 0) {
127 olePatronIngestSummaryRecord.setPatronRejectCount(rejectedPatronList.size());
128 rejectedPatronList.clear();
129 }
130 if (failedPatronList.size()>0) {
131 olePatronIngestSummaryRecord.setPatronFailedCount(failedPatronList.size());
132 }
133 olePatronIngestSummaryRecord.setPatronTotCount(patronTotCount);
134 olePatronIngestSummaryRecord.setPatronDeleteCount(deletePatronCount);
135 //deletePatronCount = 0;
136 olePatronIngestSummaryRecord.setFileName(fileName);
137 getBusinessObjectService().save(olePatronIngestSummaryRecord);
138 if (failedPatronList.size()>0) {
139 olePatronGroup.setPatronGroup(failedPatronList);
140 String patronXML = getOlePatronRecordHandler().toXML(failedPatronList);
141 saveFailureRecordsForAttachment(patronXML,olePatronIngestSummaryRecord.getOlePatronSummaryId());
142 failedPatronList.clear();
143 }
144 return savedOlePatronDocuments;
145 }
146
147 /**
148 * This method is for getting the uploaded process message
149 * @param olePatronIngestSummaryRecord
150 * @return message as a string
151 */
152 public String getUploadProcessMessage(OlePatronIngestSummaryRecord olePatronIngestSummaryRecord){
153
154 String uploadProcessMessage = OLEConstants.PATRON_RECORD_SUCCESS+" Total record : "+olePatronIngestSummaryRecord.getPatronTotCount()+
155 ", Created record : "+olePatronIngestSummaryRecord.getPatronCreateCount()+
156 ", Updated record : "+olePatronIngestSummaryRecord.getPatronUpdateCount()+
157 ", Rejected record : "+olePatronIngestSummaryRecord.getPatronRejectCount()+
158 ", Failed record : "+olePatronIngestSummaryRecord.getPatronFailedCount()+
159 ", Deleted record : "+olePatronIngestSummaryRecord.getPatronDeleteCount();
160 return uploadProcessMessage;
161 }
162
163 /**
164 * This method will check the entity id from the database , whether it is existing or not.
165 * @param patronId
166 * @return a boolean flag
167 */
168 public boolean isPatronExist(String patronId) {
169 boolean flag = false;
170 if (getPatron(patronId) != null) {
171 flag = true;
172 } else {
173 flag = false;
174 }
175 return flag;
176 }
177
178 /**
179 * This method will get the entity id from the EntityBo Object
180 * @param patronId
181 * @return EntityBo object
182 */
183 private EntityBo getPatron(String patronId) {
184 Map<String, Object> criteria = new HashMap<String, Object>(4);
185 criteria.put(OLEConstants.OlePatron.ENTITY_BO_ID, patronId);
186
187 return getBusinessObjectService().findByPrimaryKey(EntityBo.class, criteria);
188 }
189
190 /**
191 * This method is for creating a OlePatronDocument
192 * @param createPatronList
193 * @param failedPatronList
194 * @return list of OlePatronDocument
195 */
196 public List<OlePatronDocument> createOlePatronDocument(List<OlePatron> createPatronList,List<OlePatron> failedPatronList) {
197 List<OlePatronDocument> newPatrons = new ArrayList<OlePatronDocument>();
198 OlePatronDocument olePatronDocument = new OlePatronDocument();
199 OlePatron olePatron;
200 int createPatron = createPatronList.size();
201 for (int i = 0; i < createPatronList.size(); i++) {
202 boolean patronCreateFlag = true;
203 olePatron = new OlePatron();
204 olePatron = createPatronList.get(i);
205 EntityBo kimEntity = new EntityBo();
206 kimEntity.setId(olePatron.getPatronID());
207 olePatronDocument.setEntity(kimEntity);
208 olePatronDocument.setBarcode(olePatron.getBarcode());
209 olePatronDocument.setExpirationDate(olePatron.getExpirationDate());
210 olePatronDocument.setCourtesyNotice(olePatron.getPatronLevelPolicies().isReceivesCourtesyNotice());
211 olePatronDocument.setDeliveryPrivilege(olePatron.getPatronLevelPolicies().isHasDeliveryPrivilege());
212 olePatronDocument.setGeneralBlock(olePatron.getPatronLevelPolicies().isGenerallyBlocked());
213 olePatronDocument.setPagingPrivilege(olePatron.getPatronLevelPolicies().isHasPagingPrivilege());
214 patronCreateFlag &= persistPatronNames(olePatron, olePatronDocument);
215 if(patronCreateFlag)
216 patronCreateFlag &= persistPatronBorrowerType(olePatron, olePatronDocument);
217 if(patronCreateFlag)
218 patronCreateFlag &= persistPatronPostalAddress(olePatron, olePatronDocument);
219 if(patronCreateFlag)
220 patronCreateFlag &= persistPatronPhoneNumbers(olePatron, olePatronDocument);
221 if(patronCreateFlag)
222 patronCreateFlag &= persistPatronEmailAddress(olePatron, olePatronDocument);
223 if(patronCreateFlag)
224 patronCreateFlag &= persistPatronNotes(olePatron, olePatronDocument);
225 olePatronDocument.setActiveIndicator(olePatron.isActive());
226 if (patronCreateFlag) {
227 OlePatronDefinition patron = getOlePatronService().createPatron(OlePatronDocument.to(olePatronDocument));
228 newPatrons.add(OlePatronDocument.from(patron));
229 } else {
230 failedPatronList.add(olePatron);
231 }
232 }
233 createPatronList.removeAll(failedPatronList);
234 return newPatrons;
235 }
236
237 /**
238 * This method is for updating the OlePatronDocument
239 * @param updatePatronList
240 * @param failedPatronList
241 * @return list of OlePatronDocument
242 */
243 public List<OlePatronDocument> updateOlePatronDocument(List<OlePatron> updatePatronList,List<OlePatron> failedPatronList) {
244 List<OlePatronDocument> updatedPatrons = new ArrayList<OlePatronDocument>();
245 OlePatronDocument olePatronDocument = new OlePatronDocument();
246 OlePatron olePatron;
247 Map criteria = new HashMap<String, String>();
248 for (int i = 0; i < updatePatronList.size(); i++) {
249 olePatron = updatePatronList.get(i);
250 criteria.put(OLEConstants.OlePatron.PATRON_ID, olePatron.getPatronID());
251 List<OlePatronDocument> patronImpls = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, criteria);
252 OlePatronDocument patronDocument;
253 for (Iterator<OlePatronDocument> patronIterator = patronImpls.iterator(); patronIterator.hasNext(); ) {
254 boolean patronUpdateFlag = true;
255 patronDocument = patronIterator.next();
256 olePatronDocument.setOlePatronId(patronDocument.getOlePatronId());
257 olePatronDocument.setBarcode(olePatron.getBarcode());
258 olePatronDocument.setExpirationDate(olePatron.getExpirationDate());
259 olePatronDocument.setCourtesyNotice(olePatron.getPatronLevelPolicies().isReceivesCourtesyNotice());
260 olePatronDocument.setDeliveryPrivilege(olePatron.getPatronLevelPolicies().isHasDeliveryPrivilege());
261 olePatronDocument.setGeneralBlock(olePatron.getPatronLevelPolicies().isGenerallyBlocked());
262 olePatronDocument.setPagingPrivilege(olePatron.getPatronLevelPolicies().isHasPagingPrivilege());
263 olePatronDocument.setActiveIndicator(olePatron.isActive());
264 olePatronDocument.setObjectId(patronDocument.getObjectId());
265 olePatronDocument.setVersionNumber(patronDocument.getVersionNumber());
266
267 patronUpdateFlag &= persistPatronNames(olePatron, olePatronDocument);
268 if(patronUpdateFlag)
269 patronUpdateFlag &= persistPatronBorrowerType(olePatron, olePatronDocument);
270 if(patronUpdateFlag)
271 patronUpdateFlag &= persistPatronPostalAddress(olePatron, olePatronDocument);
272 if(patronUpdateFlag)
273 patronUpdateFlag &= persistPatronPhoneNumbers(olePatron, olePatronDocument);
274 if(patronUpdateFlag)
275 patronUpdateFlag &= persistPatronEmailAddress(olePatron, olePatronDocument);
276 if(patronUpdateFlag)
277 patronUpdateFlag &= persistPatronNotes(olePatron, olePatronDocument);
278 if (patronUpdateFlag) {
279 OlePatronDefinition olePatronDefinition = getOlePatronService().updatePatron(OlePatronDocument.to(olePatronDocument));
280 updatedPatrons.add(OlePatronDocument.from(olePatronDefinition));
281 } else {
282 failedPatronList.add(olePatron);
283 }
284 }
285 }
286 updatePatronList.removeAll(failedPatronList);
287 return updatedPatrons;
288 }
289
290 public void deletePatronDocument(List<OlePatron> deletePatronList) {
291
292 OlePatronDocument olePatronDocument = new OlePatronDocument();
293 OlePatron olePatron;
294 Map criteria = new HashMap<String, String>();
295 for (int i = 0; i < deletePatronList.size(); i++) {
296 olePatron = deletePatronList.get(i);
297 criteria.put(OLEConstants.OlePatron.PATRON_ID, olePatron.getPatronID());
298 List<OlePatronDocument> patronImpls = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, criteria);
299 for (Iterator<OlePatronDocument> patronIterator = patronImpls.iterator(); patronIterator.hasNext(); ) {
300 olePatronDocument = patronIterator.next();
301 List<OleLoanDocument> oleLoanDocuments = olePatronDocument.getOleLoanDocuments();
302 if(oleLoanDocuments.size() == 0 && (olePatronDocument.getExpirationDate().before(new Date()))) {
303 KRADServiceLocator.getBusinessObjectService().delete(patronImpls);
304 deletePatronCount =+ 1;
305 }
306 }
307
308 }
309 }
310 /**
311 * This method is for getting the object of OlePatronGroup from the ingested xml (used for testCase).
312 * @param fileContent
313 * @return OlePatronGroup object
314 * @throws java.net.URISyntaxException
315 * @throws java.io.IOException
316 */
317 public OlePatronGroup buildPatronFromFileContent(String fileContent) throws URISyntaxException, IOException {
318 return getOlePatronRecordHandler().buildPatronFromFileContent(fileContent);
319 }
320
321 /**
322 * This method will get the resource file(xml file) and convert object from the xml file (used for testCase)
323 * @param fileName
324 * @return OlePatronGroup object (converted from xml)
325 * @throws java.net.URISyntaxException
326 * @throws java.io.IOException
327 * @throws org.xml.sax.SAXException
328 */
329 public OlePatronGroup buildPatron(String fileName) throws URISyntaxException, IOException, SAXException {
330 URL resource = getClass().getResource(fileName);
331 File file = new File(resource.toURI());
332 String fileContent = new FileUtil().readFile(file);
333 if (validProfileXML(fileContent)) {
334 return getOlePatronRecordHandler().buildPatronFromFileContent(fileContent);
335 }
336 return null;
337 }
338
339 /**
340 * This method will validate the ingested xml against schema.
341 * @param fileContent
342 * @return boolean
343 * @throws java.io.IOException
344 * @throws org.xml.sax.SAXException
345 */
346 private boolean validProfileXML(String fileContent) throws IOException, SAXException {
347 return new OlePatronXMLSchemaValidator().validateContentsAgainstSchema(null);
348 }
349
350 /**
351 * Persist phone numbers of the patron and set the values from the patron telephone numbers to entity phone bo object
352 * @param olePatron
353 * @param olePatronDocument
354 * @return boolean flag, to check whether the patron phone number type is valid .
355 */
356 private boolean persistPatronPhoneNumbers(OlePatron olePatron, OlePatronDocument olePatronDocument) {
357 boolean phoneFlag = false;
358 List<EntityPhoneBo> phones = new ArrayList<EntityPhoneBo>();
359 EntityPhoneBo entityPhoneBo;
360 List<OlePatronTelePhoneNumber> olePatronTelePhoneNumbers = olePatron.getTelephoneNumbers();
361 for (Iterator<OlePatronTelePhoneNumber> iterator = olePatronTelePhoneNumbers.iterator(); iterator.hasNext(); ) {
362 OlePatronTelePhoneNumber phoneNumbers = iterator.next();
363 entityPhoneBo = new EntityPhoneBo();
364 entityPhoneBo.setPhoneNumber(phoneNumbers.getTelephoneNumber());
365 Map criteria = new HashMap<String, String>();
366 Map criteriaCountry = new HashMap<String, String>();
367 if(!phoneNumbers.getTelephoneNumberType().equals("")){
368 criteria.put(OLEConstants.CODE, phoneNumbers.getTelephoneNumberType());
369 List<EntityPhoneTypeBo> entityType = (List<EntityPhoneTypeBo>) getBusinessObjectService().findMatching(EntityPhoneTypeBo.class, criteria);
370 if (entityType.size()> 0) {
371 entityPhoneBo.setPhoneType(entityType.get(0));
372 entityPhoneBo.setPhoneTypeCode(entityType.get(0).getCode());
373 if(phoneNumbers.getExtension() != null && !phoneNumbers.getExtension().equals("")){
374 entityPhoneBo.setExtensionNumber(phoneNumbers.getExtension());
375 }
376 if(phoneNumbers.getCountry() != null && !phoneNumbers.getCountry().equals("")){
377 criteriaCountry.put(OLEConstants.CODE, phoneNumbers.getCountry());
378 List<CountryBo> countryList = (List<CountryBo>) getBusinessObjectService().findMatching(CountryBo.class, criteriaCountry);
379 if (countryList.size()>0) {
380 entityPhoneBo.setCountryCode(phoneNumbers.getCountry());
381 } else {
382 olePatron.setErrorMessage(OLEPatronConstant.COUNTRY_PHONE_ERROR);
383 return false;
384 }
385 }
386 entityPhoneBo.setActive(phoneNumbers.isActive());
387 entityPhoneBo.setDefaultValue(phoneNumbers.isDefaults());
388 phones.add(entityPhoneBo);
389 boolean defaultValue = checkPhoneMultipleDefault(phones);
390 if(defaultValue){
391 olePatronDocument.setPhones(phones);
392 phoneFlag = true;
393 } else {
394 olePatron.setErrorMessage(OLEPatronConstant.PHONE_DEFAULT_VALUE_ERROR);
395 phoneFlag = false;
396 }
397 } else{
398 olePatron.setErrorMessage(OLEPatronConstant.PHONETYPE_ERROR);
399 }
400 } else{
401 olePatron.setErrorMessage(OLEPatronConstant.PHONETYPE_BLANK_ERROR);
402 }
403 }
404 return phoneFlag;
405 }
406
407 /**
408 * Persist email Address of the patron and set the values from the patron email address to entity email bo object
409 * @param olePatron
410 * @param olePatronDocument
411 * @return boolean flag ,to check whether the patron email type is valid .
412 */
413 private boolean persistPatronEmailAddress(OlePatron olePatron, OlePatronDocument olePatronDocument) {
414 boolean emailFlag = false;
415 List<EntityEmailBo> email = new ArrayList<EntityEmailBo>();
416 EntityEmailBo entityEmailBo;
417 List<OlePatronEmailAddress> olePatronEmailAddresses = olePatron.getEmailAddresses();
418 for (Iterator<OlePatronEmailAddress> iterator = olePatronEmailAddresses.iterator(); iterator.hasNext(); ) {
419 OlePatronEmailAddress emailAddresses = iterator.next();
420 entityEmailBo = new EntityEmailBo();
421 entityEmailBo.setEmailAddress(emailAddresses.getEmailAddress());
422 if(!emailAddresses.getEmailAddressType().equals("")){
423 Map criteria = new HashMap<String, String>();
424 criteria.put(OLEConstants.CODE, emailAddresses.getEmailAddressType());
425 List<EntityEmailTypeBo> entityType = (List<EntityEmailTypeBo>) getBusinessObjectService().findMatching(EntityEmailTypeBo.class, criteria);
426 if (entityType.size()>0) {
427 entityEmailBo.setEmailType(entityType.get(0));
428 entityEmailBo.setEmailTypeCode(entityType.get(0).getCode());
429 entityEmailBo.setActive(emailAddresses.isActive());
430 entityEmailBo.setDefaultValue(emailAddresses.isDefaults());
431 email.add(entityEmailBo);
432 boolean defaultValue = checkEmailMultipleDefault(email);
433 if(defaultValue){
434 olePatronDocument.setEmails(email);
435 emailFlag = true;
436 } else {
437 olePatron.setErrorMessage(OLEPatronConstant.EMAIL_DEFAULT_VALUE_ERROR);
438 emailFlag = false;
439 }
440 } else{
441 olePatron.setErrorMessage(OLEPatronConstant.EMAILTYPE_ERROR);
442 }
443 } else{
444 olePatron.setErrorMessage(OLEPatronConstant.EMAILTYPE_BLANK_ERROR);
445 }
446 }
447 return emailFlag;
448 }
449
450 /**
451 * Persist postal address of the patron and set the values from the patron postal address to entity address bo object
452 * @param olePatron
453 * @param olePatronDocument
454 * @return boolean flag , to check whether the patron address type is valid .
455 */
456 private boolean persistPatronPostalAddress(OlePatron olePatron, OlePatronDocument olePatronDocument) {
457 boolean addressFlag = false;
458 List<EntityAddressBo> address = new ArrayList<EntityAddressBo>();
459 EntityAddressBo entityAddressBo;
460 List<OlePatronPostalAddress> olePatronPostalAddresses = olePatron.getPostalAddresses();
461 for (Iterator<OlePatronPostalAddress> iterator = olePatronPostalAddresses.iterator(); iterator.hasNext(); ) {
462 OlePatronPostalAddress postalAddresses = iterator.next();
463 entityAddressBo = new EntityAddressBo();
464 List<OleAddressLine> addressLineList = postalAddresses.getAddressLinesList();
465 if(addressLineList.size()>0){
466 for(int i=0;i<addressLineList.size();i++){
467 if(i==0){
468 entityAddressBo.setLine1(addressLineList.get(i).getAddressLine());
469 }
470 if(i==1){
471 entityAddressBo.setLine2(addressLineList.get(i).getAddressLine());
472 }
473 if(i==2){
474 entityAddressBo.setLine3(addressLineList.get(i).getAddressLine());
475 }
476 }
477 }
478 Map criteria = new HashMap<String, String>();
479 Map criteriaState = new HashMap<String, String>();
480 Map criteriaCountry = new HashMap<String, String>();
481 if(postalAddresses.getPostalAddressType() != null && !postalAddresses.getPostalAddressType().equals("")){
482 criteria.put(OLEConstants.CODE, postalAddresses.getPostalAddressType());
483 List<EntityAddressTypeBo> entityType = (List<EntityAddressTypeBo>) getBusinessObjectService().findMatching(EntityAddressTypeBo.class, criteria);
484 if (entityType.size()>0) {
485 entityAddressBo.setAddressType(entityType.get(0));
486 entityAddressBo.setAddressTypeCode(entityType.get(0).getCode());
487 } else{
488 olePatron.setErrorMessage(OLEPatronConstant.ADDRESSTYPE_ERROR);
489 return false;
490 }
491 } else{
492 olePatron.setErrorMessage(OLEPatronConstant.ADDRESS_TYPE_BLANK_ERROR);
493 return false;
494 }
495 if(postalAddresses.getStateProvince() != null && !postalAddresses.getStateProvince().equals("")){
496 criteriaState.put(OLEConstants.CODE, postalAddresses.getStateProvince());
497 List<StateBo> stateBoList = (List<StateBo>) getBusinessObjectService().findMatching(StateBo.class, criteriaState);
498 if (stateBoList.size()>0) {
499 entityAddressBo.setStateProvinceCode(postalAddresses.getStateProvince());
500 } else {
501 olePatron.setErrorMessage(OLEPatronConstant.STATE_ERROR);
502 return false;
503 }
504 }
505 if(postalAddresses.getCountry() != null && !postalAddresses.getCountry().equals("")){
506 criteriaCountry.put(OLEConstants.CODE, postalAddresses.getCountry());
507 List<CountryBo> countryList = (List<CountryBo>) getBusinessObjectService().findMatching(CountryBo.class, criteriaCountry);
508 if (countryList.size()>0) {
509 entityAddressBo.setCountryCode(postalAddresses.getCountry());
510 } else {
511 olePatron.setErrorMessage(OLEPatronConstant.COUNTRY_ADDRESS_ERROR);
512 return false;
513 }
514 }
515 entityAddressBo.setCity(postalAddresses.getCity());
516 entityAddressBo.setPostalCode(postalAddresses.getPostalCode());
517 entityAddressBo.setActive(postalAddresses.isActive());
518 entityAddressBo.setDefaultValue(postalAddresses.isDefaults());
519 address.add(entityAddressBo);
520 boolean defaultValue = checkAddressMultipleDefault(address);
521 if(defaultValue){
522 olePatronDocument.setAddresses(address);
523 addressFlag = true;
524 } else {
525 olePatron.setErrorMessage(OLEPatronConstant.ADDRESS_DEFAULT_VALUE_ERROR);
526 addressFlag = false;
527 }
528 }
529 return addressFlag;
530 }
531
532 /**
533 * Persist names of the patron and set the values from the patron names to entity name bo object
534 * @param olePatron
535 * @param olePatronDocument
536 * @return boolean flag , to check whether the patron name type is valid .
537 */
538 private boolean persistPatronNames(OlePatron olePatron, OlePatronDocument olePatronDocument) {
539 boolean nameFlag = false;
540 EntityNameBo names = new EntityNameBo();
541 Map criteria = new HashMap<String, String>();
542 criteria.put(OLEConstants.NAME, OLEConstants.PREFERRED);
543 List<EntityNameTypeBo> entityType = (List<EntityNameTypeBo>) getBusinessObjectService().findMatching(EntityNameTypeBo.class, criteria);
544 if (entityType.size()>0 ) {
545 names.setNameType(entityType.get(0));
546 names.setNameCode(entityType.get(0).getCode());
547 if (!olePatron.getName().getFirst().equals("") && !olePatron.getName().getSurname().equals("")) {
548 names.setFirstName(olePatron.getName().getFirst());
549 names.setLastName(olePatron.getName().getSurname());
550 if(olePatron.getName().getTitle()!= null && !olePatron.getName().getTitle().equals("")){
551 names.setNamePrefix(olePatron.getName().getTitle());
552 }
553 if(olePatron.getName().getSuffix()!= null && !olePatron.getName().getSuffix().equals("")){
554 names.setNameSuffix(olePatron.getName().getSuffix());
555 }
556 names.setActive(true);
557 names.setDefaultValue(true);
558 olePatronDocument.setName(names);
559 nameFlag = true;
560 }else{
561 if(olePatron.getName().getFirst().equals("")){
562 olePatron.setErrorMessage(OLEPatronConstant.FIRSTNAME_BLANK_ERROR);
563 }
564 if(olePatron.getName().getSurname().equals("")){
565 olePatron.setErrorMessage(OLEPatronConstant.SURNAME_BLANK_ERROR);
566 }
567 }
568 }
569 return nameFlag;
570 }
571
572 /**
573 * Persist notes of the patron and set the values from the patron notes ( converted object from xml) to Patron notes bo object
574 * @param olePatron
575 * @param olePatronDocument
576 * @return boolean flag , to check whether the patron note type is valid .
577 */
578 private boolean persistPatronNotes(OlePatron olePatron, OlePatronDocument olePatronDocument) {
579 boolean notesFlag = false;
580 List<OlePatronNotes> olePatronNotesList = new ArrayList<OlePatronNotes>();
581 OlePatronNotes olePatronNotes;
582 List<OlePatronNote> olePatronNoteList = olePatron.getNotes();
583 for (Iterator<OlePatronNote> iterator = olePatronNoteList.iterator(); iterator.hasNext(); ) {
584 OlePatronNote olePatronNote = iterator.next();
585 olePatronNotes = new OlePatronNotes();
586 if (olePatronNote.getNoteType() != null && !"".equals(olePatronNote.getNoteType())) {
587 Map criteria = new HashMap<String, String>();
588 criteria.put(OLEConstants.PATRON_NOTE_TYPE_CODE, olePatronNote.getNoteType());
589 List<OlePatronNoteType> olePatronNoteTypes = (List<OlePatronNoteType>) getBusinessObjectService().findMatching(OlePatronNoteType.class, criteria);
590 if (olePatronNoteTypes.size()>0) {
591 olePatronNotes.setOlePatronNoteType(olePatronNoteTypes.get(0));
592 olePatronNotes.setPatronNoteTypeId(olePatronNoteTypes.get(0).getPatronNoteTypeId());
593 olePatronNotes.setPatronNoteText(olePatronNote.getNote());
594 olePatronNotes.setActive(olePatronNote.isActive());
595 olePatronNotesList.add(olePatronNotes);
596 olePatronDocument.setNotes(olePatronNotesList);
597 notesFlag = true;
598 } else {
599 olePatron.setErrorMessage(OLEPatronConstant.NOTETYPE_ERROR);
600 }
601
602 } else{
603 olePatron.setErrorMessage(OLEPatronConstant.NOTETYPE_BLANK_ERROR);
604 }
605 }
606 if (olePatronNoteList.isEmpty()) {
607 notesFlag = true;
608 }
609 return notesFlag;
610 }
611
612 /**
613 * This method is for persisting the borrower Type of the patron
614 * @param olePatron
615 * @param olePatronDocument
616 * @return boolean flag, to check whether the borrower Type is valid.
617 */
618 private boolean persistPatronBorrowerType(OlePatron olePatron, OlePatronDocument olePatronDocument) {
619 boolean borrowerTypeFlag = false;
620 String borroweTypeId;
621 String borrowerTypeName;
622 borrowerTypeName = olePatron.getBorrowerType();
623 if(borrowerTypeName!=null && !borrowerTypeName.equals("")){
624 HashMap<String, String> map = new HashMap<String, String>();
625 map.put(OLEConstants.BORROWER_TYPE_CODE, borrowerTypeName);
626 List<OleBorrowerType> borrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, map);
627 if (borrowerTypes.size() > 0) {
628 borroweTypeId = borrowerTypes.get(0).getBorrowerTypeId();
629 olePatronDocument.setBorrowerType(borroweTypeId);
630 olePatronDocument.setOleBorrowerType(borrowerTypes.get(0));
631 borrowerTypeFlag = true;
632 }else{
633 olePatron.setErrorMessage(OLEPatronConstant.BORROWERTYPE_ERROR);
634 }
635 }else{
636 olePatron.setErrorMessage(OLEPatronConstant.BORROWERTYPE_BLANK_ERROR);
637 }
638 return borrowerTypeFlag;
639 }
640
641 /**
642 * This method is for saving the failed records in a separate location of home directory.
643 * @param patronXML
644 * @param patronReportId
645 */
646 private void saveFailureRecordsForAttachment(String patronXML,String patronReportId) {
647 OlePatronIngestSummaryRecord olePatronIngestSummaryRecord ;
648 try {
649 HashMap<String, String> map = new HashMap<String, String>();
650 String directory = PropertyUtil.getPropertyUtil().getProperty(OLEConstants.STAGING_DIRECTORY)+
651 OLEConstants.PATRON_FILE_DIRECTORY;
652 String homeDirectory = System.getProperty(OLEConstants.USER_HOME_DIRECTORY);
653 int reportId = Integer.parseInt(patronReportId);
654 File file = new File(homeDirectory+directory);
655 if (file.isDirectory()) {
656 file = new File(homeDirectory+directory+reportId+OLEConstants.FAILED_PATRON_RECORD_NAME);
657 file.createNewFile();
658 FileUtils.writeStringToFile(file, patronXML);
659 } else {
660 file.mkdirs();
661 if (file.isDirectory()) {
662 File newFile = new File(file,reportId+OLEConstants.FAILED_PATRON_RECORD_NAME);
663 newFile.createNewFile();
664 FileUtils.writeStringToFile(newFile,patronXML);
665 }
666 }
667 } catch (IOException e) {
668 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
669 }
670 }
671
672 /**
673 * This method is for checking the entity phone has multiple defaults
674 * @param phoneBoList
675 * @return true , if the entity phone has only one default in a single patron record, else false
676 */
677 protected boolean checkPhoneMultipleDefault (List<EntityPhoneBo> phoneBoList) {
678
679 boolean valid = true;
680 int defaultCounter = 0;
681 for(EntityPhoneBo entityPhoneBo:phoneBoList){
682 if(entityPhoneBo.isDefaultValue()){
683 defaultCounter++;
684 } else {
685 defaultCounter--;
686 }
687 }
688 if(defaultCounter > 1 || defaultCounter < 0){
689 valid = false;
690 }
691 return valid;
692 }
693
694 /**
695 * This method is for checking the entity address has multiple defaults
696 * @param addrBoList
697 * @return true , if the entity address has only one default in a single patron record, else false
698 */
699 protected boolean checkAddressMultipleDefault (List<EntityAddressBo> addrBoList) {
700
701 boolean valid = true;
702 int defaultCounter = 0;
703 for(EntityAddressBo entityAddressBo:addrBoList){
704 if(entityAddressBo.isDefaultValue()){
705 defaultCounter++;
706 } else {
707 defaultCounter--;
708 }
709 }
710 if(defaultCounter > 1 || defaultCounter < 0){
711 valid = false;
712 }
713 return valid;
714 }
715
716 /**
717 * This method is for checking the entity email address has multiple defaults
718 * @param emailBoList
719 * @return true , if the entity email address has only one default in a single patron record, else false
720 */
721 protected boolean checkEmailMultipleDefault (List<EntityEmailBo> emailBoList) {
722
723 boolean valid = true;
724 int defaultCounter = 0;
725 for(EntityEmailBo entityEmailBo:emailBoList){
726 if(entityEmailBo.isDefaultValue()){
727 defaultCounter++;
728 }else {
729 defaultCounter--;
730 }
731 }
732 if(defaultCounter > 1 || defaultCounter < 0){
733 valid = false;
734 }
735 return valid;
736 }
737
738 /**
739 * Gets the instance of BusinessObjectService
740 * @return businessObjectService(BusinessObjectService)
741 */
742 private BusinessObjectService getBusinessObjectService() {
743 if (null == businessObjectService) {
744 businessObjectService = KRADServiceLocator.getBusinessObjectService();
745 }
746 return businessObjectService;
747 }
748
749 /**
750 * Gets the instance of OlePatronRecordHandler
751 * @return olePatronRecordHandler(OlePatronRecordHandler)
752 */
753 public OlePatronRecordHandler getOlePatronRecordHandler() {
754 if (null == olePatronRecordHandler) {
755 olePatronRecordHandler = new OlePatronRecordHandler();
756 }
757 return olePatronRecordHandler;
758 }
759
760 /**
761 * Sets the olePatronRecordHandler which is of type OlePatronRecordHandler
762 * @param olePatronRecordHandler(OlePatronRecordHandler)
763 */
764 public void setOlePatronRecordHandler(OlePatronRecordHandler olePatronRecordHandler) {
765 this.olePatronRecordHandler = olePatronRecordHandler;
766 }
767 }