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.Group; 52 import net.jforum.util.preferences.SystemGlobals; 53 54 58 public class GenericGroupDAO implements net.jforum.dao.GroupDAO 59 { 60 63 public Group selectById(int groupId) throws Exception 64 { 65 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectById")); 66 p.setInt(1, groupId); 67 68 ResultSet rs = p.executeQuery(); 69 70 Group g = new Group(); 71 72 if (rs.next()) { 73 g = this.getGroup(rs); 74 } 75 76 rs.close(); 77 p.close(); 78 79 return g; 80 } 81 82 85 public boolean canDelete(int groupId) throws Exception 86 { 87 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.canDelete")); 88 p.setInt(1, groupId); 89 90 boolean status = false; 91 92 ResultSet rs = p.executeQuery(); 93 if (!rs.next() || rs.getInt("total") < 1) { 94 status = true; 95 } 96 97 rs.close(); 98 p.close(); 99 100 return status; 101 } 102 103 106 public void delete(int groupId) throws Exception 107 { 108 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.delete")); 109 p.setInt(1, groupId); 110 111 p.executeUpdate(); 112 p.close(); 113 } 114 115 118 public void update(Group group) throws Exception 119 { 120 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.update")); 121 p.setString(1, group.getName()); 122 p.setInt(2, group.getParentId()); 123 p.setString(3, group.getDescription()); 124 p.setInt(4, group.getId()); 125 126 p.executeUpdate(); 127 p.close(); 128 } 129 130 133 public void addNew(Group group) throws Exception 134 { 135 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.addNew")); 136 p.setString(1, group.getName()); 137 p.setString(2, group.getDescription()); 138 p.setInt(3, group.getParentId()); 139 140 p.executeUpdate(); 141 p.close(); 142 } 143 144 147 public List selectUsersIds(int groupId) throws Exception 148 { 149 ArrayList l = new ArrayList (); 150 151 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectUsersIds")); 152 p.setInt(1, groupId); 153 154 ResultSet rs = p.executeQuery(); 155 while (rs.next()) { 156 l.add(new Integer (rs.getInt("user_id"))); 157 } 158 159 rs.close(); 160 p.close(); 161 162 return l; 163 } 164 165 protected List fillGroups(ResultSet rs) throws Exception 166 { 167 List l = new ArrayList (); 168 169 while (rs.next()) { 170 l.add(this.getGroup(rs)); 171 } 172 173 return l; 174 } 175 176 protected Group getGroup(ResultSet rs) throws Exception 177 { 178 Group g = new Group(); 179 180 g.setId(rs.getInt("group_id")); 181 g.setDescription(rs.getString("group_description")); 182 g.setName(rs.getString("group_name")); 183 g.setParentId(rs.getInt("parent_id")); 184 185 return g; 186 } 187 188 191 public List selectAll() throws Exception 192 { 193 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectAll")); 194 ResultSet rs = p.executeQuery(); 195 196 List l = this.fillGroups(rs); 197 198 rs.close(); 199 p.close(); 200 201 return l; 202 } 203 204 } 205 | Popular Tags |