View Javadoc
1   package org.kuali.ole.docstore.common.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.docstore.common.document.*;
5   import org.kuali.ole.docstore.common.document.HoldingsTree;
6   import org.kuali.ole.docstore.common.document.Item;
7   import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
8   import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
9   import org.kuali.ole.docstore.common.document.content.instance.*;
10  import org.kuali.rice.core.api.config.property.ConfigContext;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  import org.springframework.util.StopWatch;
14  
15  import java.beans.PropertyVetoException;
16  import java.io.File;
17  import java.io.FileWriter;
18  import java.io.IOException;
19  import java.sql.*;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.*;
23  import java.util.Date;
24  
25  /**
26   * Created with IntelliJ IDEA.
27   * User: sambasivam
28   * Date: 5/30/14
29   * Time: 12:47 PM
30   * To change this template use File | Settings | File Templates.
31   */
32  public class BatchBibTreeDBUtil {
33  
34      private static final Logger LOG = LoggerFactory.getLogger(BatchBibTreeDBUtil.class);
35      public static Map<String, String> callNumberType = new HashMap<>();
36      public static Map<String, String> receiptStatus = new HashMap<>();
37      public static Map<String, String> authenticationType = new HashMap<>();
38      public static Map<String, String> itemTypeMap = new HashMap<>();
39      public static Map<String, String> itemStatusMap = new HashMap<>();
40      public static Map<String, String> statisticalSearchCodeMap = new HashMap<>();
41      public static Map<String, String> extentOfOwnershipTypeMap = new HashMap<>();
42  
43      private Connection connection = null;
44      private Connection bibConnection = null;
45      private Connection holdingsConnection = null;
46      private Connection itemConnection = null;
47  
48      private Statement bibStatement = null;
49      private PreparedStatement holdingsPreparedStatement = null;
50      private PreparedStatement itemPreparedStatement = null;
51      private ResultSet bibResultSet = null;
52      private ResultSet bibHoldingsResultSet = null;
53      private ResultSet holdingItemResultSet = null;
54      private final static String dbVendor = ConfigContext.getCurrentContextConfig().getProperty("db.vendor");
55      static BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
56      private boolean isStaffOnly = true;
57  
58  
59      PreparedStatement bibInsertPreparedStatement = null;
60      PreparedStatement bibUpdatePreparedStatement = null;
61  
62  
63      private String bibQuery = "SELECT * FROM OLE_DS_BIB_T ORDER BY BIB_ID";
64  
65      private String holdingsQuery = "SELECT * FROM OLE_DS_HOLDINGS_T " +
66              " LEFT JOIN ole_ds_holdings_uri_t ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=ole_ds_holdings_uri_t.HOLDINGS_ID " +
67              " LEFT JOIN ole_ds_holdings_note_t ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=ole_ds_holdings_note_t.HOLDINGS_ID " +
68              " LEFT JOIN OLE_DS_HOLDINGS_DONOR_T ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=OLE_DS_HOLDINGS_DONOR_T.HOLDINGS_ID" +
69              " LEFT JOIN OLE_DS_HOLDINGS_COVERAGE_T ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=OLE_DS_HOLDINGS_COVERAGE_T.HOLDINGS_ID " +
70              " LEFT JOIN OLE_DS_PERPETUAL_ACCESS_T ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=OLE_DS_PERPETUAL_ACCESS_T.HOLDINGS_ID " +
71              " LEFT JOIN OLE_DS_HOLDINGS_STAT_SEARCH_T ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=OLE_DS_HOLDINGS_STAT_SEARCH_T.HOLDINGS_ID " +
72              " LEFT JOIN OLE_DS_ACCESS_LOCATION_T ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=OLE_DS_ACCESS_LOCATION_T.HOLDINGS_ID " +
73              " LEFT JOIN OLE_DS_ACCESS_LOCATION_CODE_T ON OLE_DS_ACCESS_LOCATION_T.ACCESS_LOCATION_CODE_ID=OLE_DS_ACCESS_LOCATION_CODE_T.ACCESS_LOCATION_CODE_ID " +
74              " LEFT JOIN ole_ds_ext_ownership_t ON OLE_DS_HOLDINGS_T.HOLDINGS_ID=ole_ds_ext_ownership_t.holdings_id " +
75              " LEFT JOIN ole_ds_ext_ownership_note_t ON ole_ds_ext_ownership_t.EXT_OWNERSHIP_ID=ole_ds_ext_ownership_note_t.EXT_OWNERSHIP_ID " +
76              " WHERE OLE_DS_HOLDINGS_T.BIB_ID=?";
77  
78      private String itemQuery = "SELECT * FROM ole_ds_item_t " +
79              " LEFT JOIN ole_ds_item_donor_t ON ole_ds_item_t.item_id=ole_ds_item_donor_t.item_id " +
80              " LEFT JOIN ole_ds_item_note_t ON ole_ds_item_t.item_id = ole_ds_item_note_t.item_id " +
81              " LEFT JOIN ole_ds_item_stat_search_t ON ole_ds_item_t.item_id=ole_ds_item_stat_search_t.item_id " +
82              " LEFT JOIN OLE_DS_HIGH_DENSITY_STORAGE_T ON ole_ds_item_t.HIGH_DENSITY_STORAGE_ID = OLE_DS_HIGH_DENSITY_STORAGE_T.HIGH_DENSITY_STORAGE_ID " +
83              " WHERE OLE_DS_ITEM_T.HOLDINGS_ID=?";
84  
85      private String bibStaffOnly = " SELECT * FROM OLE_DS_BIB_T  WHERE STAFF_ONLY= 'N' ";
86      private String staffOnly = " WHERE STAFF_ONLY= 'N' ";
87      private String staffOnlyHoldings = " AND OLE_DS_HOLDINGS_T.STAFF_ONLY= 'N' ";
88      private String staffOnlyItem = " AND OLE_DS_ITEM_T.STAFF_ONLY= 'N' ";
89  
90      private String bibCountQuery = "SELECT count(*) as totalRecords FROM ole_ds_bib_t";
91  
92  
93      public  BatchBibTreeDBUtil(){
94  
95      }
96  
97     public  BatchBibTreeDBUtil(boolean isStaffOnly){
98             this.isStaffOnly=isStaffOnly;
99     }
100 
101     public void init() throws SQLException {
102         if(!isStaffOnly){
103             bibQuery = bibStaffOnly;
104             holdingsQuery =  holdingsQuery + staffOnlyHoldings;
105             itemQuery =  itemQuery + staffOnlyItem;
106         }
107         if (connection == null || connection.isClosed()) {
108             connection = getConnection();
109         }
110         fetchCallNumberType();
111         fetchReceiptStatus();
112         fetchAuthenticationType();
113         fetchItemType();
114         fetchItemStatus();
115         fetchStatisticalSearchCode();
116         fetchExtentOfOwnershipType();
117 
118         bibConnection = getConnection();
119         holdingsConnection = getConnection();
120         itemConnection = getConnection();
121 
122         bibConnection.setAutoCommit(false);
123 
124         bibStatement = bibConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
125         if (dbVendor.equalsIgnoreCase("oracle")) {
126             bibStatement.setFetchSize(1);
127         } else if (dbVendor.equalsIgnoreCase("mysql")) {
128             bibStatement.setFetchSize(Integer.MIN_VALUE);
129         }
130 
131         bibResultSet = bibStatement.executeQuery(bibQuery);
132 
133         holdingsPreparedStatement = holdingsConnection.prepareStatement(holdingsQuery);
134 
135         itemPreparedStatement = itemConnection.prepareStatement(itemQuery);
136 
137         String insertQuery = "INSERT INTO OLE_DS_BIB_INFO_T(BIB_ID, BIB_ID_STR, TITLE, AUTHOR, PUBLISHER, ISXN) VALUES (?,?,?,?,?,?)";
138         bibInsertPreparedStatement = connection.prepareStatement(insertQuery);
139 
140         String updateQuery = "UPDATE OLE_DS_BIB_INFO_T SET TITLE=?, AUTHOR=?, PUBLISHER=?, ISXN=?, BIB_ID_STR=? WHERE BIB_ID=?";
141         bibUpdatePreparedStatement = connection.prepareStatement(updateQuery);
142 
143     }
144 
145 
146 
147 
148 
149 
150     private Connection getConnection() throws SQLException {
151         DataSource dataSource = null;
152         try {
153             dataSource = DataSource.getInstance();
154         } catch (IOException e) {
155             LOG.error("IOException : " + e);
156         } catch (SQLException e) {
157             LOG.error("SQLException : " + e);
158         } catch (PropertyVetoException e) {
159             LOG.error("PropertyVetoException : " + e);
160         }
161         return dataSource.getConnection();
162     }
163 
164 
165     public void closeConnections() throws SQLException {
166         if (itemPreparedStatement != null) {
167             itemPreparedStatement.close();
168         }
169 
170         if (itemConnection != null) {
171             itemConnection.close();
172         }
173 
174         if (holdingsPreparedStatement != null) {
175             holdingsPreparedStatement.close();
176         }
177 
178         if (holdingsConnection != null) {
179             holdingsConnection.close();
180         }
181 
182         if (bibResultSet != null) {
183             bibResultSet.close();
184         }
185         if (bibStatement != null) {
186             bibStatement.close();
187         }
188 
189         if (bibConnection != null) {
190             bibConnection.close();
191         }
192         if (connection != null) {
193             connection.close();
194         }
195 
196     }
197 
198     private void fetchCallNumberType() throws SQLException {
199 
200         Statement statement = connection.createStatement();
201         ResultSet resultSet = statement.executeQuery("SELECT SHVLG_SCHM_ID,SHVLG_SCHM_CD,SHVLG_SCHM_NM from OLE_CAT_SHVLG_SCHM_T");
202         while (resultSet.next()) {
203             callNumberType.put(resultSet.getString("SHVLG_SCHM_ID"), resultSet.getString("SHVLG_SCHM_CD") + "|" + resultSet.getString("SHVLG_SCHM_NM"));
204         }
205         resultSet.close();
206     }
207 
208     private void fetchReceiptStatus() throws SQLException {
209 
210         Statement statement = connection.createStatement();
211         ResultSet resultSet = statement.executeQuery("SELECT RCPT_STAT_CD,RCPT_STAT_NM from OLE_CAT_RCPT_STAT_T");
212         while (resultSet.next()) {
213             receiptStatus.put(resultSet.getString("RCPT_STAT_CD"), resultSet.getString("RCPT_STAT_NM"));
214         }
215         resultSet.close();
216 
217     }
218 
219     private void fetchAuthenticationType() throws SQLException {
220         Statement statement = connection.createStatement();
221         ResultSet resultSet = statement.executeQuery("SELECT CODE,NAME from OLE_DS_AUTHENTICATION_TYPE_T");
222         while (resultSet.next()) {
223             authenticationType.put(resultSet.getString("CODE"), resultSet.getString("NAME"));
224         }
225         resultSet.close();
226 
227     }
228 
229     private void fetchItemType() throws SQLException {
230 
231         Statement statement = connection.createStatement();
232         ResultSet resultSet = statement.executeQuery("SELECT ITM_TYP_CD_ID,ITM_TYP_CD,ITM_TYP_NM from OLE_CAT_ITM_TYP_T");
233         while (resultSet.next()) {
234             itemTypeMap.put(resultSet.getString("ITM_TYP_CD_ID"), resultSet.getString("ITM_TYP_CD") + "|" + resultSet.getString("ITM_TYP_NM"));
235         }
236         resultSet.close();
237 
238     }
239 
240     private void fetchItemStatus() throws SQLException {
241         Statement statement = connection.createStatement();
242         ResultSet resultSet = statement.executeQuery("SELECT ITEM_AVAIL_STAT_ID,ITEM_AVAIL_STAT_CD,ITEM_AVAIL_STAT_NM from OLE_DLVR_ITEM_AVAIL_STAT_T");
243         while (resultSet.next()) {
244             itemStatusMap.put(resultSet.getString("ITEM_AVAIL_STAT_ID"), resultSet.getString("ITEM_AVAIL_STAT_CD") + "|" + resultSet.getString("ITEM_AVAIL_STAT_NM"));
245         }
246         resultSet.close();
247 
248     }
249 
250     private void fetchStatisticalSearchCode() throws SQLException {
251         Statement statement = connection.createStatement();
252         ResultSet resultSet = statement.executeQuery("SELECT STAT_SRCH_CD_ID, STAT_SRCH_CD, STAT_SRCH_NM from OLE_CAT_STAT_SRCH_CD_T");
253         while (resultSet.next()) {
254             statisticalSearchCodeMap.put(resultSet.getString("STAT_SRCH_CD_ID"), resultSet.getString("STAT_SRCH_CD") + "|" + resultSet.getString("STAT_SRCH_NM"));
255         }
256         resultSet.close();
257     }
258 
259     private void fetchExtentOfOwnershipType() throws SQLException {
260         Statement statement = connection.createStatement();
261         ResultSet resultSet = statement.executeQuery("SELECT TYPE_OWNERSHIP_ID, TYPE_OWNERSHIP_CD,TYPE_OWNERSHIP_NM  from OLE_CAT_TYPE_OWNERSHIP_T");
262         while (resultSet.next()) {
263             extentOfOwnershipTypeMap.put(resultSet.getString("TYPE_OWNERSHIP_ID"), resultSet.getString("TYPE_OWNERSHIP_CD") + "|" + resultSet.getString("TYPE_OWNERSHIP_NM"));
264         }
265         resultSet.close();
266     }
267     public void fetchHoldingItems() throws SQLException {
268         Statement statement = connection.createStatement();
269         holdingItemResultSet = statement.executeQuery("SELECT * FROM OLE_DS_ITEM_HOLDINGS_T");
270 
271     }
272 
273     public Map<String, List> fetchHoldingItem(boolean cursor) throws SQLException {
274 
275         String holdingId = "";
276         String tempHoldingId = "";
277         List itemIds = new ArrayList();
278 
279         Map<String, List> map = null;
280         if (cursor) {
281             while (holdingItemResultSet.next()) {
282                 holdingId = "who-" + holdingItemResultSet.getString("HOLDINGS_ID");
283                 if (StringUtils.isNotEmpty(tempHoldingId) && !tempHoldingId.equals(holdingId)) {
284                     if(itemIds.size() > 0) {
285                         map = new HashMap<>();
286                         map.put(tempHoldingId, itemIds);
287                         return map;
288                     }
289                 }
290                 itemIds.add("wio-" + holdingItemResultSet.getString("ITEM_ID"));
291                 tempHoldingId = holdingId;
292             }
293 
294         } else {
295             holdingItemResultSet.previous();
296 //           while(holdingItemResultSet.previous()) {
297 //               break;
298 //           }
299             while (holdingItemResultSet.next()) {
300                 holdingId = "who-" + holdingItemResultSet.getString("HOLDINGS_ID");
301                 if (StringUtils.isNotEmpty(tempHoldingId) && !tempHoldingId.equals(holdingId)) {
302                     if(itemIds.size() > 0) {
303                         map = new HashMap<>();
304                         map.put(tempHoldingId, itemIds);
305                         return map;
306                     }
307 
308                 }
309                 itemIds.add("wio-" + holdingItemResultSet.getString("ITEM_ID"));
310                 tempHoldingId = holdingId;
311             }
312 
313         }
314 
315         if (itemIds.size() > 0) {
316             map = new HashMap<>();
317             map.put(tempHoldingId, itemIds);
318             return map;
319         } else {
320             map = null;
321         }
322         holdingItemResultSet.close();
323         return map;
324 
325     }
326 
327 
328     public void fetchBibHoldings() throws SQLException {
329         Statement statement = connection.createStatement();
330         bibHoldingsResultSet = statement.executeQuery("SELECT * FROM OLE_DS_BIB_HOLDINGS_T");
331 
332     }
333 
334 
335     public Map<String, List> fetchBibHolding(boolean cursor) throws SQLException {
336         String holdingId = "";
337         String tempHoldingId = "";
338         List bibIds = new ArrayList();
339         Map<String, List> map = null;
340 
341         if(cursor) {
342             while (bibHoldingsResultSet.next()) {
343                 holdingId = "who-"+bibHoldingsResultSet.getString("HOLDINGS_ID");
344                 if(StringUtils.isNotEmpty(tempHoldingId) && !tempHoldingId.equals(holdingId)) {
345 
346                     if(bibIds.size() > 0) {
347                         map = new HashMap<>();
348                         bibIds.remove(0);
349                         map.put(tempHoldingId, bibIds);
350                         return map;
351                     }
352                 }
353 
354                 bibIds.add("wbm-"+bibHoldingsResultSet.getString("BIB_ID"));
355                 tempHoldingId = holdingId;
356             }
357 
358         } else {
359             while (bibHoldingsResultSet.next()) {
360                 holdingId = "who-"+bibHoldingsResultSet.getString("HOLDINGS_ID");
361                 if(StringUtils.isNotEmpty(tempHoldingId) && !tempHoldingId.equals(holdingId)) {
362 
363                     map = new HashMap<>();
364                     map.put(tempHoldingId, bibIds);
365                     return map;
366                 }
367                 bibIds.add("wbm-"+bibHoldingsResultSet.getString("BIB_ID"));
368                 tempHoldingId = holdingId;
369 
370             }
371 
372 
373         }
374 
375         if(bibIds.size() > 0) {
376             map = new HashMap<>();
377             bibIds.remove(0);
378             map.put(tempHoldingId, bibIds);
379             return map;
380         }
381         else {
382             map = null;
383         }
384         bibHoldingsResultSet.close();
385         return map;
386     }
387 
388     public synchronized BibTrees fetchNextBatch(int batchSize, BatchStatistics batchStatistics, Boolean isBibOnly) throws Exception {
389         return fetchResultSet(batchSize, batchStatistics, isBibOnly);
390     }
391 
392     public synchronized BibTrees fetchNextBatch(int batchSize, BatchStatistics batchStatistics) throws Exception {
393         return fetchResultSet(batchSize, batchStatistics, false);
394     }
395 
396     private BibTrees fetchResultSet(int batchSize, BatchStatistics batchStatistics, Boolean isBibOnly) throws Exception {
397         StopWatch stopWatch = new StopWatch();
398         stopWatch.start();
399         BibTrees bibTrees = new BibTrees();
400         int count = 0;
401         if (batchSize <= 0) {
402             return bibTrees;
403         }
404         while (bibResultSet.next()) {
405             count++;
406             BibTree bibTree = new BibTree();
407             Bib bib = fetchBibRecord();
408             bibTree.setBib(bib);
409             if (!isBibOnly) {
410                 List<HoldingsTree> holdingsList = fetchHoldingsTreeForBib(Integer.parseInt(bib.getLocalId()));
411                 bibTree.getHoldingsTrees().addAll(holdingsList);
412                 batchStatistics.addHoldingsCount(holdingsList.size());
413 
414                 for (HoldingsTree holdingsTree : holdingsList) {
415                     batchStatistics.addItemCount(holdingsTree.getItems().size());
416                 }
417             }
418 
419             bibTrees.getBibTrees().add(bibTree);
420             if (count == batchSize) {
421                 break;
422             }
423         }
424         stopWatch.stop();
425         batchStatistics.addTimeTaken(stopWatch.getTotalTimeMillis());
426         batchStatistics.addBibCount(bibTrees.getBibTrees().size());
427         return bibTrees;
428     }
429 
430     private Bib fetchBibRecord() throws SQLException {
431         Bib bib = new BibMarc();
432         bib.setCreatedBy(bibResultSet.getString("CREATED_BY"));
433         bib.setCreatedOn(bibResultSet.getString("DATE_CREATED"));
434         if(bibResultSet.getString("STAFF_ONLY") !=null){
435             bib.setStaffOnly((bibResultSet.getString("STAFF_ONLY").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE));
436         }
437         bib.setContent(bibResultSet.getString("CONTENT"));
438         bib.setUpdatedBy(bibResultSet.getString("UPDATED_BY"));
439         bib.setUpdatedOn(bibResultSet.getString("DATE_UPDATED"));
440         bib.setStatus(bibResultSet.getString("STATUS"));
441         bib.setStatusUpdatedBy(bibResultSet.getString("STATUS_UPDATED_BY"));
442         bib.setStatusUpdatedOn(bibResultSet.getString("STATUS_UPDATED_DATE"));
443         bib.setLastUpdated(bibResultSet.getString("DATE_UPDATED"));
444         String uuid = bibResultSet.getString("UNIQUE_ID_PREFIX") + "-" + bibResultSet.getString(1);
445         bib.setId(uuid);
446         bib.setLocalId(bibResultSet.getString(1));
447         return bib;
448     }
449 
450 
451     public List<HoldingsTree> fetchHoldingsTreeForBib(int bibId) throws Exception {
452         List<HoldingsTree> holdingsTrees = new ArrayList<HoldingsTree>();
453 
454         holdingsPreparedStatement.setInt(1, bibId);
455         ResultSet resultSet = holdingsPreparedStatement.executeQuery();
456         Map<String, HoldingsTree> map = new HashMap<>();
457         Map<String, ExtentOfOwnership> extentOfOwnershipMap = new HashMap<>();
458         Set<String> uriSet = null;
459         Set<String> noteSet = null;
460         Set<String> donorSet = null;
461         Set<String> coverageSet = null;
462         Set<String> perpetualSet = null;
463         Set<String> extentOfOwnershipNoteSet = null;
464         Set<String> linkSet = null;
465 
466         while (resultSet.next()) {
467             String holdingsType = resultSet.getString("HOLDINGS_TYPE");
468             String id = resultSet.getString("HOLDINGS_ID");
469             OleHoldings oleHoldings = null;
470             if (map.containsKey(id)) {
471                 oleHoldings = map.get(id).getHoldings().getContentObject();
472             } else {
473                 oleHoldings = new OleHoldings();
474                 Location location = getLocationDetails(resultSet.getString("LOCATION"), resultSet.getString("LOCATION_LEVEL"));
475                 oleHoldings.setLocation(location);
476                 oleHoldings.setHoldingsType(holdingsType);
477                 CallNumber callNumber = new CallNumber();
478                 callNumber.setNumber(resultSet.getString("CALL_NUMBER"));
479                 callNumber.setPrefix(resultSet.getString("CALL_NUMBER_PREFIX"));
480                 ShelvingOrder shelvingOrder = new ShelvingOrder();
481                 if (resultSet.getString("SHELVING_ORDER") != null && !"null".equals(resultSet.getString("SHELVING_ORDER"))) {
482                     shelvingOrder.setCodeValue(resultSet.getString("SHELVING_ORDER"));
483                     shelvingOrder.setFullValue(resultSet.getString("SHELVING_ORDER"));
484                 }
485                 callNumber.setShelvingOrder(shelvingOrder);
486                 ShelvingScheme shelvingScheme = new ShelvingScheme();
487                 if (resultSet.getString("CALL_NUMBER_TYPE_ID") != null) {
488                     String[] strings = callNumberType.get(resultSet.getString("CALL_NUMBER_TYPE_ID")).split("[|]");
489                     shelvingScheme.setCodeValue(strings[0]);
490                     shelvingScheme.setFullValue(strings[1]);
491                 }
492                 callNumber.setShelvingScheme(shelvingScheme);
493                 oleHoldings.setCallNumber(callNumber);
494                 oleHoldings.setCopyNumber(resultSet.getString("COPY_NUMBER"));
495                 HoldingsTree holdingsTree = new HoldingsTree();
496 
497                 Holdings holdings = null;
498 
499                 if (holdingsType.equalsIgnoreCase(PHoldings.PRINT)) {
500                     holdings = new PHoldings();
501 
502                     if (resultSet.getString("RECEIPT_STATUS_ID") != null) {
503                         oleHoldings.setReceiptStatus(resultSet.getString("RECEIPT_STATUS_ID"));
504                     }
505                     extentOfOwnershipNoteSet = new HashSet<>();
506                     donorSet = coverageSet = perpetualSet = null;
507                 } else {
508                     holdings = new EHoldings();
509                     oleHoldings.setAccessStatus(resultSet.getString("ACCESS_STATUS"));
510                     oleHoldings.setImprint(resultSet.getString("IMPRINT"));
511                     Platform platform = new Platform();
512                     platform.setPlatformName(resultSet.getString("PLATFORM"));
513                     platform.setAdminUrl(resultSet.getString("ADMIN_URL"));
514                     platform.setAdminUserName(resultSet.getString("ADMIN_USERNAME"));
515                     platform.setAdminPassword(resultSet.getString("ADMIN_PASSWORD"));
516                     oleHoldings.setPlatform(platform);
517 
518                     oleHoldings.setPublisher(resultSet.getString("PUBLISHER"));
519                     HoldingsAccessInformation holdingsAccessInformation = new HoldingsAccessInformation();
520 
521                     holdingsAccessInformation.setProxiedResource(resultSet.getString("PROXIED_RESOURCE"));
522                     holdingsAccessInformation.setAccessUsername(resultSet.getString("ACCESS_USERNAME"));
523                     holdingsAccessInformation.setAccessPassword(resultSet.getString("ACCESS_PASSWORD"));
524                     holdingsAccessInformation.setNumberOfSimultaneousUser(resultSet.getString("NUMBER_SIMULT_USERS"));
525                     holdingsAccessInformation.setAccessLocation(resultSet.getString("CODE"));
526                     holdingsAccessInformation.setAuthenticationType(resultSet.getString("AUTHENTICATION_TYPE_ID"));
527                     oleHoldings.setHoldingsAccessInformation(holdingsAccessInformation);
528                     String statisticalSearchId = resultSet.getString("STAT_SEARCH_CODE_ID");
529                     if(StringUtils.isNotEmpty(statisticalSearchId)) {
530                         String[] strings = statisticalSearchCodeMap.get(statisticalSearchId).split("[|]");
531                         StatisticalSearchingCode statisticalSearchingCode = new StatisticalSearchingCode();
532                         statisticalSearchingCode.setCodeValue(strings[0]);
533                         statisticalSearchingCode.setFullValue(strings[1]);
534                         oleHoldings.setStatisticalSearchingCode(statisticalSearchingCode);
535                     }
536                     oleHoldings.setLocalPersistentLink(resultSet.getString("LOCAL_PERSISTENT_URI"));
537                     oleHoldings.setSubscriptionStatus(resultSet.getString("SUBSCRIPTION_STATUS"));
538                     oleHoldings.setInterLibraryLoanAllowed(Boolean.valueOf(resultSet.getString("ALLOW_ILL")));
539                     coverageSet = new HashSet<>();
540                     perpetualSet = new HashSet<>();
541                     donorSet = new HashSet<>();
542                     ExtentOfOwnership extentOfOwnership = new ExtentOfOwnership();
543                     Coverages coverages = new Coverages();
544                     PerpetualAccesses perpetualAccesses = new PerpetualAccesses();
545                     extentOfOwnership.setCoverages(coverages);
546                     extentOfOwnership.setPerpetualAccesses(perpetualAccesses);
547                     oleHoldings.getExtentOfOwnership().add(extentOfOwnership);
548 
549                 }
550                 holdings.setHoldingsType(holdingsType);
551                 holdings.setId("who-" + id);
552                 holdings.setContentObject(oleHoldings);
553                 holdings.setCreatedBy(resultSet.getString("CREATED_BY"));
554                 holdings.setCreatedOn(resultSet.getString("DATE_CREATED"));
555                 if(resultSet.getString("STAFF_ONLY") !=null){
556                     holdings.setStaffOnly((resultSet.getString("STAFF_ONLY").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE));
557                 }
558                 holdings.setUpdatedBy(resultSet.getString("UPDATED_BY"));
559                 holdings.setUpdatedOn(resultSet.getString("DATE_UPDATED"));
560                 holdings.setLastUpdated(resultSet.getString("DATE_UPDATED"));
561 
562                 uriSet = new HashSet<>();
563                 noteSet = new HashSet<>();
564                 linkSet = new HashSet<>();
565                 List<Item> itemList = fetchItemForHoldings(Integer.parseInt(id));
566                 holdingsTree.setHoldings(holdings);
567                 holdingsTree.getItems().addAll(itemList);
568 
569                 map.put(id, holdingsTree);
570 
571                 holdingsTrees.add(holdingsTree);
572             }
573 
574             if(StringUtils.isNotEmpty(holdingsType) && holdingsType.equalsIgnoreCase(PHoldings.PRINT)) {
575                 if (uriSet.add(resultSet.getString("HOLDINGS_URI_ID"))) {
576                     Uri uri = new Uri();
577                     uri.setValue(resultSet.getString("TEXT"));
578                     oleHoldings.getUri().add(uri);
579                 }
580                 ExtentOfOwnership extentOfOwnership = null;
581                 if (extentOfOwnershipMap.containsKey(resultSet.getString("EXT_OWNERSHIP_ID"))) {
582                     extentOfOwnership = extentOfOwnershipMap.get(resultSet.getString("EXT_OWNERSHIP_ID"));
583                 } else {
584                     extentOfOwnership = new ExtentOfOwnership();
585                     if(StringUtils.isNotEmpty(resultSet.getString("EXT_OWNERSHIP_TYPE_ID"))) {
586                         String[] strings = extentOfOwnershipTypeMap.get(resultSet.getString("EXT_OWNERSHIP_TYPE_ID")).split("[|]");
587                         extentOfOwnership.setType(strings[1]);
588                     }
589                     extentOfOwnershipMap.put(resultSet.getString("EXT_OWNERSHIP_ID"), extentOfOwnership);
590                     oleHoldings.getExtentOfOwnership().add(extentOfOwnership);
591                 }
592                 String extOwnershipNoteId = resultSet.getString("EXT_OWNERSHIP_NOTE_ID");
593                 if (extentOfOwnershipNoteSet != null && StringUtils.isNotEmpty(extOwnershipNoteId) && extentOfOwnershipNoteSet.add(resultSet.getString("EXT_OWNERSHIP_NOTE_ID"))) {
594                     Note note = new Note();
595                     note.setValue(resultSet.getString(83));
596                     note.setType(resultSet.getString(82));
597 
598                     extentOfOwnership.getNote().add(note);
599                 }
600             }
601             else {
602                 if (linkSet.add(resultSet.getString("HOLDINGS_URI_ID"))) {
603                     Link link = new Link();
604                     link.setUrl(resultSet.getString("URI"));
605                     link.setText(resultSet.getString("TEXT"));
606                     oleHoldings.getLink().add(link);
607                 }
608                 if(oleHoldings.getExtentOfOwnership() != null && oleHoldings.getExtentOfOwnership().size() > 0) {
609                     if (coverageSet != null && coverageSet.add(resultSet.getString("HOLDINGS_COVERAGE_ID"))) {
610                         Coverage coverage = new Coverage();
611                         coverage.setCoverageStartIssue(resultSet.getString("COVERAGE_START_ISSUE"));
612                         coverage.setCoverageStartDate(resultSet.getString("COVERAGE_START_DATE"));
613                         coverage.setCoverageStartVolume(resultSet.getString("COVERAGE_START_VOLUME"));
614                         coverage.setCoverageEndIssue(resultSet.getString("HOLDINGS_COVERAGE_ID"));
615                         coverage.setCoverageEndDate(resultSet.getString("COVERAGE_END_DATE"));
616                         coverage.setCoverageEndVolume(resultSet.getString("COVERAGE_END_VOLUME"));
617                         oleHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().add(coverage);
618                     }
619 
620                     if (perpetualSet != null && perpetualSet.add(resultSet.getString("HOLDINGS_PERPETUAL_ACCESS_ID"))) {
621                         PerpetualAccess perpetualAccess = new PerpetualAccess();
622                         perpetualAccess.setPerpetualAccessStartDate(resultSet.getString("PERPETUAL_ACCESS_START_DATE"));
623                         perpetualAccess.setPerpetualAccessStartIssue(resultSet.getString("PERPETUAL_ACCESS_START_ISSUE"));
624                         perpetualAccess.setPerpetualAccessStartVolume(resultSet.getString("PERPETUAL_ACCESS_START_VOLUME"));
625                         perpetualAccess.setPerpetualAccessEndDate(resultSet.getString("PERPETUAL_ACCESS_END_DATE"));
626                         perpetualAccess.setPerpetualAccessEndVolume(resultSet.getString("PERPETUAL_ACCESS_END_VOLUME"));
627                         perpetualAccess.setPerpetualAccessEndIssue(resultSet.getString("PERPETUAL_ACCESS_END_ISSUE"));
628                         oleHoldings.getExtentOfOwnership().get(0).getPerpetualAccesses().getPerpetualAccess().add(perpetualAccess);
629                     }
630                 }
631                 if (donorSet != null && donorSet.add(resultSet.getString("HOLDINGS_DONOR_ID"))) {
632                     DonorInfo donorInfo = new DonorInfo();
633                     donorInfo.setDonorCode(resultSet.getString("DONOR_CODE"));
634                     donorInfo.setDonorNote(resultSet.getString("DONOR_NOTE"));
635                     donorInfo.setDonorPublicDisplay(resultSet.getString("DONOR_DISPLAY_NOTE"));
636                     oleHoldings.getDonorInfo().add(donorInfo);
637                 }
638             }
639 
640             if (noteSet.add(resultSet.getString("HOLDINGS_NOTE_ID"))) {
641                 Note note = new Note();
642                 note.setValue(resultSet.getString("NOTE"));
643                 note.setType(resultSet.getString("TYPE"));
644                 oleHoldings.getNote().add(note);
645             }
646         }
647         resultSet.close();
648         return holdingsTrees;
649     }
650 
651     public Location getLocationDetails(String locationName, String locationLevelName) {
652         Location location = new Location();
653         LocationLevel locationLevel = createLocationLevel(locationName, locationLevelName);
654         location.setLocationLevel(locationLevel);
655         return location;
656     }
657 
658     public LocationLevel createLocationLevel(String locationName, String locationLevelName) {
659         LocationLevel locationLevel = null;
660         if (StringUtils.isNotEmpty(locationName) && StringUtils.isNotEmpty(locationLevelName)) {
661             String[] locations = locationName.split("/");
662             String[] locationLevels = locationLevelName.split("/");
663             String locName = "";
664             String levelName = "";
665             if (locations.length > 0) {
666                 locName = locations[0];
667                 levelName = locationLevels[0];
668                 if (locationName.contains("/")) {
669                     locationName = locationName.replaceFirst(locations[0] + "/", "");
670                 } else {
671                     locationName = locationName.replace(locations[0], "");
672                 }
673 
674                 if (locationLevelName.contains("/")) {
675                     locationLevelName = locationLevelName.replaceFirst(locationLevels[0] + "/", "");
676                 } else {
677                     locationLevelName = locationLevelName.replace(locationLevels[0], "");
678                 }
679                 if (locName != null && locations.length != 0) {
680                     locationLevel = new LocationLevel();
681                     locationLevel.setLevel(levelName);
682                     locationLevel.setName(locName);
683                     locationLevel.setLocationLevel(createLocationLevel(locationName, locationLevelName));
684                 }
685             }
686         }
687         return locationLevel;
688     }
689 
690 
691     public List<Item> fetchItemForHoldings(int holdingsId) throws Exception {
692         List<Item> itemList = new ArrayList<Item>();
693         Map<String, Item> itemHashMap = new HashMap<>();
694         itemPreparedStatement.setInt(1, holdingsId);
695         ResultSet resultSet = itemPreparedStatement.executeQuery();
696         Set<String> highDensityStorageSet = null;
697         Set<String> donorNoteSet = null;
698         Set<String> itemNoteSet = null;
699         Set<String> statisticalSearchSet = null;
700         org.kuali.ole.docstore.common.document.content.instance.Item itemObj = null;
701 
702         while (resultSet.next()) {
703             String id = resultSet.getString("ITEM_ID");
704             if (itemHashMap.containsKey(id)) {
705                 itemObj = (org.kuali.ole.docstore.common.document.content.instance.Item) itemHashMap.get(id).getContentObject();
706             } else {
707                 itemObj = new org.kuali.ole.docstore.common.document.content.instance.Item();
708                 Item item = new Item();
709                 itemList.add(item);
710                 item.setId("wio-" + resultSet.getString("ITEM_ID"));
711                 item.setContentObject(itemObj);
712                 Location location = getLocationDetails(resultSet.getString("LOCATION"), resultSet.getString("LOCATION_LEVEL"));
713                 itemObj.setLocation(location);
714                 CallNumber callNumber = new CallNumber();
715                 callNumber.setNumber(resultSet.getString("CALL_NUMBER"));
716                 callNumber.setPrefix(resultSet.getString("CALL_NUMBER_PREFIX"));
717                 ShelvingOrder shelvingOrder = new ShelvingOrder();
718                 if (resultSet.getString("SHELVING_ORDER") != null && !"null".equals(resultSet.getString("SHELVING_ORDER"))) {
719                     shelvingOrder.setCodeValue(resultSet.getString("SHELVING_ORDER"));
720                     shelvingOrder.setFullValue(resultSet.getString("SHELVING_ORDER"));
721                 }
722                 callNumber.setShelvingOrder(shelvingOrder);
723                 ShelvingScheme shelvingScheme = new ShelvingScheme();
724                 if (callNumberType.get(resultSet.getString("CALL_NUMBER_TYPE_ID")) != null) {
725                     String[] strings = callNumberType.get(resultSet.getString("CALL_NUMBER_TYPE_ID")).split("[|]");
726                     shelvingScheme.setCodeValue(strings[0]);
727                     shelvingScheme.setFullValue(strings[1]);
728                 }
729                 callNumber.setShelvingScheme(shelvingScheme);
730                 itemObj.setCallNumber(callNumber);
731                 itemObj.setBarcodeARSL(resultSet.getString("BARCODE_ARSL"));
732                 itemObj.setEnumeration(resultSet.getString("ENUMERATION"));
733                 itemObj.setChronology(resultSet.getString("CHRONOLOGY"));
734                 itemObj.setCopyNumber(resultSet.getString("COPY_NUMBER"));
735                 AccessInformation accessInformation = new AccessInformation();
736                 accessInformation.setBarcode(resultSet.getString("BARCODE"));
737                 Uri uri = new Uri();
738                 uri.setValue(resultSet.getString("URI"));
739                 accessInformation.setUri(uri);
740                 itemObj.setAccessInformation(accessInformation);
741                 itemObj.setPurchaseOrderLineItemIdentifier(resultSet.getString("PURCHASE_ORDER_LINE_ITEM_ID"));
742                 itemObj.setVendorLineItemIdentifier(resultSet.getString("VENDOR_LINE_ITEM_ID"));
743                 itemObj.setFund(resultSet.getString("FUND"));
744                 itemObj.setPrice(resultSet.getString("PRICE"));
745                 itemObj.setItemStatusEffectiveDate(convertDateFormat(resultSet.getString("ITEM_STATUS_DATE_UPDATED")));
746                 if(resultSet.getString("FAST_ADD") != null){
747                     itemObj.setFastAddFlag(resultSet.getString("FAST_ADD").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE);
748                 }
749                 itemObj.setCheckinNote(resultSet.getString("CHECK_IN_NOTE"));
750                 if(resultSet.getString("CLAIMS_RETURNED") != null) {
751                     itemObj.setClaimsReturnedFlag(resultSet.getString("CLAIMS_RETURNED").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE);
752                 }
753                 String claimsReturnFlagCreatedDate = resultSet.getString("CLAIMS_RETURNED_DATE_CREATED");
754                 itemObj.setClaimsReturnedFlagCreateDate(convertDateFormat(claimsReturnFlagCreatedDate));
755                 itemObj.setClaimsReturnedNote(resultSet.getString("CLAIMS_RETURNED_NOTE"));
756                 itemObj.setCurrentBorrower(resultSet.getString("CURRENT_BORROWER"));
757                 itemObj.setProxyBorrower(resultSet.getString("PROXY_BORROWER"));
758                 String dueDateTime = resultSet.getString("DUE_DATE_TIME");
759                 itemObj.setDueDateTime(convertDateFormat(dueDateTime));
760                 String checkOutDateTime = resultSet.getString("CHECK_OUT_DATE_TIME");
761                 itemObj.setCheckOutDateTime(convertDateFormat(checkOutDateTime));
762                 itemObj.setDamagedItemNote(resultSet.getString("ITEM_DAMAGED_NOTE"));
763                 if (resultSet.getString("ITEM_DAMAGED_STATUS") != null) {
764                     itemObj.setItemDamagedStatus(resultSet.getString("ITEM_DAMAGED_STATUS").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE);
765                 }
766                 if(resultSet.getString("MISSING_PIECES") !=null) {
767                     itemObj.setMissingPieceFlag(resultSet.getString("MISSING_PIECES").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE);
768                 }
769                 itemObj.setMissingPiecesCount(resultSet.getString("MISSING_PIECES_COUNT"));
770                 itemObj.setMissingPieceFlagNote(resultSet.getString("MISSING_PIECES_NOTE"));
771                 itemObj.setMissingPieceEffectiveDate(resultSet.getString("MISSING_PIECES_EFFECTIVE_DATE"));
772                 itemObj.setNumberOfPieces(resultSet.getString("NUM_PIECES"));
773                 itemObj.setNumberOfRenew(resultSet.getInt("NUM_OF_RENEW"));
774                 highDensityStorageSet = new HashSet<>();
775                 itemNoteSet = new HashSet<>();
776                 statisticalSearchSet = new HashSet<>();
777                 donorNoteSet = new HashSet<>();
778 
779                 ItemStatus itemStatus = new ItemStatus();
780                 if (itemStatusMap.containsKey(resultSet.getString("ITEM_STATUS_ID"))) {
781                     String[] strings = itemStatusMap.get(resultSet.getString("ITEM_STATUS_ID")).split("[|]");
782                     itemStatus.setCodeValue(strings[0]);
783                     itemStatus.setFullValue(strings[1]);
784                 }
785 
786                 itemObj.setItemStatus(itemStatus);
787                 ItemType itemType = new ItemType();
788                 if (itemTypeMap.containsKey(resultSet.getString("ITEM_TYPE_ID"))) {
789                     String[] strings = itemTypeMap.get(resultSet.getString("ITEM_TYPE_ID")).split("[|]");
790                     itemType.setCodeValue(strings[0]);
791                     itemType.setFullValue(strings[1]);
792                 }
793                 itemObj.setItemType(itemType);
794                 ItemType tempItemType = new ItemType();
795                 if (itemTypeMap.containsKey(resultSet.getString("TEMP_ITEM_TYPE_ID"))) {
796                     String[] strings = itemTypeMap.get(resultSet.getString("TEMP_ITEM_TYPE_ID")).split("[|]");
797                     tempItemType.setCodeValue(strings[0]);
798                     tempItemType.setFullValue(strings[1]);
799                 }
800                 itemObj.setTemporaryItemType(tempItemType);
801                 item.setContentObject(itemObj);
802                 item.setCreatedBy(resultSet.getString("CREATED_BY"));
803                 item.setCreatedOn(resultSet.getString("DATE_CREATED"));
804                 if (resultSet.getString("STAFF_ONLY") != null) {
805                     item.setStaffOnly((resultSet.getString("STAFF_ONLY").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE));
806                 }
807                 item.setUpdatedBy(resultSet.getString("UPDATED_BY"));
808                 item.setUpdatedOn(resultSet.getString("DATE_UPDATED"));
809                 item.setLastUpdated(resultSet.getString("DATE_UPDATED"));
810 
811             }
812             if (itemNoteSet.add(resultSet.getString("ITEM_NOTE_ID"))) {
813                 Note note = new Note();
814                 note.setValue(resultSet.getString("NOTE"));
815                 note.setType(resultSet.getString("TYPE"));
816                 itemObj.getNote().add(note);
817             }
818             if (resultSet.getString("STAT_SEARCH_CODE_ID") != null && statisticalSearchSet.add(resultSet.getString("STAT_SEARCH_CODE_ID"))) {
819                 String[] strings = statisticalSearchCodeMap.get(resultSet.getString("STAT_SEARCH_CODE_ID")).split("[|]");
820                 StatisticalSearchingCode statisticalSearchingCode = new StatisticalSearchingCode();
821                 statisticalSearchingCode.setCodeValue(strings[0]);
822                 statisticalSearchingCode.setFullValue(strings[1]);
823                 itemObj.getStatisticalSearchingCode().add(statisticalSearchingCode);
824             }
825             if (donorNoteSet.add(resultSet.getString("ITEM_DONOR_ID"))) {
826                 DonorInfo donorInfo = new DonorInfo();
827                 donorInfo.setDonorCode(resultSet.getString("DONOR_CODE"));
828                 donorInfo.setDonorNote(resultSet.getString("DONOR_NOTE"));
829                 donorInfo.setDonorPublicDisplay(resultSet.getString("DONOR_DISPLAY_NOTE"));
830                 itemObj.getDonorInfo().add(donorInfo);
831             }
832             if (highDensityStorageSet.add(resultSet.getString("ITEM_DONOR_ID"))) {
833                 HighDensityStorage highDensityStorage = new HighDensityStorage();
834                 highDensityStorage.setRow(resultSet.getString("HIGH_DENSITY_ROW"));
835                 itemObj.setHighDensityStorage(highDensityStorage);
836             }
837         }
838         resultSet.close();
839         return itemList;
840     }
841 
842     public String convertDateFormat(String date) {
843         String convertedDate = "";
844         if (date != null && !date.isEmpty()) {
845             SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
846             SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
847             Date itemDate = null;
848             try {
849                 itemDate = format2.parse(date);
850             } catch (ParseException e) {
851                 LOG.error("format string to Date " + e);
852             }
853             convertedDate = format1.format(itemDate).toString();
854         }
855         return convertedDate;
856     }
857 
858 
859     public String getTotalNoOfRecords() throws SQLException {
860         String totalRecords = "0";
861         Connection connection = getConnection();
862         PreparedStatement preparedStatement =null;
863         if(isStaffOnly){
864              preparedStatement = connection.prepareStatement(bibCountQuery);
865         }else{
866             preparedStatement = connection.prepareStatement(bibCountQuery+staffOnly);
867         }
868 
869         ResultSet resultSet = preparedStatement.executeQuery();
870         while (resultSet.next()) {
871             totalRecords = resultSet.getString("totalRecords");
872         }
873         preparedStatement.close();
874         resultSet.close();
875         connection.close();
876         return totalRecords;
877     }
878 
879     public static void writeStatusToFile(String directoryPath, String fileName, String content) {
880         try {
881             String fileSeparator = File.separator;
882             Date date = new Date();
883             FileWriter fw = new FileWriter(directoryPath + fileSeparator + fileName, true);
884             fw.write("\n");
885             fw.write("******************************************************************");
886             fw.write("\n");
887             fw.write(date.toString());
888             fw.write("\n");
889             fw.write(content);
890             fw.write("\n");
891             fw.write("******************************************************************");
892             fw.write("\n");
893             fw.close();
894         } catch (IOException ioe) {
895             System.err.println("IOException: " + ioe.getMessage());
896         }
897     }
898 
899     public static void writeStatusToFile(String directoryPath, String fileName, String content, String first, String last) {
900         try {
901             String fileSeparator = File.separator;
902             FileWriter fw = new FileWriter(directoryPath + fileSeparator + fileName, true);
903             fw.write("\n");
904             fw.write("******************************************************************");
905             fw.write("\n");
906             fw.write(content);
907             fw.write("\n");
908             fw.write("Batch start id :" + first);
909             fw.write("\n");
910             fw.write("Batch end id :" + last);
911             fw.write("\n");
912             fw.write("******************************************************************");
913             fw.write("\n");
914             fw.close();
915         } catch (IOException ioe) {
916             System.err.println("IOException: " + ioe.getMessage());
917         }
918     }
919 
920 
921     public int storeBibInfo(int batchSize, String filePath, String fileName, BibInfoStatistics bibInfoStatistics, int batchNo) throws SQLException {
922 
923         StopWatch stopWatch = new StopWatch();
924         stopWatch.start();
925         int count = 0;
926         BibMarcUtil bibMarcUtil = new BibMarcUtil();
927 
928         while (bibResultSet.next()) {
929             count++;
930 
931             int bibId = 0;
932             int bibId2 = 0;
933             String bibIdStr = "";
934 
935 
936             try {
937 
938                 bibId = bibResultSet.getInt("BIB_ID");
939                 bibId2 = bibResultSet.getInt("BIB_ID");
940                 bibIdStr = bibResultSet.getString("UNIQUE_ID_PREFIX") + "-" + bibId;
941 
942                 if (bibId != bibId2) {
943                     LOG.error("bibId is not equal to bibId2: bibId = " + bibId + "; bibId2 = " + bibId2);
944                 }
945 
946 
947                 BibMarcRecords bibMarcRecords = bibMarcRecordProcessor.fromXML(bibResultSet.getString("CONTENT"));
948 
949                 if (bibMarcRecords != null && bibMarcRecords.getRecords() != null && bibMarcRecords.getRecords().size() > 0) {
950 
951                     Map<String, String> dataFields = bibMarcUtil.buildDataValuesForBibInfo(bibMarcRecords.getRecords().get(0));
952                     String title = dataFields.get(BibMarcUtil.TITLE_DISPLAY);
953                     String author = dataFields.get(BibMarcUtil.AUTHOR_DISPLAY);
954                     String publisher = dataFields.get(BibMarcUtil.PUBLISHER_DISPLAY);
955                     String isbn = dataFields.get(BibMarcUtil.ISBN_DISPLAY);
956                     String issn = dataFields.get(BibMarcUtil.ISSN_DISPLAY);
957 
958                     String commonIdentifier = "";
959                     if (StringUtils.isNotEmpty(isbn)) {
960                         commonIdentifier = isbn;
961                     } else {
962                         commonIdentifier = issn;
963                     }
964 
965                     bibInsertPreparedStatement.setInt(1, bibId);
966                     bibInsertPreparedStatement.setString(2, bibIdStr);
967                     bibInsertPreparedStatement.setString(3, truncateData(title, 4000));
968                     bibInsertPreparedStatement.setString(4, truncateData(author, 4000));
969                     bibInsertPreparedStatement.setString(5, truncateData(publisher, 4000));
970                     bibInsertPreparedStatement.setString(6, truncateData(commonIdentifier, 100));
971 
972 
973                     try {
974                         bibInsertPreparedStatement.executeUpdate();
975                     } catch (Exception e) {
976                         if (e.getMessage().startsWith("Duplicate entry")) {
977 
978                             bibUpdatePreparedStatement.setString(1, truncateData(title, 4000));
979                             bibUpdatePreparedStatement.setString(2, truncateData(author, 4000));
980                             bibUpdatePreparedStatement.setString(3, truncateData(publisher, 4000));
981                             bibUpdatePreparedStatement.setString(4, truncateData(commonIdentifier, 100));
982                             bibUpdatePreparedStatement.setString(5, bibIdStr);
983                             bibUpdatePreparedStatement.setInt(6, bibId);
984                             try {
985                                 bibUpdatePreparedStatement.executeUpdate();
986                             } catch (Exception e1) {
987                                 LOG.error("Exception while updating into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : ", e1);
988                                 writeStatusToFile(filePath, fileName, "Exception while updating into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : " + e1.getMessage());
989                             }
990                         } else {
991                             LOG.error("Exception while inserting into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : ", e);
992                             writeStatusToFile(filePath, fileName, "Exception while inserting into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : " + e.getMessage());
993                         }
994                     }
995                 }
996 
997             } catch (Exception e) {
998                 LOG.error("Exception inserting/updating bibId " + bibId + "; bibId2 = " + bibId2 + " BibIdStr = " + bibIdStr, e);
999                 writeStatusToFile(filePath, fileName, "Exception inserting/updating bibId " + bibId + "; bibId2 = " + bibId2 + " BibIdStr = " + bibIdStr + "\t" + e.getMessage());
1000             }
1001             bibInfoStatistics.setBibCount((batchSize * batchNo) + count);
1002             if (count == batchSize) {
1003                 break;
1004             }
1005         }
1006         stopWatch.stop();
1007         connection.commit();
1008         return count;
1009     }
1010 
1011     public static String truncateData(String data, int idLength) {
1012         //TODO: Handle the case of unicode chars where string.length() <> byte length
1013         String truncateData = data;
1014         if (data != null && data.length() > idLength) {
1015             truncateData = data.substring(0, (idLength-1));
1016         }
1017         return truncateData;
1018     }
1019 
1020 }