1 package sellwin.db; 2 3 import sellwin.domain.*; 4 import sellwin.utils.*; 5 6 import java.util.ArrayList ; 7 import java.sql.*; 8 9 13 18 public class SalesPersonRoleDB extends DBType implements DBInterface { 19 20 private Connection con; 21 22 private final static String deleteQuery = 23 "DELETE FROM sales_person_role WHERE pk = "; 24 25 private final static String deleteSalesPersonQuery= 26 "DELETE FROM sales_person_role WHERE sp_pk="; 27 28 private final static String insertQuery = 29 "INSERT INTO sales_person_role " + 30 "VALUES ("; 31 32 private final static String selectQuery = 33 "SELECT sp_pk, user_role_pk, " + 34 "modified_by, modified_date " + 35 "FROM sales_person_role " + 36 "WHERE pk = "; 37 38 private final static String selectRolesForUserQuery = 39 "SELECT user_role.pk, " + 40 "user_role.name, user_role.modified_by, " + 41 "user_role.modified_date " + 42 "FROM sales_person_role, user_role " + 43 "WHERE user_role.pk = sales_person_role.user_role_pk AND " + 44 "sales_person_role.sp_pk = "; 45 46 50 public SalesPersonRoleDB() { 51 } 52 53 57 public SalesPersonRoleDB(int dbType) { 58 DB_TYPE = dbType; 59 } 60 61 67 public SalesPersonRoleDB(Connection con) { 68 this.con = con; 69 } 70 71 76 public Connection getConnection() { 77 return this.con; 78 } 79 80 85 public final void setConnection(Connection con) 86 throws SQLException { 87 88 this.con = con; 89 } 90 91 99 public final Object selectRow(Object pk) 100 throws SQLException { 101 102 SalesPersonRole sp = new SalesPersonRole(); 103 sp.setPK(((Long )(pk)).longValue()); 104 Statement stmt = null; 105 ResultSet rs = null; 106 String query = selectQuery + sp.getPK(); 107 108 try { 109 stmt = con.createStatement(); 110 if (Prefs.DEBUG) LogWrite.write(query); 111 rs = stmt.executeQuery(query); 112 113 int i; 114 115 while (rs.next()) { 116 i=1; 117 sp.setSalesPersonPK(rs.getLong(i)); i++; 118 sp.setUserRolePK(rs.getLong(i)); i++; 119 UserRoleDB udb = new UserRoleDB(DB_TYPE); 120 udb.setConnection(getConnection()); 121 UserRole u = (UserRole)udb.selectRow(new Long (sp.getUserRolePK())); 122 sp.setUserRole(u); 123 sp.setModifiedBy(rs.getString(i)); i++; 124 sp.setModifiedDate(rs.getDate(i)); 125 } 126 127 } catch (SQLException e) { 128 throw e; 129 } finally { 130 try { 131 if (rs != null) rs.close(); 132 } catch (SQLException x) { throw x; } 133 try { 134 if (stmt != null) stmt.close(); 135 } catch (SQLException x) { throw x; } 136 } 137 138 return sp; 139 } 140 141 142 148 public final void updateRow(Object obj) 149 throws SQLException { 150 151 } 153 154 163 public final long insertRow(Object obj, boolean load) 164 throws SQLException { 165 166 SalesPersonRole sales_person_role = (SalesPersonRole)obj; 167 168 if (!load) 169 sales_person_role.setPK(DBUtils.generatePK()); 170 171 StringBuffer query = new StringBuffer (insertQuery); 172 Statement stmt = con.createStatement(); 173 174 int i=1; 175 query.append(sales_person_role.getPK()).append(","); 176 query.append(sales_person_role.getSalesPersonPK()).append(","); 177 query.append(sales_person_role.getUserRolePK()).append(","); 178 query.append(JDBC.quoteMore(sales_person_role.getModifiedBy())); 179 if (DB_TYPE == Prefs.MYSQL) 180 query.append("CURRENT_DATE"); 181 else 182 query.append("SYSDATE"); 183 query.append(")"); 184 185 if (Prefs.DEBUG) LogWrite.write(query.toString()); 186 int rc = stmt.executeUpdate(query.toString()); 187 188 return sales_person_role.getPK(); 189 } 190 191 198 public final void deleteRow(Object obj) 199 throws SQLException { 200 201 long pkValue = ((Long )obj).longValue(); 202 203 String query = deleteQuery + pkValue; 204 205 Statement stmt = null; 206 207 try { 208 stmt = con.createStatement(); 209 if (Prefs.DEBUG) LogWrite.write(query); 210 stmt.executeUpdate(query); 211 } catch (SQLException e) { 212 throw e; 213 } finally { 214 try { 215 if (stmt != null) stmt.close(); 216 } catch (SQLException x) { } 217 } 218 } 219 220 227 public final void deleteSalesPersonRows(Object obj) 228 throws SQLException { 229 230 long pkValue = ((Long )obj).longValue(); 231 232 String query = deleteSalesPersonQuery + pkValue; 233 234 Statement stmt = null; 235 236 try { 237 stmt = con.createStatement(); 238 if (Prefs.DEBUG) LogWrite.write(query); 239 stmt.executeUpdate(query); 240 } catch (SQLException e) { 241 throw e; 242 } finally { 243 try { 244 if (stmt != null) stmt.close(); 245 } catch (SQLException x) { } 246 } 247 } 248 249 260 public final void selectRolesForUser(long spPK, ArrayList roles) 261 throws SQLException { 262 263 Statement stmt = null; 264 ResultSet rs = null; 265 String query = selectRolesForUserQuery + spPK; 266 267 try { 268 stmt = con.createStatement(); 269 if (Prefs.DEBUG) LogWrite.write(query); 270 rs = stmt.executeQuery(query); 271 272 int i; 273 UserRole ur; 274 RolePermissionDB rolePermDB = new RolePermissionDB(); 275 rolePermDB.setConnection(getConnection()); 276 277 while (rs.next()) { 278 i=1; 279 ur = new UserRole(); 280 ur.setPK(rs.getLong(i)); i++; 281 ur.setName(rs.getString(i)); i++; 282 ur.setModifiedBy(rs.getString(i)); i++; 283 ur.setModifiedDate(rs.getDate(i)); 284 285 rolePermDB.selectRows(ur.getPK(), ur.getPermissions()); 287 roles.add(ur); 288 } 289 } catch (SQLException e) { 290 throw e; 291 } finally { 292 try { 293 if (rs != null) rs.close(); 294 } catch (SQLException x) { throw x; } 295 try { 296 if (stmt != null) stmt.close(); 297 } catch (SQLException x) { throw x; } 298 } 299 } 300 305 public final void truncate() 306 throws SQLException { 307 308 String query = "truncate table sales_person_role"; 309 310 Statement stmt = null; 311 try { 312 stmt = con.createStatement(); 313 if (Prefs.DEBUG) LogWrite.write(query); 314 stmt.executeUpdate(query); 315 } catch (SQLException e) { 316 throw e; 317 } finally { 318 try { if (stmt != null) stmt.close(); 319 } catch (SQLException x) { } 320 } 321 } 322 323 } 324 | Popular Tags |