View Javadoc
1   package org.kuali.ole.select.gokb.service.impl;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.xerces.dom.DeferredElementImpl;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.select.gokb.*;
7   import org.kuali.ole.select.gokb.service.GokbLocalService;
8   import org.kuali.ole.select.gokb.service.GokbRdbmsService;
9   import org.kuali.ole.select.gokb.util.OleGokbXmlUtil;
10  import org.kuali.ole.sys.OLEPropertyConstants;
11  import org.kuali.rice.krad.service.BusinessObjectService;
12  import org.kuali.rice.krad.service.KRADServiceLocator;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  import org.w3c.dom.Attr;
16  import org.w3c.dom.NamedNodeMap;
17  import org.w3c.dom.Node;
18  import org.w3c.dom.NodeList;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  /**
26   * Created by rajeshbabuk on 12/18/14.
27   */
28  public class GokbLocalServiceImpl implements GokbLocalService {
29  
30      private static final Logger LOG = LoggerFactory.getLogger(GokbLocalServiceImpl.class);
31  
32      private GokbRdbmsService gokbRdbmsService = null;
33      private BusinessObjectService businessObjectService;
34  
35      public BusinessObjectService getBusinessObjectService() {
36          if(businessObjectService == null){
37              this.businessObjectService = KRADServiceLocator.getBusinessObjectService();
38          }
39          return businessObjectService;
40      }
41  
42      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
43          this.businessObjectService = businessObjectService;
44      }
45  
46      /**
47       * This method returns GokbRdbmsService.
48       *
49       * @return
50       */
51      private GokbRdbmsService getGokbRdbmsService() {
52          if (gokbRdbmsService == null) {
53              gokbRdbmsService = new GokbRdbmsServiceImpl();
54          }
55          return gokbRdbmsService;
56      }
57  
58      /**
59       * This method is used to initialize local copy of gokb.
60       */
61      @Override
62      public void initLocalGokb() {
63          getGokbRdbmsService().truncateTables();
64          int updateId = getGokbRdbmsService().insertStatus();
65          initPackages(updateId);
66          initVendors(updateId);
67          initPlatforms(updateId);
68          initTitles(updateId);
69          getGokbRdbmsService().insertLogEndTime(updateId);
70      }
71  
72      /**
73       * This method is used to update local copy of gokb.
74       *
75       * @param lastUpdatedTime
76       */
77      @Override
78      public void updateLocalGokb(String lastUpdatedTime) {
79          int updateId = getGokbRdbmsService().insertStatus();
80          updatePackages(lastUpdatedTime, updateId);
81          updateVendors(lastUpdatedTime, updateId);
82          updatePlatforms(lastUpdatedTime, updateId);
83          updateTitles(lastUpdatedTime, updateId);
84          getGokbRdbmsService().insertLogEndTime(updateId);
85      }
86  
87      /**
88       * This method is used to initialize Packages and Tipps.
89       *
90       * @param updateId
91       */
92      private void initPackages(int updateId) {
93          List<OleGokbPackage> oleGokbPackageList = new ArrayList<>();
94          int endIndex = 0;
95          int pageSize = 0;
96          int noOfRecordsInserted = 0;
97          int noOfTippRecordsInserted = 0;
98  
99          while (true) {
100             String responseXml = OleGokbXmlUtil.getPackageResponseXmlFromGokb(endIndex);
101 
102             NodeList packageNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.PACKAGE_XPATH_EXP);
103 
104             if (packageNodeList.getLength() == 0)
105                 break;
106 
107             List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(responseXml);
108 
109             for (int i = 0; i < packageNodeList.getLength(); i++) {
110                 Node packageNode = packageNodeList.item(i);
111                 OleGokbPackage oleGokbPackage = null;
112                 if (null != updatedDates.get(i)) {
113                     oleGokbPackage = buildPackageFromPackageNode(packageNode, updatedDates.get(i));
114                     oleGokbPackageList.add(oleGokbPackage);
115                 }
116                 if (oleGokbPackageList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
117                     getGokbRdbmsService().insertPackages(oleGokbPackageList);
118                     noOfRecordsInserted = noOfRecordsInserted + oleGokbPackageList.size();
119                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PKGS + noOfRecordsInserted);
120                     oleGokbPackageList.clear();
121                 }
122                 noOfTippRecordsInserted = processTipps(updateId, oleGokbPackage, packageNode.getChildNodes(), noOfTippRecordsInserted);
123             }
124             if (endIndex == 0) {
125                 pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
126             }
127             endIndex = endIndex + pageSize;
128         }
129 
130         if (oleGokbPackageList.size() > 0) {
131             getGokbRdbmsService().insertPackages(oleGokbPackageList);
132             noOfRecordsInserted = noOfRecordsInserted + oleGokbPackageList.size();
133             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PKGS + noOfRecordsInserted);
134         }
135     }
136 
137     /**
138      * This method is used to update Packages and Tipps.
139      *
140      * @param lastUpdatedTime
141      * @param updateId
142      */
143     private void updatePackages(String lastUpdatedTime, int updateId) {
144         List<OleGokbPackage> oleGokbPackageList = new ArrayList<>();
145         int endIndex = 0;
146         int pageSize = 0;
147         int noOfRecordsInserted = 0;
148         int noOfTippRecordsInserted = 0;
149 
150         while (true) {
151             String responseXml = OleGokbXmlUtil.getPackageResponseXmlFromGokb(lastUpdatedTime, endIndex);
152 
153             NodeList headerNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.HEADER_XPATH_EXP);
154 
155             if (headerNodeList.getLength() == 0)
156                 break;
157 
158             for (int i = 0; i < headerNodeList.getLength(); i++) {
159                 Node headerNode = headerNodeList.item(i);
160                 String identifier = OleGokbXmlUtil.getIdentifierFromHeader(headerNode);
161                 String recordXml = OleGokbXmlUtil.getPackageResponseXmlFromGokb(identifier);
162 
163                 List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(recordXml);
164 
165                 NodeList packageNodeList = OleGokbXmlUtil.getElementNodeList(recordXml, OLEConstants.OleGokb.PACKAGE_XPATH_EXP);
166                 Node packageNode = packageNodeList.item(0);
167                 OleGokbPackage oleGokbPackage = buildPackageFromPackageNode(packageNode, updatedDates.get(0));
168                 oleGokbPackageList.add(oleGokbPackage);
169                 if (oleGokbPackageList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
170                     getGokbRdbmsService().insertOrUpdatePackages(oleGokbPackageList);
171                     noOfRecordsInserted = noOfRecordsInserted + oleGokbPackageList.size();
172                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PKGS + noOfRecordsInserted);
173                     oleGokbPackageList.clear();
174                 }
175                 noOfTippRecordsInserted = processTipps(updateId, oleGokbPackage, packageNode.getChildNodes(), noOfTippRecordsInserted);
176             }
177             if (endIndex == 0) {
178                 pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
179             }
180             if (pageSize == 0)
181                 break;
182             endIndex = endIndex + pageSize;
183         }
184 
185         if (oleGokbPackageList.size() > 0) {
186             getGokbRdbmsService().insertOrUpdatePackages(oleGokbPackageList);
187             noOfRecordsInserted = noOfRecordsInserted + oleGokbPackageList.size();
188             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PKGS + noOfRecordsInserted);
189         }
190     }
191 
192     /**
193      * This method is used to initialize Titles.
194      *
195      * @param updateId
196      */
197     private void initTitles(int updateId) {
198         try {
199             List<OleGokbTitle> oleGokbTitleList = new ArrayList<>();
200             int endIndex = 0;
201             int pageSize = 0;
202             int noOfRecordsInserted = 0;
203 
204             while (true) {
205                 String responseXml = OleGokbXmlUtil.getTitleResponseXmlFromGokb(endIndex);
206                 NodeList titleNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.TITLE_XPATH_EXP);
207                 int count = 0;
208                 if (titleNodeList.getLength() == 0)
209                     break;
210 
211                 List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(responseXml);
212                 for (int i = 0; i < titleNodeList.getLength(); i++) {
213                     Node titleNode = titleNodeList.item(i);
214                     if (titleNode.getAttributes().getLength() == 0) {
215                         count = count + 1;
216                         continue;
217                     }
218                     OleGokbTitle oleGokbTitle = null;
219                     if (null != updatedDates.get(i - count)) {
220                         oleGokbTitle = buildTitleFromTitleNode(titleNode, updatedDates.get(i - count));
221                         oleGokbTitleList.add(oleGokbTitle);
222                     }
223                     if (oleGokbTitleList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
224                         getGokbRdbmsService().insertTitles(oleGokbTitleList);
225                         noOfRecordsInserted = noOfRecordsInserted + oleGokbTitleList.size();
226                         getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_TITLES + noOfRecordsInserted);
227                         oleGokbTitleList.clear();
228                     }
229                 }
230                 if (endIndex == 0) {
231                     pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
232                 }
233                 endIndex = endIndex + pageSize;
234             }
235 
236             if (oleGokbTitleList.size() > 0) {
237                 getGokbRdbmsService().insertTitles(oleGokbTitleList);
238                 noOfRecordsInserted = noOfRecordsInserted + oleGokbTitleList.size();
239                 getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_TITLES + noOfRecordsInserted);
240             }
241         } catch (Exception e) {
242             LOG.error("Exception While Initializing Titles: " + e);
243         }
244     }
245 
246     /**
247      * This method is used to update Titles.
248      *
249      * @param lastUpdatedTime
250      * @param updateId
251      */
252     private void updateTitles(String lastUpdatedTime, int updateId) {
253         List<OleGokbTitle> oleGokbTitleList = new ArrayList<>();
254         int endIndex = 0;
255         int pageSize = 0;
256         int noOfRecordsInserted = 0;
257 
258         while (true) {
259             String responseXml = OleGokbXmlUtil.getTitleResponseXmlFromGokb(lastUpdatedTime, endIndex);
260 
261             NodeList headerNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.HEADER_XPATH_EXP);
262 
263             if (headerNodeList.getLength() == 0)
264                 break;
265 
266             for (int i = 0; i < headerNodeList.getLength(); i++) {
267                 Node headerNode = headerNodeList.item(i);
268                 String identifier = OleGokbXmlUtil.getIdentifierFromHeader(headerNode);
269                 String recordXml = OleGokbXmlUtil.getTitleResponseXmlFromGokb(identifier);
270 
271                 List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(recordXml);
272 
273                 NodeList titleNodeList = OleGokbXmlUtil.getElementNodeList(recordXml, OLEConstants.OleGokb.TITLE_XPATH_EXP);
274                 Node titleNode = titleNodeList.item(0);
275                 OleGokbTitle oleGokbTitle = buildTitleFromTitleNode(titleNode, updatedDates.get(0));
276                 oleGokbTitleList.add(oleGokbTitle);
277                 if (oleGokbTitleList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
278                     getGokbRdbmsService().insertOrUpdateTitles(oleGokbTitleList);
279                     noOfRecordsInserted = noOfRecordsInserted + oleGokbTitleList.size();
280                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_TITLES + noOfRecordsInserted);
281                     oleGokbTitleList.clear();
282                 }
283             }
284             if (endIndex == 0) {
285                 pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
286             }
287             if (pageSize == 0)
288                 break;
289             endIndex = endIndex + pageSize;
290         }
291 
292         if (oleGokbTitleList.size() > 0) {
293             getGokbRdbmsService().insertOrUpdateTitles(oleGokbTitleList);
294             noOfRecordsInserted = noOfRecordsInserted + oleGokbTitleList.size();
295             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_TITLES + noOfRecordsInserted);
296         }
297     }
298 
299     /**
300      * This method is used to initialize Platforms.
301      *
302      * @param updateId
303      */
304     private void initPlatforms(int updateId) {
305         List<OleGokbPlatform> oleGokbPlatformList = new ArrayList<>();
306         int endIndex = 0;
307         int pageSize = 0;
308         int noOfRecordsInserted = 0;
309 
310         while (true) {
311             String responseXml = OleGokbXmlUtil.getPlatformResponseXmlFromGokb(endIndex);
312 
313             NodeList platformNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.PLATFORM_XPATH_EXP);
314 
315             if (platformNodeList.getLength() == 0)
316                 break;
317 
318             List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(responseXml);
319 
320             for (int i = 0; i < platformNodeList.getLength(); i++) {
321                 Node platformNode = platformNodeList.item(i);
322                 OleGokbPlatform oleGokbPlatform = null;
323                 if (null != updatedDates.get(i)) {
324                     oleGokbPlatform = buildPlatformFromPlatformNode(platformNode, updatedDates.get(i));
325                     oleGokbPlatformList.add(oleGokbPlatform);
326                 }
327                 if (oleGokbPlatformList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
328                     getGokbRdbmsService().insertPlatforms(oleGokbPlatformList);
329                     noOfRecordsInserted = noOfRecordsInserted + oleGokbPlatformList.size();
330                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PLTFRMS + noOfRecordsInserted);
331                     oleGokbPlatformList.clear();
332                 }
333             }
334             if (endIndex == 0) {
335                 pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
336             }
337             endIndex = endIndex + pageSize;
338         }
339 
340         if (oleGokbPlatformList.size() > 0) {
341             getGokbRdbmsService().insertPlatforms(oleGokbPlatformList);
342             noOfRecordsInserted = noOfRecordsInserted + oleGokbPlatformList.size();
343             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PLTFRMS + noOfRecordsInserted);
344         }
345     }
346 
347     /**
348      * This method is used to update Platforms.
349      *
350      * @param lastUpdatedTime
351      * @param updateId
352      */
353     private void updatePlatforms(String lastUpdatedTime, int updateId) {
354         List<OleGokbPlatform> oleGokbPlatformList = new ArrayList<>();
355         int endIndex = 0;
356         int pageSize = 0;
357         int noOfRecordsInserted = 0;
358 
359         while (true) {
360             String responseXml = OleGokbXmlUtil.getPlatformResponseXmlFromGokb(lastUpdatedTime, endIndex);
361 
362             NodeList headerNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.HEADER_XPATH_EXP);
363 
364             if (headerNodeList.getLength() == 0)
365                 break;
366 
367             for (int i = 0; i < headerNodeList.getLength(); i++) {
368                 Node headerNode = headerNodeList.item(i);
369                 String identifier = OleGokbXmlUtil.getIdentifierFromHeader(headerNode);
370                 String recordXml = OleGokbXmlUtil.getPlatformResponseXmlFromGokb(identifier);
371 
372                 List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(recordXml);
373 
374                 NodeList platformNodeList = OleGokbXmlUtil.getElementNodeList(recordXml, OLEConstants.OleGokb.PLATFORM_XPATH_EXP);
375                 Node platformNode = platformNodeList.item(0);
376                 OleGokbPlatform oleGokbPlatform = buildPlatformFromPlatformNode(platformNode, updatedDates.get(0));
377                 oleGokbPlatformList.add(oleGokbPlatform);
378                 if (oleGokbPlatformList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
379                     getGokbRdbmsService().insertOrUpdatePlatforms(oleGokbPlatformList);
380                     noOfRecordsInserted = noOfRecordsInserted + oleGokbPlatformList.size();
381                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PLTFRMS + noOfRecordsInserted);
382                     oleGokbPlatformList.clear();
383                 }
384             }
385             if (endIndex == 0) {
386                 pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
387             }
388             if (pageSize == 0)
389                 break;
390             endIndex = endIndex + pageSize;
391         }
392 
393         if (oleGokbPlatformList.size() > 0) {
394             getGokbRdbmsService().insertOrUpdatePlatforms(oleGokbPlatformList);
395             noOfRecordsInserted = noOfRecordsInserted + oleGokbPlatformList.size();
396             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_PLTFRMS + noOfRecordsInserted);
397         }
398     }
399 
400     /**
401      * This method is used to initialize Organizations and Roles.
402      *
403      * @param updateId
404      */
405     public void initVendors(int updateId) {
406         List<OleGokbOrganization> oleGokbOrganizationList = new ArrayList<>();
407         int endIndex = 0;
408         int pageSize = 0;
409         int noOfRecordsInserted = 0;
410 
411         while (true) {
412             String responseXml = OleGokbXmlUtil.getOrgsResponseXmlFromGokb(endIndex);
413 
414             NodeList orgsNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.ORG_XPATH_EXP);
415 
416             if (orgsNodeList.getLength() == 0)
417                 break;
418 
419             List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(responseXml);
420 
421             for (int i = 0; i < orgsNodeList.getLength(); i++) {
422                 Node orgNode = orgsNodeList.item(i);
423                 OleGokbOrganization oleGokbOrganization = null;
424                 if (null != updatedDates.get(i)) {
425                     oleGokbOrganization = buildOrgFromOrgNode(orgNode, updatedDates.get(i));
426                     oleGokbOrganizationList.add(oleGokbOrganization);
427                 }
428                 if (oleGokbOrganizationList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
429                     getGokbRdbmsService().insertOrganizations(oleGokbOrganizationList);
430                     noOfRecordsInserted = noOfRecordsInserted + oleGokbOrganizationList.size();
431                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_ORGS + noOfRecordsInserted);
432                     oleGokbOrganizationList.clear();
433                 }
434                 processRoles(oleGokbOrganization, orgNode.getChildNodes());
435             }
436             if (endIndex == 0) {
437                 pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
438             }
439             endIndex = endIndex + pageSize;
440         }
441 
442         if (oleGokbOrganizationList.size() > 0) {
443             getGokbRdbmsService().insertOrganizations(oleGokbOrganizationList);
444             noOfRecordsInserted = noOfRecordsInserted + oleGokbOrganizationList.size();
445             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_ORGS + noOfRecordsInserted);
446         }
447     }
448 
449     /**
450      * This method is used to update Organizations and Roles.
451      *
452      * @param lastUpdatedTime
453      * @param updateId
454      */
455     public void updateVendors(String lastUpdatedTime, int updateId) {
456         List<OleGokbOrganization> oleGokbOrganizationList = new ArrayList<>();
457         int endIndex = 0;
458         int pageSize = 0;
459         int noOfRecordsInserted = 0;
460 
461         while (true) {
462             String responseXml = OleGokbXmlUtil.getOrgsResponseXmlFromGokb(lastUpdatedTime, endIndex);
463 
464             NodeList headerNodeList = OleGokbXmlUtil.getElementNodeList(responseXml, OLEConstants.OleGokb.HEADER_XPATH_EXP);
465 
466             if (headerNodeList.getLength() == 0)
467                 break;
468 
469             for (int i = 0; i < headerNodeList.getLength(); i++) {
470                 Node headerNode = headerNodeList.item(i);
471                 String identifier = OleGokbXmlUtil.getIdentifierFromHeader(headerNode);
472                 String recordXml = OleGokbXmlUtil.getOrgsResponseXmlFromGokb(identifier);
473 
474                 List<String> updatedDates = OleGokbXmlUtil.getUpdatedDates(recordXml);
475 
476                 NodeList orgsNodeList = OleGokbXmlUtil.getElementNodeList(recordXml, OLEConstants.OleGokb.ORG_XPATH_EXP);
477                 Node orgNode = orgsNodeList.item(0);
478                 OleGokbOrganization oleGokbOrganization = buildOrgFromOrgNode(orgNode, updatedDates.get(0));
479                 oleGokbOrganizationList.add(oleGokbOrganization);
480                 if (oleGokbOrganizationList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
481                     getGokbRdbmsService().insertOrUpdateOrganizations(oleGokbOrganizationList);
482                     noOfRecordsInserted = noOfRecordsInserted + oleGokbOrganizationList.size();
483                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_ORGS + noOfRecordsInserted);
484                     oleGokbOrganizationList.clear();
485                 }
486                 processRoles(oleGokbOrganization, orgNode.getChildNodes());
487             }
488             if (endIndex == 0) {
489                 pageSize = OleGokbXmlUtil.getPageSizeFromResponse(responseXml);
490             }
491             if (pageSize == 0)
492                 break;
493             endIndex = endIndex + pageSize;
494         }
495 
496         if (oleGokbOrganizationList.size() > 0) {
497             getGokbRdbmsService().insertOrUpdateOrganizations(oleGokbOrganizationList);
498             noOfRecordsInserted = noOfRecordsInserted + oleGokbOrganizationList.size();
499             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_ORGS + noOfRecordsInserted);
500         }
501     }
502 
503     /**
504      * This method reads the package node and builds the package object.
505      *
506      * @param packageNode
507      * @param updatedDate
508      * @return
509      */
510     public OleGokbPackage buildPackageFromPackageNode(Node packageNode, String updatedDate) {
511         OleGokbPackage oleGokbPackage = new OleGokbPackage();
512         String packageId = ((DeferredElementImpl) packageNode).getAttribute(OLEConstants.OleGokb.ID);
513         if (!packageId.isEmpty()) {
514             try {
515                 oleGokbPackage.setGokbPackageId(Integer.parseInt(packageId));
516             } catch (Exception e) {
517                 LOG.error("Exception while parsing int for package id : " + packageId + " " + e);
518             }
519         }
520         oleGokbPackage.setDateUpdated(OleGokbXmlUtil.getTimeStampFromString(updatedDate));
521         String nodeName = null;
522         Node packageChildNode = null;
523         NodeList childNodes = packageNode.getChildNodes();
524         for (int j = 0; j < childNodes.getLength(); j++) {
525             nodeName = childNodes.item(j).getNodeName();
526             packageChildNode = childNodes.item(j);
527             if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.SCOPE)) {
528                 oleGokbPackage.setPackageScope(packageChildNode.getTextContent());
529             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.STATUS)) {
530                 oleGokbPackage.setStatus(packageChildNode.getTextContent());
531             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.BREAKABLE)) {
532                 oleGokbPackage.setBreakable(packageChildNode.getTextContent());
533             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.FIXED)) {
534                 oleGokbPackage.setFixed(packageChildNode.getTextContent());
535             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.NAME)) {
536                 oleGokbPackage.setPackageName(packageChildNode.getTextContent());
537             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.VARIANT_NAMES)) {
538                 oleGokbPackage.setVariantName(getVariantNames(packageChildNode));
539             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.DATE_CREATED)) {
540                 oleGokbPackage.setDateCreated(OleGokbXmlUtil.getTimeStampFromString(packageChildNode.getTextContent()));
541             }
542         }
543         return oleGokbPackage;
544     }
545 
546     /**
547      * This method reads the Tipp nodes and initializes Tipps.
548      *
549      * @param updateId
550      * @param oleGokbPackage
551      * @param packageChildNodeList
552      * @param noOfTippRecordsInserted
553      * @return
554      */
555     public int processTipps(int updateId, OleGokbPackage oleGokbPackage, NodeList packageChildNodeList, int noOfTippRecordsInserted) {
556         List<OleGokbTipp> oleGokbTippList = new ArrayList<>();
557         for (int i = 0; i < packageChildNodeList.getLength(); i++) {
558             if (!packageChildNodeList.item(i).getNodeName().equalsIgnoreCase(OLEConstants.OleGokb.TIPPS))
559                 continue;
560             NodeList tippsNodeList = packageChildNodeList.item(i).getChildNodes();
561             for (int j = 0; j < tippsNodeList.getLength(); j++) {
562                 OleGokbTipp oleGokbTipp = new OleGokbTipp();
563                 Node tippNode = tippsNodeList.item(j);
564                 if (tippNode.getAttributes() == null)
565                     continue;
566                 String tippId = ((DeferredElementImpl) tippNode).getAttribute(OLEConstants.OleGokb.ID);
567                 if (!tippId.isEmpty()) {
568                     try {
569                         oleGokbTipp.setGokbTippId(Integer.parseInt(tippId));
570                     } catch (Exception e) {
571                         LOG.error("Exception while parsing int for tipp id : " + tippId + " " + e);
572                     }
573                 }
574                 NodeList tippChildNodes = tippNode.getChildNodes();
575                 for (int k = 0; k < tippChildNodes.getLength(); k++) {
576                     String nodeName = tippChildNodes.item(k).getNodeName();
577                     Node tippChildNode = tippChildNodes.item(k);
578                     if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.STATUS)) {
579                         oleGokbTipp.setStatus(tippChildNode.getTextContent());
580                     } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.TITLE)) {
581                         String titleId = ((DeferredElementImpl) tippChildNode).getAttribute("id");// String titleId = StringUtils.substringAfter(((DeferredElementImpl) tippChildNode).getAttribute(OLEConstants.OleGokb.ID), OLEConstants.OleGokb.TITLE + OLEConstants.SLASH);
582                         if (!titleId.isEmpty()) {
583                             try {
584                                 oleGokbTipp.setGokbTitleId(Integer.parseInt(titleId));
585                             } catch (Exception e) {
586                                 LOG.error("Exception while parsing int of title id for tipp with id : " + oleGokbTipp.getGokbTippId() + " " + e);
587                             }
588                         }
589                     } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.PLATFORM)) {
590                         String platformId = ((DeferredElementImpl) tippChildNode).getAttribute(OLEConstants.OleGokb.ID);
591                         if (!platformId.isEmpty()) {
592                             try {
593                                 oleGokbTipp.setGokbPlatformId(Integer.parseInt(platformId));
594                             } catch (Exception e) {
595                                 LOG.error("Exception while parsing int of platform id for tipp with id : " + oleGokbTipp.getGokbTippId() + " " + e);
596                             }
597                         }
598                     } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.COVERAGE)) {
599                         NamedNodeMap namedNodeMap = tippChildNode.getAttributes();
600                         for (int l = 0; l < namedNodeMap.getLength(); l++) {
601                             Attr attribute = (Attr) namedNodeMap.item(l);
602                             if (attribute.getName().equalsIgnoreCase(OLEConstants.OleGokb.START_DATE)) {
603                                 oleGokbTipp.setStartdate(OleGokbXmlUtil.getTimeStampFromString(attribute.getTextContent()));
604                             } else if (attribute.getName().equalsIgnoreCase(OLEConstants.OleGokb.START_VOLUME)) {
605                                 oleGokbTipp.setStartVolume(attribute.getTextContent());
606                             } else if (attribute.getName().equalsIgnoreCase(OLEConstants.OleGokb.START_ISSUE)) {
607                                 oleGokbTipp.setStartIssue(attribute.getTextContent());
608                             } else if (attribute.getName().equalsIgnoreCase(OLEConstants.OleGokb.END_DATE)) {
609                                 oleGokbTipp.setEndDate(OleGokbXmlUtil.getTimeStampFromString(attribute.getTextContent()));
610                             } else if (attribute.getName().equalsIgnoreCase(OLEConstants.OleGokb.END_VOLUME)) {
611                                 oleGokbTipp.setEndVolume(attribute.getTextContent());
612                             } else if (attribute.getName().equalsIgnoreCase(OLEConstants.OleGokb.END_ISSUE)) {
613                                 oleGokbTipp.setEndIssue(attribute.getTextContent());
614                             }
615                         }
616                     } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.URL)) {
617                         oleGokbTipp.setPlatformHostUrl(tippChildNode.getTextContent());
618                     }
619                 }
620                 oleGokbTipp.setGokbPackageId(oleGokbPackage.getGokbPackageId());
621                 oleGokbTipp.setDateCreated(oleGokbPackage.getDateCreated());
622                 oleGokbTipp.setDateUpdated(oleGokbPackage.getDateUpdated());
623                 oleGokbTippList.add(oleGokbTipp);
624                 if (oleGokbTippList.size() == OLEConstants.OleGokb.BATCH_SIZE) {
625                     getGokbRdbmsService().insertOrUpdateTipps(oleGokbTippList);
626                     noOfTippRecordsInserted = noOfTippRecordsInserted + oleGokbTippList.size();
627                     getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_TIPPS + noOfTippRecordsInserted);
628                     oleGokbTippList.clear();
629                 }
630             }
631         }
632         if (oleGokbTippList.size() > 0) {
633             getGokbRdbmsService().insertOrUpdateTipps(oleGokbTippList);
634             noOfTippRecordsInserted = noOfTippRecordsInserted + oleGokbTippList.size();
635             getGokbRdbmsService().updateStatus(updateId, OLEConstants.OleGokb.NUM_TIPPS + noOfTippRecordsInserted);
636         }
637         return noOfTippRecordsInserted;
638     }
639 
640     /**
641      * This method reads the Organization role nodes and initializes roles.
642      *
643      * @param oleGokbOrganization
644      * @param orgChildNodeList
645      */
646     private void processRoles(OleGokbOrganization oleGokbOrganization, NodeList orgChildNodeList) {
647         List<OleGokbOrganizationRole> oleGokbOrganizationRoles = new ArrayList<>();
648         for (int i = 0; i < orgChildNodeList.getLength(); i++) {
649             if (!orgChildNodeList.item(i).getNodeName().equalsIgnoreCase(OLEConstants.OleGokb.ROLES))
650                 continue;
651             NodeList rolesNodeList = orgChildNodeList.item(i).getChildNodes();
652             for (int j = 0; j < rolesNodeList.getLength(); j++) {
653                 Node roleNode = rolesNodeList.item(j);
654                 if (roleNode.getAttributes() == null)
655                     continue;
656                 OleGokbOrganizationRole oleGokbOrganizationRole = new OleGokbOrganizationRole();
657                 oleGokbOrganizationRole.setGokbOrganizationId(oleGokbOrganization.getGokbOrganizationId());
658                 oleGokbOrganizationRole.setRole(roleNode.getTextContent());
659                 oleGokbOrganizationRoles.add(oleGokbOrganizationRole);
660             }
661         }
662         if (oleGokbOrganizationRoles.size() > 0) {
663             getGokbRdbmsService().insertOrUpdateOrganizationRoles(oleGokbOrganizationRoles);
664         }
665     }
666 
667     /**
668      * This method reads the title node and builds the title object.
669      *
670      * @param titleNode
671      * @param updatedDate
672      * @return
673      */
674     public OleGokbTitle buildTitleFromTitleNode(Node titleNode, String updatedDate) {
675         OleGokbTitle oleGokbTitle = new OleGokbTitle();
676         String titleId = ((DeferredElementImpl) titleNode).getAttribute(OLEConstants.OleGokb.ID);
677         if (!titleId.isEmpty()) {
678             try {
679                 oleGokbTitle.setGokbTitleId(Integer.parseInt(titleId));
680             } catch (Exception e) {
681                 LOG.error("Exception while parsing int for title id : " + titleId + " " + e);
682             }
683         }
684         oleGokbTitle.setDateUpdated(OleGokbXmlUtil.getTimeStampFromString(updatedDate));
685         String nodeName = null;
686         Node titleChildNode = null;
687         NodeList titleChildNodes = titleNode.getChildNodes();
688         for (int i = 0; i < titleChildNodes.getLength(); i++) {
689             nodeName = titleChildNodes.item(i).getNodeName();
690             titleChildNode = titleChildNodes.item(i);
691             if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.NAME)) {
692                 oleGokbTitle.setTitleName(titleChildNode.getTextContent());
693             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.IMPRINT)) {
694                 try {
695                     if (StringUtils.isNotEmpty(titleChildNode.getTextContent())) {
696                         oleGokbTitle.setImprint(Integer.parseInt(titleChildNode.getTextContent()));
697                     }
698                 } catch (Exception e) {
699                     LOG.error("Exception while parsing int of imprint for title with id : " + oleGokbTitle.getGokbTitleId() + " " + e);
700                 }
701             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.MEDIUM)) {
702                 oleGokbTitle.setMedium(titleChildNode.getTextContent());
703             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.IDENTIFIERS)) {
704                 NodeList identifierNodes = titleChildNode.getChildNodes();
705                 for (int j = 0; j < identifierNodes.getLength(); j++) {
706                     NamedNodeMap namedNodeMap = identifierNodes.item(j).getAttributes();
707                     if (namedNodeMap == null)
708                         continue;
709                     for (int k = 0; k < namedNodeMap.getLength(); k++) {
710                         Attr attribute = (Attr) namedNodeMap.item(k);
711                         if (attribute.getNodeValue().equalsIgnoreCase(OLEConstants.OleGokb.ISSN)) {
712                             k++;
713                             oleGokbTitle.setIssnPrint(namedNodeMap.item(k).getNodeValue());
714                         } else if (attribute.getNodeValue().equalsIgnoreCase(OLEConstants.OleGokb.EISSN)) {
715                             k++;
716                             oleGokbTitle.setIssnOnline(namedNodeMap.item(k).getNodeValue());
717                         } else if (attribute.getNodeValue().equalsIgnoreCase(OLEConstants.OleGokb.DOI)) {
718                             k++;
719                             oleGokbTitle.setDoi(namedNodeMap.item(k).getNodeValue());
720                         } else if (attribute.getNodeValue().equalsIgnoreCase(OLEConstants.OleGokb.PROPRIETARY_ID)) {
721                             k++;
722                             try {
723                                 oleGokbTitle.setProprietaryId(Integer.parseInt(namedNodeMap.item(k).getNodeValue()));
724                             } catch (Exception e) {
725                                 LOG.error("Exception while parsing int of proprietary id for title with id : " + oleGokbTitle.getGokbTitleId() + " " + e);
726                             }
727                         } else if (attribute.getNodeValue().equalsIgnoreCase(OLEConstants.OleGokb.OCLC_NUM)) {
728                             k++;
729                             try {
730                                 oleGokbTitle.setOclcNumber(Integer.parseInt(namedNodeMap.item(k).getNodeValue()));
731                             } catch (Exception e) {
732                                 LOG.error("Exception while parsing int of oclc number for title with id : " + oleGokbTitle.getGokbTitleId() + " " + e);
733                             }
734                         }
735                     }
736                 }
737             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.PUBLISHER)) {
738                 String publisherId = ((DeferredElementImpl) titleChildNode).getAttribute(OLEConstants.OleGokb.ID);
739                 if (!publisherId.isEmpty()) {
740                     try {
741                         oleGokbTitle.setPublisherId(Integer.parseInt(publisherId));
742                     } catch (Exception e) {
743                         LOG.error("Exception while parsing int of publisher for title with id : " + oleGokbTitle.getGokbTitleId() + " " + e);
744                     }
745                 }
746             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.DATE_CREATED)) {
747                 oleGokbTitle.setDateCreated(OleGokbXmlUtil.getTimeStampFromString(titleChildNode.getTextContent()));
748             }
749         }
750         return oleGokbTitle;
751     }
752 
753     /**
754      * This method reads the platform node and builds the platform object.
755      *
756      * @param platformNode
757      * @param updatedDate
758      * @return
759      */
760     public OleGokbPlatform buildPlatformFromPlatformNode(Node platformNode, String updatedDate) {
761         OleGokbPlatform oleGokbPlatform = new OleGokbPlatform();
762         String platformId = ((DeferredElementImpl) platformNode).getAttribute(OLEConstants.OleGokb.ID);
763         if (!platformId.isEmpty()) {
764             try {
765                 oleGokbPlatform.setGokbPlatformId(Integer.parseInt(platformId));
766             } catch (Exception e) {
767                 LOG.error("Exception while parsing int for platform id : " + platformId + " " + e);
768             }
769         }
770         oleGokbPlatform.setDateUpdated(OleGokbXmlUtil.getTimeStampFromString(updatedDate));
771         String nodeName = null;
772         Node platformChildNode = null;
773         NodeList platformChildNodes = platformNode.getChildNodes();
774         for (int i = 0; i < platformChildNodes.getLength(); i++) {
775             nodeName = platformChildNodes.item(i).getNodeName();
776             platformChildNode = platformChildNodes.item(i);
777             if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.NAME)) {
778                 oleGokbPlatform.setPlatformName(platformChildNode.getTextContent());
779             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.AUTHENTICATION)) {
780                 oleGokbPlatform.setAuthentication(platformChildNode.getTextContent());
781             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.SOFTWARE)) {
782                 oleGokbPlatform.setSoftwarePlatform(platformChildNode.getTextContent());
783             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.DATE_CREATED)) {
784                 oleGokbPlatform.setDateCreated(OleGokbXmlUtil.getTimeStampFromString(platformChildNode.getTextContent()));
785             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.STATUS)) {
786                 oleGokbPlatform.setStatus(platformChildNode.getTextContent());
787             }  else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.SERVICE)) {
788                 if (StringUtils.isNotBlank(platformChildNode.getTextContent())) {
789                     Map<String, String> orgMap = new HashMap<>();
790                     orgMap.put(OLEPropertyConstants.ORGANIZATION_NAME, platformChildNode.getTextContent());
791                     List<OleGokbOrganization> oleGokbOrganizations = (List<OleGokbOrganization>) getBusinessObjectService().findMatching(OleGokbOrganization.class, orgMap);
792                     if (oleGokbOrganizations.size() > 0) {
793                         oleGokbPlatform.setPlatformProviderId(oleGokbOrganizations.get(0).getGokbOrganizationId());
794                     }
795                 }
796             }
797         }
798         return oleGokbPlatform;
799     }
800 
801     /**
802      * This method reads the organization node and builds the organization object.
803      *
804      * @param orgNode
805      * @param updatedDate
806      * @return
807      */
808     public OleGokbOrganization buildOrgFromOrgNode(Node orgNode, String updatedDate) {
809         OleGokbOrganization oleGokbOrganization = new OleGokbOrganization();
810         String orgId = ((DeferredElementImpl) orgNode).getAttribute(OLEConstants.OleGokb.ID);
811         if (!orgId.isEmpty()) {
812             try {
813                 oleGokbOrganization.setGokbOrganizationId(Integer.parseInt(orgId));
814             } catch (Exception e) {
815                 LOG.error("Exception while parsing int for organization id : " + orgId + " " + e);
816             }
817         }
818         oleGokbOrganization.setDateUpdated(OleGokbXmlUtil.getTimeStampFromString(updatedDate));
819         String nodeName = null;
820         Node orgChildNode = null;
821         NodeList orgChildNodes = orgNode.getChildNodes();
822         for (int i = 0; i < orgChildNodes.getLength(); i++) {
823             nodeName = orgChildNodes.item(i).getNodeName();
824             orgChildNode = orgChildNodes.item(i);
825             if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.NAME)) {
826                 oleGokbOrganization.setOrganizationName(orgChildNode.getTextContent());
827             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.VARIANT_NAMES)) {
828                 oleGokbOrganization.setVariantName(getVariantNames(orgChildNode));
829             } else if (nodeName.equalsIgnoreCase(OLEConstants.OleGokb.DATE_CREATED)) {
830                 oleGokbOrganization.setDateCreated(OleGokbXmlUtil.getTimeStampFromString(orgChildNode.getTextContent()));
831             }
832         }
833         return oleGokbOrganization;
834     }
835 
836     /**
837      * This methods builds the variant names separated by pipe symbol.
838      *
839      * @param orgChildNode
840      * @return
841      */
842     private String getVariantNames(Node orgChildNode) {
843         StringBuilder variantNameBuilder = new StringBuilder();
844         NodeList variantNameNodeList = orgChildNode.getChildNodes();
845         for (int j = 0; j < variantNameNodeList.getLength(); j++) {
846             variantNameBuilder.append(variantNameNodeList.item(j).getTextContent());
847             if (j < variantNameNodeList.getLength() - 1) {
848                 variantNameBuilder.append(OLEConstants.OleGokb.PIPE);
849             }
850         }
851         return variantNameBuilder.toString();
852     }
853 
854 }