1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.util.ArrayList ; 48 import java.util.List ; 49 50 import net.jforum.JForumExecutionContext; 51 import net.jforum.entities.Banner; 52 import net.jforum.util.preferences.SystemGlobals; 53 54 58 public class GenericBannerDAO extends AutoKeys implements net.jforum.dao.BannerDAO 59 { 60 public Banner selectById(int bannerId) throws Exception 61 { 62 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.selectById")); 63 p.setInt(1, bannerId); 64 65 ResultSet rs = p.executeQuery(); 66 67 Banner b = new Banner(); 68 if (rs.next()) { 69 b = this.getBanner(rs); 70 } 71 72 rs.close(); 73 p.close(); 74 75 return b; 76 } 77 78 public List selectAll() throws Exception 79 { 80 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.selectAll")); 81 List l = new ArrayList (); 82 83 ResultSet rs = p.executeQuery(); 84 while (rs.next()) { 85 l.add(this.getBanner(rs)); 86 } 87 88 rs.close(); 89 p.close(); 90 91 return l; 92 } 93 94 protected Banner getBanner(ResultSet rs) throws Exception 95 { 96 Banner b = new Banner(); 97 98 b.setId(rs.getInt("banner_id")); 99 b.setName(rs.getString("banner_name")); 100 b.setPlacement(rs.getInt("banner_placement")); 101 b.setDescription(rs.getString("banner_description")); 102 b.setClicks(rs.getInt("banner_clicks")); 103 b.setViews(rs.getInt("banner_views")); 104 b.setUrl(rs.getString("banner_url")); 105 b.setWeight(rs.getInt("banner_weight")); 106 b.setActive(rs.getInt("banner_active") == 1); 107 b.setComment(rs.getString("banner_comment")); 108 b.setType(rs.getInt("banner_type")); 109 b.setWidth(rs.getInt("banner_width")); 110 b.setHeight(rs.getInt("banner_height")); 111 112 return b; 113 } 114 115 public boolean canDelete(int bannerId) throws Exception 116 { 117 boolean result = true; 118 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.canDelete")); 119 p.setInt(1, bannerId); 120 121 ResultSet rs = p.executeQuery(); 122 if (!rs.next() || rs.getInt("total") < 1) { 123 result = false; 124 } 125 126 rs.close(); 127 p.close(); 128 129 return result; 130 } 131 132 public void delete(int bannerId) throws Exception 133 { 134 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.delete")); 135 p.setInt(1, bannerId); 136 p.executeUpdate(); 137 138 p.close(); 139 } 140 141 public void update(Banner banner) throws Exception 142 { 143 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.update")); 144 setBannerParam(p, banner); 145 p.setInt(13, banner.getId()); 146 p.executeUpdate(); 147 p.close(); 148 } 149 150 public int addNew(Banner banner) throws Exception 151 { 152 PreparedStatement p = this.getStatementForAutoKeys("BannerDAO.addNew"); 153 setBannerParam(p, banner); 154 int id = this.executeAutoKeysQuery(p); 155 p.close(); 156 157 banner.setId(id); 158 return id; 159 } 160 161 protected void setBannerParam(PreparedStatement p, Banner b) throws Exception 162 { 163 p.setString(1, b.getName()); 164 p.setInt(2, b.getPlacement()); 165 p.setString(3, b.getDescription()); 166 p.setInt(4, b.getClicks()); 167 p.setInt(5, b.getViews()); 168 p.setString(6, b.getUrl()); 169 p.setInt(7, b.getWeight()); 170 p.setInt(8, b.isActive() ? 1 : 0); 171 p.setString(9, b.getComment()); 172 p.setInt(10, b.getType()); 173 p.setInt(11, b.getWidth()); 174 p.setInt(12, b.getHeight()); 175 } 176 177 public List selectActiveBannerByPlacement(int placement) throws Exception 178 { 179 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 180 SystemGlobals.getSql("BannerDAO.selectActiveBannerByPlacement")); 181 p.setInt(1, placement); 182 183 List l = new ArrayList (); 184 185 ResultSet rs = p.executeQuery(); 186 while (rs.next()) { 187 l.add(this.getBanner(rs)); 188 } 189 190 rs.close(); 191 p.close(); 192 193 return l; 194 } 195 } 196 | Popular Tags |