ColumnService.java
上传用户:junmaots
上传日期:2022-07-09
资源大小:2450k
文件大小:7k
- /*
- * 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.util.ArrayList;
- import java.util.List;
- import com.mycompany.database.Database;
- import com.mycompany.news.dao.ColumnDAO;
- import com.mycompany.news.dao.NewsDAO;
- import com.mycompany.news.dao.impl.ColumnDAOImpl;
- import com.mycompany.news.dao.impl.NewsDAOImpl;
- import com.mycompany.news.dto.Column;
- import com.mycompany.news.dto.News;
- /**
- * @author Administrator
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- public class ColumnService extends BaseService{
- private ColumnDAO columnDAO=new ColumnDAOImpl();
- private int count;
- public boolean addColumn(Column Column) {
- Connection connection = null;
- try {
- connection = Database.getConnection();
- columnDAO.setConnection(connection);
- columnDAO.addColumn(Column);
- Database.commit();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- message = e.getMessage();
- Database.rollback();
- return false;
- } finally {
- Database.releaseConnection(connection);
- }
- };//添加Column信息
- public boolean updateColumn(Column column) {
- Connection conn=null;
- try{
- conn = Database.getConnection();
- columnDAO.setConnection(conn);
- columnDAO.updateColumn(column);
- conn.commit();
- return true;
- }catch(Exception e){
- e.printStackTrace();
- message = e.getMessage();
- Database.rollback();
- return false;
- }finally{
- Database.releaseConnection(conn);
- }
-
- };//修改Column信息
- //
- public boolean deleteColumn(Column column) {
- Connection connection = null;
- try {
- connection = Database.getConnection();
- NewsDAO newsDAO = new NewsDAOImpl();
- newsDAO.setConnection(connection);
- News condition = new News();
- condition.setColumnId(column.getColumnId());
- List list = newsDAO.listNews(condition,1,100);
- if(list.size()>0){
- setMessage("本栏目已经存在新闻,不能删除");
- return false;
- }
- columnDAO.setConnection(connection);
- Column cCondition = new Column();
- cCondition.setParentColumn(column.getColumnId());
- List list2 = columnDAO.listColumns(cCondition,1,100);
- if(list2.size()>0){
- setMessage("本栏目已经存在子栏目,不能删除");
- return false;
- }
- columnDAO.deleteColumn(column);
- Database.commit();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- message = e.getMessage();
- Database.rollback();
- return false;
- } finally {
- Database.releaseConnection(connection);
- }
- };//删除Column信息
- //
- public List listAllColumns() {
- Connection connection = null;
- try {
- connection = Database.getConnection();
- columnDAO.setConnection(connection);
- return columnDAO.listAllColumns();
- } catch (Exception e) {
- e.printStackTrace();
- message = e.getMessage();
- Database.rollback();
- } finally {
- Database.releaseConnection(connection);
- }
- return new ArrayList();
- };//列出所有的Column信息
- //
- public List listColumns(Column columnCondition,int pageNo,int pageSize) {
- Connection conn =null;
- try{
- conn = Database.getConnection();
- columnDAO.setConnection(conn);
- return columnDAO.listColumns(columnCondition,pageNo,pageSize);
- }catch(Exception e){
- e.printStackTrace();
- message = e.getMessage();
- return null;
- }finally{
- Database.releaseConnection(conn);
- }
- };//组合条件查询
- public Column getByID(long id) {
- Connection connection = null;
- try {
- connection = Database.getConnection();
- columnDAO.setConnection(connection);
- return columnDAO.getByID(id);
- } catch (Exception e) {
- e.printStackTrace();
- message = e.getMessage();
- } finally {
- Database.releaseConnection(connection);
- }
- return null;
-
- };//根据主键获取对象
- /**
- * @param message
- * The message to set.
- */
- /**
- * @return Returns the ColumnDAO.
- */
- public ColumnDAO getColumnDAO() {
- return columnDAO;
- }
- /**
- * @param columnDAO
- * The ColumnDAO to set.
- */
- public void setColumnDAO(ColumnDAO columnDAO) {
- this.columnDAO = columnDAO;
- }
- /**
- * 取得root下的所有递归子节点
- * @param root
- * @return
- */
- public List listSublings(Column root){
- Connection con = null;
- try{
- con=Database.getConnection();
- columnDAO.setConnection(con);
- Column columnCondition=new Column();
- columnCondition.setTopColumn(root.getColumnId());
- return columnDAO.listColumns(columnCondition,1,Integer.MAX_VALUE);
- }catch(Exception e){
- e.printStackTrace();
- message = e.getMessage();
- return new ArrayList();
- }finally{
- Database.releaseConnection(con);
- }
- }
- /**
- * 取得parent下的直接子节点
- * @param parent
- * @return
- */
- public List listChildren(Column parent){
- Connection con = null;
- try{
- con=Database.getConnection();
- columnDAO.setConnection(con);
- Column columnCondition=new Column();
- columnCondition.setParentColumn(parent.getColumnId());
- return columnDAO.listColumns(columnCondition,1,Integer.MAX_VALUE);
- }catch(Exception e){
- e.printStackTrace();
- message = e.getMessage();
- return new ArrayList();
- }finally{
- Database.releaseConnection(con);
- }
- }
- /**
- * 取得栏目树的字符串表示
- * @param root
- * @param treeList
- * @return
- */
- public StringBuffer getTreeString(String url,Column root, List treeList){
- StringBuffer strBuf = new StringBuffer();
- strBuf.
- append("['"+root.getColumnName()+"',").
- append("'"+url).
- append(root.getColumnId()).
- append("'");
-
- for(int i=0;i<treeList.size();i++){
- Column column = (Column) treeList.get(i);
- if(column.getParentColumn().equals(root.getColumnId())){
- strBuf.append(",").append("n");
- strBuf.append(getTreeString(url,column,treeList));
- }
- }
- strBuf.append("]");
- return strBuf;
- }
- public static void main(String[] args) {
- ColumnService cs = new ColumnService();
- List columns = new ArrayList();
- Column c0 = new Column();
- c0.setColumnId(new Long(1));
- c0.setParentColumn(new Long(0));
- c0.setColumnName("根");
- c0.setCurrentLevel(new Integer(0));
-
- columns.add(c0);
- Column c1 = new Column();
- c1.setColumnId(new Long(2));
- c1.setParentColumn(new Long(1));
- c1.setColumnName("根-叶节点1");
- c1.setCurrentLevel(new Integer(1));
- columns.add(c1);
- Column c2 = new Column();
- c2.setColumnId(new Long(3));
- c2.setParentColumn(new Long(1));
- c2.setColumnName("根-叶节点2");
- c2.setCurrentLevel(new Integer(1));
- columns.add(c2);
- StringBuffer buffer = cs.getTreeString("columns.jsp?columnid=",c0,columns);
- System.out.println(buffer);
- }
- /**
- * @return Returns the count.
- */
- public int getCount() {
- return count;
- }
- /**
- * @param count The count to set.
- */
- public void setCount(int count) {
- this.count = count;
- }
- }