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.mssql;
029
030import org.opencms.db.CmsDbContext;
031import org.opencms.db.CmsDbSqlException;
032import org.opencms.db.I_CmsHistoryDriver;
033import org.opencms.db.generic.CmsSqlManager;
034import org.opencms.file.CmsDataAccessException;
035import org.opencms.file.history.CmsHistoryProject;
036
037import java.sql.Connection;
038import java.sql.PreparedStatement;
039import java.sql.ResultSet;
040import java.sql.SQLException;
041import java.util.ArrayList;
042import java.util.HashMap;
043import java.util.List;
044import java.util.Map;
045
046/**
047 * MS SQL implementation of the history driver methods.<p>
048 *
049 * @since 6.9.1
050 */
051public class CmsHistoryDriver extends org.opencms.db.generic.CmsHistoryDriver {
052
053    /**
054     * @see org.opencms.db.I_CmsHistoryDriver#initSqlManager(String)
055     */
056    @Override
057    public org.opencms.db.generic.CmsSqlManager initSqlManager(String classname) {
058
059        return CmsSqlManager.getInstance(classname);
060    }
061
062    /**
063     * @see org.opencms.db.I_CmsHistoryDriver#readProjects(org.opencms.db.CmsDbContext)
064     */
065    @Override
066    public List<CmsHistoryProject> readProjects(CmsDbContext dbc) throws CmsDataAccessException {
067
068        List<CmsHistoryProject> projects = new ArrayList<CmsHistoryProject>();
069        ResultSet res = null;
070        PreparedStatement stmt = null;
071        Connection conn = null;
072
073        Map<Integer, CmsHistoryProject> tmpProjects = new HashMap<Integer, CmsHistoryProject>();
074
075        try {
076            // create the statement
077            conn = m_sqlManager.getConnection(dbc);
078            stmt = m_sqlManager.getPreparedStatement(conn, "C_MSSQL_PROJECTS_READLAST_HISTORY");
079
080            res = stmt.executeQuery();
081            while (res.next()) {
082                tmpProjects.put(Integer.valueOf(res.getInt("PUBLISH_TAG")), internalCreateProject(res, null));
083            }
084        } catch (SQLException e) {
085            throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
086                org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
087                CmsDbSqlException.getErrorQuery(stmt)), e);
088        } finally {
089            m_sqlManager.closeAll(dbc, conn, stmt, res);
090        }
091        I_CmsHistoryDriver historyDriver = m_driverManager.getHistoryDriver(dbc);
092        for (Map.Entry<Integer, CmsHistoryProject> entry : tmpProjects.entrySet()) {
093            List<String> resources = historyDriver.readProjectResources(dbc, entry.getKey().intValue());
094            entry.getValue().setProjectResources(resources);
095            projects.add(entry.getValue());
096        }
097        return projects;
098    }
099}