1 31 32 package org.opencms.db.oracle; 33 34 import org.opencms.db.CmsDbContext; 35 import org.opencms.db.CmsDbEntryNotFoundException; 36 import org.opencms.db.CmsDbException; 37 import org.opencms.db.CmsDbIoException; 38 import org.opencms.db.CmsDbSqlException; 39 import org.opencms.db.CmsDbUtil; 40 import org.opencms.db.generic.CmsSqlManager; 41 import org.opencms.db.generic.Messages; 42 import org.opencms.file.CmsBackupProject; 43 import org.opencms.file.CmsBackupResource; 44 import org.opencms.file.CmsDataAccessException; 45 import org.opencms.file.CmsFile; 46 import org.opencms.file.CmsProperty; 47 import org.opencms.file.CmsResource; 48 import org.opencms.util.CmsUUID; 49 50 import java.io.IOException ; 51 import java.io.OutputStream ; 52 import java.sql.Connection ; 53 import java.sql.PreparedStatement ; 54 import java.sql.ResultSet ; 55 import java.sql.SQLException ; 56 import java.util.ArrayList ; 57 import java.util.List ; 58 59 import org.apache.commons.dbcp.DelegatingResultSet; 60 61 72 public class CmsBackupDriver extends org.opencms.db.generic.CmsBackupDriver { 73 74 77 public void deleteBackups(CmsDbContext dbc, List existingBackups, int maxVersions) throws CmsDataAccessException { 78 79 PreparedStatement stmt1 = null; 80 PreparedStatement stmt2 = null; 81 PreparedStatement stmt3 = null; 82 PreparedStatement stmt4 = null; 83 84 Connection conn = null; 85 CmsBackupResource currentResource = null; 86 int count = existingBackups.size() - maxVersions; 87 88 try { 89 conn = m_sqlManager.getConnection(dbc); 90 stmt1 = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_BACKUP_DELETE_CONTENT"); 91 stmt2 = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_BACKUP_DELETE_RESOURCES"); 92 stmt3 = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_BACKUP_DELETE_STRUCTURE"); 93 stmt4 = m_sqlManager.getPreparedStatement(conn, "C_PROPERTIES_DELETEALL_BACKUP"); 94 95 for (int i = 0; i < count; i++) { 96 currentResource = (CmsBackupResource)existingBackups.get(i); 97 stmt1.setString(1, currentResource.getBackupId().toString()); 99 stmt1.addBatch(); 100 stmt2.setString(1, currentResource.getBackupId().toString()); 102 stmt2.addBatch(); 103 stmt3.setString(1, currentResource.getBackupId().toString()); 105 stmt3.addBatch(); 106 stmt4.setString(1, currentResource.getBackupId().toString()); 108 stmt4.setInt(2, currentResource.getTagId()); 109 stmt4.setString(3, currentResource.getStructureId().toString()); 110 stmt4.setInt(4, CmsProperty.STRUCTURE_RECORD_MAPPING); 111 stmt4.setString(5, currentResource.getResourceId().toString()); 112 stmt4.setInt(6, CmsProperty.RESOURCE_RECORD_MAPPING); 113 stmt4.addBatch(); 114 } 115 116 if (count > 0) { 117 stmt1.executeBatch(); 118 stmt2.executeBatch(); 119 stmt3.executeBatch(); 120 stmt4.executeBatch(); 121 } 122 123 } catch (Exception e) { 124 throw new CmsDbException(Messages.get().container( 125 Messages.ERR_DELETE_BACKUP_VERSIONS_1, 126 currentResource.getRootPath()), e); 127 } finally { 128 m_sqlManager.closeAll(dbc, conn, stmt1, null); 129 m_sqlManager.closeAll(dbc, conn, stmt2, null); 130 m_sqlManager.closeAll(dbc, conn, stmt3, null); 131 m_sqlManager.closeAll(dbc, conn, stmt4, null); 132 } 133 } 134 135 138 public org.opencms.db.generic.CmsSqlManager initSqlManager(String classname) { 139 140 return CmsSqlManager.getInstance(classname); 141 } 142 143 146 public List readBackupProjects(CmsDbContext dbc) throws CmsDataAccessException { 147 148 List projects = new ArrayList (); 149 ResultSet res = null; 150 PreparedStatement stmt = null; 151 Connection conn = null; 152 153 try { 154 conn = m_sqlManager.getConnection(dbc); 156 stmt = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_PROJECTS_READLAST_BACKUP"); 157 stmt.setInt(1, 300); 158 res = stmt.executeQuery(); 159 while (res.next()) { 160 List resources = m_driverManager.getBackupDriver().readBackupProjectResources( 161 dbc, 162 res.getInt("PUBLISH_TAG")); 163 projects.add(new CmsBackupProject( 164 res.getInt("PUBLISH_TAG"), 165 res.getInt("PROJECT_ID"), 166 res.getString("PROJECT_NAME"), 167 res.getString("PROJECT_DESCRIPTION"), 168 res.getInt("TASK_ID"), 169 new CmsUUID(res.getString("USER_ID")), 170 new CmsUUID(res.getString("GROUP_ID")), 171 new CmsUUID(res.getString("MANAGERGROUP_ID")), 172 res.getLong("DATE_CREATED"), 173 res.getInt("PROJECT_TYPE"), 174 CmsDbUtil.getTimestamp(res, "PROJECT_PUBLISHDATE"), 175 new CmsUUID(res.getString("PROJECT_PUBLISHED_BY")), 176 res.getString("PROJECT_PUBLISHED_BY_NAME"), 177 res.getString("USER_NAME"), 178 res.getString("GROUP_NAME"), 179 res.getString("MANAGERGROUP_NAME"), 180 resources)); 181 } 182 } catch (SQLException e) { 183 throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container( 184 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1, 185 CmsDbSqlException.getErrorQuery(stmt)), e); 186 } finally { 187 m_sqlManager.closeAll(dbc, conn, stmt, res); 188 } 189 190 return (projects); 191 } 192 193 196 protected void internalWriteBackupFileContent( 197 CmsDbContext dbc, 198 CmsUUID backupId, 199 CmsResource resource, 200 int tagId, 201 int versionId) throws CmsDataAccessException { 202 203 PreparedStatement stmt = null, stmt2 = null; 204 PreparedStatement commit = null; 205 PreparedStatement rollback = null; 206 Connection conn = null; 207 ResultSet res = null; 208 209 CmsUUID contentId; 210 byte[] fileContent; 211 if (resource instanceof CmsFile) { 212 contentId = ((CmsFile)resource).getContentId(); 213 fileContent = ((CmsFile)resource).getContents(); 214 } else { 215 contentId = CmsUUID.getNullUUID(); 216 fileContent = new byte[0]; 217 } 218 219 try { 220 conn = m_sqlManager.getConnection(dbc); 221 stmt = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_CONTENTS_ADDBACKUP"); 222 223 stmt.setString(1, contentId.toString()); 226 stmt.setString(2, resource.getResourceId().toString()); 227 stmt.setInt(3, tagId); 228 stmt.setInt(4, versionId); 229 stmt.setString(5, backupId.toString()); 230 stmt.executeUpdate(); 231 232 } catch (SQLException e) { 233 throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container( 234 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1, 235 CmsDbSqlException.getErrorQuery(stmt)), e); 236 } finally { 237 m_sqlManager.closeAll(dbc, conn, stmt, res); 238 } 239 240 boolean wasInTransaction = false; 241 try { 242 conn = m_sqlManager.getConnection(dbc); 243 244 wasInTransaction = !conn.getAutoCommit(); 245 if (!wasInTransaction) { 246 conn.setAutoCommit(false); 247 } 248 249 stmt = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_CONTENTS_UPDATEBACKUP"); 251 stmt.setString(1, contentId.toString()); 252 stmt.setString(2, backupId.toString()); 253 254 res = ((DelegatingResultSet)stmt.executeQuery()).getInnermostDelegate(); 255 if (!res.next()) { 256 throw new CmsDbEntryNotFoundException(Messages.get().container( 257 Messages.ERR_NO_BACKUP_CONTENT_ID_2, 258 contentId, 259 backupId)); 260 } 261 262 OutputStream output = CmsUserDriver.getOutputStreamFromBlob(res, "FILE_CONTENT"); 264 output.write(fileContent); 265 output.close(); 266 res.close(); 267 res = null; 268 fileContent = null; 269 270 if (!wasInTransaction) { 271 commit = m_sqlManager.getPreparedStatement(conn, "C_COMMIT"); 272 commit.execute(); 273 commit.close(); 274 commit = null; 275 } 276 277 stmt.close(); 278 stmt = null; 279 280 if (!wasInTransaction) { 281 conn.setAutoCommit(true); 282 } 283 } catch (IOException e) { 284 throw new CmsDbIoException(Messages.get().container(Messages.ERR_WRITING_TO_OUTPUT_STREAM_1, resource), e); 285 } catch (SQLException e) { 286 throw new CmsDbSqlException(Messages.get().container(Messages.ERR_GENERIC_SQL_1, stmt), e); 287 } finally { 288 289 if (res != null) { 290 try { 291 res.close(); 292 } catch (SQLException exc) { 293 } 295 } 296 if (commit != null) { 297 try { 298 commit.close(); 299 } catch (SQLException exc) { 300 } 302 } 303 if (stmt2 != null) { 304 try { 305 stmt2.close(); 306 } catch (SQLException exc) { 307 } 309 } 310 311 if (!wasInTransaction) { 312 if (stmt != null) { 313 try { 314 rollback = m_sqlManager.getPreparedStatement(conn, "C_ROLLBACK"); 315 rollback.execute(); 316 rollback.close(); 317 } catch (SQLException se) { 318 } 320 try { 321 stmt.close(); 322 } catch (SQLException exc) { 323 } 325 } 326 if (conn != null) { 327 try { 328 conn.setAutoCommit(true); 329 conn.close(); 330 } catch (SQLException se) { 331 } 333 } 334 } 335 } 336 } 337 } 338 | Popular Tags |