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
28
29
30
31
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
325
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 originalDueDate = resultSet.getString("ORG_DUE_DATE_TIME");
792 itemObj.setOriginalDueDate(convertDateFormat(originalDueDate));
793 String checkOutDateTime = resultSet.getString("CHECK_OUT_DATE_TIME");
794 itemObj.setCheckOutDateTime(convertDateFormat(checkOutDateTime));
795 itemObj.setDamagedItemNote(resultSet.getString("ITEM_DAMAGED_NOTE"));
796 if (resultSet.getString("ITEM_DAMAGED_STATUS") != null) {
797 itemObj.setItemDamagedStatus(resultSet.getString("ITEM_DAMAGED_STATUS").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE);
798 }
799 if(resultSet.getString("MISSING_PIECES") !=null) {
800 itemObj.setMissingPieceFlag(resultSet.getString("MISSING_PIECES").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE);
801 }
802 itemObj.setMissingPiecesCount(resultSet.getString("MISSING_PIECES_COUNT"));
803 itemObj.setMissingPieceFlagNote(resultSet.getString("MISSING_PIECES_NOTE"));
804 itemObj.setMissingPieceEffectiveDate(resultSet.getString("MISSING_PIECES_EFFECTIVE_DATE"));
805 itemObj.setNumberOfPieces(resultSet.getString("NUM_PIECES"));
806 itemObj.setNumberOfRenew(resultSet.getInt("NUM_OF_RENEW"));
807 highDensityStorageSet = new HashSet<>();
808 itemNoteSet = new HashSet<>();
809 statisticalSearchSet = new HashSet<>();
810 donorNoteSet = new HashSet<>();
811
812 ItemStatus itemStatus = new ItemStatus();
813 if (itemStatusMap.containsKey(resultSet.getString("ITEM_STATUS_ID"))) {
814 String[] strings = itemStatusMap.get(resultSet.getString("ITEM_STATUS_ID")).split("[|]");
815 itemStatus.setCodeValue(strings[0]);
816 itemStatus.setFullValue(strings[1]);
817 }
818
819 itemObj.setItemStatus(itemStatus);
820 ItemType itemType = new ItemType();
821 if (itemTypeMap.containsKey(resultSet.getString("ITEM_TYPE_ID"))) {
822 String[] strings = itemTypeMap.get(resultSet.getString("ITEM_TYPE_ID")).split("[|]");
823 itemType.setCodeValue(strings[0]);
824 itemType.setFullValue(strings[1]);
825 }
826 itemObj.setItemType(itemType);
827 ItemType tempItemType = new ItemType();
828 if (itemTypeMap.containsKey(resultSet.getString("TEMP_ITEM_TYPE_ID"))) {
829 String[] strings = itemTypeMap.get(resultSet.getString("TEMP_ITEM_TYPE_ID")).split("[|]");
830 tempItemType.setCodeValue(strings[0]);
831 tempItemType.setFullValue(strings[1]);
832 }
833 itemObj.setTemporaryItemType(tempItemType);
834 item.setContentObject(itemObj);
835 item.setCreatedBy(resultSet.getString("CREATED_BY"));
836 item.setCreatedOn(resultSet.getString("DATE_CREATED"));
837 if (resultSet.getString("STAFF_ONLY") != null) {
838 item.setStaffOnly((resultSet.getString("STAFF_ONLY").equalsIgnoreCase("Y") ? Boolean.TRUE : Boolean.FALSE));
839 }
840 item.setUpdatedBy(resultSet.getString("UPDATED_BY"));
841 item.setUpdatedOn(resultSet.getString("DATE_UPDATED"));
842 item.setLastUpdated(resultSet.getString("DATE_UPDATED"));
843
844 }
845 if (itemNoteSet.add(resultSet.getString("ITEM_NOTE_ID"))) {
846 Note note = new Note();
847 note.setValue(resultSet.getString("NOTE"));
848 note.setType(resultSet.getString("TYPE"));
849 itemObj.getNote().add(note);
850 }
851 if (resultSet.getString("STAT_SEARCH_CODE_ID") != null && statisticalSearchSet.add(resultSet.getString("STAT_SEARCH_CODE_ID"))) {
852 String[] strings = statisticalSearchCodeMap.get(resultSet.getString("STAT_SEARCH_CODE_ID")).split("[|]");
853 StatisticalSearchingCode statisticalSearchingCode = new StatisticalSearchingCode();
854 statisticalSearchingCode.setCodeValue(strings[0]);
855 statisticalSearchingCode.setFullValue(strings[1]);
856 itemObj.getStatisticalSearchingCode().add(statisticalSearchingCode);
857 }
858 if (donorNoteSet.add(resultSet.getString("ITEM_DONOR_ID"))) {
859 DonorInfo donorInfo = new DonorInfo();
860 donorInfo.setDonorCode(resultSet.getString("DONOR_CODE"));
861 donorInfo.setDonorNote(resultSet.getString("DONOR_NOTE"));
862 donorInfo.setDonorPublicDisplay(resultSet.getString("DONOR_DISPLAY_NOTE"));
863 itemObj.getDonorInfo().add(donorInfo);
864 }
865 if (highDensityStorageSet.add(resultSet.getString("ITEM_DONOR_ID"))) {
866 HighDensityStorage highDensityStorage = new HighDensityStorage();
867 highDensityStorage.setRow(resultSet.getString("HIGH_DENSITY_ROW"));
868 itemObj.setHighDensityStorage(highDensityStorage);
869 }
870 }
871 resultSet.close();
872 return itemList;
873 }
874
875 public String convertDateFormat(String date) {
876 String convertedDate = "";
877 if (date != null && !date.isEmpty()) {
878 SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
879 SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
880 Date itemDate = null;
881 try {
882 itemDate = format2.parse(date);
883 } catch (ParseException e) {
884 LOG.error("format string to Date " + e);
885 }
886 convertedDate = format1.format(itemDate).toString();
887 }
888 return convertedDate;
889 }
890
891
892 public String getTotalNoOfRecords() throws SQLException {
893 String totalRecords = "0";
894 Connection connection = getConnection();
895 PreparedStatement preparedStatement =null;
896 if(isStaffOnly){
897 preparedStatement = connection.prepareStatement(bibCountQuery);
898 }else{
899 preparedStatement = connection.prepareStatement(bibCountQuery+staffOnly);
900 }
901
902 ResultSet resultSet = preparedStatement.executeQuery();
903 while (resultSet.next()) {
904 totalRecords = resultSet.getString("totalRecords");
905 }
906 preparedStatement.close();
907 resultSet.close();
908 connection.close();
909 return totalRecords;
910 }
911
912 public static void writeStatusToFile(String directoryPath, String fileName, String content) {
913 try {
914 String fileSeparator = File.separator;
915 Date date = new Date();
916 FileWriter fw = new FileWriter(directoryPath + fileSeparator + fileName, true);
917 fw.write("\n");
918 fw.write("******************************************************************");
919 fw.write("\n");
920 fw.write(date.toString());
921 fw.write("\n");
922 fw.write(content);
923 fw.write("\n");
924 fw.write("******************************************************************");
925 fw.write("\n");
926 fw.close();
927 } catch (IOException ioe) {
928 System.err.println("IOException: " + ioe.getMessage());
929 }
930 }
931
932 public static void writeStatusToFile(String directoryPath, String fileName, String content, String first, String last) {
933 try {
934 String fileSeparator = File.separator;
935 FileWriter fw = new FileWriter(directoryPath + fileSeparator + fileName, true);
936 fw.write("\n");
937 fw.write("******************************************************************");
938 fw.write("\n");
939 fw.write(content);
940 fw.write("\n");
941 fw.write("Batch start id :" + first);
942 fw.write("\n");
943 fw.write("Batch end id :" + last);
944 fw.write("\n");
945 fw.write("******************************************************************");
946 fw.write("\n");
947 fw.close();
948 } catch (IOException ioe) {
949 System.err.println("IOException: " + ioe.getMessage());
950 }
951 }
952
953
954 public int storeBibInfo(int batchSize, String filePath, String fileName, BibInfoStatistics bibInfoStatistics, int batchNo) throws SQLException {
955
956 StopWatch stopWatch = new StopWatch();
957 stopWatch.start();
958 int count = 0;
959 BibMarcUtil bibMarcUtil = new BibMarcUtil();
960
961 while (bibResultSet.next()) {
962 count++;
963
964 int bibId = 0;
965 int bibId2 = 0;
966 String bibIdStr = "";
967
968
969 try {
970
971 bibId = bibResultSet.getInt("BIB_ID");
972 bibId2 = bibResultSet.getInt("BIB_ID");
973 bibIdStr = bibResultSet.getString("UNIQUE_ID_PREFIX") + "-" + bibId;
974
975 if (bibId != bibId2) {
976 LOG.error("bibId is not equal to bibId2: bibId = " + bibId + "; bibId2 = " + bibId2);
977 }
978
979
980 BibMarcRecords bibMarcRecords = bibMarcRecordProcessor.fromXML(bibResultSet.getString("CONTENT"));
981
982 if (bibMarcRecords != null && bibMarcRecords.getRecords() != null && bibMarcRecords.getRecords().size() > 0) {
983
984 Map<String, String> dataFields = bibMarcUtil.buildDataValuesForBibInfo(bibMarcRecords.getRecords().get(0));
985 String title = dataFields.get(BibMarcUtil.TITLE_DISPLAY);
986 String author = dataFields.get(BibMarcUtil.AUTHOR_DISPLAY);
987 String publisher = dataFields.get(BibMarcUtil.PUBLISHER_DISPLAY);
988 String isbn = dataFields.get(BibMarcUtil.ISBN_DISPLAY);
989 String issn = dataFields.get(BibMarcUtil.ISSN_DISPLAY);
990
991 String commonIdentifier = "";
992 if (StringUtils.isNotEmpty(isbn)) {
993 commonIdentifier = isbn;
994 } else {
995 commonIdentifier = issn;
996 }
997
998 bibInsertPreparedStatement.setInt(1, bibId);
999 bibInsertPreparedStatement.setString(2, bibIdStr);
1000 bibInsertPreparedStatement.setString(3, truncateData(title, 4000));
1001 bibInsertPreparedStatement.setString(4, truncateData(author, 4000));
1002 bibInsertPreparedStatement.setString(5, truncateData(publisher, 4000));
1003 bibInsertPreparedStatement.setString(6, truncateData(commonIdentifier, 100));
1004
1005
1006 try {
1007 bibInsertPreparedStatement.executeUpdate();
1008 } catch (Exception e) {
1009 if (e.getMessage().startsWith("Duplicate entry")) {
1010
1011 bibUpdatePreparedStatement.setString(1, truncateData(title, 4000));
1012 bibUpdatePreparedStatement.setString(2, truncateData(author, 4000));
1013 bibUpdatePreparedStatement.setString(3, truncateData(publisher, 4000));
1014 bibUpdatePreparedStatement.setString(4, truncateData(commonIdentifier, 100));
1015 bibUpdatePreparedStatement.setString(5, bibIdStr);
1016 bibUpdatePreparedStatement.setInt(6, bibId);
1017 try {
1018 bibUpdatePreparedStatement.executeUpdate();
1019 } catch (Exception e1) {
1020 LOG.error("Exception while updating into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : ", e1);
1021 writeStatusToFile(filePath, fileName, "Exception while updating into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : " + e1.getMessage());
1022 }
1023 } else {
1024 LOG.error("Exception while inserting into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : ", e);
1025 writeStatusToFile(filePath, fileName, "Exception while inserting into BIB_INFO_T, BibId = " + bibId + " BibIdStr = " + bibIdStr + " : " + e.getMessage());
1026 }
1027 }
1028 }
1029
1030 } catch (Exception e) {
1031 LOG.error("Exception inserting/updating bibId " + bibId + "; bibId2 = " + bibId2 + " BibIdStr = " + bibIdStr, e);
1032 writeStatusToFile(filePath, fileName, "Exception inserting/updating bibId " + bibId + "; bibId2 = " + bibId2 + " BibIdStr = " + bibIdStr + "\t" + e.getMessage());
1033 }
1034 bibInfoStatistics.setBibCount((batchSize * batchNo) + count);
1035 if (count == batchSize) {
1036 break;
1037 }
1038 }
1039 stopWatch.stop();
1040 connection.commit();
1041 return count;
1042 }
1043
1044 public static String truncateData(String data, int idLength) {
1045
1046 String truncateData = data;
1047 if (data != null && data.length() > idLength) {
1048 truncateData = data.substring(0, (idLength-1));
1049 }
1050 return truncateData;
1051 }
1052
1053 }