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