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 35 import com.genimen.djeneric.repository.DjIdProvider; 36 import com.genimen.djeneric.repository.DjModelView; 37 import com.genimen.djeneric.repository.DjPersistenceManager; 38 import com.genimen.djeneric.repository.DjSession; 39 import com.genimen.djeneric.repository.DjUser; 40 import com.genimen.djeneric.repository.exceptions.DjenericException; 41 42 public class RdbmsModelView extends DjModelView 43 { 44 String _originalDefinition = null; 45 String _originalCode = null; 46 47 protected RdbmsModelView(DjPersistenceManager mgr) 48 { 49 super(mgr); 50 } 51 52 protected RdbmsModelView(DjPersistenceManager mgr, long id, String code, String definition) 53 { 54 super(mgr, id, code, definition); 55 _originalDefinition = definition; 56 _originalCode = code; 57 } 58 59 public void delete(DjSession session) throws DjenericException 60 { 61 try 62 { 63 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 64 + RdbmsPersistenceManager.USER_VWS_TABLE 65 + " where metaview_id = :id"); 66 stmt.setLong("id", getId()); 67 stmt.executeUpdate(); 68 69 stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.VIEW_TABLE 70 + " where id = :id"); 71 stmt.setLong("id", getId()); 72 stmt.executeUpdate(); 73 } 74 catch (SQLException x) 75 { 76 throw new DjenericException(x); 77 } 78 } 79 80 public void reload(DjSession session) throws DjenericException 81 { 82 try 83 { 84 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select code, metaview from " 85 + RdbmsPersistenceManager.VIEW_TABLE 86 + " where id = :id"); 87 stmt.setLong("id", getId()); 88 ResultSet rs = stmt.executeQuery(); 89 if (rs.next()) 90 { 91 _originalCode = rs.getString("code"); 92 setCode(_originalCode); 93 _originalDefinition = new String (SqlStatement.getBlob(rs, "metaview")); 94 setDefinition(_originalDefinition); 95 } 96 rs.close(); 97 } 98 catch (SQLException x) 99 { 100 throw new DjenericException(x); 101 } 102 catch (IOException x) 103 { 104 throw new DjenericException(x); 105 } 106 } 107 108 public boolean isModifiedExternally(DjSession session) throws DjenericException 111 { 112 if (_originalCode == null || _originalDefinition == null) return false; 113 try 114 { 115 if (!_originalCode.equals(getCode())) return false; 116 117 boolean modified = true; 118 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select code, metaview from " 119 + RdbmsPersistenceManager.VIEW_TABLE 120 + " where id = :id"); 121 stmt.setLong("id", getId()); 122 ResultSet rs = stmt.executeQuery(); 123 if (rs.next()) 124 { 125 String inDb = new String (SqlStatement.getBlob(rs, "metaview")); 126 modified = !inDb.equals(_originalDefinition); 127 } 128 rs.close(); 129 return modified; 130 } 131 catch (SQLException x) 132 { 133 throw new DjenericException(x); 134 } 135 catch (IOException x) 136 { 137 throw new DjenericException(x); 138 } 139 } 140 141 public void persist(DjSession session) throws DjenericException 142 { 143 try 144 { 145 if (getId() == -1) 146 { 147 DjIdProvider ip = new RdbmsIdProvider(1); 148 ip.setSequenceName(RdbmsPersistenceManager.VIEW_TABLE); 150 151 setId(ip.getNextId(session)); 152 } 153 SqlStatement stmt = ((RdbmsSession) session) 154 .getInternalSqlStatement("update " + RdbmsPersistenceManager.VIEW_TABLE 155 + " set code = :code, metaview = :metaview where id = :id"); 156 stmt.setString("code", getCode()); 157 stmt.setBlob("metaview", getDefinition(), DjPersistenceManager.ENCODING_METHOD); 158 stmt.setLong("id", getId()); 159 160 if (stmt.executeUpdate() == 0) 161 { 162 insert(session); 164 } 166 _originalDefinition = getDefinition(); 167 _originalCode = getCode(); 168 } 169 catch (SQLException x) 170 { 171 throw new DjenericException(x); 172 } 173 174 } 175 176 public void insert(DjSession session) throws DjenericException 177 { 178 try 179 { 180 SqlStatement stmt = ((RdbmsSession) session) 181 .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.VIEW_TABLE 182 + "(id, code, metaview) values(:id, :code, :metaview)"); 183 stmt.setLong("id", getId()); 184 stmt.setString("code", getCode()); 185 stmt.setBlob("metaview", getDefinition(), DjPersistenceManager.ENCODING_METHOD); 186 stmt.executeUpdate(); 187 } 188 catch (SQLException x) 189 { 190 throw new DjenericException(x); 191 } 192 } 193 194 public DjUser[] getUsers(DjSession session) throws DjenericException 195 { 196 try 197 { 198 ArrayList result = new ArrayList (); 199 SqlStatement stmt = ((RdbmsSession) session) 200 .getInternalSqlStatement("select dev.id, dev.code, dev.name, dev.password, dev.administrator_yn, dev.modeler_yn " 201 + "from " 202 + RdbmsPersistenceManager.USER_TABLE 203 + " dev, " 204 + " " 205 + RdbmsPersistenceManager.USER_VWS_TABLE 206 + " dv " 207 + "where dev.id = dv.user_id " 208 + "and dv.metaview_id = :id " + "order by dev.code "); 209 stmt.setLong("id", getId()); 210 ResultSet rs = stmt.executeQuery(); 211 while (rs.next()) 212 { 213 DjUser dev = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs 214 .getString("name"), rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y" 215 .equalsIgnoreCase(rs.getString("modeler_yn"))); 216 result.add(dev); 217 } 218 rs.close(); 219 stmt.close(); 220 221 return (DjUser[]) result.toArray(new DjUser[0]); 222 } 223 catch (SQLException x) 224 { 225 throw new DjenericException(x); 226 } 227 } 228 229 public void removeUser(DjSession session, DjUser user) throws DjenericException 230 { 231 try 232 { 233 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 234 + RdbmsPersistenceManager.USER_VWS_TABLE 235 + " " + "where user_id = :devId " 236 + "and metaview_id = :mvwId"); 237 stmt.setLong("devId", user.getId()); 238 stmt.setLong("mvwId", getId()); 239 stmt.executeUpdate(); 240 } 241 catch (SQLException x) 242 { 243 throw new DjenericException(x); 244 } 245 } 246 247 } | Popular Tags |