View Javadoc
1   package org.kuali.ole.loaders.deliver.service.impl;
2   
3   import com.sun.jersey.api.core.HttpContext;
4   import org.apache.commons.collections.CollectionUtils;
5   import org.apache.commons.lang.StringUtils;
6   import org.apache.log4j.Logger;
7   import org.codehaus.jettison.json.JSONArray;
8   import org.codehaus.jettison.json.JSONException;
9   import org.codehaus.jettison.json.JSONObject;
10  import org.kuali.ole.OLEConstants;
11  import org.kuali.ole.deliver.bo.*;
12  import org.kuali.ole.deliver.constant.OLEPatronConstant;
13  import org.kuali.ole.deliver.service.OLEDeliverService;
14  import org.kuali.ole.ingest.pojo.*;
15  import org.kuali.ole.loaders.common.bo.OLELoaderResponseBo;
16  import org.kuali.ole.loaders.common.constants.OLELoaderConstants;
17  import org.kuali.ole.loaders.common.service.OLELoaderRestClient;
18  import org.kuali.ole.loaders.common.service.OLELoaderService;
19  import org.kuali.ole.loaders.common.service.impl.OLELoaderServiceImpl;
20  import org.kuali.ole.loaders.deliver.service.OLEPatronLoaderHelperService;
21  import org.kuali.rice.core.api.util.type.KualiDecimal;
22  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
23  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
24  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
25  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
26  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
27  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  
31  import java.util.*;
32  
33  /**
34   * Created by sheiksalahudeenm on 27/3/15.
35   */
36  public class OLEPatronLoaderHelperServiceImpl implements OLEPatronLoaderHelperService {
37  
38      private static final Logger LOG = Logger.getLogger(OLEPatronLoaderHelperServiceImpl.class);
39      private BusinessObjectService businessObjectService;
40      private OLELoaderService oleLoaderService;
41  
42  
43      public BusinessObjectService getBusinessObjectService() {
44          if(businessObjectService == null){
45              businessObjectService = KRADServiceLocator.getBusinessObjectService();
46          }
47          return businessObjectService;
48      }
49  
50      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
51          this.businessObjectService = businessObjectService;
52      }
53  
54      public OLELoaderService getOleLoaderService() {
55          if(oleLoaderService == null){
56              oleLoaderService = new OLELoaderServiceImpl();
57          }
58          return oleLoaderService;
59      }
60  
61      public void setOleLoaderService(OLELoaderService oleLoaderService) {
62          this.oleLoaderService = oleLoaderService;
63      }
64  
65      @Override
66      public OlePatronDocument getPatronById(String patronId) {
67          Map patronMap = new HashMap();
68          patronMap.put("olePatronId", patronId);
69          return getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
70      }
71  
72      @Override
73      public OlePatronDocument getPatronByCode(String patronCode) {
74          Map patronMap = new HashMap();
75          patronMap.put("patronCode", patronCode);
76          return getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, patronMap);
77      }
78  
79      @Override
80      public List<OlePatronDocument> getAllPatrons() {
81          return (List<OlePatronDocument>) getBusinessObjectService().findAll(OlePatronDocument.class);
82      }
83  
84      @Override
85      public Object formPatronExportResponse(Object object, String patronContext, String uri, boolean addContext) {
86          OlePatronDocument olePatron = (OlePatronDocument) object;
87          JSONObject jsonObject = new JSONObject();
88          try {
89              if(addContext)
90                  jsonObject.put("@context",patronContext);
91              jsonObject.put("@id", OLELoaderConstants.PATRON_URI + OLELoaderConstants.SLASH + olePatron.getOlePatronId());
92              jsonObject.put("borrowerType",olePatron.getBorrowerTypeCode());
93              jsonObject.put("barcode",olePatron.getBarcode());
94              jsonObject.put("activationDate",olePatron.getActivationDate());
95              jsonObject.put("expirationDate",olePatron.getExpirationDate());
96              jsonObject.put("active",olePatron.isActiveIndicator());
97              jsonObject.put("statisticalCategory",olePatron.getStatisticalCategory());
98              JSONObject patronLevelPolicyJsonObject = new JSONObject();
99              patronLevelPolicyJsonObject.put("receivesCourtesyNotice",olePatron.isCourtesyNotice());
100             patronLevelPolicyJsonObject.put("deliveryPrivilege",olePatron.isDeliveryPrivilege());
101             patronLevelPolicyJsonObject.put("generalBlock",olePatron.isGeneralBlock());
102             patronLevelPolicyJsonObject.put("blockNotes",olePatron.getGeneralBlockNotes());
103             patronLevelPolicyJsonObject.put("pagingPrivilege",olePatron.isPagingPrivilege());
104             jsonObject.put("patronLevelPolicies",patronLevelPolicyJsonObject);
105             jsonObject = formPatronName(jsonObject,olePatron);
106             jsonObject = populateOlePatronPhone(jsonObject, olePatron);
107             jsonObject = populateOlePatronEmail(jsonObject, olePatron);
108             jsonObject = populateOlePatronAddress(jsonObject, olePatron);
109             jsonObject = populateOlePatronAffiliations(jsonObject, olePatron);
110             jsonObject = populateOlePatronNotes(jsonObject, olePatron);
111         } catch (JSONException e) {
112             e.printStackTrace();
113         }
114         return jsonObject;
115     }
116 
117     @Override
118     public Object formAllPatronExportResponse(HttpContext context, List<OlePatronDocument> olePatronList, String patronContext, String uri) {
119         JSONObject jsonResponseObject = new JSONObject();
120         JSONArray paginationArray = new JSONArray();
121         JSONObject paginationObject = new JSONObject();
122         JSONArray jsonArray = new JSONArray();
123         try{
124             jsonResponseObject.put("@context",patronContext);
125             int startIndex = 0;
126             int maxResults = 50;
127             boolean validStartIndex = true;
128             if(context.getRequest().getQueryParameters().containsKey("start")){
129                 try{
130                     String start = context.getRequest().getQueryParameters().getFirst("start");
131                     startIndex = Integer.parseInt(start);
132                     if(startIndex < 0)
133                         startIndex =0;
134                     if(startIndex > olePatronList.size()){
135                         validStartIndex = false;
136                     }
137                 }catch (Exception e){
138                     LOG.info("Invalid Start Index : " + e.getMessage());
139                     startIndex = 0;
140                 }
141             }
142             if(context.getRequest().getQueryParameters().containsKey("maxResults")){
143                 try{
144                     String maxCount = context.getRequest().getQueryParameters().getFirst("maxResults");
145                     maxResults = Integer.parseInt(maxCount);
146                     if(maxResults < 0)
147                         maxResults =50;
148                 }catch (Exception e){
149                     LOG.info("Invalid Max Result count : " + e.getMessage());
150                     maxResults = 50;
151                 }
152             }
153             int loopIterationEnd = 0;
154             if(startIndex+maxResults > olePatronList.size())
155                 loopIterationEnd = olePatronList.size();
156             else{
157                 loopIterationEnd = startIndex+maxResults;
158             }
159 
160             if(validStartIndex){
161                 if(startIndex != 0){
162                     paginationObject.put("rel","prev");
163                     paginationObject.put("href",OLELoaderConstants.PATRON_URI + "?start="+((startIndex-1)-maxResults < 0 ? 0 : (startIndex-1)-maxResults)+"&maxResults="+maxResults);
164                     paginationArray.put(paginationObject);
165                 }
166                 if(loopIterationEnd != olePatronList.size()){
167                     paginationObject = new JSONObject();
168                     paginationObject.put("rel","next");
169                     paginationObject.put("href",OLELoaderConstants.PATRON_URI + "?start="+(loopIterationEnd+1)+"&maxResults="+maxResults);
170                     paginationArray.put(paginationObject);
171                 }
172 
173                 jsonResponseObject.put("links",paginationArray);
174                 for(int index = (startIndex == 0 ? 0 : startIndex-1) ; index < loopIterationEnd-1 ; index++){
175                     OlePatronDocument olePatron = olePatronList.get(index);
176                     JSONObject jsonObject = new JSONObject();
177                     try {
178                         jsonObject.put("@id", OLELoaderConstants.PATRON_URI + OLELoaderConstants.SLASH + olePatron.getOlePatronId());
179                         jsonObject.put("borrowerType",olePatron.getBorrowerTypeCode());
180                         jsonObject.put("barcode",olePatron.getBarcode());
181                         jsonObject.put("activationDate",olePatron.getActivationDate());
182                         jsonObject.put("expirationDate",olePatron.getExpirationDate());
183                         jsonObject.put("active",olePatron.isActiveIndicator());
184                         jsonObject.put("statisticalCategory",olePatron.getStatisticalCategory());
185                         JSONObject patronLevelPolicyJsonObject = new JSONObject();
186                         patronLevelPolicyJsonObject.put("receivesCourtesyNotice",olePatron.isCourtesyNotice());
187                         patronLevelPolicyJsonObject.put("deliveryPrivilege",olePatron.isDeliveryPrivilege());
188                         patronLevelPolicyJsonObject.put("generalBlock",olePatron.isGeneralBlock());
189                         patronLevelPolicyJsonObject.put("blockNotes",olePatron.getGeneralBlockNotes());
190                         patronLevelPolicyJsonObject.put("pagingPrivilege",olePatron.isPagingPrivilege());
191                         jsonObject.put("patronLevelPolicies",patronLevelPolicyJsonObject);
192                         jsonObject = formPatronName(jsonObject,olePatron);
193                         jsonObject = populateOlePatronPhone(jsonObject, olePatron);
194                         jsonObject = populateOlePatronEmail(jsonObject, olePatron);
195                         jsonObject = populateOlePatronAddress(jsonObject, olePatron);
196                         jsonObject = populateOlePatronAffiliations(jsonObject, olePatron);
197                         jsonObject = populateOlePatronNotes(jsonObject, olePatron);
198                     } catch (JSONException e) {
199                         e.printStackTrace();
200                     }
201                     jsonArray.put(jsonObject);
202                 }
203                 jsonResponseObject.put("items",jsonArray);
204             }
205         }catch (Exception e){
206             e.printStackTrace();
207         }
208         return jsonResponseObject;
209     }
210 
211    /* @Override
212     public OLELoaderResponseBo updateOlePatronDocument(OlePatronDocument olePatron, OLEPatronBo olePatronBo, HttpContext context) {
213         if(StringUtils.isNotBlank(olePatronBo.getPatronName()))
214             olePatron.setPatronName(olePatronBo.getPatronName());
215         if(StringUtils.isNotBlank(olePatronBo.getPatronDescription()))
216             olePatron.setPatronDescription(olePatronBo.getPatronDescription());
217         olePatron.setActive(olePatronBo.isActive());
218         try{
219             olePatron = KRADServiceLocator.getBusinessObjectService().save(olePatron);
220             String details = formOlePatronDocumentExportResponse(olePatron, OLELoaderConstants.OLELoaderContext.PATRON,
221                     context.getRequest().getRequestUri().toASCIIString(), true).toString();
222             return getOleLoaderService().generateResponse(
223                     OLELoaderConstants.OLEloaderMessage.PATRON_SUCCESS,
224                     OLELoaderConstants.OLEloaderStatus.PATRON_SUCCESS,details);
225         }catch(Exception e){
226             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.PATRON_FAILED, OLELoaderConstants.OLEloaderStatus.PATRON_FAILED);
227         }
228 
229     }*/
230 
231     private JSONObject formPatronName(JSONObject patronJsonObject,OlePatronDocument olePatronDocument){
232         JSONObject nameJsonObject = new JSONObject();
233         try{
234             olePatronDocument = OLEDeliverService.populatePatronName(olePatronDocument);
235             nameJsonObject.put("firstName", olePatronDocument.getFirstName());
236             nameJsonObject.put("surName", olePatronDocument.getLastName());
237             nameJsonObject.put("suffix", olePatronDocument.getNameSuffix());
238             nameJsonObject.put("prefix", olePatronDocument.getNamePrefix());
239             patronJsonObject.put("name",nameJsonObject);
240         }catch (Exception e){
241             LOG.error("Error while process name object");
242         }
243         return patronJsonObject;
244     }
245 
246     private JSONObject populateOlePatronPhone(JSONObject patronJsonObject, OlePatronDocument olePatronDocument) {
247         EntityBo entity = olePatronDocument.getEntity();
248         JSONArray phoneJsonArray = new JSONArray();
249         try{
250             if (!entity.getEntityTypeContactInfos().isEmpty()) {
251                 List<EntityPhoneBo> entityPhoneBoList = entity.getEntityTypeContactInfos().get(0).getPhoneNumbers();
252                 for(EntityPhoneBo entityPhoneBo : entityPhoneBoList){
253                     JSONObject phoneJsonObject = new JSONObject();
254                     phoneJsonObject.put("phoneType",entityPhoneBo.getPhoneTypeCode());
255                     phoneJsonObject.put("phoneNumber", entityPhoneBo.getPhoneNumber());
256                     phoneJsonObject.put("country", entityPhoneBo.getCountryCode());
257                     phoneJsonObject.put("default", entityPhoneBo.isDefaultValue());
258                     phoneJsonObject.put("extensionNumber", entityPhoneBo.getExtensionNumber());
259                     phoneJsonObject.put("active", entityPhoneBo.isActive());
260                     phoneJsonArray.put(phoneJsonObject);
261                 }
262             }
263             patronJsonObject.put("phone",phoneJsonArray);
264         }catch(Exception e){
265             LOG.error("Error while process phone object");
266         }
267 
268         return patronJsonObject;
269     }
270 
271     private JSONObject populateOlePatronEmail(JSONObject patronJsonObject, OlePatronDocument olePatronDocument) {
272         EntityBo entity = olePatronDocument.getEntity();
273         JSONArray emailJsonArray = new JSONArray();
274         try{
275             if (!entity.getEntityTypeContactInfos().isEmpty()) {
276                 List<EntityEmailBo> entityEmailList = entity.getEntityTypeContactInfos().get(0).getEmailAddresses();
277                 for(EntityEmailBo entityEmailBo : entityEmailList){
278                     JSONObject emailJsonObject = new JSONObject();
279                     emailJsonObject.put("emailAddressType",entityEmailBo.getEmailTypeCode());
280                     emailJsonObject.put("emailAddress", entityEmailBo.getEmailAddress());
281                     emailJsonObject.put("active", entityEmailBo.isActive());
282                     emailJsonObject.put("default", entityEmailBo.isDefaultValue());
283                     emailJsonArray.put(emailJsonObject);
284                 }
285                 patronJsonObject.put("email",emailJsonArray);
286             }
287         }catch(Exception e){
288             LOG.error("Error while process email object");
289         }
290         return patronJsonObject;
291     }
292 
293     private JSONObject populateOlePatronAddress(JSONObject patronJsonObject, OlePatronDocument olePatronDocument) {
294         List<OleAddressBo> oleAddressBoList = olePatronDocument.getOleAddresses();
295         EntityBo entity = olePatronDocument.getEntity();
296         JSONArray addressJsonArray = new JSONArray();
297         try{
298             if (!entity.getEntityTypeContactInfos().isEmpty()) {
299                 List<EntityAddressBo> entityAddressList = entity.getEntityTypeContactInfos().get(0).getAddresses();
300                 for (EntityAddressBo entityAdd : entityAddressList) {
301                     JSONObject addressJsonObject = new JSONObject();
302                     addressJsonObject.put("line1",entityAdd.getLine1());
303                     addressJsonObject.put("line2", entityAdd.getLine2());
304                     addressJsonObject.put("line3", entityAdd.getLine3());
305                     addressJsonObject.put("city", entityAdd.getCity());
306                     addressJsonObject.put("active", entityAdd.isActive());
307                     addressJsonObject.put("country", entityAdd.getCountryCode());
308                     addressJsonObject.put("default", entityAdd.isDefaultValue());
309                     addressJsonObject.put("postalCode", entityAdd.getPostalCode());
310                     addressJsonObject.put("addressTypeCode",entityAdd.getAddressTypeCode());
311                     addressJsonObject.put("stateProvinceCode", entityAdd.getStateProvinceCode());
312                     for(OleAddressBo oleAddress : oleAddressBoList){
313                         if(oleAddress.getId().equalsIgnoreCase(entityAdd.getId())) {
314                             addressJsonObject.put("validFrom", oleAddress.getAddressValidFrom());
315                             addressJsonObject.put("validTo", oleAddress.getAddressValidTo());
316                             addressJsonObject.put("addressVerified",oleAddress.isAddressVerified());
317                             String addressSourceId =oleAddress.getAddressSource();
318                             HashMap<String, String> map = new HashMap<String, String>();
319                             map.put("oleAddressSourceId", addressSourceId);
320                             List<OleAddressSourceBo> addressSourceList = (List<OleAddressSourceBo>) getBusinessObjectService().findMatching(OleAddressSourceBo.class, map);
321                             if (CollectionUtils.isNotEmpty(addressSourceList)) {
322                                 addressJsonObject.put("addressSource",addressSourceList.get(0).getOleAddressSourceCode());
323                             }
324                             break;
325                         }
326                     }
327                     addressJsonArray.put(addressJsonObject);
328                 }
329                 patronJsonObject.put("address",addressJsonArray);
330             }
331         }catch(Exception e){
332             LOG.error("Error while process address object");
333         }
334 
335         return patronJsonObject;
336     }
337 
338     private JSONObject populateOlePatronAffiliations(JSONObject patronJsonObject, OlePatronDocument olePatronDocument) {
339         EntityBo entity = olePatronDocument.getEntity();
340         JSONArray affiliationJsonArray = new JSONArray();
341         try{
342             if (CollectionUtils.isNotEmpty(entity.getEmploymentInformation())) {
343                 for(EntityEmploymentBo entityEmploymentBo : entity.getEmploymentInformation()){
344                     JSONObject affiliationJsonObject = new JSONObject();
345                     JSONObject employmentJsonObject = new JSONObject();
346                     employmentJsonObject.put("employeeTypeCode",entityEmploymentBo.getEmployeeTypeCode());
347                     employmentJsonObject.put("primary", entityEmploymentBo.isPrimary());
348                     employmentJsonObject.put("active", entityEmploymentBo.isActive());
349                     employmentJsonObject.put("salaryAmount", entityEmploymentBo.getBaseSalaryAmount());
350                     employmentJsonObject.put("employeeStatusCode", entityEmploymentBo.getEmployeeStatusCode());
351                     employmentJsonObject.put("employeeId", entityEmploymentBo.getEmployeeId());
352                     employmentJsonObject.put("primaryDepartmentCode", entityEmploymentBo.getPrimaryDepartmentCode());
353                     EntityAffiliationBo entityAffiliationBo = entityEmploymentBo.getEntityAffiliation();
354                     affiliationJsonObject.put("campusCode", entityAffiliationBo.getCampusCode());
355                     affiliationJsonObject.put("active", entityAffiliationBo.isActive());
356                     affiliationJsonObject.put("affiliationType", entityAffiliationBo.getAffiliationTypeCode());
357                     affiliationJsonObject.put("employment", employmentJsonObject);
358                     affiliationJsonArray.put(affiliationJsonObject);
359                 }
360                 patronJsonObject.put("affiliation",affiliationJsonArray);
361             }
362         }catch(Exception e){
363             LOG.error("Error while process affiliation object");
364         }
365         return patronJsonObject;
366     }
367 
368     private JSONObject populateOlePatronNotes(JSONObject patronJsonObject, OlePatronDocument olePatronDocument) {
369         JSONArray noteJsonArray = new JSONArray();
370         try{
371             if(CollectionUtils.isNotEmpty(olePatronDocument.getNotes())){
372                 for(OlePatronNotes olePatronNotes : olePatronDocument.getNotes()){
373                     JSONObject noteJsonObject = new JSONObject();
374                     noteJsonObject.put("noteType", olePatronNotes.getOlePatronNoteType().getPatronNoteTypeCode());
375                     noteJsonObject.put("note", olePatronNotes.getPatronNoteText());
376                     noteJsonObject.put("active", olePatronNotes.isActive());
377                     noteJsonArray.put(noteJsonObject);
378                 }
379             }
380             patronJsonObject.put("note",noteJsonArray);
381         }catch(Exception e){
382             LOG.error("Error while process note object");
383         }
384         return patronJsonObject;
385     }
386 
387     @Override
388     public List<OlePatron> formIngestOlePatrons(JSONArray patronJsonArray,Map<String,Integer> rejectedPatronBarcodeIndexMap, Map<String,Integer> selectedPatronBarcodeIndexMap) {
389         List<OlePatron> olePatrons = new ArrayList<>();
390         for (int index = 0; index < patronJsonArray.length(); index ++) {
391             JSONObject jsonObject = null;
392             OlePatron patron = new OlePatron();
393             try {
394                 String barcode = "";
395                 jsonObject = (JSONObject)patronJsonArray.get(index);
396                 if(jsonObject == null){
397                     rejectedPatronBarcodeIndexMap.put("InvalidPatron : " + index,index);
398                     continue;
399                 }
400                 if(jsonObject.has("barcode")){
401                     barcode = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"barcode");
402                     if(StringUtils.isNotBlank(barcode)){
403                         patron.setBarcode(barcode);
404                     }else{
405                         rejectedPatronBarcodeIndexMap.put("InvalidPatron : " + index,index);
406                         continue;
407                     }
408                 }else{
409                     rejectedPatronBarcodeIndexMap.put("InvalidPatron : " + index, index);
410                     continue;
411                 }
412                 if(jsonObject.has("@id")) {
413                     String patronIdUrl = getOleLoaderService().getStringValueFromJsonObject(jsonObject, "@id");
414                     if (StringUtils.isNotBlank(patronIdUrl)) {
415                             Map<String, Object> restResponseMap = OLELoaderRestClient.jerseryClientGet(patronIdUrl);
416                             if ((Integer) restResponseMap.get("status") != 200) {
417                                 String patronId = patronIdUrl.substring(patronIdUrl.indexOf("/api/patron/") + 12);
418                                 patron.setPatronID(patronId);
419                             } else{
420                                 rejectedPatronBarcodeIndexMap.put("InvalidPatron : " + index, index);
421                                 continue;
422                             }
423                         }
424                 }
425                 if(jsonObject.has("activationDate")){
426                     String activationDate = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"activationDate");
427                     if(StringUtils.isNotBlank(activationDate)){
428                         try{
429                             Date date = OLELoaderConstants.DATE_FORMAT.parse(activationDate);
430                             patron.setActivationDate(new java.sql.Date(date.getTime()));
431                         }catch(Exception e){
432                             e.printStackTrace();
433                             rejectedPatronBarcodeIndexMap.put("InvalidPatron : " + index, index);
434                             continue;
435                         }
436                     }
437                 }
438                 if(jsonObject.has("expirationDate")){
439                     String expirationDate = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"expirationDate");
440                     if(StringUtils.isNotBlank(expirationDate)){
441                         try{
442                             Date date = OLELoaderConstants.DATE_FORMAT.parse(expirationDate);
443                             patron.setExpirationDate(new java.sql.Date(date.getTime()));
444                         }catch(Exception e){
445                             e.printStackTrace();
446                             rejectedPatronBarcodeIndexMap.put("InvalidPatron : " + index, index);
447                             continue;
448                         }
449                     }
450                 }
451 
452                 if(jsonObject.has("active")){
453                     try{
454                         boolean active = Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(jsonObject, "active"));
455                         patron.setActive(active);
456                     }catch(Exception e){
457                         e.printStackTrace();
458                         rejectedPatronBarcodeIndexMap.put("InvalidPatron : " + index, index);
459                         continue;
460                     }
461 
462                 }
463 
464                 if(jsonObject.has("borrowerType")){
465                     String borrowerType = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"borrowerType");
466                     if(StringUtils.isNotBlank(borrowerType)){
467                         patron.setBorrowerType(borrowerType);
468                     }
469                 }
470 
471                 if(jsonObject.has("patronLevelPolicies")){
472                     JSONObject patronLevelPolicies = getOleLoaderService().getJsonObjectValueFromJsonObject(jsonObject, "patronLevelPolicies");
473                     OlePatronLevelPolicies olePatronLevelPolicies = new OlePatronLevelPolicies();
474                     if(patronLevelPolicies.has("receivesCourtesyNotice")){
475                         olePatronLevelPolicies.setReceivesCourtesyNotice(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(patronLevelPolicies, "receivesCourtesyNotice")));
476                     }
477                     if(patronLevelPolicies.has("deliveryPrivilege")){
478                         olePatronLevelPolicies.setHasDeliveryPrivilege(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(patronLevelPolicies, "deliveryPrivilege")));
479                     }
480                     if(patronLevelPolicies.has("generalBlock")){
481                         olePatronLevelPolicies.setGenerallyBlocked(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(patronLevelPolicies, "generalBlock")));
482                     }
483                     if(patronLevelPolicies.has("pagingPrivilege")){
484                         olePatronLevelPolicies.setHasPagingPrivilege(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(patronLevelPolicies, "pagingPrivilege")));
485                     }
486                     if(patronLevelPolicies.has("generalBlockNotes")){
487                         olePatronLevelPolicies.setGeneralBlockNotes(getOleLoaderService().getStringValueFromJsonObject(patronLevelPolicies, "generalBlockNotes"));
488                     }
489                     patron.setPatronLevelPolicies(olePatronLevelPolicies);
490                 }
491                 if(jsonObject.has("name")){
492                     JSONObject nameJsonObject = getOleLoaderService().getJsonObjectValueFromJsonObject(jsonObject, "name");
493                     OleNameTypes oleNameTypes = new OleNameTypes();
494                     if(nameJsonObject.has("firstName")){
495                         oleNameTypes.setFirst(getOleLoaderService().getStringValueFromJsonObject(nameJsonObject, "firstName"));
496                     }
497                     if(nameJsonObject.has("surName")){
498                         oleNameTypes.setSurname(getOleLoaderService().getStringValueFromJsonObject(nameJsonObject, "surName"));
499                     }
500                     if(nameJsonObject.has("prefix")){
501                         oleNameTypes.setTitle(getOleLoaderService().getStringValueFromJsonObject(nameJsonObject, "prefix"));
502                     }
503                     if(nameJsonObject.has("suffix")){
504                         oleNameTypes.setSuffix(getOleLoaderService().getStringValueFromJsonObject(nameJsonObject, "suffix"));
505                     }
506                     patron.setName(oleNameTypes);
507                 }
508 
509                 if(jsonObject.has("phone")){
510                     JSONArray phoneJsonArray = getOleLoaderService().getJsonArrayValueFromJsonObject(jsonObject,"phone");
511                     patron = populateIngestOlePatronPhone(phoneJsonArray, patron);
512                 }
513 
514                 if(jsonObject.has("email")){
515                     JSONArray emailJsonArray = getOleLoaderService().getJsonArrayValueFromJsonObject(jsonObject,"email");
516                     patron = populateIngestOlePatronEmail(emailJsonArray, patron);
517                 }
518 
519                 if(jsonObject.has("address")){
520                     JSONArray addressJsonArray = getOleLoaderService().getJsonArrayValueFromJsonObject(jsonObject,"address");
521                     patron = populateIngestOlePatronAddress(addressJsonArray, patron , olePatrons);
522                 }
523 
524                 if(jsonObject.has("affiliation")){
525                     JSONArray affiliationJsonArray = getOleLoaderService().getJsonArrayValueFromJsonObject(jsonObject,"affiliation");
526                     patron = populateIngestOlePatronAffiliations(affiliationJsonArray, patron);
527                 }
528 
529                 if(jsonObject.has("note")){
530                     JSONArray noteJsonArray = getOleLoaderService().getJsonArrayValueFromJsonObject(jsonObject, "note");
531                     patron = populateIngestOlePatronNotes(noteJsonArray, patron);
532                 }
533                 selectedPatronBarcodeIndexMap.put(barcode, index);
534             } catch (JSONException e) {
535                 e.printStackTrace();
536             }
537             olePatrons.add(patron);
538         }
539         return olePatrons;
540     }
541 
542     public OlePatron populateIngestOlePatronEmail(JSONArray emailJsonArray, OlePatron olePatron) {
543         List<OlePatronEmailAddress> olePatronEmailAddressList = new ArrayList<>();
544         for (int index = 0; index < emailJsonArray.length(); index++) {
545             JSONObject emailJsonObject = null;
546             OlePatronEmailAddress olePatronEmailAddress = new OlePatronEmailAddress();
547             try {
548                 emailJsonObject = (JSONObject) emailJsonArray.get(index);
549                 if(emailJsonObject.has("emailAddressType")){
550                     olePatronEmailAddress.setEmailAddressType(getOleLoaderService().getStringValueFromJsonObject(emailJsonObject, "emailAddressType"));
551                 }
552                 if(emailJsonObject.has("emailAddress")){
553                     olePatronEmailAddress.setEmailAddress(getOleLoaderService().getStringValueFromJsonObject(emailJsonObject, "emailAddress"));
554                 }
555                 if(emailJsonObject.has("active")){
556                     olePatronEmailAddress.setActive(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(emailJsonObject, "active")));
557                 }
558                 if(emailJsonObject.has("default")){
559                     olePatronEmailAddress.setDefaults(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(emailJsonObject, "default")));
560                 }
561                 olePatronEmailAddressList.add(olePatronEmailAddress);
562             } catch (Exception e) {
563                 e.printStackTrace();
564             }
565         }
566             olePatron.setEmailAddresses(olePatronEmailAddressList);
567             return olePatron;
568 
569     }
570 
571     public OlePatron populateIngestOlePatronName(JSONArray emailJsonArray, OlePatron olePatron) {
572         /*OleNameTypes oleNameTypes = new OleNameTypes();
573         oleNameTypes.setFirst(olePatronBo.getName().getFirstName());
574         oleNameTypes.setSuffix(olePatronBo.getName().getSuffix());
575         oleNameTypes.setSurname(olePatronBo.getName().getSurname());
576         oleNameTypes.setTitle(olePatronBo.getName().getTitle());
577         olePatron.setName(oleNameTypes);*/
578         return olePatron;
579     }
580 
581     public OlePatron populateIngestOlePatronAddress(JSONArray addressJsonArray, OlePatron olePatron, List<OlePatron> olePatrons) {
582         List<OlePatronPostalAddress> olePatronPostalAddressList = new ArrayList<>();
583         for (int index = 0; index < addressJsonArray.length(); index++) {
584             JSONObject addressJsonObject = null;
585             OlePatronPostalAddress olePatronPostalAddress = new OlePatronPostalAddress();
586             try {
587                 addressJsonObject = (JSONObject) addressJsonArray.get(index);
588                 if(addressJsonObject.has("city")){
589                     olePatronPostalAddress.setCity(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "city"));
590                 }
591                 if(addressJsonObject.has("country")){
592                     olePatronPostalAddress.setCountry(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "country"));
593                 }
594                 if(addressJsonObject.has("postalCode")){
595                     olePatronPostalAddress.setPostalCode(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "postalCode"));
596                 }
597                 if(addressJsonObject.has("addressTypeCode")){
598                     olePatronPostalAddress.setPostalAddressType(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "addressTypeCode"));
599                 }
600                 if(addressJsonObject.has("stateProvinceCode")){
601                     olePatronPostalAddress.setStateProvince(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "stateProvinceCode"));
602                 }
603                 if(addressJsonObject.has("active")){
604                     olePatronPostalAddress.setActive(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "active")));
605                 }
606                 if(addressJsonObject.has("default")){
607                     olePatronPostalAddress.setDefaults(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "default")));
608                 }
609 
610                 if(addressJsonObject.has("addressSource")){
611                     olePatronPostalAddress.setAddressSource(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "addressSource"));
612                 }
613 
614                 List<OleAddressLine> oleAddressLineList = new ArrayList<>();
615                 for(int lineIndex = 1 ; lineIndex <= 3 ; lineIndex ++){
616                     OleAddressLine oleAddressLine = new OleAddressLine();
617                     if(addressJsonObject.has("line"+lineIndex)){
618                         oleAddressLine.setAddressLine(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "line" + lineIndex));
619                     }
620                     oleAddressLineList.add(oleAddressLine);
621                 }
622                 olePatronPostalAddress.setAddressLinesList(oleAddressLineList);
623 
624                 if(addressJsonObject.has("validFrom")){
625                     String validFrom = getOleLoaderService().getStringValueFromJsonObject(addressJsonObject,"validFrom");
626                     if(StringUtils.isNotBlank(validFrom )){
627                         try{
628                             Date date = OLELoaderConstants.DATE_FORMAT.parse(validFrom);
629                             olePatronPostalAddress.setAddressValidFrom(new java.sql.Date(date.getTime()));
630                         }catch(Exception e){
631                             e.printStackTrace();
632                             olePatron.setErrorMessage(OLELoaderConstants.OLEloaderMessage.INVALID_DATE);
633                             olePatrons.add(olePatron);
634                             break;
635                         }
636                     }
637                 }
638 
639                 if(addressJsonObject.has("validTo")){
640                     String validTo = getOleLoaderService().getStringValueFromJsonObject(addressJsonObject,"validTo");
641                     if(StringUtils.isNotBlank(validTo )){
642                         try{
643                             Date date = OLELoaderConstants.DATE_FORMAT.parse(validTo);
644                             olePatronPostalAddress.setAddressValidTo(new java.sql.Date(date.getTime()));
645                         }catch(Exception e){
646                             e.printStackTrace();
647                             olePatron.setErrorMessage(OLELoaderConstants.OLEloaderMessage.INVALID_DATE);
648                             olePatrons.add(olePatron);
649                             break;
650                         }
651                     }
652                 }
653                 if(addressJsonObject.has("addressVerified")){
654                     try{
655                         boolean addressVerified = Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(addressJsonObject, "addressVerified"));
656                         olePatronPostalAddress.setAddressVerified(addressVerified);
657                     }catch(Exception e){
658                         e.printStackTrace();
659                         olePatron.setErrorMessage(OLELoaderConstants.OLEloaderMessage.INVALID_BOOLEAN);
660                         olePatrons.add(olePatron);
661                         break;
662                     }
663 
664                 }
665                 olePatronPostalAddressList.add(olePatronPostalAddress);
666             } catch (Exception e) {
667                 e.printStackTrace();
668             }
669         }
670         olePatron.setPostalAddresses(olePatronPostalAddressList);
671         return olePatron;
672     }
673 
674     public OlePatron populateIngestOlePatronPhone(JSONArray phoneJsonArray, OlePatron olePatron) {
675         List<OlePatronTelePhoneNumber> telephoneNumbers = new ArrayList<>();
676         for (int index = 0; index < phoneJsonArray.length(); index++) {
677             JSONObject phoneJsonObject = null;
678             OlePatronTelePhoneNumber olePatronTelePhoneNumber = new OlePatronTelePhoneNumber();
679             try {
680                 phoneJsonObject = (JSONObject) phoneJsonArray.get(index);
681                 if(phoneJsonObject.has("phoneType")){
682                     olePatronTelePhoneNumber.setTelephoneNumberType(getOleLoaderService().getStringValueFromJsonObject(phoneJsonObject, "phoneType"));
683                 }
684                 if(phoneJsonObject.has("phoneNumber")){
685                     olePatronTelePhoneNumber.setTelephoneNumber(getOleLoaderService().getStringValueFromJsonObject(phoneJsonObject, "phoneNumber"));
686                 }
687                 if(phoneJsonObject.has("country")){
688                     olePatronTelePhoneNumber.setCountry(getOleLoaderService().getStringValueFromJsonObject(phoneJsonObject, "country"));
689                 }
690                 if(phoneJsonObject.has("extensionNumber")){
691                     olePatronTelePhoneNumber.setExtension(getOleLoaderService().getStringValueFromJsonObject(phoneJsonObject, "extensionNumber"));
692                 }
693                 if(phoneJsonObject.has("active")){
694                     olePatronTelePhoneNumber.setActive(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(phoneJsonObject, "active")));
695                 }
696                 if(phoneJsonObject.has("default")){
697                     olePatronTelePhoneNumber.setDefaults(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(phoneJsonObject, "default")));
698                 }
699                 telephoneNumbers.add(olePatronTelePhoneNumber);
700             } catch (Exception e) {
701                 e.printStackTrace();
702             }
703         }
704         olePatron.setTelephoneNumbers(telephoneNumbers);
705         return olePatron;
706     }
707 
708 
709     public OlePatron populateIngestOlePatronAffiliations(JSONArray affiliationJsonArray, OlePatron olePatron) {
710         List<OlePatronAffiliations> affiliations = new ArrayList<>();
711         for (int index = 0; index < affiliationJsonArray.length(); index++) {
712             JSONObject affiliationJsonObject = null;
713             OlePatronAffiliations olePatronAffiliations = new OlePatronAffiliations();
714             try {
715                 affiliationJsonObject = (JSONObject) affiliationJsonArray.get(index);
716                 if(affiliationJsonObject.has("employment")){
717                     JSONObject employmentJsonObject = getOleLoaderService().getJsonObjectValueFromJsonObject(affiliationJsonObject, "employment");
718                     List<OlePatronEmployments> olePatronEmploymentsList = new ArrayList<>();
719                     OlePatronEmployments olePatronEmployments = new OlePatronEmployments();
720                     if(employmentJsonObject.has("employeeTypeCode")) {
721                         olePatronEmployments.setEmployeeTypeCode(getOleLoaderService().getStringValueFromJsonObject(employmentJsonObject, "employeeTypeCode"));
722                     }
723                     if(employmentJsonObject.has("salaryAmount")){
724                         try{
725                             olePatronEmployments.setBaseSalaryAmount(new KualiDecimal(getOleLoaderService().getStringValueFromJsonObject(employmentJsonObject, "salaryAmount")));
726                         }catch (Exception e){
727                             olePatron.setErrorMessage(OLELoaderConstants.OLEloaderMessage.PATRON_INVALID_SALARY);
728                             break;
729                         }
730 
731                     }
732                     if (employmentJsonObject.has("employeeStatusCode")) {
733                         olePatronEmployments.setEmployeeStatusCode(getOleLoaderService().getStringValueFromJsonObject(employmentJsonObject, "employeeStatusCode"));
734                     }
735                     if (employmentJsonObject.has("employeeId")) {
736                         olePatronEmployments.setEmployeeId(getOleLoaderService().getStringValueFromJsonObject(employmentJsonObject, "employeeId"));
737                     }
738                     if (employmentJsonObject.has("active")) {
739                         olePatronEmployments.setActive(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(employmentJsonObject, "active")));
740                     }
741                     if (employmentJsonObject.has("primary")) {
742                         olePatronEmployments.setPrimary(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(employmentJsonObject, "primary")));
743                     }
744                     if (employmentJsonObject.has("primaryDepartmentCode")) {
745                         olePatronEmployments.setPrimaryDepartmentCode(getOleLoaderService().getStringValueFromJsonObject(employmentJsonObject, "primaryDepartmentCode"));
746                     }
747                     olePatronEmploymentsList.add(olePatronEmployments);
748                     olePatronAffiliations.setEmployments(olePatronEmploymentsList);
749                 }
750                 if(affiliationJsonObject.has("campusCode")){
751                     olePatronAffiliations.setCampusCode(getOleLoaderService().getStringValueFromJsonObject(affiliationJsonObject, "campusCode"));
752                 }
753                 if(affiliationJsonObject.has("affiliationType")){
754                     olePatronAffiliations.setAffiliationType(getOleLoaderService().getStringValueFromJsonObject(affiliationJsonObject, "affiliationType"));
755                 }
756                 if(affiliationJsonObject.has("active")){
757                     olePatronAffiliations.setActive(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(affiliationJsonObject, "active")));
758                 }
759                 affiliations.add(olePatronAffiliations);
760             } catch (Exception e) {
761                 e.printStackTrace();
762             }
763         }
764         olePatron.setAffiliations(affiliations);
765         return olePatron;
766     }
767 
768     public OlePatron populateIngestOlePatronNotes(JSONArray noteJsonArray, OlePatron olePatron) {
769         List<OlePatronNote> notes = new ArrayList<>();
770         for (int index = 0; index < noteJsonArray.length(); index++) {
771             JSONObject noteJsonObject = null;
772             OlePatronNote olePatronNote = new OlePatronNote();
773             try {
774                 noteJsonObject = (JSONObject) noteJsonArray.get(index);
775                 if(noteJsonObject.has("noteType")){
776                     olePatronNote.setNoteType(getOleLoaderService().getStringValueFromJsonObject(noteJsonObject, "noteType"));
777                 }
778                 if(noteJsonObject.has("note")){
779                     olePatronNote.setNote(getOleLoaderService().getStringValueFromJsonObject(noteJsonObject, "note"));
780                 }
781                 if(noteJsonObject.has("active")){
782                     olePatronNote.setActive(Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(noteJsonObject, "active")));
783                 }
784                 notes.add(olePatronNote);
785             } catch (Exception e) {
786                 e.printStackTrace();
787             }
788         }
789         olePatron.setNotes(notes);
790         return olePatron;
791     }
792 }