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

📄 newsservice.java

📁 介绍关于JSP网站开发方面的内容,特别详尽
💻 JAVA
字号:
/*
 * Created on 2005-11-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.mycompany.news.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.mycompany.database.Database;
import com.mycompany.news.dao.NewsAttachmentDAO;
import com.mycompany.news.dao.NewsAttributeDAO;
import com.mycompany.news.dao.NewsCommentDAO;
import com.mycompany.news.dao.NewsDAO;
import com.mycompany.news.dao.impl.NewsAttachmentDAOImpl;
import com.mycompany.news.dao.impl.NewsAttributeDAOImpl;
import com.mycompany.news.dao.impl.NewsCommentDAOImpl;
import com.mycompany.news.dao.impl.NewsDAOImpl;
import com.mycompany.news.dto.News;
import com.mycompany.news.dto.NewsAttachment;
import com.mycompany.tools.DTOPopulator;

/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class NewsService extends BaseService{
	private NewsDAO newsDAO=new NewsDAOImpl();

	private int count;
/**
 * modify 
 * @param news
 * @param upload
 * @return
 */
	public boolean modifyNews(News news,SmartUpload upload) {
		Connection connection = null;
		try {
			String updateAttr=upload.getRequest().getParameter("updateattr");
			connection = Database.getConnection();
			NewsAttachmentDAO attachmentDAO = new NewsAttachmentDAOImpl();			
			newsDAO.setConnection(connection);
			attachmentDAO.setConnection(connection);	
			newsDAO.updateNews(news);
			if(updateAttr!=null&&updateAttr.equals("y")){
				attachmentDAO.deleteAttrByNews(news);
				Long currentID = news.getNewsId();
				for(int i=0;i<upload.getFiles().getCount();i++){
					File f = upload.getFiles().getFile(i);
					if(f.isMissing())continue;
					
					NewsAttachment newsAttachment=new NewsAttachment();
					newsAttachment.setNewsId(currentID);
					newsAttachment.setAttachmentName(f.getFileName());
					newsAttachment.setAttachmentContent(f.getFileBinaryData());
					attachmentDAO.addNewsAttachment(newsAttachment);				
				}				
			}
			Database.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			message = e.getMessage();
			Database.rollback();
			return false;
		} finally {
			Database.releaseConnection(connection);
		}
	};	
	public boolean addNews(News news,SmartUpload upload) {
		Connection connection = null;
		try {
			
			connection = Database.getConnection();
			NewsAttachmentDAO attachmentDAO = new NewsAttachmentDAOImpl();			
			newsDAO.setConnection(connection);
			attachmentDAO.setConnection(connection);	
			newsDAO.addNews(news);
			Long currentID = newsDAO.getCurrentID();
			for(int i=0;i<upload.getFiles().getCount();i++){
				File f = upload.getFiles().getFile(i);
				if(f.isMissing())continue;
				
				NewsAttachment newsAttachment=new NewsAttachment();
				newsAttachment.setNewsId(currentID);
				newsAttachment.setAttachmentName(f.getFileName());
				newsAttachment.setAttachmentContent(f.getFileBinaryData());
				attachmentDAO.addNewsAttachment(newsAttachment);				
			}
			Database.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			message = e.getMessage();
			Database.rollback();
			return false;
		} finally {
			Database.releaseConnection(connection);
		}
	};//添加News信息
//
	public boolean updateNews(News news) {
		Connection connection = null;
		try {			
			connection = Database.getConnection();
			newsDAO.setConnection(connection);
			newsDAO.updateNews(news);
			Database.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			message = e.getMessage();
			Database.rollback();
			return false;
		} finally {
			Database.releaseConnection(connection);
		}		
	};//修改News信息
//
	public boolean deleteNews(News news) {
		Connection connection = null;
		try {			
			connection = Database.getConnection();
			NewsAttachmentDAO attachmentDAO =new NewsAttachmentDAOImpl();
			NewsAttributeDAO attrDAO = new NewsAttributeDAOImpl();
			NewsCommentDAO commentDAO = new NewsCommentDAOImpl();
			newsDAO.setConnection(connection);
			attachmentDAO.setConnection(connection);
			attrDAO.setConnection(connection);
			newsDAO.deleteNews(news);
			attachmentDAO.deleteAttrByNews(news);
			attrDAO.deleteByNews(news);
			commentDAO.deleteCommentByNews(news);
			Database.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			message = e.getMessage();
			Database.rollback();
			return false;
		} finally {
			Database.releaseConnection(connection);
		}				
	};//删除News信息
//
	public List listAllNewses() {
		Connection connection = null;
		try {			
			connection = Database.getConnection();
			newsDAO.setConnection(connection);
			return newsDAO.listAllNews();
		} catch (Exception e) {
			e.printStackTrace();
			message = e.getMessage();
			Database.rollback();
		} finally {
			Database.releaseConnection(connection);
		}				
		return new ArrayList();
	};//列出所有的News信息
//
	public List listNewses(News newsCondition,int pageNo,int pageSize) {

		Connection conn =null;
		try{
		conn = Database.getConnection();
		newsDAO.setConnection(conn);
		return newsDAO.listNews(newsCondition,pageNo,pageSize);
		}catch(Exception e){
			e.printStackTrace();
			message = e.getMessage();
			return null;
		}finally{
		Database.releaseConnection(conn);	
		}		


	};//组合条件查询
//
	public News getByID(long id) {
		Connection conn =null;
		try{
		conn = Database.getConnection();
		newsDAO.setConnection(conn);
		newsDAO.updateVisitCount(id);		
		News news =  newsDAO.getByID(id);
		Database.commit();
		return news;
		}catch(Exception e){
			e.printStackTrace();
			message = e.getMessage();
			return null;
		}finally{
		Database.releaseConnection(conn);	
		}		
	}

	public List getRecommendedNews(long channelid,int curPage,int max){
		Connection conn =null;
		try{
		conn = Database.getConnection();
		newsDAO.setConnection(conn);
		return newsDAO.getRecommendedNews(channelid,curPage,max);
		}catch(Exception e){
			e.printStackTrace();
			message = e.getMessage();
			return new ArrayList();
		}finally{
		Database.releaseConnection(conn);	
		}
	}

	public int getRecommendedNewsCount(long channelid){
		Connection conn =null;
		try{
		conn = Database.getConnection();
		newsDAO.setConnection(conn);
		return newsDAO.getRecommendedNewsCount(channelid);
		}catch(Exception e){
			e.printStackTrace();
			message = e.getMessage();
		}finally{
		Database.releaseConnection(conn);	
		}
		return 0;
	}	
	/**
	 * @return Returns the NewsDAO.
	 */
	public NewsDAO getNewsDAO() {
		return newsDAO;
	}

	/**
	 * @param NewsDAO
	 *            The NewsDAO to set.
	 */
	public void setNewsDAO(NewsDAO NewsDAO) {
		this.newsDAO = NewsDAO;
	}
	public NewsAttachment loadAttachment(long attid){
		Connection conn =null;
		NewsAttachmentDAO attDAO = new NewsAttachmentDAOImpl();
		try {
			conn = Database.getConnection();
			attDAO.setConnection(conn);
			return attDAO.getByID(attid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		Database.releaseConnection(conn);	
		}
		return null;
	}
	public long getVisitCount(long newsid){
		Connection conn =null;
		NewsAttributeDAO attDAO = new NewsAttributeDAOImpl();
		try {
			conn = Database.getConnection();
			attDAO.setConnection(conn);
			String strVisitCount = attDAO.getAttributeValue(newsid,"visit_count",0);
			if(strVisitCount==null)
				return 0;
			return Long.parseLong(strVisitCount);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Database.releaseConnection(conn);
		}
		return 0;
	}


	public List getAttachments(long newsId){
		Connection conn =null;
		
		
		NewsAttachment condition=new NewsAttachment();
		condition.setNewsId(new Long(newsId));
		try {
			NewsAttachmentDAO attDAO = new NewsAttachmentDAOImpl();
			conn = Database.getConnection();
			attDAO.setConnection(conn);
			return attDAO.listNewsAttachments(condition);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Database.releaseConnection(conn);
		}
		return null;
	}
	public int searchCount(String keyword){
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("select count(*) as count from News_Info where"); 
		sqlStr.append(" UPPER(content) like ? ");
		sqlStr.append(" or UPPER(author) like ?");
		sqlStr.append(" or UPPER(subject) like ?");
		sqlStr.append(" or news_id ");
		sqlStr.append(" in(select entity_id from News_Attribute"); 
		sqlStr.append(" where");
		sqlStr.append(" (");
		sqlStr.append(" (");
		sqlStr.append(" news_attr_name='Vice_Subject'"); 
		sqlStr.append(" or news_attr_name ='Source'");
		sqlStr.append(" )");
		sqlStr.append(" and UPPER(news_attr_value) like ?");
		sqlStr.append(" and news_attr_type='0'");
		sqlStr.append(" ))");
		Connection conn =null;
		try {
		conn = Database.getConnection();
		PreparedStatement preparedStatement = conn.prepareStatement(sqlStr.toString());
		int i=1;
		for(i=1;i<=4;i++){
			preparedStatement.setString(i,"%"+ keyword.toUpperCase().replace(' ','%')+"%");
		}
		ResultSet resultSet = preparedStatement.executeQuery();
		if(resultSet.next()){
			return resultSet.getInt("count");
		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Database.releaseConnection(conn);
		}
		return 0;
	}
	public List search(String keyword,int pageNo,int pageSize){
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("select * from News_Info where"); 
		sqlStr.append(" UPPER(content) like ? ");
		sqlStr.append(" or UPPER(author) like ?");
		sqlStr.append(" or UPPER(subject) like ?");
		sqlStr.append(" or news_id ");
		sqlStr.append(" in(select entity_id from News_Attribute"); 
		sqlStr.append(" where");
		sqlStr.append(" (");
		sqlStr.append(" (");
		sqlStr.append(" news_attr_name='Vice_Subject'"); 
		sqlStr.append(" or news_attr_name ='Source'");
		sqlStr.append(" )");
		sqlStr.append(" and UPPER(news_attr_value) like ?");
		sqlStr.append(" and news_attr_type='0'");
		sqlStr.append(" )) limit ?,?");
		System.out.println("sqlstr = "+sqlStr);
		Connection conn =null;
		try {
		conn = Database.getConnection();
		PreparedStatement preparedStatement = conn.prepareStatement(sqlStr.toString());
		int i=1;
		for(i=1;i<=4;i++){
			preparedStatement.setString(i,"%"+ keyword.toUpperCase().replace(' ','%')+"%");
		}
		preparedStatement.setInt(i++,(pageNo-1)*pageSize);
		preparedStatement.setInt(i++,pageNo*pageSize);
		ResultSet resultSet = preparedStatement.executeQuery();

		return DTOPopulator.populate(resultSet,News.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Database.releaseConnection(conn);
		}
		return new ArrayList();

	}

	public int getNewsCountByColumn(Long columnid){
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("select count(*) as count from News_Info where"); 
		sqlStr.append(" column_id=?");
		Connection conn =null;
		try {
		conn = Database.getConnection();
		PreparedStatement preparedStatement = conn.prepareStatement(sqlStr.toString());
		int i=1;
		preparedStatement.setLong(i++, columnid.longValue());
		ResultSet resultSet = preparedStatement.executeQuery();
		if(resultSet.next())
		return resultSet.getInt("count");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Database.releaseConnection(conn);
		}
		return 0;		
	}
	public List listNewsByColumn(Long columnid,int pageNo,int pageSize){
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("select * from News_Info where"); 
		sqlStr.append(" column_id=?");
		sqlStr.append(" limit ?,?");
		Connection conn =null;
		try {
		conn = Database.getConnection();
		PreparedStatement preparedStatement = conn.prepareStatement(sqlStr.toString());
		int i=1;
		preparedStatement.setLong(i++, columnid.longValue());
		preparedStatement.setInt(i++,(pageNo-1)*pageSize);
		preparedStatement.setInt(i++,pageNo*pageSize);
		ResultSet resultSet = preparedStatement.executeQuery();

		return DTOPopulator.populate(resultSet,News.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Database.releaseConnection(conn);
		}
		return new ArrayList();
	}

	public boolean isImage(NewsAttachment attachment){
		if(
		attachment.getAttachmentName().toUpperCase().endsWith("JPG")||
		attachment.getAttachmentName().toUpperCase().endsWith("BMP")||
		attachment.getAttachmentName().toUpperCase().endsWith("JPEG")||
		attachment.getAttachmentName().toUpperCase().endsWith("GIF")
		)
			return true;
		return false;
	}
	public boolean recommendNews(long newsid) throws Exception{
		
		Connection conn =null;
		try{
		conn = Database.getConnection();
		NewsAttributeDAO naDAO = new NewsAttributeDAOImpl();
		naDAO.setConnection(conn);
		naDAO.recommendNews(newsid);
		Database.commit();
		return true;
		}finally{
		Database.releaseConnection(conn);	
		}				

	}
	public boolean cancelRecommendNews(long newsid) throws Exception{
		
		Connection conn =null;
		try{
		conn = Database.getConnection();
		NewsAttributeDAO naDAO = new NewsAttributeDAOImpl();
		naDAO.setConnection(conn);
		naDAO.cancelRecommend(newsid);
		Database.commit();
		return true;
		}finally{
		Database.releaseConnection(conn);	
		}				

	}	
	public static void main(String[] args) {
//		NewsService cs = new NewsService();
//		List Newss = new ArrayList();
//		News c0 = new News();
//		c0.setNewsId(new Long(1));
//		try {
//			cs.setNewsDAO(new NewsDAOImpl());
//			News byID = cs.getByID(13);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		NewsService s = new NewsService();
		s.search("adf",1,23);
	}

	/**
	 * @return Returns the count.
	 */
	public int getCount() {
		return count;
	}
	/**
	 * @param count The count to set.
	 */
	public void setCount(int count) {
		this.count = count;
	}
}

⌨️ 快捷键说明

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