KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > oracle > CmsBackupDriver


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/oracle/CmsBackupDriver.java,v $
3  * Date : $Date: 2005/06/27 23:22:25 $
4  * Version: $Revision: 1.56 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

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 JavaDoc;
51 import java.io.OutputStream JavaDoc;
52 import java.sql.Connection JavaDoc;
53 import java.sql.PreparedStatement JavaDoc;
54 import java.sql.ResultSet JavaDoc;
55 import java.sql.SQLException JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.List JavaDoc;
58
59 import org.apache.commons.dbcp.DelegatingResultSet;
60
61 /**
62  * Oracle implementation of the backup driver methods.<p>
63  *
64  * @author Thomas Weckert
65  * @author Michael Emmerich
66  * @author Carsten Weinholz
67  *
68  * @version $Revision: 1.56 $
69  *
70  * @since 6.0.0
71  */

72 public class CmsBackupDriver extends org.opencms.db.generic.CmsBackupDriver {
73
74     /**
75      * @see org.opencms.db.I_CmsBackupDriver#deleteBackups(org.opencms.db.CmsDbContext, java.util.List, int)
76      */

77     public void deleteBackups(CmsDbContext dbc, List JavaDoc existingBackups, int maxVersions) throws CmsDataAccessException {
78
79         PreparedStatement JavaDoc stmt1 = null;
80         PreparedStatement JavaDoc stmt2 = null;
81         PreparedStatement JavaDoc stmt3 = null;
82         PreparedStatement JavaDoc stmt4 = null;
83
84         Connection JavaDoc 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                 // add the values to delete the file table
98
stmt1.setString(1, currentResource.getBackupId().toString());
99                 stmt1.addBatch();
100                 // add the values to delete the resource table
101
stmt2.setString(1, currentResource.getBackupId().toString());
102                 stmt2.addBatch();
103                 // add the values to delete the structure table
104
stmt3.setString(1, currentResource.getBackupId().toString());
105                 stmt3.addBatch();
106                 // delete the properties
107
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 JavaDoc 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     /**
136      * @see org.opencms.db.I_CmsBackupDriver#initSqlManager(String)
137      */

138     public org.opencms.db.generic.CmsSqlManager initSqlManager(String JavaDoc classname) {
139
140         return CmsSqlManager.getInstance(classname);
141     }
142
143     /**
144      * @see org.opencms.db.I_CmsBackupDriver#readBackupProjects(org.opencms.db.CmsDbContext)
145      */

146     public List JavaDoc readBackupProjects(CmsDbContext dbc) throws CmsDataAccessException {
147
148         List JavaDoc projects = new ArrayList JavaDoc();
149         ResultSet JavaDoc res = null;
150         PreparedStatement JavaDoc stmt = null;
151         Connection JavaDoc conn = null;
152
153         try {
154             // create the statement
155
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 JavaDoc 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 JavaDoc 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     /**
194      * @see org.opencms.db.generic.CmsBackupDriver#internalWriteBackupFileContent(org.opencms.db.CmsDbContext, org.opencms.util.CmsUUID, org.opencms.file.CmsResource, int, int)
195      */

196     protected void internalWriteBackupFileContent(
197         CmsDbContext dbc,
198         CmsUUID backupId,
199         CmsResource resource,
200         int tagId,
201         int versionId) throws CmsDataAccessException {
202
203         PreparedStatement JavaDoc stmt = null, stmt2 = null;
204         PreparedStatement JavaDoc commit = null;
205         PreparedStatement JavaDoc rollback = null;
206         Connection JavaDoc conn = null;
207         ResultSet JavaDoc 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             // first insert new file without file_content, then update the file_content
224
// these two steps are necessary because of using BLOBs in the Oracle DB
225
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 JavaDoc 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             // select the backup record for update
250
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             // write file content
263
OutputStream JavaDoc 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 JavaDoc e) {
284             throw new CmsDbIoException(Messages.get().container(Messages.ERR_WRITING_TO_OUTPUT_STREAM_1, resource), e);
285         } catch (SQLException JavaDoc 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 JavaDoc exc) {
293                     // ignore
294
}
295             }
296             if (commit != null) {
297                 try {
298                     commit.close();
299                 } catch (SQLException JavaDoc exc) {
300                     // ignore
301
}
302             }
303             if (stmt2 != null) {
304                 try {
305                     stmt2.close();
306                 } catch (SQLException JavaDoc exc) {
307                     // ignore
308
}
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 JavaDoc se) {
318                         // ignore
319
}
320                     try {
321                         stmt.close();
322                     } catch (SQLException JavaDoc exc) {
323                         // ignore
324
}
325                 }
326                 if (conn != null) {
327                     try {
328                         conn.setAutoCommit(true);
329                         conn.close();
330                     } catch (SQLException JavaDoc se) {
331                         // ignore
332
}
333                 }
334             }
335         }
336     }
337 }
338
Popular Tags