KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/postgresql/CmsBackupDriver.java,v $
3  * Date : $Date: 2005/06/27 23:22:20 $
4  * Version: $Revision: 1.14 $
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.postgresql;
33
34 import org.opencms.db.CmsDbContext;
35 import org.opencms.db.CmsDbException;
36 import org.opencms.db.CmsDbSqlException;
37 import org.opencms.db.CmsDbUtil;
38 import org.opencms.db.generic.CmsSqlManager;
39 import org.opencms.db.generic.Messages;
40 import org.opencms.file.CmsBackupProject;
41 import org.opencms.file.CmsBackupResource;
42 import org.opencms.file.CmsDataAccessException;
43 import org.opencms.file.CmsProperty;
44 import org.opencms.util.CmsUUID;
45
46 import java.sql.Connection JavaDoc;
47 import java.sql.PreparedStatement JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.sql.SQLException JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.List JavaDoc;
52
53 /**
54  * PostgreSql implementation of the backup driver methods.<p>
55  *
56  * @author Antonio Core
57  *
58  * @version $Revision: 1.14 $
59  *
60  * @since 6.0.0
61  */

62 public class CmsBackupDriver extends org.opencms.db.generic.CmsBackupDriver {
63
64     /**
65      * @see org.opencms.db.I_CmsBackupDriver#deleteBackups(org.opencms.db.CmsDbContext, java.util.List, int)
66      */

67     public void deleteBackups(CmsDbContext dbc, List JavaDoc existingBackups, int maxVersions) throws CmsDataAccessException {
68
69         PreparedStatement JavaDoc stmt1 = null;
70         PreparedStatement JavaDoc stmt2 = null;
71         PreparedStatement JavaDoc stmt3 = null;
72         PreparedStatement JavaDoc stmt4 = null;
73
74         Connection JavaDoc conn = null;
75         CmsBackupResource currentResource = null;
76         int count = existingBackups.size() - maxVersions;
77
78         try {
79             conn = m_sqlManager.getConnection(dbc);
80             stmt1 = m_sqlManager.getPreparedStatement(conn, "C_POSTGRESQL_BACKUP_DELETE_CONTENT");
81             stmt2 = m_sqlManager.getPreparedStatement(conn, "C_POSTGRESQL_BACKUP_DELETE_RESOURCES");
82             stmt3 = m_sqlManager.getPreparedStatement(conn, "C_POSTGRESQL_BACKUP_DELETE_STRUCTURE");
83             stmt4 = m_sqlManager.getPreparedStatement(conn, "C_PROPERTIES_DELETEALL_BACKUP");
84
85             for (int i = 0; i < count; i++) {
86                 currentResource = (CmsBackupResource)existingBackups.get(i);
87                 // add the values to delete the file table
88
stmt1.setString(1, currentResource.getBackupId().toString());
89                 stmt1.addBatch();
90                 // add the values to delete the resource table
91
stmt2.setString(1, currentResource.getBackupId().toString());
92                 stmt2.addBatch();
93                 // add the values to delete the structure table
94
stmt3.setString(1, currentResource.getBackupId().toString());
95                 stmt3.addBatch();
96                 // delete the properties
97
stmt4.setString(1, currentResource.getBackupId().toString());
98                 stmt4.setInt(2, currentResource.getTagId());
99                 stmt4.setString(3, currentResource.getStructureId().toString());
100                 stmt4.setInt(4, CmsProperty.STRUCTURE_RECORD_MAPPING);
101                 stmt4.setString(5, currentResource.getResourceId().toString());
102                 stmt4.setInt(6, CmsProperty.RESOURCE_RECORD_MAPPING);
103                 stmt4.addBatch();
104             }
105
106             if (count > 0) {
107                 stmt1.executeBatch();
108                 stmt2.executeBatch();
109                 stmt3.executeBatch();
110                 stmt4.executeBatch();
111             }
112
113         } catch (Exception JavaDoc e) {
114             throw new CmsDbException(Messages.get().container(
115                 Messages.ERR_DELETE_BACKUP_VERSIONS_1,
116                 currentResource.getRootPath()), e);
117         } finally {
118             m_sqlManager.closeAll(dbc, conn, stmt1, null);
119             m_sqlManager.closeAll(dbc, conn, stmt2, null);
120             m_sqlManager.closeAll(dbc, conn, stmt3, null);
121             m_sqlManager.closeAll(dbc, conn, stmt4, null);
122         }
123     }
124
125     /**
126      * @see org.opencms.db.I_CmsBackupDriver#initSqlManager(String)
127      */

128     public org.opencms.db.generic.CmsSqlManager initSqlManager(String JavaDoc classname) {
129
130         return CmsSqlManager.getInstance(classname);
131     }
132
133     /**
134      * @see org.opencms.db.I_CmsBackupDriver#readBackupProjects(org.opencms.db.CmsDbContext)
135      */

136     public List JavaDoc readBackupProjects(CmsDbContext dbc) throws CmsDataAccessException {
137
138         List JavaDoc projects = new ArrayList JavaDoc();
139         ResultSet JavaDoc res = null;
140         PreparedStatement JavaDoc stmt = null;
141         Connection JavaDoc conn = null;
142
143         try {
144             // create the statement
145
conn = m_sqlManager.getConnection(dbc);
146             stmt = m_sqlManager.getPreparedStatement(conn, "C_PROJECTS_READLAST_BACKUP");
147             stmt.setInt(1, 300);
148             res = stmt.executeQuery();
149             while (res.next()) {
150                 List JavaDoc resources = m_driverManager.getBackupDriver().readBackupProjectResources(
151                     dbc,
152                     res.getInt("PUBLISH_TAG"));
153                 projects.add(new CmsBackupProject(
154                     res.getInt("PUBLISH_TAG"),
155                     res.getInt("PROJECT_ID"),
156                     res.getString("PROJECT_NAME"),
157                     res.getString("PROJECT_DESCRIPTION"),
158                     res.getInt("TASK_ID"),
159                     new CmsUUID(res.getString("USER_ID")),
160                     new CmsUUID(res.getString("GROUP_ID")),
161                     new CmsUUID(res.getString("MANAGERGROUP_ID")),
162                     res.getLong("DATE_CREATED"),
163                     res.getInt("PROJECT_TYPE"),
164                     CmsDbUtil.getTimestamp(res, "PROJECT_PUBLISHDATE"),
165                     new CmsUUID(res.getString("PROJECT_PUBLISHED_BY")),
166                     res.getString("PROJECT_PUBLISHED_BY_NAME"),
167                     res.getString("USER_NAME"),
168                     res.getString("GROUP_NAME"),
169                     res.getString("MANAGERGROUP_NAME"),
170                     resources));
171             }
172         } catch (SQLException JavaDoc e) {
173             throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
174                 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
175                 CmsDbSqlException.getErrorQuery(stmt)), e);
176         } finally {
177             m_sqlManager.closeAll(dbc, conn, stmt, res);
178         }
179         return (projects);
180     }
181
182 }
Popular Tags