📄 bookbo.java
字号:
package com.wrox.publish.bo;import com.wrox.publish.db.BookDAO;import com.wrox.publish.db.BookNewsDAO;import com.wrox.publish.db.EntityNotFoundException;import com.wrox.publish.db.InvalidFieldException;import com.wrox.publish.om.Book;import com.wrox.publish.om.BookNews;import java.sql.Connection;import java.sql.SQLException;import javax.servlet.http.HttpServletRequest;import javax.sql.DataSource;/** * Business Object providing access to the functionality of the * Book Publishing application, i.e. everything related to either * Books or BookNews items. */public class BookBO { /** The role name for editors */ public static final String EDITOR_ROLE = "editor"; private final String user; private final boolean editor; private final DataSource dataSource; /** * Creates a new instance of BookBO * @param user The user for which the object needs to be created. * @param editor Whether the user is an editor or not. * @param dataSource The DataSource to get connections from */ BookBO(String user, boolean editor, DataSource dataSource) { super(); this.user = user; this.editor = editor; this.dataSource = dataSource; } /** * Finds a book by id (primary key). * @param id The book's id. * @throws EntityNotFoundException If the book was not found. * @throws SQLException If there was a database problem. * @return The Book */ public Book findBookById(long id) throws EntityNotFoundException, SQLException { Connection conn = dataSource.getConnection(); try { Book book = new BookDAO(conn).findById(id); conn.commit(); return book; } catch (EntityNotFoundException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } /** * Adds a new book to the database. * @param book The Book to add. * @throws ActionDeniedException If the user does not have enough * privileges to add the book. * @throws InvalidFieldException If there was an invalid field in * the book, such as a duplicate title. * @throws SQLException If there was a database access problem. */ public void create(Book book) throws ActionDeniedException, InvalidFieldException, SQLException { mustBeEditor(); Connection conn = dataSource.getConnection(); try { new BookDAO(conn).create(book); conn.commit(); } catch (InvalidFieldException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } /** * Updates a book in the database. * @param book The book to update. * @throws ActionDeniedException If the user does not have enough * privileges to update the book. * @throws InvalidFieldException If there was an invalid field in * the book, such as a duplicate title. * @throws EntityNotFoundException If the book was not found. * @throws SQLException If there was a database access problem. */ public void update(Book book) throws ActionDeniedException, InvalidFieldException, EntityNotFoundException, SQLException { mustBeEditor(); Connection conn = dataSource.getConnection(); try { new BookDAO(conn).update(book); conn.commit(); } catch (InvalidFieldException e) { conn.rollback(); throw e; } catch (EntityNotFoundException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } /** * Deletes a book from the database. * @param book The book to delete. * @throws ActionDeniedException If the user does not have enough * privileges to delete the book. * @throws EntityNotFoundException If the book was not found. * @throws SQLException If there was a database access problem. */ public void remove(Book book) throws ActionDeniedException, EntityNotFoundException, SQLException { mustBeEditor(); Connection conn = dataSource.getConnection(); try { new BookDAO(conn).remove(book); conn.commit(); } catch (EntityNotFoundException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } private void mustBeEditor() throws ActionDeniedException { if (!editor) { throw new ActionDeniedException("Only editors can do this."); } } /** * Finds a BookNews item in the database. * @param id The id (primary key) * @throws EntityNotFoundException If the item was not found. * @throws SQLException If there was a database access problem. * @return The BookNews item. */ public BookNews findBookNewsById(long id) throws EntityNotFoundException, SQLException { Connection conn = dataSource.getConnection(); try { BookNews news = new BookNewsDAO(conn).findById(id); conn.commit(); return news; } catch (EntityNotFoundException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } /** * Adds a new news item to the database. * @param news The item to add. * @throws ActionDeniedException If the user does not have enough * privileges to add the news item. * @throws SQLException If there was a database access problem. */ public void create(BookNews news) throws ActionDeniedException, SQLException { mustBeEditor(); Connection conn = dataSource.getConnection(); try { new BookNewsDAO(conn).create(news); conn.commit(); } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } /** * Updates a BookNews item in the database. * @param news The item to update. * @throws ActionDeniedException If the user does not have enough * privileges to update the news item. * @throws EntityNotFoundException If the item was not found. * @throws SQLException If there was a database access problem. */ public void update(BookNews news) throws ActionDeniedException, EntityNotFoundException, SQLException { mustBeOwnerOrEditor(news.getUser()); Connection conn = dataSource.getConnection(); try { new BookNewsDAO(conn).update(news); conn.commit(); } catch (EntityNotFoundException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } /** * Deletes a BookNews item from the database. * @param news The item to delete. * @throws ActionDeniedException If the user does not have enough * privileges to delete the news item. * @throws EntityNotFoundException If the item was not found. * @throws SQLException If there was a database access problem. */ public void remove(BookNews news) throws ActionDeniedException, EntityNotFoundException, SQLException { mustBeOwnerOrEditor(news.getUser()); Connection conn = dataSource.getConnection(); try { new BookNewsDAO(conn).remove(news); conn.commit(); } catch (EntityNotFoundException e) { conn.rollback(); throw e; } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } private void mustBeOwnerOrEditor(String owner) throws ActionDeniedException { if (!editor && !user.equals(owner)) { throw new ActionDeniedException( "Only owners or editors can do this."); } } /** * Factory method to create a BookBO business object. * @return The Business Object. * @param request The HttServletRequest in which this action is performed. * @param dataSource The DataSource to get connections from */ public static BookBO getInstance(HttpServletRequest request, DataSource dataSource) { return new BookBO(request.getRemoteUser(), request.isUserInRole(EDITOR_ROLE), dataSource); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -