Skip to content

Commit

Permalink
OLE-8518 - Migration process for new shelving order key with treads u…
Browse files Browse the repository at this point in the history
…pdate as batch
  • Loading branch information
jayared committed Dec 24, 2015
1 parent 874e76e commit 0d348bc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,39 @@ private void calculateAndUpdateHoldingsShelvingOrder(SqlRowSet holdingsCallNumbe
ExecutorService executorService = Executors.newFixedThreadPool(10);
stopWatch.start();
int count = 0;

while (holdingsCallNumberResultSet.next()) {
count++;
Map<String,String> holdingsDetails = new HashMap<>();
holdingsDetails.put("callNumberTypeId",holdingsCallNumberResultSet.getString("CALL_NUMBER_TYPE_ID"));
holdingsDetails.put("callNumber", holdingsCallNumberResultSet.getString("CALL_NUMBER"));
holdingsDetails.put("holdingsId", String.valueOf(holdingsCallNumberResultSet.getInt("HOLDINGS_ID")));
futures.add(executorService.submit(new HoldingsCallNumberProcessor(holdingsDetails, getJdbcTemplate(), callNumberType)));
futures.add(executorService.submit(new HoldingsCallNumberProcessor(holdingsDetails, callNumberType)));
}
List<String> batchSqls= new ArrayList<>();
for (Iterator<Future> iterator = futures.iterator(); iterator.hasNext(); ) {
Future future = iterator.next();
try {
Map<String,String> holdingsDetails = (Map<String,String>) future.get();
String sql = (String) future.get();
batchSqls.add(sql);
} catch (InterruptedException e) {
LOG.info(e.getMessage());
} catch (ExecutionException e) {
LOG.info(e.getMessage());
}

if(batchSqls.size() == 10){
String[] arraysqls = batchSqls.toArray(new String[batchSqls.size()]);
getJdbcTemplate().batchUpdate(arraysqls);
batchSqls.clear();
}
}
executorService.shutdown();

if(batchSqls.size() > 0){
String[] arraysqls = batchSqls.toArray(new String[batchSqls.size()]);
getJdbcTemplate().batchUpdate(arraysqls);
}
stopWatch.stop();
LOG.debug("Total Time taken " + count + " - for Holdings ::" + stopWatch.getTotalTimeMillis());
}
Expand All @@ -93,19 +107,33 @@ private void calculateAndUpdateItemShelvingOrder(SqlRowSet itemCallNumberResultS
ItemDetails.put("callNumberTypeId",itemCallNumberResultSet.getString("CALL_NUMBER_TYPE_ID"));
ItemDetails.put("callNumber", itemCallNumberResultSet.getString("CALL_NUMBER"));
ItemDetails.put("itemId", String.valueOf(itemCallNumberResultSet.getInt("ITEM_ID")));
futures.add(executorService.submit(new ItemCallNumberProcessor(ItemDetails, getJdbcTemplate(), callNumberType)));
futures.add(executorService.submit(new ItemCallNumberProcessor(ItemDetails, callNumberType)));
}
List<String> batchSqls= new ArrayList<>();
for (Iterator<Future> iterator = futures.iterator(); iterator.hasNext(); ) {
Future future = iterator.next();
try {
Map<String,String> itemDetails = (Map<String,String>) future.get();
String sql = (String) future.get();
batchSqls.add(sql);
} catch (InterruptedException e) {
LOG.info(e.getMessage());
} catch (ExecutionException e) {
LOG.info(e.getMessage());
}

if(batchSqls.size() == 10){
String[] arraysqls = batchSqls.toArray(new String[batchSqls.size()]);
getJdbcTemplate().batchUpdate(arraysqls);
batchSqls.clear();
}
}
executorService.shutdown();

if(batchSqls.size() > 0){
String[] arraysqls = batchSqls.toArray(new String[batchSqls.size()]);
getJdbcTemplate().batchUpdate(arraysqls);
}

stopWatch.stop();
LOG.debug("Total Time taken " + count + " - for Item ::" + stopWatch.getTotalTimeMillis());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
Expand All @@ -21,40 +19,33 @@
public class HoldingsCallNumberProcessor implements Callable {
private static final Logger LOG = LoggerFactory.getLogger(HoldingsCallNumberProcessor.class);
Map<String, String> holdingsDetails;
private JdbcTemplate jdbcTemplate;
Map<String, String> callNumberType;
private PlatformTransactionManager transactionManager;
StringBuilder holdingsQuery = new StringBuilder("UPDATE OLE_DS_HOLDINGS_T SET SHELVING_ORDER = ");

public HoldingsCallNumberProcessor(Map<String, String> holdingsDetails, JdbcTemplate jdbcTemplate, Map<String, String> callNumberType) {
public HoldingsCallNumberProcessor(Map<String, String> holdingsDetails, Map<String, String> callNumberType) {
this.holdingsDetails = holdingsDetails;
this.jdbcTemplate = jdbcTemplate;
this.callNumberType = callNumberType;
}

@Override
public Object call() throws Exception {
final Map<String, String> localHoldingsDetails = this.holdingsDetails;
final TransactionTemplate template = new TransactionTemplate(getTransactionManager());

try {
template.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
StringBuilder holdingsQuery = new StringBuilder("UPDATE OLE_DS_HOLDINGS_T SET SHELVING_ORDER = ");
String callNumberTypeId = holdingsDetails.get("callNumberTypeId");
String callNumber = holdingsDetails.get("callNumber");
String holdingsId = holdingsDetails.get("holdingsId");
if (StringUtils.isNotEmpty(callNumberTypeId) && StringUtils.isNotEmpty(callNumber)) {
String callNumberCode = callNumberType.get(callNumberTypeId);
holdingsQuery.append("'" + CallNumberMigrationDao.getShelfKey(callNumber, callNumberCode) + "' WHERE HOLDINGS_ID = '");
holdingsQuery.append(holdingsId + "'");
try {
jdbcTemplate.update(holdingsQuery.toString());
} catch (Exception e1) {
LOG.error("Exception while updating into OLE_DS_HOLDINGS_T, Holdings Id = " + holdingsId + " callNumber = " + callNumber + " : ", e1);
}
}
return localHoldingsDetails;

return holdingsQuery.toString();
}
});
} catch (Exception ex) {
Expand All @@ -64,7 +55,7 @@ public Object doInTransaction(TransactionStatus status) {
this.transactionManager = null;

}
return localHoldingsDetails;
return holdingsQuery.toString();
}

public PlatformTransactionManager getTransactionManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
Expand All @@ -21,13 +19,12 @@
public class ItemCallNumberProcessor implements Callable {
private static final Logger LOG = LoggerFactory.getLogger(ItemCallNumberProcessor.class);
Map<String, String> itemdetails;
private JdbcTemplate jdbcTemplate;
Map<String, String> callNumberType;
private PlatformTransactionManager transactionManager;
StringBuilder itemQuery = new StringBuilder("UPDATE OLE_DS_ITEM_T SET SHELVING_ORDER = ");

public ItemCallNumberProcessor(Map<String, String> itemdetails, JdbcTemplate jdbcTemplate, Map<String, String> callNumberType) {
public ItemCallNumberProcessor(Map<String, String> itemdetails, Map<String, String> callNumberType) {
this.itemdetails = itemdetails;
this.jdbcTemplate = jdbcTemplate;
this.callNumberType = callNumberType;
}

Expand All @@ -39,21 +36,15 @@ public Object call() throws Exception {
template.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
StringBuilder itemQuery = new StringBuilder("UPDATE OLE_DS_ITEM_T SET SHELVING_ORDER = ");
String callNumberTypeId = localItemdetails.get("callNumberTypeId");
String callNumber = localItemdetails.get("callNumber");
String itemId = localItemdetails.get("itemId");
if (StringUtils.isNotEmpty(callNumberTypeId) && StringUtils.isNotEmpty(callNumber)) {
String callNumberCode = callNumberType.get(callNumberTypeId);
itemQuery.append("'" + CallNumberMigrationDao.getShelfKey(callNumber, callNumberCode) + "' WHERE ITEM_ID = '");
itemQuery.append(itemId + "'");
try {
jdbcTemplate.update(itemQuery.toString());
} catch (Exception e1) {
LOG.error("Exception while updating into OLE_DS_ITEM_T, Item Id = " + itemId + " callNumber = " + callNumber + " : ", e1);
}
}
return localItemdetails;
return itemQuery.toString();

}
});
Expand All @@ -64,7 +55,7 @@ public Object doInTransaction(TransactionStatus status) {
this.transactionManager = null;

}
return localItemdetails;
return itemQuery.toString();
}

public PlatformTransactionManager getTransactionManager() {
Expand Down

0 comments on commit 0d348bc

Please sign in to comment.