FacilityDAOImpl

package com.citi.risk.credit.facility.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.impl.SQLQueryImpl;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.citi.risk.credit.domain.fac.maker.FacFacilityMaker;
import com.citi.risk.credit.domain.fac.master.FacFacilityMaster;
import com.citi.risk.credit.facility.util.FacilityDAOUtils;
import com.citi.risk.credit.facility.util.exception.ApplicationException;
import com.citi.risk.credit.facility.util.exception.SystemRuntimeException;

/**
* The Class FacilityDAOImpl.
*/
@SuppressWarnings("unchecked")
public class FacilityDAOImpl implements FacilityDAO {

/**
* The hibernate template.
*/
private HibernateTemplate hibernateTemplate = null;

/**
* Sets the session factory.
*
* @param sessionFactory the new session factory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
hibernateTemplate = new HibernateTemplate(sessionFactory);
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#findEntity(java.lang.Object)
*/
@Override
public <T> T findEntity(final T entity) throws ApplicationException, SystemRuntimeException {
if (entity == null) {
return null;
}
try {
return hibernateTemplate.executeWithNativeSession(new HibernateCallback<T>() {

public T doInHibernate(Session session) throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(entity.getClass()).add(Example.create(entity));
java.io.Serializable identifier;
try {
identifier = FacilityDAOUtils.getIdentifier(session, entity);
} catch (SystemRuntimeException e) {
throw new RuntimeException(e);
}
if (identifier != null) {
criteria.add(Restrictions.idEq(identifier));
}
return (T) criteria.uniqueResult();
}

});
} catch (Throwable throwable) {
handleException(throwable);
return null;
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#findEntities(java.lang.Object)
*/
@Override
public <T> List<T> findEntities(final T entity) throws ApplicationException, SystemRuntimeException {
return findEntities(entity, null, null);
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#getEntityWithIdInScope(java.lang.Object, java.lang.Object, java.lang.Object)
*/
@Override
public <T> T getEntityWithIdInScope(final T entity, final Object start, final Object end)
throws ApplicationException, SystemRuntimeException {
if (entity == null) {
return null;
}
try {
return hibernateTemplate.executeWithNativeSession(new HibernateCallback<T>() {
public T doInHibernate(Session session) throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(entity.getClass()).add(Example.create(entity));
java.io.Serializable identifier;
try {
identifier = FacilityDAOUtils.getIdentifier(session, entity);
} catch (SystemRuntimeException e) {
throw new RuntimeException(e);
}
if (identifier != null) {
criteria.add(Restrictions.idEq(identifier));
}
String pk = session.getSessionFactory().getClassMetadata(entity.getClass()).getIdentifierPropertyName();
criteria.add(Restrictions.ge(pk, start));
criteria.add(Restrictions.le(pk, end));
return (T) criteria.uniqueResult();
}
});
} catch (Throwable throwable) {
handleException(throwable);
return null;
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#findEntities(java.lang.Object, java.lang.String[], java.lang.String[])
*/
@Override
public <T> List<T> findEntities(final T entity, final String[] ascProperties, final String[] descProperties)
throws ApplicationException, SystemRuntimeException {

if (entity == null) {
return null;
}

try {
return hibernateTemplate.executeWithNativeSession(new HibernateCallback<List<T>>() {
public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(entity.getClass()).add(Example.create(entity));
java.io.Serializable identifier;
try {
identifier = FacilityDAOUtils.getIdentifier(session, entity);
} catch (SystemRuntimeException e) {
throw new RuntimeException(e);
}
if (identifier != null) {
criteria.add(Restrictions.idEq(identifier));
}
if (ascProperties != null) {
for (String asc : ascProperties) {
if (StringUtils.isNotBlank(asc)) {
criteria.addOrder(Order.asc(asc));
}
}
}
if (descProperties != null) {
for (String desc : descProperties) {
if (StringUtils.isNotBlank(desc)) {
criteria.addOrder(Order.desc(desc));
}
}
}
return (List<T>) criteria.list();
}
});
} catch (Throwable throwable) {
handleException(throwable);
return null;
}

}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#findEntityById(java.lang.Class, java.lang.Object)
*/
@Override
public <T> T findEntityById(final Class<T> clazz, final Object identifier)
throws ApplicationException, SystemRuntimeException, ObjectNotFoundException {
if (identifier == null) {
return null;
}
T retEntity = null;
try {
retEntity = hibernateTemplate.executeWithNativeSession(new HibernateCallback<T>() {
public T doInHibernate(Session session) throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(clazz).add(Restrictions.idEq(identifier));
List<?> result = criteria.list();
if (result == null || result.isEmpty()) {
return null;
}
return (T) result.get(0);
}
});
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
if (retEntity == null) {
throw new ObjectNotFoundException((Serializable) identifier, clazz.getName());
}
return retEntity;
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#doHibernate(org.springframework.orm.hibernate3.HibernateCallback)
*/
@Override
public <T> T doHibernate(HibernateCallback<T> callBack) throws ApplicationException, SystemRuntimeException {
if (callBack == null) {
return null;
}
try {
return hibernateTemplate.executeWithNativeSession(callBack);
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#saveEntity(java.lang.Object)
*/
@Override
public <T> T saveEntity(final T entity) throws ApplicationException, SystemRuntimeException {
try {
return saveEntity(entity, false);
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#saveEntity(java.lang.Object, boolean)
*/
@Override
public <T> T saveEntity(final T entity, final boolean needCopy2Master) throws ApplicationException, SystemRuntimeException {

if (entity == null) {
return null;
}
try {
hibernateTemplate.executeWithNativeSession(new HibernateCallback<Void>() {
public Void doInHibernate(Session session) throws HibernateException, SQLException {
session.saveOrUpdate(entity);
session.flush();
if (entity instanceof FacFacilityMaker && needCopy2Master) {
FacFacilityMaker facFacilityMaker = (FacFacilityMaker)entity;
facFacilityMaker.setFacilityVersion(facFacilityMaker.getFacilityVersion() + 1);
FacFacilityMaster targetEntity = (FacFacilityMaster) session.get(FacFacilityMaster.class, facFacilityMaker.getFacilityMasterId());
if (targetEntity == null) {
targetEntity = new FacFacilityMaster();
}
try {
FacilityDAOUtils.copyEntity(entity, targetEntity, session);
} catch (SystemRuntimeException e) {
throw new RuntimeException(e);
}
session.saveOrUpdate(targetEntity);
session.flush();
}
return null;
}
});
} catch (Throwable throwable) {
handleException(throwable);
}
return entity;
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#deleteEntity(java.lang.Object)
*/
@Override
public <T> T deleteEntity(final T entity) throws ApplicationException, SystemRuntimeException {
if (entity == null) {
return null;
}
try {
return hibernateTemplate.executeWithNativeSession(new HibernateCallback<T>() {
public T doInHibernate(Session session) throws HibernateException, SQLException {
java.io.Serializable identifier;
try {
identifier = FacilityDAOUtils.getIdentifier(session, entity);
} catch (SystemRuntimeException e) {
throw new RuntimeException(e);
}
T returnValue = (T) session.get(entity.getClass(), identifier);
session.delete(returnValue);
session.flush();
return returnValue;
}
});
} catch (Throwable throwable) {
handleException(throwable);
return null;
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#findAllEntity(java.lang.Class)
*/
@Override
public <T> List<T> findAllEntity(Class<T> clazz) throws ApplicationException, SystemRuntimeException {
try {
return (List<T>) hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(clazz).list();
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#getSequence(java.lang.String, java.lang.String)
*/
@Override
public long getSequence(String schema, String sequnceName) throws ApplicationException, SystemRuntimeException {
if (StringUtils.isBlank(sequnceName) || StringUtils.isBlank(schema)) {
return -1;
}
final String sql = "select " + StringUtils.trimToEmpty(schema) + "." + StringUtils.trimToEmpty(sequnceName) + ".NEXTVAL as id from dual";
try {
return hibernateTemplate.executeWithNewSession(new HibernateCallback<Long>() {
public Long doInHibernate(Session session) throws HibernateException, SQLException {
Long sequence = (Long) session.createSQLQuery(sql).addScalar("id", StandardBasicTypes.LONG).uniqueResult();
session.flush();
return sequence;
}
});
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#findEntityByIds(java.lang.Class, java.lang.String, java.lang.Object[])
*/
@Override
public <T> List<T> findEntityByIds(Class<T> clazz, String criteriaColumn, Object... ids) throws ApplicationException, SystemRuntimeException {
try {
return (List<T>) hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(clazz).add(Restrictions.in(criteriaColumn, ids)).list();
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#getEntityByHQL(java.lang.Class, java.lang.String)
*/
@Override
public <T> List<T> getEntityByHQL(Class<T> clazz, final String hql) throws ApplicationException, SystemRuntimeException {
try {
return hibernateTemplate.executeWithNativeSession(new HibernateCallback<List<T>>() {
public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);

return (List<T>) query.list();
}
});
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
}

/* (non-Javadoc)
* @see com.citi.risk.credit.facility.dao.FacilityDAO#getEntityBySQL(java.lang.String, org.hibernate.transform.ResultTransformer)
*/
@Override
public List<?> getEntityBySQL(final String sql, final ResultTransformer transformer) throws ApplicationException, SystemRuntimeException {
try {
return hibernateTemplate.executeWithNativeSession(new HibernateCallback<List<?>>() {
public List<?> doInHibernate(Session session) throws HibernateException, SQLException {
SQLQueryImpl query = (SQLQueryImpl) session.createSQLQuery(sql);
if (transformer != null) {
query.setResultTransformer(transformer);
}
return query.list();
}
});
} catch (DataAccessException daex) {
throw new SystemRuntimeException(daex.getMessage(), daex);
}
}

/**
* Handle exception.
*
* @param throwable the throwable
* @throws ApplicationException the application exception
* @throws SystemRuntimeException the system runtime exception
*/
private void handleException(Throwable throwable) throws ApplicationException, SystemRuntimeException {
if (throwable instanceof DataAccessException) {
throw new SystemRuntimeException(throwable.getMessage(), throwable);
} else if (throwable instanceof RuntimeException) {
if (throwable.getCause() instanceof SystemRuntimeException) {
throw (SystemRuntimeException)throwable.getCause();
} else if (throwable.getCause() instanceof ApplicationException) {
throw (ApplicationException)throwable.getCause();
} else {
throw new SystemRuntimeException(throwable.getMessage(), throwable);
}
} else if (throwable instanceof SystemRuntimeException) {
throw (SystemRuntimeException)throwable;
} else if (throwable instanceof ApplicationException) {
throw (ApplicationException)throwable;
} else {
throw new SystemRuntimeException(throwable.getMessage(), throwable);
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值