?? attractionbo.java
字號:
package com.wrox.tourism.business;import com.wrox.tourism.entity.Attraction;import com.wrox.tourism.entity.Event;import com.wrox.tourism.entity.UserRole;import com.wrox.tourism.db.EventDAO;import com.wrox.tourism.db.AttractionDAO;import com.wrox.tourism.db.UserRoleDAO;import com.wrox.tourism.db.CreateException;import com.wrox.tourism.db.FinderException;import com.wrox.tourism.db.util.ConnectionPool;import java.sql.SQLException;import java.sql.Connection;import java.util.Iterator;public class AttractionBO { private ConnectionPool pool; public AttractionBO() { pool = ConnectionPool.getInstance(); } public void registerAttraction(Attraction attraction) throws AttractionException { validateAttraction(attraction); Connection con = null; try { con = pool.getConnection(); AttractionDAO attractionDAO = new AttractionDAO(con); attractionDAO.create(attraction); UserRoleDAO userRoleDAO = new UserRoleDAO(con); UserRole userRole = new UserRole(); userRole.setUserId(attraction.getUserId()); userRole.setRoleName(Role.ATTRACTION); userRoleDAO.create(userRole); con.commit(); } catch (Exception e) { try { if (con != null) { con.rollback(); throw new AttractionException(e.getMessage()); } } catch (SQLException sqle) { e.printStackTrace(); throw new RuntimeException("error.unexpected"); } } finally { try { if (con != null) { con.close(); } } catch (SQLException sqle) { sqle.printStackTrace(); throw new RuntimeException("error.unexpected"); } } } public void updateAttraction(Attraction attraction) throws AttractionException { validateAttraction(attraction); Connection con = null; try { con = pool.getConnection(); AttractionDAO attractionDAO = new AttractionDAO(con); attractionDAO.update(attraction); con.commit(); } catch (Exception e) { try { if (con != null) { con.rollback(); throw new AttractionException(e.getMessage()); } } catch (SQLException sqle) { e.printStackTrace(); throw new RuntimeException("error.unexpected"); } } finally { try { if (con != null) { con.close(); } } catch (SQLException sqle) { sqle.printStackTrace(); throw new RuntimeException("error.unexpected"); } } } public void deregisterAttraction(String userId) throws AttractionException { Connection con = null; try { con = pool.getConnection(); AttractionDAO attractionDAO = new AttractionDAO(con); attractionDAO.remove(userId); UserRoleDAO userRoleDAO = new UserRoleDAO(con); userRoleDAO.remove(userId,Role.ATTRACTION); EventDAO eventDAO = new EventDAO(con); Iterator it = eventDAO.findAll(userId).iterator(); while(it.hasNext()) { Event event = (Event)it.next(); eventDAO.remove(event.getEventId()); } con.commit(); } catch (Exception e) { try { if (con != null) { con.rollback(); throw new AttractionException(e.getMessage()); } } catch (SQLException sqle) { e.printStackTrace(); throw new RuntimeException("error.unexpected"); } } finally { try { if (con != null) { con.close(); } } catch (SQLException sqle) { sqle.printStackTrace(); throw new RuntimeException("error.unexpected"); } } } private void validateAttraction(Attraction attraction) throws AttractionException { if (attraction.getUserId().trim().equals("")) { throw new AttractionException("error.missing.userId"); } if (attraction.getPassword().trim().equals("")) { throw new AttractionException("error.missing.password"); } if (attraction.getName().trim().equals("")) { throw new AttractionException("error.missing.name"); } if (attraction.getDescription().trim().equals("")) { throw new AttractionException("error.missing.description"); } if (attraction.getWebSite().trim().equals("")) { throw new AttractionException("error.missing.webSite"); } if (attraction.getAddress().trim().equals("")) { throw new AttractionException("error.missing.address"); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -