1 22 23 package org.xquark.mapper.storage; 24 25 import java.sql.*; 26 import java.util.NoSuchElementException ; 27 import java.util.StringTokenizer ; 28 29 import org.xquark.mapper.RepositoryConnection; 30 import org.xquark.mapper.RepositoryException; 31 import org.xquark.mapper.dbms.AbstractConnection; 32 import org.xquark.mapper.dbms.TableInfo; 33 import org.xquark.mapper.metadata.CollectionInfo; 34 import org.xquark.mapper.metadata.RepositoryConstants; 35 import org.xquark.xml.xdbc.Configurable; 36 import org.xquark.xml.xdbc.XMLDBCException; 37 import org.xquark.xml.xdbc.XMLDBCNotRecognizedException; 38 39 45 46 public class PersistentCollectionInfo implements RepositoryConstants 47 { 48 private static final String RCSRevision = "$Revision: 1.1 $"; 49 private static final String RCSName = "$Name: $"; 50 51 52 private String collectionName = null; 53 private short CID = -1; 54 private short parentCID = 0; 55 private short maxCID = -1; 56 57 private CollectionInfo config; 58 private TableInfo configTable; 59 private String selectLockStmt; 60 61 65 66 private AbstractConnection connection; 67 68 69 private Statement stmt = null; 70 private ResultSet rs = null; 71 72 76 public PersistentCollectionInfo(TableInfo configTable, AbstractConnection connection) 77 { 78 this.config = new CollectionInfo(false); 79 this.connection = connection; 80 this.configTable = configTable; 81 initStatements(); 82 } 83 84 91 public PersistentCollectionInfo( 92 TableInfo configTable, 93 String name, 94 CollectionInfo config, 95 short maxCID, 96 AbstractConnection connection 97 ) 98 { 99 this.connection = connection; 100 this.configTable = configTable; 101 this.config = config; 102 this.collectionName = name; 103 this.maxCID = maxCID; 104 initStatements(); 105 } 106 107 public PersistentCollectionInfo( 108 TableInfo configTable, 109 String name, 110 Configurable config, 111 short maxCID, 112 AbstractConnection connection 113 ) 114 { 115 this(configTable, name, new CollectionInfo(config), maxCID, connection); 116 } 117 118 private void initStatements() 119 { 120 selectLockStmt = connection.getLockSelectStatement("CID", configTable.getName(), "ORDER BY CID"); 121 } 122 123 126 public String getCollectionName() 127 { 128 return this.collectionName; 129 } 130 131 public short getCollectionID() 132 { 133 return CID; 134 } 135 136 public CollectionInfo getCollectionInfo() 137 { 138 return config; 139 } 140 141 144 public void setCollectionName(String name) 145 { 146 collectionName = name; 147 } 148 149 public void setCollectionID(short id) 150 { 151 CID = id; 152 } 153 154 157 public String toString() 158 { 159 StringBuffer buffer = new StringBuffer (super.toString()); 160 161 buffer.append("Name:\t\t"); 162 buffer.append(collectionName); 163 buffer.append("\nId :\t\t"); 164 buffer.append(CID); 165 buffer.append("\n"); 166 167 return buffer.toString(); 168 } 169 170 175 public void createRow() 176 throws SQLException, RepositoryException 177 { 178 PreparedStatement pStmt = null; 179 Statement stmt = null; 180 ResultSet rs =null; 181 int wCID = RepositoryConstants.SEQUENCE_FIRST_VALUE; 182 183 connection.start(); 185 186 try 187 { 188 stmt = connection.getConnection().createStatement(); 190 rs = stmt.executeQuery(selectLockStmt); 191 192 while (rs.next()) 193 { 194 if (rs.getShort(1) != wCID) 195 break; 196 wCID++; 197 } 198 rs.close(); 199 rs = null; 200 stmt.close(); 201 stmt = null; 202 203 if (wCID > maxCID) 204 throw new RepositoryException(RepositoryException.OUT_OF_RANGE, 205 "Maximum number of collections is reached"); 206 else 207 CID =(short)wCID; 208 209 pStmt = connection.getConnection().prepareStatement(configTable.getInsertStatement()); 211 pStmt.setString(1, collectionName.toUpperCase()); 212 pStmt.setShort(2, CID); 213 pStmt.setShort(3, parentCID); 214 pStmt.setByte(4, config.getType()); 215 pStmt.setString(5, config.getDescription()); 216 pStmt.setString(6, config.getMappingID()); 217 pStmt.setByte(7, config.getDocOIDSize()); 218 pStmt.setByte(8, config.getDocOIDSize()); pStmt.setShort(9, config.getDocOIDPreallocationSize()); pStmt.setString(10, generateConfigString()); 221 pStmt.executeUpdate(); 222 223 } 224 catch (SQLException e) 225 { 226 connection.rollback(); 227 throw new RepositoryException(RepositoryException.DB_ERROR, 228 "JDBC error while creating a new entry in collection table", e); 229 } 230 finally 231 { 232 connection.commit(); 233 if (rs != null) 234 rs.close(); 235 if (stmt != null) 236 stmt.close(); 237 if (pStmt != null) 238 pStmt.close(); 239 } 240 } 241 242 245 public void close() 246 { 247 try { 249 if (rs != null) 250 rs.close(); 251 if (stmt != null) 252 stmt.close(); 253 connection = null; 254 } 255 catch(SQLException e) { 256 } 258 } 259 260 public void retrieveDataSet() 261 throws SQLException, RepositoryException 262 { 263 retrieveDataSet((short)-1); 264 } 265 270 public void retrieveDataSet(short type) 271 throws SQLException, RepositoryException 272 { 273 stmt = connection.getConnection().createStatement(); 274 String query = configTable.getSelectAllStatement(); 275 if (type >= 0) 276 query += " WHERE TYPE=" + type; 277 try { 278 rs = stmt.executeQuery(query); 279 } 280 catch (SQLException e) { 281 stmt.close(); 282 throw e; 283 } 284 } 285 286 290 public boolean fetchNextRow() 291 throws SQLException, XMLDBCException 292 { 293 if (stmt == null) 294 return false; 295 try { 296 if (fetchNextResult(rs)) 297 return true; 298 else 299 { 300 config.setWriteProtected(true); 301 rs.close(); 302 stmt.close(); 303 stmt = null; return false; } 306 } 307 catch (SQLException e) { 308 rs.close(); 309 stmt.close(); 310 throw e; 311 } 312 } 313 314 319 public boolean fetchRow(String name) 320 throws SQLException, XMLDBCException 321 { 322 Statement stmt = connection.getConnection().createStatement(); 323 ResultSet rs = null; 324 try { 325 rs = stmt.executeQuery(configTable.getSelectAllStatement() 326 + " WHERE NAME='" + name + "'"); 327 328 if (fetchNextResult(rs)) 329 { 330 config.setWriteProtected(true); 331 return true; } 333 else 334 return false; } 336 finally 337 { 338 if (rs != null) 339 rs.close(); 340 stmt.close(); 341 } 342 } 343 344 349 public void deleteRow(String name) 350 throws SQLException 351 { 352 connection.executeUpdate("DELETE FROM " + configTable.getName() 353 + " WHERE NAME='" + name + "'"); 354 } 355 356 public void deleteRow() 357 throws SQLException 358 { 359 deleteRow(collectionName); 360 } 361 362 public void rename(String oldName, String newName) 363 throws SQLException 364 { 365 connection.executeUpdate("UPDATE " + configTable.getName() 366 + " SET NAME='" + newName + "' WHERE NAME='" + oldName + "'"); 367 } 368 369 372 private void parseConfigString(String configString) 373 throws XMLDBCException 374 { 375 String keyword; 376 StringTokenizer st = new StringTokenizer (configString, "<"); 377 while (st.hasMoreTokens()) 378 { 379 try { 380 keyword = st.nextToken(); 381 if (keyword.startsWith("maxTextDataLength")) 382 config.setProperty(RepositoryConnection.TEXT_LENGTH_PROPERTY, 383 keyword.substring(keyword.indexOf('>') + 1)); 384 else if (keyword.startsWith("maxExtraDataLength")) 385 config.setProperty(RepositoryConnection.EXTRA_DATA_LENGTH_PROPERTY, 386 keyword.substring(keyword.indexOf('>') + 1)); 387 else if (keyword.startsWith("useSchemaPrefixes")) 388 config.setFeature(RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE, 389 Boolean.valueOf(keyword.substring(keyword.indexOf('>') + 1)).booleanValue()); 390 } 391 catch (NoSuchElementException e) { 392 } 394 } 395 } 396 397 400 private String generateConfigString() 401 { 402 StringBuffer buf = new StringBuffer (); 403 try 404 { 405 buf.append("<maxTextDataLength>"); 406 buf.append(config.getProperty(RepositoryConnection.TEXT_LENGTH_PROPERTY)); 407 buf.append("<maxExtraDataLength>"); 408 buf.append(config.getProperty(RepositoryConnection.EXTRA_DATA_LENGTH_PROPERTY)); 409 buf.append("<useSchemaPrefixes>"); 410 buf.append(config.getFeature(RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE)); 411 } 412 catch (XMLDBCNotRecognizedException e) 413 { 414 throw new RuntimeException ("Internal error: unknown feature or property."); 416 } 417 418 return buf.toString(); 419 } 420 421 private boolean fetchNextResult(ResultSet rs) 422 throws SQLException, XMLDBCException 423 { 424 if(rs.next()) 425 { 426 collectionName = rs.getString(1); 427 CID = rs.getShort(2); 428 parentCID = rs.getShort(3); 429 config.setType(rs.getByte(4)); 430 config.setDescription(rs.getString(5)); 431 config.setMappingID(rs.getString(6)); 432 config.setDocOIDSize(rs.getByte(7)); 433 config.setDocOIDPreallocationSize(rs.getShort(9)); 434 parseConfigString(rs.getString(10)); 435 return true; 436 } 437 else 438 return false; } 440 } 441 | Popular Tags |