1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.io.*; 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 32 import org.apache.slide.common.Service; 33 import org.apache.slide.common.ServiceAccessException; 34 import org.apache.slide.common.Uri; 35 import org.apache.slide.content.NodeRevisionDescriptor; 36 import org.apache.slide.content.NodeRevisionNumber; 37 import org.apache.slide.content.NodeRevisionContent; 38 import org.apache.slide.lock.LockTokenNotFoundException; 39 import org.apache.slide.lock.NodeLock; 40 import org.apache.slide.security.NodePermission; 41 import org.apache.slide.structure.ObjectNode; 42 import org.apache.slide.structure.ObjectNotFoundException; 43 import org.apache.slide.util.logger.Logger; 44 45 46 51 public class CommonRDBMSAdapter extends StandardRDBMSAdapter { 52 53 55 public CommonRDBMSAdapter(Service service, Logger logger) { 56 super(service, logger); 57 } 58 59 60 61 public void removeObject(Connection connection, Uri uri, ObjectNode object) 62 throws ServiceAccessException, ObjectNotFoundException 63 { 64 PreparedStatement statement = null; 65 try { 66 clearBinding(connection, uri); 67 68 try { 70 statement = 71 connection.prepareStatement( 72 "delete from LINKS l where l.URI_ID in (" + 73 "select u.URI_ID from URI u where u.URI_STRING = ?)"); 74 statement.setString(1, uri.toString()); 75 statement.executeUpdate(); 76 } finally { 77 close(statement); 78 } 79 try { 82 statement = 83 connection.prepareStatement( 84 "delete from VERSION_HISTORY vh where vh.URI_ID in (" + 85 "select u.URI_ID from URI u where u.URI_STRING = ?)"); 86 statement.setString(1, uri.toString()); 87 statement.executeUpdate(); 88 } finally { 89 close(statement); 90 } 91 try { 93 statement = 94 connection.prepareStatement( 95 "delete from VERSION v where v.URI_ID in (" + 96 "select u.URI_ID from URI u where u.URI_STRING = ?)"); 97 statement.setString(1, uri.toString()); 98 statement.executeUpdate(); 99 } finally { 100 close(statement); 101 } 102 try { 104 statement = 105 connection.prepareStatement( 106 "delete from OBJECT o where o.URI_ID in (" + 107 "select u.URI_ID from URI u where u.URI_STRING = ?)"); 108 statement.setString(1, uri.toString()); 109 statement.executeUpdate(); 110 } finally { 111 close(statement); 112 } 113 try { 115 statement = connection.prepareStatement("delete from URI where URI_STRING = ?"); 116 statement.setString(1, uri.toString()); 117 statement.executeUpdate(); 118 } finally { 119 close(statement); 120 } 121 } catch (SQLException e) { 122 throw createException(e, uri.toString()); 123 124 } 125 } 126 127 public void removeRevisionContent(Connection connection, Uri uri, NodeRevisionDescriptor revisionDescriptor) 128 throws ServiceAccessException { 129 try { 130 PreparedStatement statement = null; 131 try { 132 statement = 133 connection.prepareStatement( 134 "delete from VERSION_CONTENT vc where vc.VERSION_ID in (" + 135 "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.REVISION_NO = ? and vh.URI_ID=u.URI_ID AND u.URI_STRING=?)"); 136 statement.setString(1, revisionDescriptor.getRevisionNumber().toString()); 137 statement.setString(2, uri.toString()); 138 statement.executeUpdate(); 139 } finally { 140 close(statement); 141 } 142 } catch (SQLException e) { 143 throw createException(e, uri.toString()); 144 } 145 } 146 147 public void removeRevisionDescriptors(Connection connection, Uri uri) throws ServiceAccessException { 148 PreparedStatement statement = null; 149 try { 150 statement = 151 connection.prepareStatement( 152 "delete from VERSION_PREDS vp where vp.VERSION_ID in (" + 153 "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.URI_ID = u.URI_ID and u.URI_STRING = ?)"); 154 statement.setString(1, uri.toString()); 155 statement.executeUpdate(); 156 } catch (SQLException e) { 157 throw createException(e, uri.toString()); 158 } finally { 159 close(statement); 160 } 161 } 162 163 public void removeRevisionDescriptor(Connection connection, Uri uri, NodeRevisionNumber revisionNumber) 164 throws ServiceAccessException 165 { 166 PreparedStatement statement = null; 167 try { 168 try { 169 statement = 170 connection.prepareStatement( 171 "delete from VERSION_LABELS vl where vl.VERSION_ID in (" + 172 "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.REVISION_NO = ? and vh.URI_ID = u.URI_ID AND u.URI_STRING = ?)"); 173 statement.setString(1, revisionNumber.toString()); 174 statement.setString(2, uri.toString()); 175 statement.executeUpdate(); 176 } finally { 177 close(statement); 178 } 179 try { 180 statement = 181 connection.prepareStatement( 182 "delete from PROPERTIES p where p.VERSION_ID in (" + 183 "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.REVISION_NO = ? and vh.URI_ID = u.URI_ID AND u.URI_STRING = ?)"); 184 statement.setString(1, revisionNumber.toString()); 185 statement.setString(2, uri.toString()); 186 statement.executeUpdate(); 187 } finally { 188 close(statement); 189 } 190 } catch (SQLException e) { 191 throw createException(e, uri.toString()); 192 } 193 } 194 195 public void removeLock(Connection connection, Uri uri, NodeLock lock) 196 throws ServiceAccessException, LockTokenNotFoundException { 197 PreparedStatement statement = null; 198 try { 199 try { 201 statement = 202 connection.prepareStatement( 203 "delete from LOCKS where LOCKS.LOCK_ID in (select u.URI_ID from URI u where u.URI_STRING=?)"); 204 statement.setString(1, lock.getLockId()); 205 statement.executeUpdate(); 206 } finally { 207 close(statement); 208 } 209 try { 210 statement = 211 connection.prepareStatement( 212 "delete from URI where URI_STRING=?"); 213 statement.setString(1, lock.getLockId()); 214 statement.executeUpdate(); 215 } finally { 216 close(statement); 217 } 218 } catch (SQLException e) { 219 throw createException(e, uri.toString()); 220 } 221 } 222 223 public void revokePermission(Connection connection, Uri uri, NodePermission permission) 224 throws ServiceAccessException { 225 if (permission == null) return; 226 PreparedStatement statement = null; 227 try { 228 NodeRevisionNumber revisionNumber = permission.getRevisionNumber(); 229 statement = 230 connection.prepareStatement( 231 "delete from PERMISSIONS where PERMISSIONS.OBJECT_ID in (select ou.URI_ID from URI ou, URI su, URI au where ou.URI_STRING = ? and SUBJECT_ID = su.URI_ID and su.URI_STRING = ? and ACTION_ID = au.URI_ID and au.URI_STRING = ? and VERSION_NO" + ((revisionNumber == null) ? " IS NULL " : " = '" + revisionNumber.toString() + "'")); 232 statement.setString(1, permission.getObjectUri()); 233 statement.setString(2, permission.getSubjectUri()); 234 statement.setString(3, permission.getActionUri()); 235 statement.executeUpdate(); 236 } catch (SQLException e) { 237 throw createException(e, uri.toString()); 238 } finally { 239 close(statement); 240 } 241 } 242 243 public void revokePermissions(Connection connection, Uri uri) throws ServiceAccessException { 244 PreparedStatement statement = null; 245 try { 246 statement = 247 connection.prepareStatement( 248 "delete from PERMISSIONS where PERMISSIONS.OBJECT_ID in (select u.URI_ID from URI u where u.URI_STRING = ?)"); 249 statement.setString(1, uri.toString()); 250 statement.executeUpdate(); 251 } catch (SQLException e) { 252 throw createException(e, uri.toString()); 253 } finally { 254 close(statement); 255 } 256 } 257 258 protected void clearBinding(Connection connection, Uri uri) 259 throws ServiceAccessException, ObjectNotFoundException, SQLException 260 { 261 PreparedStatement statement = null; 262 263 try { 264 statement = 265 connection.prepareStatement( 266 "delete from BINDING where BINDING.URI_ID in (select URI_ID from URI where URI.URI_STRING = ?)"); 267 statement.setString(1, uri.toString()); 268 statement.executeUpdate(); 269 } finally { 270 close(statement); 271 } 272 273 try { 274 statement = 275 connection.prepareStatement( 276 "delete from PARENT_BINDING where PARENT_BINDING.URI_ID in (select URI_ID from URI where URI.URI_STRING = ?)"); 277 statement.setString(1, uri.toString()); 278 statement.executeUpdate(); 279 } finally { 280 close(statement); 281 } 282 } 283 284 protected void storeContent( 285 Connection connection, Uri uri, 286 NodeRevisionDescriptor revisionDescriptor, 287 NodeRevisionContent revisionContent) throws IOException, SQLException 288 { 289 assureVersionInfo(connection, uri, revisionDescriptor); 290 291 InputStream is = revisionContent.streamContent(); 292 if (is != null) { 293 File tempFile = null; 294 295 if (bcompress) { 296 getLogger().log("Compressing the data", LOG_CHANNEL, Logger.DEBUG); 297 StoreContentZip ziputil = new StoreContentZip(); 298 ziputil.Zip(is); 299 is = ziputil.getInputStream(); 300 301 if (revisionDescriptor.getContentLength() == -1) { 303 revisionDescriptor.setContentLength(ziputil.getInitialContentLength()); 304 } 305 } else { 306 if (revisionDescriptor.getContentLength() == -1) { 308 try { 309 tempFile = File.createTempFile("content", null); 310 FileOutputStream fos = new FileOutputStream(tempFile); 311 try { 312 byte buffer[] = new byte[2048]; 313 do { 314 int nChar = is.read(buffer); 315 if (nChar == -1) { 316 break; 317 } 318 fos.write(buffer, 0, nChar); 319 } while (true); 320 } finally { 321 fos.close(); 322 } 323 is.close(); 324 is = new FileInputStream(tempFile); 325 326 revisionDescriptor.setContentLength(tempFile.length()); 327 328 } catch (IOException ex) { 329 getLogger().log(ex.toString() + " during the calculation of the content length.", 330 LOG_CHANNEL, Logger.ERROR); 331 throw ex; 332 } 333 } 334 } 335 336 PreparedStatement statement = null; 337 try { 338 long versionID = getVersionID(connection, uri.toString(), revisionDescriptor); 339 statement = connection.prepareStatement( 340 "insert into VERSION_CONTENT (VERSION_ID, CONTENT) values (?,?)"); 341 statement.setLong(1, versionID); 342 statement.setBinaryStream(2, is, (int) revisionDescriptor.getContentLength()); 343 statement.executeUpdate(); 344 if (tempFile != null) { 345 is.close(); 346 is = null; 347 tempFile.delete(); 348 } 349 } finally { 350 try { 351 close(statement); 352 } finally { 353 if (is != null) { 354 try { 358 is.close(); 359 } catch (IOException ioe) { 360 logger.log("Could not close stream", ioe, LOG_CHANNEL, Logger.DEBUG); 361 } 362 } 363 } 364 } 365 } 366 } 367 368 protected long getVersionID( 369 Connection connection, 370 String uriString, 371 NodeRevisionDescriptor revisionDescriptor) throws SQLException 372 { 373 PreparedStatement statement = null; 374 ResultSet rs = null; 375 long versionID = 0l; 376 try { 377 statement = connection.prepareStatement( 378 "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.URI_ID = u.URI_ID and u.URI_STRING = ? and vh.REVISION_NO = ?"); 379 statement.setString(1,uriString); 380 statement.setString(2,revisionDescriptor.getRevisionNumber().toString()); 381 rs = statement.executeQuery(); 382 if (rs.next()) { 383 versionID = rs.getLong(1); 384 } 385 } finally { 386 close(statement,rs); 387 } 388 389 return versionID; 390 } 391 392 protected String convertRevisionNumberToComparable(String revisioNumber) { 393 return "to_number(substr("+revisioNumber+",1,instr("+revisioNumber+",'.')-1)), to_number(substr("+revisioNumber+",instr("+revisioNumber+",'.')+1))"; 394 } 395 396 } 397 398 399 | Popular Tags |