Skip to content

Commit

Permalink
Merge pull request #139 from jayared/release-2.0
Browse files Browse the repository at this point in the history
Release 2.0
  • Loading branch information
Peri Subrahmanya committed Dec 24, 2015
2 parents ef559e9 + 0d348bc commit 281d0d5
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 245 deletions.
@@ -1,13 +1,10 @@
package org.kuali.ole.deliver.util;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.kuali.ole.OLETestCaseBase;
import org.kuali.ole.docstore.common.util.CallNumberMigration;

import java.io.File;
import java.sql.SQLException;
import org.kuali.ole.docstore.common.dao.CallNumberMigrationDao;
import org.kuali.ole.sys.context.SpringContext;

/**
* Created by jayabharathreddy on 12/17/15.
Expand All @@ -18,13 +15,8 @@ public class CallNumberMigration_IT extends OLETestCaseBase {

@Test
public void calculateAndUpdateShelvingOrder() throws Exception {
CallNumberMigration callNumberMigration = new CallNumberMigration();
CallNumberMigrationDao callNumberMigration = (CallNumberMigrationDao) SpringContext.getBean("callNumberMigrationDao");
callNumberMigration.init();
callNumberMigration.getHoldingsDetails();
callNumberMigration.calculateAndUpdateHoldingsShelvingOrder();
callNumberMigration.getItemDetails();
callNumberMigration.calculateAndUpdateItemShelvingOrder();
callNumberMigration.closeConnections();
}


Expand Down
31 changes: 31 additions & 0 deletions ole-common/ole-docstore-common/pom.xml
Expand Up @@ -94,5 +94,36 @@
<version>4.3.5</version>
</dependency>

<dependency>
<groupId>org.kuali.rice</groupId>
<artifactId>rice-core-framework</artifactId>
<version>${rice.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

</dependencies>
</project>
@@ -0,0 +1,154 @@
package org.kuali.ole.docstore.common.dao;

import org.apache.commons.lang.StringUtils;
import org.kuali.ole.utility.callnumber.CallNumberFactory;
import org.kuali.rice.core.framework.persistence.jdbc.dao.PlatformAwareDaoBaseJdbc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.util.StopWatch;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
* Created by jayabharathreddy on 12/16/15.
*/
public class CallNumberMigrationDao extends PlatformAwareDaoBaseJdbc {

private static final Logger LOG = LoggerFactory.getLogger(CallNumberMigrationDao.class);
private static Map<String, String> callNumberType = new HashMap<>();


private String holdingsCallNumberQuery = "SELECT HOLDINGS_ID,CALL_NUMBER_TYPE_ID,CALL_NUMBER FROM OLE_DS_HOLDINGS_T Where CALL_NUMBER !='null' ORDER BY HOLDINGS_ID";
private String itemCallNumberQuery = "SELECT ITEM_ID,CALL_NUMBER_TYPE_ID,CALL_NUMBER FROM OLE_DS_ITEM_T Where CALL_NUMBER !='null' ORDER BY ITEM_ID";

public void init() throws Exception {
fetchCallNumberType();
retriveAndUpdateHoldingsCallNumber();
retriveAndUpdateItemCallNumber();
}


private void fetchCallNumberType() throws Exception {
SqlRowSet resultSet = getJdbcTemplate().queryForRowSet("SELECT SHVLG_SCHM_ID,SHVLG_SCHM_CD from OLE_CAT_SHVLG_SCHM_T");
while (resultSet.next()) {
callNumberType.put(resultSet.getString("SHVLG_SCHM_ID"), resultSet.getString("SHVLG_SCHM_CD"));
}
}

private void retriveAndUpdateHoldingsCallNumber() throws Exception {
SqlRowSet holdingsCallNumberResultSet = getJdbcTemplate().queryForRowSet(holdingsCallNumberQuery);
calculateAndUpdateHoldingsShelvingOrder(holdingsCallNumberResultSet);
}


private void retriveAndUpdateItemCallNumber() throws Exception {
SqlRowSet itemCallNumberResultSet = getJdbcTemplate().queryForRowSet(itemCallNumberQuery);
calculateAndUpdateItemShelvingOrder(itemCallNumberResultSet);

}

private void calculateAndUpdateHoldingsShelvingOrder(SqlRowSet holdingsCallNumberResultSet) throws Exception {
StopWatch stopWatch = new StopWatch();
List<Future> futures = new ArrayList<>();
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, callNumberType)));
}
List<String> batchSqls= new ArrayList<>();
for (Iterator<Future> iterator = futures.iterator(); iterator.hasNext(); ) {
Future future = iterator.next();
try {
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());
}

private void calculateAndUpdateItemShelvingOrder(SqlRowSet itemCallNumberResultSet) throws Exception {
StopWatch stopWatch = new StopWatch();
List<Future> futures = new ArrayList<>();
ExecutorService executorService = Executors.newFixedThreadPool(10);
stopWatch.start();
int count = 0;
while (itemCallNumberResultSet.next()) {
count++;
Map<String,String> ItemDetails = new HashMap<>();
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, callNumberType)));
}
List<String> batchSqls= new ArrayList<>();
for (Iterator<Future> iterator = futures.iterator(); iterator.hasNext(); ) {
Future future = iterator.next();
try {
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());
}

public static String getShelfKey(String callNumber, String codeValue) {
String Shelfkey = null;
if (StringUtils.isNotEmpty(callNumber) && StringUtils.isNotEmpty(codeValue)) {
org.solrmarc.callnum.CallNumber callNumberObj = CallNumberFactory.getInstance().getCallNumber(codeValue);
if (callNumberObj != null) {
callNumberObj.parse(callNumber);
Shelfkey = callNumberObj.getShelfKey();
}
}
return Shelfkey;
}


}
@@ -0,0 +1,67 @@
package org.kuali.ole.docstore.common.dao;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Map;
import java.util.concurrent.Callable;

/**
* Created by jayabharathreddy on 12/23/15.
*/

public class HoldingsCallNumberProcessor implements Callable {
private static final Logger LOG = LoggerFactory.getLogger(HoldingsCallNumberProcessor.class);
Map<String, String> holdingsDetails;
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, Map<String, String> callNumberType) {
this.holdingsDetails = holdingsDetails;
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) {
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 + "'");
}
return holdingsQuery.toString();
}
});
} catch (Exception ex) {
throw ex;
} finally {
holdingsDetails.clear();
this.transactionManager = null;

}
return holdingsQuery.toString();
}

public PlatformTransactionManager getTransactionManager() {
if (transactionManager == null) {
transactionManager = GlobalResourceLoader.getService("transactionManager");
}
return this.transactionManager;
}
}
@@ -0,0 +1,67 @@
package org.kuali.ole.docstore.common.dao;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Map;
import java.util.concurrent.Callable;

/**
* Created by jayabharathreddy on 12/23/15.
*/

public class ItemCallNumberProcessor implements Callable {
private static final Logger LOG = LoggerFactory.getLogger(ItemCallNumberProcessor.class);
Map<String, String> itemdetails;
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, Map<String, String> callNumberType) {
this.itemdetails = itemdetails;
this.callNumberType = callNumberType;
}

@Override
public Object call() throws Exception {
final Map<String, String> localItemdetails = this.itemdetails;
final TransactionTemplate template = new TransactionTemplate(getTransactionManager());
try {
template.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
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 + "'");
}
return itemQuery.toString();

}
});
} catch (Exception ex) {
throw ex;
} finally {
itemdetails.clear(); ;
this.transactionManager = null;

}
return itemQuery.toString();
}

public PlatformTransactionManager getTransactionManager() {
if (transactionManager == null) {
transactionManager = GlobalResourceLoader.getService("transactionManager");
}
return this.transactionManager;
}
}

0 comments on commit 281d0d5

Please sign in to comment.