⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postsdaoimpl.java

📁 论坛软件系统亦称电子公告板(BBS)系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package cn.jsprun.dao.posts;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.jsprun.domain.Posts;
import cn.jsprun.domain.Members;
import cn.jsprun.struts.form.PostsPageForm;
import cn.jsprun.struts.form.posts.ModrepliesPageForm;
import cn.jsprun.struts.form.posts.PageForm;
import cn.jsprun.utils.HibernateUtil;
public class PostsDaoImpl implements PostsDao {

	public Posts getPostsById(Integer pid) {
		Transaction tr = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Posts posts = (Posts)session.get(Posts.class, pid);
			tr.commit();
			return posts;
		}catch(Exception exception){
			if(tr!=null){
				tr.rollback();
			}
			return null;
		}
	}
	public boolean deletePostsByUserName(String userName) {
		Transaction tr = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("delete from Posts p where p.author = ?");
			query.setString(0, userName);
			query.executeUpdate();
			query = session.createQuery("delete from Threads t where t.author = ?");
			query.setString(0, userName);
			query.executeUpdate();
			query = session.createQuery("delete from Forumrecommend f where f.author = ?");
			query.setString(0, userName);
			query.executeUpdate();
			tr.commit();
			return true;
		}catch(Exception exception){
			exception.printStackTrace();
			if(tr!=null){
				tr.rollback();
			}
			return false;
		}
	}
	public boolean modifyPosts(Posts posts) {
		Transaction tr = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			session.update(posts);
			tr.commit();
			return true;
		}catch(Exception exception){
			exception.printStackTrace();
			if(tr!=null){
				tr.rollback();
			}
			return false;
		}
	}
	public List<Posts> findByUserName(String userName) {
		Transaction tr = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Posts p where p.author = ?");
			query.setString(0, userName);
			List<Posts> postList = query.list();
			tr.commit();
			return postList;
		}catch(Exception exception){
			exception.printStackTrace();
			if(tr!=null){
				tr.rollback();
			}
			return null;
		}
	}
	public List<Posts> findByPosts(Posts posts) {
		Transaction tr = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Posts p");
			List<Posts> postList = query.list();
			tr.commit();
			return postList;
		}catch(Exception exception){
			if(tr!=null){
				tr.rollback();
			}
			exception.printStackTrace();
			return null;
		}
	}
	public PostsPageForm fidnByForums(short fid,String displayorder) {

		StringBuffer queryStr = new StringBuffer(
				"select p.*,f.name from jrun_posts as p left join jrun_threads as t on p.tid=t.tid left join jrun_forums as f on p.fid=f.fid where p.first=1 and t.displayorder="
						+ displayorder+ " and p.invisible=-2 and t.moderated=0");
		StringBuffer countStr = new StringBuffer(
				"select count(*) from Posts as p,Threads as t where p.tid=t.tid and first=1 and t.displayorder="
						+ displayorder+ " and p.invisible=-2 and t.moderated=0");
		if (fid > 0) {
			queryStr.append(" and p.fid=" + fid );
			countStr.append(" and p.fid=" + fid );
		}
		PostsPageForm ppf = new PostsPageForm(countStr.toString(), queryStr.toString());
		return ppf;
	}
	private void updateSQL(String str) throws Exception {
		Transaction tr = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			 tr = session.beginTransaction();
			Query query = session.createQuery(str);
			query.executeUpdate();
			session.flush();
			tr.commit();
		} catch (Exception e) {
			if (tr != null)
				tr.rollback();
			tr = null;
			e.printStackTrace();
		}
	}
	public List selectSQL(String str) {
		List<Posts> postsList = new ArrayList<Posts>();
		Transaction tr= null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query query = session.createQuery(str);
			postsList = query.list();
			session.flush();
			tr.commit();
		} catch (Exception e) {
			postsList.clear();
			if (tr != null)
				tr.rollback();
			tr = null;
			e.printStackTrace();
		}

		return postsList;
	}

	public PageForm showPostsPage() {
		StringBuffer queryStr = new StringBuffer(
				"select p from Posts as p,Threads as t where p.tid=t.tid and t.displayorder=-2 and p.invisible=-2 and t.moderated=0");
		List<Posts> postList = new ArrayList<Posts>();
		Transaction tr = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query query = session.createQuery(queryStr.toString());
			postList = query.list();
			tr.commit();
		} catch (Exception e) {
			if(tr!=null){
				tr.rollback();
			}
			e.printStackTrace();
		}
		return null;
	}
	public ModrepliesPageForm fidnByModreplies(short fid,String invisible) {
		StringBuffer queryStr = new StringBuffer("select p.*,f.name,t.subject as threadsubject from jrun_posts as p left join jrun_forums as f on p.fid=f.fid left join jrun_threads as t on t.tid=p.tid where p.first=0 and p.invisible="+invisible);
		StringBuffer countStr = new StringBuffer("select count(*) from Posts as p where p.first=0 and p.invisible="+invisible);
			if (fid > 0) {
				queryStr.append(" and p.fid=" + fid );
				countStr.append(" and p.fid=" + fid );
			}
		ModrepliesPageForm mpf = new ModrepliesPageForm(countStr.toString(),queryStr.toString());
		return mpf;
	}
	public Map batchPrune(String sb, boolean b) {
		Map m = new HashMap<String, Integer>();
		Transaction tr = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				tr = session.beginTransaction();
				if (b == true) {
					StringBuffer showPosts = new StringBuffer(
							"from Posts as p where p.pid in (");
	
					Object[] tids = sb.split(",");
					for (int i = 0; i < tids.length; i++) {
						showPosts.append(tids[i].toString());
						showPosts.append(",");
					}
					String str = showPosts.substring(0, showPosts.length() - 1);
					str = str + ")";
					Query query = session.createQuery(str);
					List<Posts> postsList = query.list();
					session.flush();
	
					for (int i = 0; i < postsList.size(); i++) {
						Integer num = postsList.get(i).getAuthorid();
						Members member = (Members) session.get(Members.class, num);
						member.setPosts(member.getPosts() - 1);
						member.setCredits(member.getCredits() - 1);
						Date d = member.getBday();
						if (d == null) {
							member.setBday(new Date());
						}
						session.update(member);
					}
					session.flush();
				}
				String[] pids = sb.split(",");
				int threads = 0;
				int posts = 0;
				for (int i = 0; i < pids.length; i++) {
					if (pids[i] != null && !pids[i].equals("")) {
						Posts p = (Posts) session.get(Posts.class, Integer
								.valueOf(pids[i]));
						if (p != null) {
							if (p.getFirst() == 1) {
								posts += this.deleteThreads(p.getTid());
								threads++;

							}
							if (p.getFirst() == 0) {
								Query myquery = session
										.createQuery("delete from Posts as p where p.pid = :pid");
								myquery.setInteger("pid", p.getPid());
								posts = myquery.executeUpdate();
								session.flush();
							}
						}
					}

				}
				m.put("threads", threads);
				m.put("posts", posts);
				tr.commit();
			} catch (HibernateException he) {
				if(tr!=null){
					tr.rollback();
				}
				he.printStackTrace();
			} 
		return m;
	}
	private Integer deleteThreads(Integer tid) {
		int ps = 0;
		Transaction tr = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query myquery = session.createQuery("delete from Posts as p where p.tid = :tid");
			myquery.setInteger("tid", tid);
			ps = myquery.executeUpdate();
			session.flush();
			myquery = session
					.createQuery("delete from Threads as t where t.tid = :tid");
			myquery.setInteger("tid", tid);
			myquery.executeUpdate();
			session.flush();
			tr.commit();
		}catch (Exception e) {
			if(tr!=null){
				tr.rollback();
			}
		}
		return ps;
	}
	public int tableTemplate(StringBuffer querystr, String sbtid)
			throws Exception {
		int num = -1;
		if (sbtid == null && sbtid.equals(""))
			return num;

		querystr.append(" where t.tid in (");
		String[] tids = sbtid.split(",");
		if (tids.length == 1) {
			if (tids[0].trim() == null || tids[0].trim().equals("")) {
				return num;
			}
		}

		for (int i = 0; i < tids.length; i++) {
			querystr.append(tids[i]);
			querystr.append(",");
		}
		String str = querystr.substring(0, querystr.length() - 1);
		str = str + ")";
		Transaction tr = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query myquery = session.createQuery(str);
			num = myquery.executeUpdate();
			tr.commit();
		} catch (HibernateException he) {
			if(tr!=null){
				tr.rollback();
			}
			he.printStackTrace();
		}

		return num;
	}
	public void deleteForumrecommend(StringBuffer deleteSB) {

		StringBuffer deleteString = new StringBuffer(
				"delete from Forumrecommend as t");
		try {
			this.tableTemplate(deleteString, deleteSB.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Integer updateForumrecommendList(List list) {
		int updateNumber = -1;
		String updateStr = null;

		if (list == null && list.size() <= 0)
			return updateNumber = 0;
		Transaction tr = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			for (int i = 0; i < list.size(); i++) {
				Map m = (Map) list.get(i);
				if (m != null) {
					updateStr = "update Forumrecommend as f set f.displayorder =:displayorder where f.tid = :tid";
					Query query = session.createQuery(updateStr);

					String displayorder = m.get("displayorder").toString();
					try {
						int dis = Integer.valueOf(displayorder);
						if (dis > 127) {
							displayorder = "127";
						}
					} catch (NumberFormatException nfe) {
						displayorder = "127";
					}

					query.setString("displayorder", displayorder);
					query.setInteger("tid", Integer.valueOf(m.get("tid")
							.toString()));
					query.executeUpdate();
				}
			}
			tr.commit();
		} catch (HibernateException he) {
			if(tr!=null){
				tr.rollback();
			}
			he.printStackTrace();
		}
		return updateNumber;
	}
	public boolean deleteModrepliesIDArray(List<String> modrepliesList) {

		StringBuffer sb = new StringBuffer( "delete from Posts as p where p.pid in (");
		StringBuffer sba = new StringBuffer( "delete from Attachments as p where p.pid in (");
		for (int i = 0; i < modrepliesList.size(); i++) {
			sb.append(modrepliesList.get(i) + ",");
			sba.append(modrepliesList.get(i) + ",");
		}
		String strsql = sb.substring(0, sb.length() - 1);
		String strsqla = sba.substring(0, sba.length() - 1);
		strsql = strsql + ")";
		strsqla = strsqla + ")";
		try {
			updateSQL(strsql);
			updateSQL(strsqla);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public boolean ignoreModrepliesIDArray(List<String> modrepliesList) {
		StringBuffer sb = new StringBuffer(
				"update Posts as p set p.invisible=-3 where p.pid in (");
		for (int i = 0; i < modrepliesList.size(); i++) {
			sb.append(modrepliesList.get(i) + ",");
		}
		String strsql = sb.substring(0, sb.length() - 1);
		strsql = strsql + ")";
		try {
			updateSQL(strsql);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	public boolean validateModrepliesIDArray(List<Posts> modrepliesList) {
		StringBuffer sb = new StringBuffer("update Posts as p set p.invisible=0 where p.pid in (");
		for (int i = 0; i < modrepliesList.size(); i++) {
			Posts p = modrepliesList.get(i);
			sb.append(p.getPid() + ",");
		}
		String strsql = sb.substring(0, sb.length() - 1);
		strsql = strsql + ")";
		try {
			updateSQL(strsql); 
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public List validatePostsIDArray(List validateList) {
		StringBuffer updateThreadsSQL = new StringBuffer(
				"update Threads as t set t.displayorder=0,t.moderated=1 where t.tid in (select p.tid from Posts as p where p.pid in (");
		StringBuffer updatePostsSQL = new StringBuffer(
				"update Posts as p set p.invisible=0 where p.pid in (");
		StringBuffer showSb = new StringBuffer("from Posts as p where p.pid in (");
		for (int i = 0; i < validateList.size(); i++) {
			updateThreadsSQL.append(validateList.get(i) + ",");
			updatePostsSQL.append(validateList.get(i) + ",");
			showSb.append(validateList.get(i) + ",");
		}
		String updateThreadssql = updateThreadsSQL.substring(0,
				updateThreadsSQL.length() - 1);
		updateThreadssql = updateThreadssql + "))";
		String strsql = updatePostsSQL
				.substring(0, updatePostsSQL.length() - 1);
		strsql = strsql + ")";

		String showSQL = showSb.substring(0, showSb.length() - 1);
		showSQL = showSQL + ")";
		try {
			updateSQL(updateThreadssql);
			updateSQL(strsql);
			List<Posts> postsList = selectSQL(showSQL);
			if (postsList.size() > 0 && postsList != null) {
				return postsList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -