import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class BatchInOperationUtil {
private static final int PAGE_SIZE = 30;
private static final Probe PROBE = ProbeFactory.getProbe();
// private final static Log log = LogFactory.getLog(MethodUtils.class);
public static List<List<Object>> getSpiltListResult(List<Object> objectList) {
// 全集合大小
int size = objectList.size();
// 分割阀值1000
int pageSize = PAGE_SIZE;
// // 分割后的大小
// int spiltSize = (size + pageSize - 1) / pageSize;
if (size > pageSize) {
int spiltSize = (size + pageSize - 1) / pageSize;
List<List<Object>> spiltList = new ArrayList<List<Object>>(
spiltSize);
List<Object> spiltItem = null;
for (int i = 0; i < spiltSize; i++) {
int toIndex = (i + 1) * pageSize > (size - 1) ? size : (i + 1)
* pageSize;
spiltItem = objectList.subList(i * pageSize, toIndex);
spiltList.add(spiltItem);
}
return spiltList;
} else {
List<List<Object>> spiltList = new ArrayList<List<Object>>(1);
spiltList.add(objectList);
return spiltList;
}
// return spiltList;
}
/**
* add by tianliang
* @param objectList 参数不定义泛型
* @return
*/
public static List<List<Object>> getSpiltListResult2(List objectList) {
// 全集合大小
int size = objectList.size();
// 分割阀值1000
int pageSize = PAGE_SIZE;
// // 分割后的大小
// int spiltSize = (size + pageSize - 1) / pageSize;
if (size > pageSize) {
int spiltSize = (size + pageSize - 1) / pageSize;;
List<List<Object>> spiltList = new ArrayList<List<Object>>(
spiltSize);
List<Object> spiltItem = null;
for (int i = 0; i < spiltSize; i++) {
int toIndex = (i + 1) * pageSize > (size - 1) ? size : (i + 1)
* pageSize;
spiltItem = objectList.subList(i * pageSize, toIndex);
spiltList.add(spiltItem);
}
return spiltList;
} else {
List<List<Object>> spiltList = new ArrayList<List<Object>>(1);
spiltList.add(objectList);
return spiltList;
}
// return spiltList;
}
public static List<Object[]> getSpiltListResult(Object[] objectArray) {
// 全集合大小
int size = objectArray.length;
// 分割阀值1000
int pageSize = PAGE_SIZE;
// // 分割后的大小
// int spiltSize = (size + pageSize - 1) / pageSize;
Object[] spiltItem = null;
List<Object> spiltItemList = null;
if (size > pageSize) {
int spiltSize = (size + pageSize - 1) / pageSize;
List<Object[]> spiltList = new ArrayList<Object[]>(spiltSize);
List<Object> targetList = Arrays.asList(objectArray);
for (int i = 0; i < spiltSize; i++) {
int toIndex = (i + 1) * pageSize > (size - 1) ? size : (i + 1) * pageSize;
spiltItemList = targetList.subList(i * pageSize, toIndex);
spiltItem = new Object[spiltItemList.size()];
spiltList.add(spiltItemList.toArray(spiltItem));
}
return spiltList;
} else {
List<Object[]> spiltList = new ArrayList<Object[]>(1);
spiltList.add(objectArray);
return spiltList;
}
// return spiltList;
}
/**
* 根据属性名得到属性值
*
* @param paramObject
* 对象
* @param propertisName
* 对象属性
* @return 属性值
*/
public static Object getPropertisValue(Object paramObject,String propertisName) {
return PROBE.getObject(paramObject, propertisName);
}
/**
* 根据属性名设置对应的属性值
*
* @param paramObject
* 对象
* @param propertisName
* 对象属性
* @param objectValue
* 对象属性值
*/
public static void setPropertisValue(Object paramObject,String propertisName, Object objectValue) {
PROBE.setObject(paramObject, propertisName,objectValue);
}
}
public List doSpiltQueryForList(Object keyValue, String sqlMapId) {
// 返回值类型
List resultList = null;
List resultItem = null;
// 如果集合参数值为null
if (keyValue == null) {
resultList = getSqlMapClientTemplate().queryForList(sqlMapId, keyValue);
return resultList;
}
// 如果参数中集合属性是List
if (keyValue instanceof List) {
int size = ((List<Object>)keyValue).size();
if (size == 0) {
resultList = getSqlMapClientTemplate().queryForList(sqlMapId, keyValue);
return resultList;
}
List<List<Object>> spiltList = BatchInOperationUtil.getSpiltListResult((List<Object>)keyValue);
for (List<Object> item : spiltList) {
resultItem = getSqlMapClientTemplate().queryForList(sqlMapId, item);
if (resultList == null) {
resultList = new ArrayList();
}
resultList.addAll(resultItem);
}
// 如果参数中集合属性是数组
} else if (keyValue instanceof Object[]){
int size = ((Object[])keyValue).length;
if (size == 0) {
resultList = getSqlMapClientTemplate().queryForList(sqlMapId, keyValue);
return resultList;
}
List<Object[]> spiltList = BatchInOperationUtil.getSpiltListResult((Object[])keyValue);
for (Object[] item : spiltList) {
resultItem = getSqlMapClientTemplate().queryForList(sqlMapId, keyValue);
if (resultList == null) {
resultList = new ArrayList();
}
resultList.addAll(resultItem);
}
}
return resultList;
}
/**
* <pre>
* In集合分割查询通用方法,返回list结果集
* </pre>
*
* @param paramObject
* 查询参数(只支持map和javaBean)
* @param keyName
* 查询参数中集合类属性名(支持List和数组)
* @param sqlMapId
* sqlMap的id
* @return 返回需要执行的结果集
*/
public List doSpiltQueryForList(Object paramObject, String keyName, String sqlMapId) {
// 检测参数
if (paramObject == null || keyName == null) {
throw new BusinessException("ALL-MESSAGE", new String[] { "参数有误." });
}
// 获取参数中集合类型的属性值
Object keyValue = BatchInOperationUtil.getPropertisValue(paramObject, keyName);
// 返回值类型
List resultList = null;
List resultItem = null;
// 如果集合参数值为null
if (keyValue == null) {
resultList = getSqlMapClientTemplate().queryForList(sqlMapId, paramObject);
return resultList;
}
// 如果参数中集合属性是List
if (keyValue instanceof List) {
int size = ((List<Object>)keyValue).size();
if (size == 0) {
resultList = getSqlMapClientTemplate().queryForList(sqlMapId, paramObject);
return resultList;
}
List<List<Object>> spiltList = BatchInOperationUtil.getSpiltListResult((List<Object>)keyValue);
for (List<Object> item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
resultItem = getSqlMapClientTemplate().queryForList(sqlMapId, paramObject);
if (resultList == null) {
resultList = new ArrayList();
}
resultList.addAll(resultItem);
}
// 如果参数中集合属性是数组
} else if (keyValue instanceof Object[]){
int size = ((Object[])keyValue).length;
if (size == 0) {
resultList = getSqlMapClientTemplate().queryForList(sqlMapId, paramObject);
return resultList;
}
List<Object[]> spiltList = BatchInOperationUtil.getSpiltListResult((Object[])keyValue);
for (Object[] item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
resultItem = getSqlMapClientTemplate().queryForList(sqlMapId, paramObject);
if (resultList == null) {
resultList = new ArrayList();
}
resultList.addAll(resultItem);
}
}
// 还原参数
BatchInOperationUtil.setPropertisValue(paramObject, keyName, keyValue);
return resultList;
}
/**
* <pre>
* In集合分割查询通用方法,返回Map结果集
* </pre>
*
* @param paramObject
* 查询参数(只支持map和javaBean)
* @param keyName
* 查询参数中集合类属性名(支持List和数组)
* @param mapKey
* 返回map集合中key值对应的属性名
* @param sqlMapId
* sqlMap的id
* @return 返回执行的结果集
*/
public Map doSpiltQueryForMap(Object paramObject, String keyName, String mapKey, String sqlMapId) {
// 检测参数
if (paramObject == null || keyName == null) {
throw new BusinessException("ALL-MESSAGE", new String[] { "参数有误." });
}
// 获取参数中集合类型的属性值
Object keyValue = BatchInOperationUtil.getPropertisValue(paramObject, keyName);
// 返回值类型
Map resultMap = null;
Map resultItem = null;
// 如果集合参数值为null
if (keyValue == null) {
resultMap = getSqlMapClientTemplate().queryForMap(sqlMapId, paramObject,mapKey);
return resultMap;
}
// 如果参数中集合属性是List
if (keyValue instanceof List) {
int size = ((List<Object>)keyValue).size();
if (size == 0) {
resultMap = getSqlMapClientTemplate().queryForMap(sqlMapId, paramObject,mapKey);
return resultMap;
}
List<List<Object>> spiltList = BatchInOperationUtil.getSpiltListResult((List<Object>)keyValue);
for (List<Object> item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
resultItem = getSqlMapClientTemplate().queryForMap(sqlMapId, paramObject,mapKey);
if (resultMap == null) {
resultMap = new HashMap();
}
resultMap.putAll((Map) resultItem);
}
// 如果参数中集合属性是数组
} else if (keyValue instanceof Object[]){
int size = ((Object[])keyValue).length;
if (size == 0) {
resultMap = getSqlMapClientTemplate().queryForMap(sqlMapId, paramObject,mapKey);
return resultMap;
}
List<Object[]> spiltList = BatchInOperationUtil.getSpiltListResult((Object[])keyValue);
for (Object[] item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
resultItem = getSqlMapClientTemplate().queryForMap(sqlMapId, paramObject,mapKey);
if (resultMap == null) {
resultMap = new HashMap();
}
resultMap.putAll((Map) resultItem);
}
}
// 还原参数
BatchInOperationUtil.setPropertisValue(paramObject, keyName, keyValue);
return resultMap;
}
/**
* <pre>
* In集合分割更新通用方法
* </pre>
*
* @param paramObject
* 查询参数(只支持map和javaBean)
* @param keyName
* 查询参数中集合类属性名(支持List和数组)
* @param sqlMapId
* sqlMap的id
* @return 返回执行结果影响的行数
*/
public int doSpiltForUpdate(Object paramObject, String keyName, String sqlMapId) {
// 检测参数
if (paramObject == null || keyName == null) {
throw new BusinessException("ALL-MESSAGE", new String[] { "参数有误." });
}
// 获取参数中集合类型的属性值
Object keyValue = BatchInOperationUtil.getPropertisValue(paramObject, keyName);
// 返回值类型
int resultCout = 0;
if (keyValue == null) {
return resultCout;
}
// 如果参数中集合属性是List
if (keyValue instanceof List) {
List<List<Object>> spiltList = BatchInOperationUtil.getSpiltListResult((List<Object>)keyValue);
for (List<Object> item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
int resultItem = getSqlMapClientTemplate().update(sqlMapId, paramObject);
resultCout = resultCout + resultItem;
}
// 如果参数中集合属性是数组
} else if (keyValue instanceof Object[]) {
List<Object[]> spiltList = BatchInOperationUtil.getSpiltListResult((Object[])keyValue);
for (Object[] item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
int resultItem = getSqlMapClientTemplate().update(sqlMapId, paramObject);
resultCout = resultCout + resultItem;
}
}
// 还原参数
BatchInOperationUtil.setPropertisValue(paramObject, keyName, keyValue);
return resultCout;
}
public int doSpiltForDelete(Object keyValue, String sqlMapId) {
// 返回值类型
int resultCout = 0;
if (keyValue == null) {
return resultCout;
}
// 如果参数中集合属性是List
if (keyValue instanceof List) {
List<List<Object>> spiltList = BatchInOperationUtil.getSpiltListResult((List<Object>)keyValue);
for (List<Object> item : spiltList) {
int resultItem = getSqlMapClientTemplate().delete(sqlMapId, item);
resultCout = resultCout + resultItem;
}
// 如果参数中集合属性是数组
} else if (keyValue instanceof Object[]){
List<Object[]> spiltList = BatchInOperationUtil.getSpiltListResult((Object[])keyValue);
for (Object[] item : spiltList) {
int resultItem = getSqlMapClientTemplate().delete(sqlMapId, item);
resultCout = resultCout + resultItem;
}
}
return resultCout;
}
/**
* <pre>
* In集合分割删除通用方法
* </pre>
*
* @param paramObject
* 查询参数(只支持map和javaBean)
* @param keyName
* 查询参数中集合类属性名(支持List和数组)
* @param sqlMapId
* sqlMap的id
* @return 返回执行结果影响的行数
*/
public int doSpiltForDelete(Object paramObject, String keyName, String sqlMapId) {
// 检测参数
if (paramObject == null || keyName == null) {
throw new BusinessException("ALL-MESSAGE", new String[] { "参数有误." });
}
// 获取参数中集合类型的属性值
Object keyValue = BatchInOperationUtil.getPropertisValue(paramObject, keyName);
// 返回值类型
int resultCout = 0;
if (keyValue == null) {
return resultCout;
}
// 如果参数中集合属性是List
if (keyValue instanceof List) {
List<List<Object>> spiltList = BatchInOperationUtil.getSpiltListResult((List<Object>)keyValue);
for (List<Object> item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
int resultItem = getSqlMapClientTemplate().delete(sqlMapId, paramObject);
resultCout = resultCout + resultItem;
}
// 如果参数中集合属性是数组
} else if (keyValue instanceof Object[]){
List<Object[]> spiltList = BatchInOperationUtil.getSpiltListResult((Object[])keyValue);
for (Object[] item : spiltList) {
// 重新把分割单元参数赋值
BatchInOperationUtil.setPropertisValue(paramObject, keyName, item);
int resultItem = getSqlMapClientTemplate().delete(sqlMapId, paramObject);
resultCout = resultCout + resultItem;
}
}
// 还原参数
BatchInOperationUtil.setPropertisValue(paramObject, keyName, keyValue);
return resultCout;
}
|
/**
* Data Access Object (Dao) interface. This is an interface used to tag our Dao
* classes and to provide common methods to all Daos.
*
* @author
* @version 0.1, 2008-3-9
* @since JDK1.6
*/
public interface BaseDao <T extends BaseModel, PK extends Serializable> {
/**
* Generic method used to get all objects of a particular type. This
* is the same as lookup up all rows in a table.
* @return List of populated objects
*/
List<T> getAll();
/**
* Generic method to get an object based on class and identifier. An
* ObjectRetrievalFailureException Runtime Exception is thrown if
* nothing is found.
*
* @param id the identifier (primary key) of the object to get
* @return a populated object
* @see org.springframework.orm.ObjectRetrievalFailureException
*/
T get(PK id);
/**
* Checks for existence of an object of type T using the id arg.
* @param id the id of the entity
* @return - true if it exists, false if it doesn't
*/
boolean exists(PK id);
/**
* Generic method to save an object - handles both update and insert.
* @param object the object to save
* @return the persisted object
*/
T save(T object);
/**
* Generic method to save an object - handles both update and insert.
* @param object the object to save
*/
void updateSelective(T object);
/**
* Generic method to delete an object based on class and id
* @param id the identifier (primary key) of the object to remove
*/
void remove(PK id);
}
/**
* Data Access Object (Dao) class. This is an base class used to tag our Dao
* classes and to provide common methods to all Daos.
*
* @author
* @version 0.1, 2008-3-9
* @since JDK1.6
*/
public abstract class BaseDaoImpl <T extends BaseModel, PK extends Serializable>
extends SqlMapClientDaoSupport implements BaseDao<T, PK> {
/**
* Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
*/
protected final Log log = LogFactory.getLog(getClass());
private Class<T> persistentClass;
/**
* Constructor that takes in a class to see which type of entity to persist
* @param persistentClass the class type you'd like to persist
*/
public BaseDaoImpl(final Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<T> getAll() {
return getSqlMapClientTemplate().queryForList(
IBatisDaoUtils.getAllObjectQuery(ClassUtils.getShortName(this.persistentClass)));
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public T get(PK id) {
T object = (T) getSqlMapClientTemplate().queryForObject(
IBatisDaoUtils.getObjectQuery(ClassUtils.getShortName(this.persistentClass)), id);
if (object == null) {
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
//throw new ObjectRetrievalFailureException(ClassUtils.getShortName(this.persistentClass), id);
BusinessException e = new BusinessException("D0000002");
e.addParam(ClassUtils.getShortName(this.persistentClass));
e.addParam(id.toString());
throw e;
}
return object;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public boolean exists(PK id) {
T object = (T) getSqlMapClientTemplate().queryForObject(
IBatisDaoUtils.getObjectQuery(ClassUtils.getShortName(this.persistentClass)), id);
return object != null;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public T save(final T object) {
String className = ClassUtils.getShortName(object.getClass());
Object primaryKey = IBatisDaoUtils.getPrimaryKeyValue(object);
int affectRecNum = 0;
String keyId = null;
// check for null id
if (primaryKey != null
&& ((Long) primaryKey).longValue() != 0L) {
keyId = primaryKey.toString();
}
// check for new record
if (StringUtils.isBlank(keyId)) {
IBatisDaoUtils.prepareObjectForSaveOrUpdate(object);
primaryKey = getSqlMapClientTemplate().insert(IBatisDaoUtils.getInsertQuery(className), object);
IBatisDaoUtils.setPrimaryKey(object, primaryKey);
} else {
IBatisDaoUtils.prepareObjectForSaveOrUpdate(object);
affectRecNum = getSqlMapClientTemplate().update(IBatisDaoUtils.getUpdateQuery(className), object);
if (affectRecNum == 0){
BusinessException e = new BusinessException("D0000001");
String parma = (primaryKey == null || ((Long) primaryKey).longValue() != 0L) ? null : primaryKey.toString();
e.addParam(parma);
throw e;
}
if(object instanceof ReqCacheable){
((ReqCacheable)object).freshReqCache();
}
}
// check for null id
if (IBatisDaoUtils.getPrimaryKeyValue(object) == null) {
//throw new ObjectRetrievalFailureException(className, object);
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + primaryKey + "' not found...");
BusinessException e = new BusinessException("D0000002");
e.addParam(ClassUtils.getShortName(this.persistentClass));
String parma = (primaryKey == null || ((Long) primaryKey).longValue() != 0L) ? null : primaryKey.toString();
e.addParam(parma);
throw e;
} else {
return object;
}
}
/**
* {@inheritDoc}
*/
public void updateSelective(T object){
String className = ClassUtils.getShortName(object.getClass());
Object primaryKey = IBatisDaoUtils.getPrimaryKeyValue(object);
// check for null id
if (primaryKey != null && ((Long) primaryKey).longValue() != 0L) {
IBatisDaoUtils.prepareObjectForSaveOrUpdate(object);
getSqlMapClientTemplate().update(IBatisDaoUtils.getUpdateSelectiveQuery(className), object);
if(object instanceof ReqCacheable){
((ReqCacheable)object).freshReqCache();
}
} else {
//throw new ObjectRetrievalFailureException(className, object);
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + primaryKey + "' not found...");
BusinessException e = new BusinessException("D0000002");
e.addParam(ClassUtils.getShortName(this.persistentClass));
String parma = (primaryKey == null || ((Long) primaryKey).longValue() != 0L) ? null : primaryKey.toString();
e.addParam(parma);
throw e;
}
}
/**
* {@inheritDoc}
*/
public void remove(PK id) {
getSqlMapClientTemplate().update(
IBatisDaoUtils.getRemoveQuery(ClassUtils.getShortName(this.persistentClass)), id);
}
@SuppressWarnings("unchecked")
public Pagination findObjectsWithPg(String statementName, BaseDto dto){
Pagination pg = new Pagination();
//dto.setNeedCount(true);
Object count= getSqlMapClientTemplate().queryForObject(IBatisDaoUtils.getCountQuery(statementName), dto);
int totalCount = Integer.parseInt(count.toString());
dto.setTotalCount(totalCount);
dto.calStart();
//dto.setNeedCount(false);
List rstList = getSqlMapClientTemplate()
.queryForList(statementName, dto);
BeanUtil.copyProperties(pg, dto);
pg.setTotalCount(totalCount);
pg.setResultList(rstList);
return pg;
}
}
/**
* Base class for Model objects. This is an interface used to tag our Model
* classes and to provide common methods to all Model.
*
* @author evan
* @version 0.1, 2008-3-9
* @since JDK1.6
*/
public interface BaseModel extends Serializable{
public abstract String toString();
public abstract boolean equals(Object o);
public abstract int hashCode();
}
public abstract class BaseModelImpl implements BaseModel{
public Integer mcSiteId = null;
public Integer getMcSiteId(){
return mcSiteId;
}
public void setMcSiteId(Integer mcSiteId){
this.mcSiteId = mcSiteId;
}
}
public abstract class BaseModelAdapter extends BaseModelImpl {
/**
*
*/
private static final long serialVersionUID = 247618411957181774L;
protected final Log log = LogFactory.getLog(getClass());
public static final int FLAG_NO = 0;
public static final int FLAG_YES = 1;
public static final int IS_DELETE_NO = 0;
public static final int IS_DELETE_YES = 1;
// add by chensj
protected String moduleTable; //对应的表名
protected Map<String,Object> fieldMap; //字段Map,核心,用于存储字段及其值
public String toString() {
StringBuffer propBuffer = new StringBuffer();
Field[] fields = this.getClass().getDeclaredFields();
propBuffer.append("[").append(this.getClass()).append("]");
for (Field field : fields) {
String fieldName = field.getName();
Object fieldValue = null;
String getterMethod = "get" + Character.toUpperCase(fieldName.charAt(0))
+ fieldName.substring(1);
try {
Method getMethod = this.getClass().getMethod(getterMethod, (Class[]) null);
Object o = getMethod.invoke(this, (Object[]) null);
if (o instanceof Map || o instanceof List) {
continue;
}
if (o instanceof BaseModel) {
fieldName += "Id";
fieldValue = IBatisDaoUtils.getPrimaryKeyValue((BaseModel) o);
} else {
fieldValue = o;
}
propBuffer.append(fieldName).append(":").append(fieldValue).append(",");
} catch (Exception e) {
}
}
return propBuffer.substring(0, propBuffer.length() - 1);
}
public String getModuleTable() {
return moduleTable;
}
public void setModuleTable(String moduleTable) {
this.moduleTable = moduleTable;
}
public void setFieldValue(String key,Object value){
if(null != fieldMap && fieldMap.size() > 0){
fieldMap.put(key,value);
}else {
fieldMap = new HashMap<String, Object>();
fieldMap.put(key,value);
}
}
/**
* @see 返回fieldValueList
* @return
*/
public List getFieldValueList(){
List<KeyVO> keyVoList = new ArrayList<KeyVO>();
if(null != fieldMap && fieldMap.size() > 0){
for ( Map.Entry<String,Object> entry : fieldMap.entrySet()) {
KeyVO keyVo = new KeyVO();
keyVo.setKey(entry.getKey());
keyVo.setValue(entry.getValue());
keyVoList.add(keyVo);
}
return keyVoList;
}
return null;
}
}
public abstract class BaseModelImpl implements BaseModel{
public Integer mcSiteId = null;
public Integer getMcSiteId(){
return mcSiteId;
}
public void setMcSiteId(Integer mcSiteId){
this.mcSiteId = mcSiteId;
}
}
public class TempProduct extends BaseModelAdapter{
}
public interface TempProductDao extends BaseDao<TempProduct, Long> {
}
public class TempProductDaoImpl extends BaseProductDao<TempProduct, Long> implements TempProductDao{
public TempProductDaoImpl() {
super(TempProduct.class);
}
}
public class BaseProductDao <T extends BaseModel, PK extends Serializable>
extends SqlMapClientDaoSupport implements BaseDao<T, PK>{
@Autowired(required=false)@Qualifier("dalClientTemplate")
private org.springframework.orm.ibatis.SqlMapClientTemplate dalClientTemplate;
public String getNameSpace() {
return this.getClass().getName();
}
@PostConstruct
public void initSqlMapClient(){
if(dalClientTemplate!=null){
this.setSqlMapClientTemplate(dalClientTemplate);
}
}
/**
* Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
*/
protected final Log log = LogFactory.getLog(getClass());
private Class<T> persistentClass;
/**
* Constructor that takes in a class to see which type of entity to persist
* @param persistentClass the class type you'd like to persist
*/
public BaseProductDao(final Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<T> getAll() {
return getSqlMapClientTemplate().queryForList(
IBatisDaoUtils.getAllObjectQuery(ClassUtils.getShortName(this.persistentClass)));
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public T get(PK id) {
T object = (T) getSqlMapClientTemplate().queryForObject(
IBatisDaoUtils.getObjectQuery(ClassUtils.getShortName(this.persistentClass)), id);
if (object == null) {
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
//throw new ObjectRetrievalFailureException(ClassUtils.getShortName(this.persistentClass), id);
BusinessException e = new BusinessException("D0000002");
e.addParam(ClassUtils.getShortName(this.persistentClass));
e.addParam(id.toString());
throw e;
}
return object;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public boolean exists(PK id) {
T object = (T) getSqlMapClientTemplate().queryForObject(
IBatisDaoUtils.getObjectQuery(ClassUtils.getShortName(this.persistentClass)), id);
return object != null;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public T save(final T object) {
String className = ClassUtils.getShortName(object.getClass());
Object primaryKey = IBatisDaoUtils.getPrimaryKeyValue(object);
int affectRecNum = 0;
String keyId = null;
// check for null id
if (primaryKey != null
&& ((Long) primaryKey).longValue() != 0L) {
keyId = primaryKey.toString();
}
// check for new record
if (StringUtils.isBlank(keyId)) {
//IBatisDaoUtils.prepareObjectForSaveOrUpdate(object);
this.prepareObjectForSaveOrUpdate(object);
primaryKey = getSqlMapClientTemplate().insert(IBatisDaoUtils.getInsertQuery(className), object);
//IBatisDaoUtils.setPrimaryKey(object, primaryKey);
this.setPrimaryKey(object, primaryKey);
} else {
//IBatisDaoUtils.prepareObjectForSaveOrUpdate(object);
this.prepareObjectForSaveOrUpdate(object);
affectRecNum = getSqlMapClientTemplate().update(IBatisDaoUtils.getUpdateQuery(className), object);
if (affectRecNum == 0){
BusinessException e = new BusinessException("D0000001");
String parma = (primaryKey == null || ((Long) primaryKey).longValue() != 0L) ? null : primaryKey.toString();
e.addParam(parma);
throw e;
}
if(object instanceof ReqCacheable){
((ReqCacheable)object).freshReqCache();
}
}
// check for null id
if (IBatisDaoUtils.getPrimaryKeyValue(object) == null) {
//throw new ObjectRetrievalFailureException(className, object);
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + primaryKey + "' not found...");
BusinessException e = new BusinessException("D0000002");
e.addParam(ClassUtils.getShortName(this.persistentClass));
String parma = (primaryKey == null || ((Long) primaryKey).longValue() != 0L) ? null : primaryKey.toString();
e.addParam(parma);
throw e;
} else {
return object;
}
}
private void prepareObjectForSaveOrUpdate(Object object){
try {
Method mothod = IBatisDaoUtils.class.getDeclaredMethod("prepareObjectForSaveOrUpdate", new Class[]{Object.class});
mothod.setAccessible(true);
mothod.invoke(null, object);
} catch (Exception e) {
// TODO: handle exception
}
}
private void setPrimaryKey(BaseModel object, Object primaryKey){
try {
Method mothod = IBatisDaoUtils.class.getDeclaredMethod("setPrimaryKey", new Class[]{BaseModel.class,Object.class});
mothod.setAccessible(true);
mothod.invoke(null, new Object[]{object,primaryKey});
} catch (Exception e) {
}
}
private String getCountQuery(String statementName){
try {
Method mothod = IBatisDaoUtils.class.getDeclaredMethod("getCountQuery", new Class[]{String.class});
mothod.setAccessible(true);
String a = (String)mothod.invoke(null, new Object[]{statementName});
return a;
} catch (Exception e) {
return "";
}
}
/**
* {@inheritDoc}
*/
public void updateSelective(T object){
String className = ClassUtils.getShortName(object.getClass());
Object primaryKey = IBatisDaoUtils.getPrimaryKeyValue(object);
// check for null id
if (primaryKey != null && ((Long) primaryKey).longValue() != 0L) {
//IBatisDaoUtils.prepareObjectForSaveOrUpdate(object);
this.prepareObjectForSaveOrUpdate(object);
getSqlMapClientTemplate().update(IBatisDaoUtils.getUpdateSelectiveQuery(className), object);
if(object instanceof ReqCacheable){
((ReqCacheable)object).freshReqCache();
}
} else {
//throw new ObjectRetrievalFailureException(className, object);
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + primaryKey + "' not found...");
BusinessException e = new BusinessException("D0000002");
e.addParam(ClassUtils.getShortName(this.persistentClass));
String parma = (primaryKey == null || ((Long) primaryKey).longValue() != 0L) ? null : primaryKey.toString();
e.addParam(parma);
throw e;
}
}
/**
* {@inheritDoc}
*/
public void remove(PK id) {
getSqlMapClientTemplate().update(
IBatisDaoUtils.getRemoveQuery(ClassUtils.getShortName(this.persistentClass)), id);
}
@SuppressWarnings("unchecked")
public Pagination findObjectsWithPg(String statementName, BaseDto dto){
Pagination pg = new Pagination();
//dto.setNeedCount(true);
Object count= getSqlMapClientTemplate().queryForObject(this.getCountQuery(statementName), dto);
int totalCount = Integer.parseInt(count.toString());
dto.setTotalCount(totalCount);
dto.calStart();
//dto.setNeedCount(false);
List rstList = getSqlMapClientTemplate()
.queryForList(statementName, dto);
BeanUtil.copyProperties(pg, dto);
pg.setTotalCount(totalCount);
pg.setResultList(rstList);
return pg;
}
}
/**
* BaseManager
*
* @author
* @version 0.1, 2008-3-9
* @since JDK1.6
*/
public interface BaseManager extends MessageSourceAware{
}
/**
* BaseManagerImpl
*
* @author
* @version 0.1, 2008-3-9
* @since JDK1.6
*/
public abstract class BaseManagerImpl implements BaseManager {
protected final Log log = LogFactory.getLog(getClass());
protected MessageSource messageSource;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
}
public interface TempProductManager extends BaseManager {
}
public class TempProductManagerImpl extends BaseManagerImpl implements TempProductManager {
}
|