Omaha #3454 Fixing text purge

Former-commit-id: 9a5f4f99e0 [formerly 9bc5473d29] [formerly 2f7c98d960] [formerly 5606597b56 [formerly 2f7c98d960 [formerly 43299673f2172188bc44866c6ccd5552fda348cb]]]
Former-commit-id: 5606597b56
Former-commit-id: 88aa7932aa4efe7322740d7ecd5c6c49a3a0fb8a [formerly e2759d38e4]
Former-commit-id: 7a9277d53d
This commit is contained in:
Benjamin Phillippe 2014-10-23 12:54:26 -05:00
parent 58bf92c68c
commit 39614d2bb2

View file

@ -41,13 +41,13 @@ import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import com.raytheon.uf.common.dataplugin.text.db.OperationalStdTextProduct;
import com.raytheon.uf.common.dataplugin.text.db.PracticeStdTextProduct;
@ -255,43 +255,52 @@ public class StdTextProductDao extends CoreDao {
* @param pastHours
* @return
*/
public List<StdTextProduct> cccnnnxxxReadVersion(String ccc, String nnn,
String xxx, int version) {
public List<StdTextProduct> cccnnnxxxReadVersion(final String ccc, final String nnn,
final String xxx, final int version) {
List<StdTextProduct> products = null;
ccc = StringUtils.rightPad(ccc, MAX_FIELD_LENGTH);
nnn = StringUtils.rightPad(nnn, MAX_FIELD_LENGTH);
xxx = StringUtils.rightPad(xxx, MAX_FIELD_LENGTH);
boolean hasCCC = ((ccc != null) && (ccc.length() > 0) && (!ccc
.equals("000")));
boolean hasNNN = ((nnn != null) && (nnn.length() > 0) && (!nnn
.equals("000")));
boolean hasXXX = ((xxx != null) && (xxx.length() > 0) && (!xxx
.equals("000")));
boolean createInitialFilter = !(hasCCC && hasNNN && hasXXX);
AFOSProductId[] afosIds = null;
StatelessSession session = null;
Transaction tx = null;
final boolean createInitialFilter = !(hasCCC && hasNNN && hasXXX);
try {
session = getSessionFactory().openStatelessSession();
tx = session.beginTransaction();
final AFOSProductId[] afosIds = txTemplate
.execute(new TransactionCallback<AFOSProductId[]>() {
@Override
public AFOSProductId[] doInTransaction(
TransactionStatus status) {
String paddedccc = StringUtils.rightPad(ccc,
MAX_FIELD_LENGTH);
String paddednnn = StringUtils.rightPad(nnn,
MAX_FIELD_LENGTH);
String paddedxxx = StringUtils.rightPad(xxx,
MAX_FIELD_LENGTH);
Session session = getCurrentSession();
AFOSProductId[] afosIds = null;
StdTextProduct stdTextProduct = getStdTextProductInstance();
if (createInitialFilter) {
stdTextProduct.setCccid(ccc);
stdTextProduct.setNnnid(nnn);
stdTextProduct.setXxxid(xxx);
stdTextProduct.setCccid(paddedccc);
stdTextProduct.setNnnid(paddednnn);
stdTextProduct.setXxxid(paddedxxx);
Map<String, String> map = buildCriterions(ProdCCC_ID, ccc,
ProdNNN_ID, nnn, ProdXXX_ID, xxx);
Criteria criteria = session.createCriteria(stdTextProduct
Map<String, String> map = buildCriterions(
ProdCCC_ID, paddedccc, ProdNNN_ID,
paddednnn, ProdXXX_ID, paddedxxx);
Criteria criteria = session
.createCriteria(stdTextProduct
.getClass());
ProjectionList projList = Projections.projectionList();
ProjectionList projList = Projections
.projectionList();
projList.add(Projections.property(ProdCCC_ID));
projList.add(Projections.property(ProdNNN_ID));
projList.add(Projections.property(ProdXXX_ID));
criteria.setProjection(Projections.distinct(projList));
criteria.setProjection(Projections
.distinct(projList));
criteria.add(Restrictions.allEq(map));
criteria.addOrder(Order.asc(ProdCCC_ID));
criteria.addOrder(Order.asc(ProdNNN_ID));
@ -303,25 +312,36 @@ public class StdTextProductDao extends CoreDao {
int i = 0;
for (Object row : list) {
Object[] cols = (Object[]) row;
afosIds[i++] = new AFOSProductId((String) cols[0],
(String) cols[1], (String) cols[2]);
afosIds[i++] = new AFOSProductId(
(String) cols[0],
(String) cols[1],
(String) cols[2]);
}
} else {
afosIds = new AFOSProductId[0];
}
tx.commit();
} else {
afosIds = new AFOSProductId[1];
afosIds[0] = new AFOSProductId(ccc, nnn, xxx);
afosIds[0] = new AFOSProductId(paddedccc,
paddednnn, paddedxxx);
}
return afosIds;
}
});
tx = session.beginTransaction();
products = txTemplate.execute(new TransactionCallback<List<StdTextProduct>>() {
@Override
public List<StdTextProduct> doInTransaction(
TransactionStatus status) {
List<StdTextProduct> products = null;
Session session = getCurrentSession();
/*
* DR15244 - Make sure that the query is performed on the appropriate
* table based on what StdTextProduct is requested (ultimately on CAVE mode)
*/
Matcher m = Pattern.compile("StdTextProduct").matcher(AFOS_QUERY_STMT);
String tableName = stdTextProduct.getClass().getSimpleName();
String tableName = getStdTextProductInstance().getClass().getSimpleName();
String tableQuery = m.replaceAll(tableName);
Query query = session.createQuery(tableQuery);
@ -355,18 +375,14 @@ public class StdTextProductDao extends CoreDao {
}
}
}
tx.commit();
return products;
}
});
} catch (Exception e) {
logger.error("Error occurred reading products", e);
if (tx != null) {
try {
tx.rollback();
} catch (Exception e1) {
logger.error("Error occurred rolling back transaction", e1);
}
}
} finally {
closeSession(session);
}
if (products == null) {
@ -625,16 +641,13 @@ public class StdTextProductDao extends CoreDao {
}
private AFOSProductId[] getDistinctAfosIds() throws HibernateException {
return txTemplate.execute(new TransactionCallback<AFOSProductId[]>() {
@Override
public AFOSProductId[] doInTransaction(TransactionStatus status) {
AFOSProductId[] products = null;
StatelessSession sess = null;
Transaction tx = null;
try {
sess = getSessionFactory().openStatelessSession();
tx = sess.beginTransaction();
Session sess = getCurrentSession();
Criteria crit = sess
.createCriteria((this.operationalMode ? OperationalStdTextProduct.class
.createCriteria((operationalMode ? OperationalStdTextProduct.class
: PracticeStdTextProduct.class));
ProjectionList fields = Projections.projectionList();
fields.add(Projections.property(ProdCCC_ID));
@ -660,21 +673,10 @@ public class StdTextProductDao extends CoreDao {
products[i++] = new AFOSProductId(cccid, nnnid, xxxid);
}
}
tx.commit();
tx = null;
} finally {
if (tx != null) {
try {
tx.rollback();
} catch (Exception e) {
logger.error("Caught Exception rolling back transaction", e);
}
}
closeSession(sess);
}
return products;
}
});
}
/**
* Simple purge routine. Deletes all data that has a refTime older than that
@ -693,8 +695,6 @@ public class StdTextProductDao extends CoreDao {
* @return
*/
public int versionPurge(String afosId) {
StatelessSession session = null;
Transaction tx = null;
int rval = 0;
if (PurgeLogger.isDebugEnabled()) {
if (afosId == null) {
@ -721,12 +721,7 @@ public class StdTextProductDao extends CoreDao {
}
if (ids != null && ids.size() > 0) {
String cccid = null;
String nnnid = null;
String xxxid = null;
String refTimeQueryString = null;
{
StringBuilder refTimeQueryBuilder = new StringBuilder(200);
refTimeQueryBuilder.append("SELECT refTime FROM ");
refTimeQueryBuilder.append(getStdTextProductInstance()
@ -739,11 +734,9 @@ public class StdTextProductDao extends CoreDao {
refTimeQueryBuilder.append(ProdXXX_ID).append(" = :xxxid");
refTimeQueryBuilder.append(" ORDER BY refTime DESC");
refTimeQueryBuilder.append(", insertTime DESC");
refTimeQueryString = refTimeQueryBuilder.toString();
}
final String refTimeQueryString = refTimeQueryBuilder.toString();
String delQueryString = null;
{
StringBuilder delQueryBuilder = new StringBuilder(200);
delQueryBuilder.append("DELETE FROM ");
delQueryBuilder.append(getStdTextProductInstance()
@ -756,19 +749,22 @@ public class StdTextProductDao extends CoreDao {
delQueryBuilder.append(ProdXXX_ID).append(" = :xxxid")
.append(" AND ");
delQueryBuilder.append("refTime < :refTime");
delQueryString = delQueryBuilder.toString();
}
final String delQueryString = delQueryBuilder.toString();
session = getSessionFactory().openStatelessSession();
for (TextProductInfo prodInfo : ids) {
for (final TextProductInfo prodInfo : ids) {
rval += txTemplate.execute(new TransactionCallback<Integer>() {
@Override
public Integer doInTransaction(TransactionStatus status) {
Session session = getCurrentSession();
TextProductInfoPK pk = prodInfo.getProdId();
cccid = pk.getCccid();
nnnid = pk.getNnnid();
xxxid = pk.getXxxid();
String cccid = pk.getCccid();
String nnnid = pk.getNnnid();
String xxxid = pk.getXxxid();
int rowsDeleted = 0;
try {
tx = session.beginTransaction();
Query refTimeQuery = session
.createQuery(refTimeQueryString);
refTimeQuery.setString("cccid", cccid);
@ -794,17 +790,12 @@ public class StdTextProductDao extends CoreDao {
PLUGIN_NAME);
}
int rowsDeleted = delQuery.executeUpdate();
// commit every afos id purge
tx.commit();
tx = null;
rowsDeleted = delQuery.executeUpdate();
if (PurgeLogger.isDebugEnabled()) {
PurgeLogger.logDebug("Purged [" + rowsDeleted
+ "] records for [" + cccid + nnnid
+ xxxid + "]", PLUGIN_NAME);
}
rval += rowsDeleted;
} else if (PurgeLogger.isDebugEnabled()) {
PurgeLogger.logDebug(
"VersionPurge: Product [" + cccid + nnnid
@ -817,24 +808,16 @@ public class StdTextProductDao extends CoreDao {
"Exception occurred purging text products ["
+ cccid + nnnid + xxxid + "]",
PLUGIN_NAME, e);
if (tx != null) {
try {
tx.rollback();
} catch (Exception e1) {
PurgeLogger
.logError(
"Error occurred rolling back transaction",
PLUGIN_NAME, e1);
}
}
return rowsDeleted;
}
});
}
}
} catch (Exception e) {
// don't need to worry about rolling back transaction
PurgeLogger.logError("Error purging text products", PLUGIN_NAME, e);
} finally {
closeSession(session);
}
return rval;
}
@ -2818,16 +2801,6 @@ public class StdTextProductDao extends CoreDao {
}
}
private void closeSession(StatelessSession s) {
if (s != null) {
try {
s.close();
} catch (Exception e) {
logger.error("Error closing Session", e);
}
}
}
private void closeConnection(Connection c) {
if (c != null) {
try {