gggfdgdfg

package org.hunter.hbzj.dao;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hunter.hbzj.javabean.AdvertBean;
import org.hunter.hbzj.presistbean.AdvertT;
import org.hunter.hbzj.util.HibUtil;
import org.hunter.hbzj.util.HibernateSessionFactory;
import org.hunter.hbzj.util.Utils;

import com.yourcompany.struts.form.AdvertForm;

public class AdvertDao {

private static Log log = LogFactory.getLog(AdvertDao.class);

/**
* 广告列表
*
* @param frm
* @return
*/
@SuppressWarnings("unchecked")
public ArrayList toList(AdvertForm frm) throws Exception {
ArrayList arraylist = new ArrayList();
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();

Criteria criteria = session.createCriteria(AdvertT.class);
if (!Utils.strTrim(frm.getAdposition()).equals(""))
criteria.add(Expression.eq("adposition", Utils.strTrim(frm
.getAdposition())));
if (Utils.getInt(frm.getAdtype()).intValue() > 0)
criteria
.add(Expression.eq("adtype", Utils.getInt(frm.getAdtype())));
// if(Utils.getInt(frm.getIsdisplay()).intValue()>0)
// criteria.add(Expression.eq("isdisplay",
// Utils.getInt(frm.getIsdisplay())));
criteria.addOrder(Order.asc("adorder"));
List list = criteria.list();
AdvertT tmpT;
AdvertBean bean;
for (int i = 0; i < list.size(); i++) {
tmpT = (AdvertT) list.get(i);
bean = new AdvertBean();
try {
BeanUtils.copyProperties(bean, tmpT);
} catch (IllegalAccessException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao显示广告列表发生异常,错误在toList(AdvertForm frm) 方法内",
e);
e.printStackTrace();
} catch (InvocationTargetException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao显示广告列表发生异常,错误在toList(AdvertForm frm) 方法内",
e);
e.printStackTrace();
}
arraylist.add(bean);
}
try {
tx.commit();
} catch (Exception e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao显示广告列表发生异常,错误在toList(AdvertForm frm) 方法内",
e);
tx.rollback();
e.printStackTrace();
} finally {
log
.error("hbda/org/hunter/hbzj/dao/AdvertDao显示广告列表toList(AdvertForm frm)");
session.close();
}
return arraylist;
}

/**
* 根据广告位置类别来查询广告列表
*
* @param count
* @param adposition
* @return
*/
@SuppressWarnings("unchecked")
public static ArrayList toShowAdvertList(int count, String adposition) {
ArrayList arraylist = new ArrayList();
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();

Criteria criteria = session.createCriteria(AdvertT.class);
criteria.add(Expression.eq("isdisplay", 1));
criteria.add(Expression.eq("adposition", adposition));
criteria.addOrder(Order.asc("adorder"));

criteria.setFirstResult(0);
criteria.setMaxResults(count);

List list = criteria.list();
AdvertT tmpT;
AdvertBean bean;
for (int i = 0; i < list.size(); i++) {
tmpT = (AdvertT) list.get(i);
bean = new AdvertBean();
try {
BeanUtils.copyProperties(bean, tmpT);
} catch (IllegalAccessException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao根据广告位置类别来查询广告列表发生异常,错误在toShowAdvertList(int count, String adposition) 方法内",
e);
e.printStackTrace();
} catch (InvocationTargetException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao根据广告位置类别来查询广告列表发生异常,错误在toShowAdvertList(int count, String adposition) 方法内",
e);
e.printStackTrace();
}
arraylist.add(bean);
}
try {
tx.commit();
} catch (Exception e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao根据广告位置类别来查询广告列表发生异常,错误在toShowAdvertList(int count, String adposition) 方法内",
e);
tx.rollback();
e.printStackTrace();
} finally {
log
.info("hbda/org/hunter/hbzj/dao/AdvertDao根据广告位置类别来查询广告列表toShowAdvertList(int count, String adposition) 方法");
session.close();
}
return arraylist;
}

/**
* 插入广告
*
* @param bean
* @return
*/
public boolean insert(AdvertBean bean) throws Exception {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
if (bean.getAdother() != null && bean.getAdother().trim().length() == 0) {
bean.setAdother("#");
}
AdvertT AdvertT = new AdvertT();
try {
BeanUtils.copyProperties(AdvertT, bean);
} catch (IllegalAccessException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao插入广告发生异常,错误在insert(AdvertBean bean) 方法内",
e);
e.printStackTrace();
} catch (InvocationTargetException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao插入广告发生异常,错误在insert(AdvertBean bean) 方法内",
e);
e.printStackTrace();
}
int adorder = bean.getAdorder();
if (adorder == 1) {
HibUtil.excuteHql(session,
"update AdvertT set adorder=adorder+1 where adtype="
+ bean.getAdtype() + " and adposition='"
+ bean.getAdposition() + "'");
}
try {
session.save(AdvertT);
tx.commit();
} catch (HibernateException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao插入广告发生异常,错误在insert(AdvertBean bean) 方法内",
e);
tx.rollback();
e.printStackTrace();
} catch (Exception e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao插入广告发生异常,错误在insert(AdvertBean bean) 方法内",
e);
tx.rollback();
e.printStackTrace();
} finally {
log
.info("hbda/org/hunter/hbzj/dao/AdvertDao插入广告insert(AdvertBean bean) 方法");
session.close();
}
return true;
}

/**
* 修改广告
*
* @param bean
* @return
*/
public boolean update(AdvertBean bean) throws Exception {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
AdvertT tmpT = (AdvertT) session.get(AdvertT.class, bean.getAdid());
Integer adoldorder = tmpT.getAdorder();
Integer adorder = bean.getAdorder();
if (bean.getAdother() != null && bean.getAdother().trim().length() == 0) {
bean.setAdother("#");
}
if (adorder != null) {
if (adoldorder > adorder) {
HibUtil.excuteHql(session,
"update AdvertT set adorder=adorder+1 where adtype="
+ bean.getAdtype() + " and adposition='"
+ bean.getAdposition() + "' and adorder>="
+ adorder + " and adorder<" + adoldorder);
} else if (adoldorder < adorder) {
HibUtil.excuteHql(session,
"update AdvertT set adorder=adorder-1 where adtype="
+ bean.getAdtype() + " and adposition='"
+ bean.getAdposition() + "' and adorder>"
+ adoldorder + " and adorder<=" + adorder);
}
}

try {
Utils.copy(tmpT, bean);
session.update(tmpT);
tx.commit();
} catch (HibernateException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao修改广告发生异常,错误在update(AdvertBean bean) 方法内",
e);
tx.rollback();
e.printStackTrace();
} catch (Exception e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao修改广告发生异常,错误在update(AdvertBean bean) 方法内",
e);
tx.rollback();
e.printStackTrace();
} finally {
log
.info("hbda/org/hunter/hbzj/dao/AdvertDao修改广告update(AdvertBean bean) 方法");
session.close();
}
return true;
}

public AdvertBean getObj(Object id) throws Exception {
Session session = HibernateSessionFactory.getSession();
AdvertBean bean = new AdvertBean();
try {
AdvertT AdvertT = (AdvertT) session.get(AdvertT.class, Utils
.getInt(id));
bean = new AdvertBean();
Utils.copy(bean, AdvertT);
} catch (HibernateException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao获得广告对象发生异常,错误在getObj(Object id) 方法内",
e);
e.printStackTrace();
} finally {
log
.info("hbda/org/hunter/hbzj/dao/AdvertDao获得广告对象getObj(Object id) 方法");
session.close();
}

return bean;
}

/**
* 删除广告
*
* @param ids
* @return
*/
public boolean delete(String[] ids) throws Exception {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();

try {
for (int i = 0; i < ids.length; i++) {
AdvertT advertT = (AdvertT) session.get(AdvertT.class, Integer
.parseInt(ids[i]));
int adtype = advertT.getAdtype();
String adposition = advertT.getAdposition();
int adorder = advertT.getAdorder();

if (advertT != null)
session.delete(advertT);
HibUtil.excuteHql(session,
"update AdvertT set adorder=adorder-1 where adtype="
+ adtype + " and adposition='" + adposition
+ "' and adorder>=" + adorder);
}
tx.commit();
} catch (HibernateException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao删除广告发生异常,错误在delete(String[] ids) 方法内",
e);
e.printStackTrace();
} catch (NumberFormatException e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao删除广告发生异常,错误在delete(String[] ids) 方法内",
e);
e.printStackTrace();
} catch (Exception e) {
log
.error(
"hbda/org/hunter/hbzj/dao/AdvertDao删除广告发生异常,错误在delete(String[] ids) 方法内",
e);
e.printStackTrace();
} finally {
log
.info("hbda/org/hunter/hbzj/dao/AdvertDao删除广告delete(String[] ids) 方法");
session.close();
}
return true;
}
}

你可能感兴趣的:(DAO,apache,bean,Hibernate,struts)