1 28 package com.genimen.djeneric.repository.rdbms; 29 30 import java.io.IOException ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.util.ArrayList ; 34 import java.util.WeakHashMap ; 35 36 import com.genimen.djeneric.repository.DjContext; 37 import com.genimen.djeneric.repository.DjIdProvider; 38 import com.genimen.djeneric.repository.DjModelView; 39 import com.genimen.djeneric.repository.DjPersistenceManager; 40 import com.genimen.djeneric.repository.DjSession; 41 import com.genimen.djeneric.repository.DjUser; 42 import com.genimen.djeneric.repository.DjUserContextAssociation; 43 import com.genimen.djeneric.repository.exceptions.DjenericException; 44 45 public class RdbmsUser extends DjUser 46 { 47 protected RdbmsUser(DjPersistenceManager mgr) 48 { 49 super(mgr); 50 } 51 52 protected RdbmsUser(DjPersistenceManager mgr, long id, String code, String name, String password, 53 boolean administrator, boolean modeler) 54 { 55 super(mgr, id, code, name, password, administrator, modeler); 56 } 57 58 public void delete(DjSession session) throws DjenericException 59 { 60 try 61 { 62 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 63 + RdbmsPersistenceManager.USER_CTX_TABLE 64 + " where user_id = :id"); 65 stmt.setLong("id", getId()); 66 stmt.executeUpdate(); 67 68 stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.USER_VWS_TABLE 69 + " where user_id = :id"); 70 stmt.setLong("id", getId()); 71 stmt.executeUpdate(); 72 73 stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.USER_TABLE 74 + " where id = :id"); 75 stmt.setLong("id", getId()); 76 stmt.executeUpdate(); 77 } 78 catch (SQLException x) 79 { 80 throw new DjenericException(x); 81 } 82 } 83 84 public void reload(DjSession session) throws DjenericException 85 { 86 try 87 { 88 SqlStatement stmt = ((RdbmsSession) session) 89 .getInternalSqlStatement("select code, name, password, administrator_yn, modeler_yn from " 90 + RdbmsPersistenceManager.USER_TABLE + " where id = :id"); 91 stmt.setLong("id", getId()); 92 ResultSet rs = stmt.executeQuery(); 93 if (rs.next()) 94 { 95 setCode(rs.getString("code")); 96 setName(rs.getString("name")); 97 setPassword(rs.getString("password")); 98 setAdministrator("Y".equalsIgnoreCase(rs.getString("administrator_yn"))); 99 setModeler("Y".equalsIgnoreCase(rs.getString("modeler_yn"))); 100 } 101 rs.close(); 102 } 103 catch (SQLException x) 104 { 105 throw new DjenericException(x); 106 } 107 } 108 109 public void persist(DjSession session) throws DjenericException 110 { 111 try 112 { 113 if (getId() == -1) 114 { 115 DjIdProvider ip = new RdbmsIdProvider(1); 116 ip.setSequenceName(RdbmsPersistenceManager.USER_TABLE); 118 119 setId(ip.getNextId(session)); 120 } 121 SqlStatement stmt = ((RdbmsSession) session) 122 .getInternalSqlStatement("update " 123 + RdbmsPersistenceManager.USER_TABLE 124 + " set code = :code, name = :name, password = :password, administrator_yn = :administrator_yn, modeler_yn = :modeler_yn where id = :id"); 125 stmt.setString("code", getCode()); 126 stmt.setString("name", getName()); 127 stmt.setString("password", getPassword()); 128 stmt.setString("administrator_yn", isAdministrator() ? "Y" : "N"); 129 stmt.setString("modeler_yn", isModeler() ? "Y" : "N"); 130 stmt.setLong("id", getId()); 131 132 if (stmt.executeUpdate() == 0) 133 { 134 insert(session); 136 } 138 } 139 catch (SQLException x) 140 { 141 throw new DjenericException(x); 142 } 143 } 144 145 public void insert(DjSession session) throws DjenericException 146 { 147 try 148 { 149 SqlStatement stmt = ((RdbmsSession) session) 150 .getInternalSqlStatement("insert into " 151 + RdbmsPersistenceManager.USER_TABLE 152 + "(id, code, name, password, administrator_yn, modeler_yn) values(:id, :code, :name, :password, :administrator_yn, :modeler_yn)"); 153 stmt.setLong("id", getId()); 154 stmt.setString("code", getCode()); 155 stmt.setString("name", getName()); 156 stmt.setString("password", getPassword()); 157 stmt.setString("administrator_yn", isAdministrator() ? "Y" : "N"); 158 stmt.setString("modeler_yn", isModeler() ? "Y" : "N"); 159 stmt.executeUpdate(); 160 } 161 catch (SQLException x) 162 { 163 throw new DjenericException(x); 164 } 165 } 166 167 public DjUserContextAssociation[] getContextAssociations() throws DjenericException 168 { 169 DjSession session = null; 170 171 try 172 { 173 session = getPersistenceManager().createSession(); 174 175 ArrayList result = new ArrayList (); 176 SqlStatement stmt = ((RdbmsSession) session) 177 .getInternalSqlStatement("select ctxt.id, ctxt.code, ctxt.name, " 178 + " dc.cre_yn, dc.mod_yn, dc.del_yn, dc.qry_yn " + "from " 179 + RdbmsPersistenceManager.CONTEXT_TABLE + " ctxt, " + " " 180 + RdbmsPersistenceManager.USER_CTX_TABLE + " dc " 181 + "where ctxt.id = dc.context_id " + "and dc.user_id = :id " 182 + "order by ctxt.name "); 183 stmt.setLong("id", getId()); 184 ResultSet rs = stmt.executeQuery(); 185 while (rs.next()) 186 { 187 DjContext ctxt = new RdbmsContext(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs 188 .getString("name")); 189 DjUserContextAssociation dca = new RdbmsUserContextAssociation(this, ctxt, rs.getString("cre_yn") 190 .equalsIgnoreCase("Y"), rs.getString("mod_yn").equalsIgnoreCase("Y"), rs.getString("del_yn") 191 .equalsIgnoreCase("Y"), rs.getString("qry_yn").equalsIgnoreCase("Y")); 192 result.add(dca); 193 } 194 rs.close(); 195 stmt.close(); 196 197 return (DjUserContextAssociation[]) result.toArray(new DjUserContextAssociation[0]); 198 } 199 catch (SQLException x) 200 { 201 throw new DjenericException(x); 202 } 203 finally 204 { 205 if (session != null) session.close(); 206 } 207 } 208 209 public DjUserContextAssociation getContextAssociation(DjContext ctxt) throws DjenericException 210 { 211 DjSession session = null; 212 213 try 214 { 215 session = getPersistenceManager().createSession(); 216 217 DjUserContextAssociation result = null; 218 SqlStatement stmt = ((RdbmsSession) session) 219 .getInternalSqlStatement("select dc.cre_yn, dc.mod_yn, dc.del_yn, dc.qry_yn " + "from " 220 + RdbmsPersistenceManager.USER_CTX_TABLE + " dc " + "where dc.context_id = :cid " 221 + "and dc.user_id = :uid "); 222 stmt.setLong("uid", getId()); 223 stmt.setLong("cid", ctxt.getId()); 224 ResultSet rs = stmt.executeQuery(); 225 if (rs.next()) 226 { 227 result = new RdbmsUserContextAssociation(this, ctxt, rs.getString("cre_yn").equalsIgnoreCase("Y"), rs 228 .getString("mod_yn").equalsIgnoreCase("Y"), rs.getString("del_yn").equalsIgnoreCase("Y"), rs 229 .getString("qry_yn").equalsIgnoreCase("Y")); 230 } 231 rs.close(); 232 stmt.close(); 233 234 return result; 235 } 236 catch (SQLException x) 237 { 238 throw new DjenericException(x); 239 } 240 finally 241 { 242 if (session != null) session.close(); 243 } 244 } 245 246 public void removeAllContextAssociations(DjSession session) throws DjenericException 247 { 248 try 249 { 250 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 251 + RdbmsPersistenceManager.USER_CTX_TABLE 252 + " where user_id = :devId"); 253 stmt.setLong("devId", getId()); 254 stmt.executeUpdate(); 255 } 256 catch (SQLException x) 257 { 258 throw new DjenericException(x); 259 } 260 } 261 262 private static WeakHashMap _cachedViews = new WeakHashMap (); 267 268 public DjModelView[] getViews() throws DjenericException 269 { 270 DjSession session = null; 271 try 272 { 273 session = getPersistenceManager().createSession(); 274 ArrayList result = new ArrayList (); 275 276 String sql; 277 278 if (!isAdministrator()) 279 { 280 sql = "select mvw.id, mvw.code, mvw.metaview " + "from " + RdbmsPersistenceManager.VIEW_TABLE + " mvw, " 281 + " " + RdbmsPersistenceManager.USER_VWS_TABLE + " dv " + "where mvw.id = dv.metaview_id " 282 + "and dv.user_id = :id " + "order by mvw.code "; 283 } 284 else 285 { 286 sql = "select mvw.id, mvw.code, mvw.metaview " + "from " + RdbmsPersistenceManager.VIEW_TABLE + " mvw " 287 + "order by mvw.code "; 288 } 289 290 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement(sql); 291 292 if (!isAdministrator()) stmt.setLong("id", getId()); 293 294 ResultSet rs = stmt.executeQuery(); 295 while (rs.next()) 296 { 297 Long key = new Long (rs.getLong("id")); 298 299 if (_cachedViews.containsKey(key)) 300 { 301 result.add(_cachedViews.get(key)); 302 } 303 else 304 { 305 DjModelView vw = new RdbmsModelView(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), 306 new String (SqlStatement.getBlob(rs, "metaview"))); 307 _cachedViews.put(key, vw); 308 result.add(vw); 309 } 310 } 311 rs.close(); 312 stmt.close(); 313 314 return (DjModelView[]) result.toArray(new DjModelView[0]); 315 } 316 catch (SQLException x) 317 { 318 throw new DjenericException(x); 319 } 320 catch (IOException x) 321 { 322 throw new DjenericException(x); 323 } 324 finally 325 { 326 if (session != null) session.close(); 327 } 328 } 329 330 public void addView(DjSession session, DjModelView view) throws DjenericException 331 { 332 try 333 { 334 SqlStatement stmt = ((RdbmsSession) session) 335 .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.USER_VWS_TABLE 336 + "(user_id, metaview_id) values(:devId, :mvwId)"); 337 stmt.setLong("devId", getId()); 338 stmt.setLong("mvwId", view.getId()); 339 stmt.executeUpdate(); 340 } 341 catch (SQLException x) 342 { 343 throw new DjenericException(x); 344 } 345 } 346 347 public void removeView(DjSession session, DjModelView view) throws DjenericException 348 { 349 try 350 { 351 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 352 + RdbmsPersistenceManager.USER_VWS_TABLE 353 + " " + "where user_id = :devId " 354 + "and metaview_id = :mvwId"); 355 stmt.setLong("devId", getId()); 356 stmt.setLong("mvwId", view.getId()); 357 stmt.executeUpdate(); 358 } 359 catch (SQLException x) 360 { 361 throw new DjenericException(x); 362 } 363 } 364 365 public void removeAllViews(DjSession session) throws DjenericException 366 { 367 try 368 { 369 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 370 + RdbmsPersistenceManager.USER_VWS_TABLE 371 + " " + "where user_id = :devId"); 372 stmt.setLong("devId", getId()); 373 stmt.executeUpdate(); 374 } 375 catch (SQLException x) 376 { 377 throw new DjenericException(x); 378 } 379 } 380 } | Popular Tags |