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