1 30 package com.genimen.djeneric.repository.rdbms; 31 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.util.ArrayList ; 35 36 import com.genimen.djeneric.language.Messages; 37 import com.genimen.djeneric.repository.DjContext; 38 import com.genimen.djeneric.repository.DjIdProvider; 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 import com.genimen.djeneric.util.DjLogger; 45 46 public class RdbmsContext extends DjContext 47 { 48 protected RdbmsContext(DjPersistenceManager mgr) 49 { 50 super(mgr); 51 } 52 53 protected RdbmsContext(DjPersistenceManager mgr, long id, String code, String name) 54 { 55 super(mgr, id, code, name); 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 context_id = :id"); 65 stmt.setLong("id", getId()); 66 stmt.executeUpdate(); 67 68 stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.CONTEXT_TABLE 69 + " where id = :id"); 70 stmt.setLong("id", getId()); 71 stmt.executeUpdate(); 72 } 73 catch (SQLException x) 74 { 75 DjLogger.log(x); 76 if (x.getSQLState().equals("01000")) throw new DjenericException(Messages 77 .getString("RdbmsContext.CouldNotDeleteContext")); 78 throw new DjenericException(x); 79 } 80 } 81 82 public void deleteContents(DjSession session) throws DjenericException 83 { 84 try 85 { 86 87 String connName = session.getPersistenceManager().getCurrentRepository().getClass().getName().toLowerCase(); 90 if (connName.indexOf("hsqldb") != -1) for (int i = 1; i <= DjPersistenceManager.MAPPING_COLUMN_COUNT_REL; i++) 91 { 92 String idxStr = String.valueOf(i); 93 if (idxStr.length() == 1) idxStr = "0" + idxStr; 94 String colName = DjPersistenceManager.MAPPING_REL + idxStr; 95 96 SqlStatement stmt = ((RdbmsSession) session) 97 .getInternalSqlStatement("update " + RdbmsPersistenceManager.POLYMORPH_TABLE + " set " + colName 98 + " = null " + " where " + RdbmsPersistenceManager.INTERNAL_CONTEXT_COLUMN 99 + " = :id"); 100 stmt.setLong("id", getId()); 101 stmt.executeUpdate(); 102 } 103 104 SqlStatement stmt = ((RdbmsSession) session) 105 .getInternalSqlStatement("delete from " + RdbmsPersistenceManager.POLYMORPH_TABLE + " where " 106 + RdbmsPersistenceManager.INTERNAL_CONTEXT_COLUMN + " = :id"); 107 stmt.setLong("id", getId()); 108 stmt.executeUpdate(); 109 } 110 catch (SQLException x) 111 { 112 DjLogger.log(x); 113 if (x.getSQLState().equals("01000")) throw new DjenericException(Messages 114 .getString("RdbmsContext.CouldNotDeleteContext")); 115 throw new DjenericException(x); 116 } 117 118 } 119 120 public void reload(DjSession session) throws DjenericException 121 { 122 try 123 { 124 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select code, name from " 125 + RdbmsPersistenceManager.CONTEXT_TABLE 126 + " where id = :id"); 127 stmt.setLong("id", getId()); 128 ResultSet rs = stmt.executeQuery(); 129 if (rs.next()) 130 { 131 setCode(rs.getString("code")); 132 setName(rs.getString("name")); 133 } 134 rs.close(); 135 stmt.close(); 136 } 137 catch (SQLException x) 138 { 139 throw new DjenericException(x); 140 } 141 } 142 143 public void persist(DjSession session) throws DjenericException 144 { 145 if (getId() == -1) 146 { 147 DjIdProvider ip = new RdbmsIdProvider(1); 148 ip.setSequenceName(RdbmsPersistenceManager.CONTEXT_TABLE); 150 setId(ip.getNextId(session)); 151 } 152 153 SqlStatement stmt = ((RdbmsSession) session) 154 .getInternalSqlStatement("update " + RdbmsPersistenceManager.CONTEXT_TABLE 155 + " set code = :code, name = :name where id = :id"); 156 try 157 { 158 stmt.setString("code", getCode()); 159 stmt.setString("name", getName()); 160 stmt.setLong("id", getId()); 161 if (stmt.executeUpdate() == 0) 162 { 163 insert(session); 165 } 167 } 168 catch (SQLException x) 169 { 170 throw new DjenericException(x); 171 } 172 } 173 174 public void insert(DjSession session) throws DjenericException 175 { 176 SqlStatement stmt = ((RdbmsSession) session) 177 .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.CONTEXT_TABLE 178 + "(id, code, name) values(:id, :code, :name)"); 179 try 180 { 181 stmt.setLong("id", getId()); 182 stmt.setString("code", getCode()); 183 stmt.setString("name", getName()); 184 stmt.executeUpdate(); 185 } 186 catch (SQLException x) 187 { 188 throw new DjenericException(x); 189 } 190 } 191 192 public DjUserContextAssociation[] getUsers(DjSession session) throws DjenericException 193 { 194 ArrayList result = new ArrayList (); 195 SqlStatement stmt = ((RdbmsSession) session) 196 .getInternalSqlStatement("select dev.id, dev.code, dev.name, dev.administrator_yn, dev.modeler_yn, dev.password, " 197 + " dc.cre_yn, dc.mod_yn, dc.del_yn, dc.qry_yn " 198 + "from " 199 + RdbmsPersistenceManager.USER_TABLE 200 + " dev, " 201 + RdbmsPersistenceManager.USER_CTX_TABLE 202 + " dc " 203 + "where dc.context_id = :id " 204 + "and dc.user_id = dev.id " + "order by dev.code"); 205 try 206 { 207 stmt.setLong("id", getId()); 208 ResultSet rs = stmt.executeQuery(); 209 while (rs.next()) 210 { 211 DjUser dev = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs 212 .getString("name"), rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y" 213 .equalsIgnoreCase(rs.getString("modeler_yn"))); 214 DjUserContextAssociation dca = new RdbmsUserContextAssociation(dev, this, rs.getString("cre_yn") 215 .equalsIgnoreCase("Y"), rs.getString("mod_yn").equalsIgnoreCase("Y"), rs.getString("del_yn") 216 .equalsIgnoreCase("Y"), rs.getString("qry_yn").equalsIgnoreCase("Y")); 217 result.add(dca); 218 } 219 rs.close(); 220 stmt.close(); 221 222 return (DjUserContextAssociation[]) result.toArray(new DjUserContextAssociation[0]); 223 } 224 catch (SQLException x) 225 { 226 throw new DjenericException(x); 227 } 228 } 229 230 public void removeAllUserAssociations(DjSession session) throws DjenericException 231 { 232 try 233 { 234 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 235 + RdbmsPersistenceManager.USER_CTX_TABLE 236 + " where context_id = :ctxtId"); 237 stmt.setLong("ctxtId", getId()); 238 stmt.executeUpdate(); 239 } 240 catch (SQLException x) 241 { 242 throw new DjenericException(x); 243 } 244 } 245 246 } | Popular Tags |