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

📄 mssqlbookdao.java

📁 一个免费wap站
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.eline.wap.resource.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.eline.wap.catalog.exceptions.CatalogDAOSysException;
import com.eline.wap.common.jdbc.DBCommonUtils;
import com.eline.wap.common.jdbc.DBSqlManager;
import com.eline.wap.common.model.Page;
import com.eline.wap.common.util.AppLogger;
import com.eline.wap.common.util.SerializeData;
import com.eline.wap.common.util.StringUtils;
import com.eline.wap.resource.exceptions.ResourceDAOSysException;
import com.eline.wap.resource.model.Book;
import com.eline.wap.resource.model.BookChapter;
import com.eline.wap.resource.model.BookCondition;

/**
 * 
 * @author Lucifer
 *
 */
public class MSSqlBookDAO extends DBSqlManager implements BookDAO {
	static private final int TYPE_ADD		= 0;
	static private final int TYPE_UPDATE	= 1;

	/**
	 * @param bookId
	 * @return
	 * @throws ResourceDAOSysException
	 */
	public Book getBook(int bookId) throws ResourceDAOSysException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rst = null;
		Book item = null;
		
		String sql = "SELECT IndexID, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, " 
			+ "SearchKeywords, PropertyNames, PropertyValues FROM TAB_BOOK "
			+ "WHERE IndexID = ?";
		
		try {
			conn = super.getDBConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, bookId);
			
			rst = stmt.executeQuery();
			
			if (rst.next())
				item = populateBookFormResultSet(rst);

		} catch (Exception e) {
			AppLogger.debug("MSSqlBookDAO.getBook().e.getMessage()=" + e.getMessage());
			throw new ResourceDAOSysException(e.getMessage());
		} finally {
			try {
				super.closeResultSet(rst);
				super.closeStatement(stmt);
				super.closeConnection(conn);
			} catch (Exception e) {
				throw new ResourceDAOSysException(e.getMessage());
			}
		}

		return item;
	}

	/**
	 * 
	 */
	public Page getBooks(int parentId, int start, int count)
			throws ResourceDAOSysException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rst = null;

		String sql = "SELECT IndexID, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues FROM TAB_BOOK WHERE Deleted <> 1 AND IsActive = 1 AND ParentID = ? ORDER BY SortOrder";
		Page page = new Page();

		AppLogger.debug("MSSqlBookDAO.getBooks().sql=" + sql);
		try {
			conn = super.getDBConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, parentId);
			
			rst = stmt.executeQuery();

        	int totalRecords = 0;
        	for (totalRecords = 0; rst.next(); totalRecords ++) {
        		if ((totalRecords >= start) && (totalRecords < (start + count))) {
        			Book item = populateBookFormResultSet(rst);
        			page.getItems().add(item);
        		}
        	}
        	page.setTotalRecords(totalRecords);
		} catch (Exception e) {
			AppLogger.debug("MSSqlBookDAO.getBooks().e.getMessage()=" + e.getMessage());

			throw new ResourceDAOSysException(e.getMessage());
		} finally {
			try {
				super.closeResultSet(rst);
				super.closeStatement(stmt);
				super.closeConnection(conn);
			} catch (Exception e) {
				throw new ResourceDAOSysException(e.getMessage());
			}
		}
		return page;
	}

	protected static String[] SEARCH_BOOKS_STATEMENT_FRAGMENTS = {
		"SELECT IndexID, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues FROM TAB_BOOK WHERE Deleted <> 1 ",
		"AND ParentID = ? ",
		"AND SearchKeywords = ? ",
		"AND IsSearchable = 1 ",
		"AND IsActive = 1 ",
		"AND DateCreated >= ? ",
		"AND DateCreated < ? "
	};
	
	/**
	 * 
	 */
	public Page searchBooks(BookCondition condition, int start, int count)
			throws ResourceDAOSysException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rst = null;
		
		Page page = new Page();
		String sql = SEARCH_BOOKS_STATEMENT_FRAGMENTS[0];
		if (condition != null) {
			if (condition.getParentId() != -1)
				sql += SEARCH_BOOKS_STATEMENT_FRAGMENTS[1];
			// 注意: SearchKeywords == null的情况下就不需要判断是否ForceToSearch了
			if (condition.getSearchKeywords() != null) {
				sql += SEARCH_BOOKS_STATEMENT_FRAGMENTS[2];
				if (!condition.isForceToSearch())
					sql += SEARCH_BOOKS_STATEMENT_FRAGMENTS[3];
			}
			if (condition.isActiveOnly())
				sql += SEARCH_BOOKS_STATEMENT_FRAGMENTS[4];
			if (condition.getDateCreated() != null)
				sql += SEARCH_BOOKS_STATEMENT_FRAGMENTS[5];
			if (condition.getDateCreated2() != null)
				sql += SEARCH_BOOKS_STATEMENT_FRAGMENTS[6];
		}
		
		sql += "ORDER BY SortOrder";
		AppLogger.debug("MSSqlBookDAO.searchBooks().sql=" + sql);
		try {
			conn = super.getDBConnection();
			stmt = conn.prepareStatement(sql);
			
			// Set condition parameters
			if (condition != null) {
				int index = 1;
				if (condition.getParentId() != -1)
					stmt.setInt(index++, condition.getParentId());
				if (condition.getSearchKeywords() != null)
					stmt.setString(index++, "%" + condition.getSearchKeywords() + "%");
				if (condition.getDateCreated() != null)
					stmt.setString(index++, StringUtils.toString(condition.getDateCreated()));
				if (condition.getDateCreated2() != null)
					stmt.setString(index++, StringUtils.toString(condition.getDateCreated2()));
			}

			rst = stmt.executeQuery();

        	int totalRecords = 0;
        	for (totalRecords = 0; rst.next(); totalRecords ++) {
        		if ((totalRecords >= start) && (totalRecords < (start + count))) {
        			Book item = populateBookFormResultSet(rst);
        			page.getItems().add(item);
        		}
        	}
        	page.setTotalRecords(totalRecords);
		} catch (Exception e) {
			AppLogger.debug("MSSqlBookDAO.searchBooks().e.getMessage()=" + e.getMessage());

			throw new ResourceDAOSysException(e.getMessage());
		} finally {
			try {
				super.closeResultSet(rst);
				super.closeStatement(stmt);
				super.closeConnection(conn);
			} catch (Exception e) {
				throw new ResourceDAOSysException(e.getMessage());
			}
		}
		return page;
	}

	/**
	 * 
	 */
	public void createBook(Book item) throws ResourceDAOSysException {
		Connection conn = null;
		PreparedStatement stmt = null;

		String sql = "INSERT INTO TAB_BOOK (ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable," 
			+ "SearchKeywords, PropertyNames, PropertyValues) "
			+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

		AppLogger.debug("MSSqlBookDAO.createBook().sql=" + sql);

		try {
			conn = super.getDBConnection();
			stmt = conn.prepareStatement(sql);
			fillBookParameters(stmt, item, TYPE_ADD);

		    int rowCount = stmt.executeUpdate();
		    
		    if (rowCount != 1)
		    	throw new ResourceDAOSysException("effected row count must be 1");


		} catch (Exception e) {
			AppLogger.debug("MSSqlCategoryDAO.createBook().e.getMessage()=" + e.getMessage());

			throw new ResourceDAOSysException(e.getMessage());

		} finally {
			try {
				super.closeStatement(stmt);
				super.closeConnection(conn);
			} catch (Exception e) {
				throw new ResourceDAOSysException(e.getMessage());
			}
		}
	}

	/**
	 * 
	 */
	public void updateBook(Book item) throws ResourceDAOSysException {
		Connection conn = null;
		PreparedStatement stmt = null;

		String sql = "UPDATE TAB_BOOK SET SortOrder = ?, LastUpdate = ?, IsActive = ?, IsSearchable = ?," 
			+ "SearchKeywords = ?, PropertyNames = ?, PropertyValues = ? "
			+ "WHERE IndexID = ?";

		AppLogger.debug("MSSqlBookDAO.updateBook().sql=" + sql);

		try {
			conn = super.getDBConnection();
			stmt = conn.prepareStatement(sql);
			fillBookParameters(stmt, item, TYPE_UPDATE);

		    int rowCount = stmt.executeUpdate();
		    
		    if (rowCount != 1)
		    	throw new ResourceDAOSysException("effected row count must be 1");


		} catch (Exception e) {
			AppLogger.debug("MSSqlCategoryDAO.updateBook().e.getMessage()=" + e.getMessage());

			throw new ResourceDAOSysException(e.getMessage());

		} finally {
			try {
				super.closeStatement(stmt);
				super.closeConnection(conn);
			} catch (Exception e) {
				throw new ResourceDAOSysException(e.getMessage());
			}
		}
	}

	/**
	 * 
	 */
	public void deleteBook(int bookId) throws ResourceDAOSysException {
		Connection conn = null;
		PreparedStatement stmt = null;

		String sql = "UPDATE TAB_BOOK SET Deleted = 1 WHERE IndexID = ?";

		AppLogger.debug("MSSqlBookDAO.updateBook().sql=" + sql);

		try {
			conn = super.getDBConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, bookId);

		    int rowCount = stmt.executeUpdate();
		    
		    if (rowCount != 1)
		    	throw new ResourceDAOSysException("effected row count must be 1");


		} catch (Exception e) {
			AppLogger.debug("MSSqlCategoryDAO.deleteBook().e.getMessage()=" + e.getMessage());

			throw new ResourceDAOSysException(e.getMessage());

		} finally {
			try {
				super.closeStatement(stmt);

⌨️ 快捷键说明

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