001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.db.oracle;
029
030import org.opencms.db.CmsDbContext;
031import org.opencms.db.CmsDbEntryNotFoundException;
032import org.opencms.db.CmsDbIoException;
033import org.opencms.db.CmsDbSqlException;
034import org.opencms.db.generic.CmsSqlManager;
035import org.opencms.db.generic.Messages;
036import org.opencms.file.CmsDataAccessException;
037import org.opencms.publish.CmsPublishJobInfoBean;
038import org.opencms.util.CmsUUID;
039
040import java.io.IOException;
041import java.io.OutputStream;
042import java.sql.Connection;
043import java.sql.PreparedStatement;
044import java.sql.ResultSet;
045import java.sql.SQLException;
046
047/**
048 * Oracle/OCI implementation of the project driver methods.<p>
049 *
050 * @since 6.0.0
051 */
052public class CmsProjectDriver extends org.opencms.db.generic.CmsProjectDriver {
053
054    /**
055     * @see org.opencms.db.I_CmsProjectDriver#createPublishJob(org.opencms.db.CmsDbContext, org.opencms.publish.CmsPublishJobInfoBean)
056     */
057    @Override
058    public void createPublishJob(CmsDbContext dbc, CmsPublishJobInfoBean publishJob) throws CmsDataAccessException {
059
060        Connection conn = null;
061        PreparedStatement stmt = null;
062
063        try {
064            conn = m_sqlManager.getConnection(dbc);
065            stmt = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_PUBLISHJOB_CREATE");
066            stmt.setString(1, publishJob.getPublishHistoryId().toString());
067            stmt.setString(2, publishJob.getProjectId().toString());
068            stmt.setString(3, publishJob.getProjectName());
069            stmt.setString(4, publishJob.getUserId().toString());
070            stmt.setString(5, publishJob.getLocale().toString());
071            stmt.setInt(6, publishJob.getFlags());
072            stmt.setInt(7, publishJob.getSize());
073            stmt.setLong(8, publishJob.getEnqueueTime());
074            stmt.setLong(9, publishJob.getStartTime());
075            stmt.setLong(10, publishJob.getFinishTime());
076
077            stmt.executeUpdate();
078        } catch (SQLException e) {
079            throw new CmsDbSqlException(
080                Messages.get().container(Messages.ERR_GENERIC_SQL_1, CmsDbSqlException.getErrorQuery(stmt)),
081                e);
082        } finally {
083            m_sqlManager.closeAll(dbc, conn, stmt, null);
084        }
085
086        try {
087            // now write the publish list
088            internalWritePublishJobData(
089                dbc,
090                publishJob.getPublishHistoryId(),
091                "C_ORACLE_PUBLISHJOB_UPDATE_PUBLISHLIST",
092                "PUBLISH_LIST",
093                internalSerializePublishList(publishJob.getPublishList()));
094        } catch (IOException e) {
095            throw new CmsDbIoException(
096                Messages.get().container(
097                    Messages.ERR_SERIALIZING_PUBLISHLIST_1,
098                    publishJob.getPublishHistoryId().toString()),
099                e);
100        }
101    }
102
103    /**
104     * @see org.opencms.db.I_CmsProjectDriver#initSqlManager(String)
105     */
106    @Override
107    public org.opencms.db.generic.CmsSqlManager initSqlManager(String classname) {
108
109        return CmsSqlManager.getInstance(classname);
110    }
111
112    /**
113     * @see org.opencms.db.I_CmsProjectDriver#writePublishReport(org.opencms.db.CmsDbContext, org.opencms.util.CmsUUID, byte[])
114     */
115    @Override
116    public void writePublishReport(CmsDbContext dbc, CmsUUID publishId, byte[] content) throws CmsDataAccessException {
117
118        internalWritePublishJobData(
119            dbc,
120            publishId,
121            "C_ORACLE_PUBLISHJOB_UPDATE_PUBLISHREPORT",
122            "PUBLISH_REPORT",
123            content);
124    }
125
126    /**
127     * Writes data for a publish job.<p>
128     *
129     * @param dbc the database context
130     * @param publishJobHistoryId the publish job id
131     * @param queryKey the query to use
132     * @param fieldName the fiueld to use
133     * @param data the data to write
134     * @throws CmsDataAccessException if something goes wrong
135     */
136    private void internalWritePublishJobData(
137        CmsDbContext dbc,
138        CmsUUID publishJobHistoryId,
139        String queryKey,
140        String fieldName,
141        byte[] data)
142    throws CmsDataAccessException {
143
144        Connection conn = null;
145        PreparedStatement stmt = null;
146        PreparedStatement commit = null;
147        ResultSet res = null;
148        boolean wasInTransaction = false;
149
150        try {
151            conn = m_sqlManager.getConnection(dbc);
152            stmt = m_sqlManager.getPreparedStatement(conn, queryKey);
153
154            wasInTransaction = !conn.getAutoCommit();
155            if (!wasInTransaction) {
156                conn.setAutoCommit(false);
157            }
158
159            // update the file content in the contents table
160            stmt.setString(1, publishJobHistoryId.toString());
161            res = stmt.executeQuery();
162            if (!res.next()) {
163                throw new CmsDbEntryNotFoundException(
164                    Messages.get().container(Messages.ERR_READ_PUBLISH_JOB_1, publishJobHistoryId));
165            }
166
167            // write file content
168            OutputStream output = CmsUserDriver.getOutputStreamFromBlob(res, fieldName);
169            output.write(data);
170            output.close();
171
172            if (!wasInTransaction) {
173                commit = m_sqlManager.getPreparedStatement(conn, "C_COMMIT");
174                commit.execute();
175                m_sqlManager.closeAll(dbc, null, commit, null);
176            }
177
178            m_sqlManager.closeAll(dbc, null, stmt, res);
179
180            // this is needed so the finally block works correctly
181            commit = null;
182            stmt = null;
183            res = null;
184
185            if (!wasInTransaction) {
186                conn.setAutoCommit(true);
187            }
188        } catch (IOException e) {
189            throw new CmsDbIoException(
190                Messages.get().container(Messages.ERR_WRITING_TO_OUTPUT_STREAM_1, publishJobHistoryId),
191                e);
192        } catch (SQLException e) {
193            throw new CmsDbSqlException(
194                org.opencms.db.generic.Messages.get().container(
195                    org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
196                    CmsDbSqlException.getErrorQuery(stmt)),
197                e);
198        } finally {
199            org.opencms.db.oracle.CmsSqlManager.closeAllInTransaction(
200                m_sqlManager,
201                dbc,
202                conn,
203                stmt,
204                res,
205                commit,
206                wasInTransaction);
207        }
208    }
209}