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.setup.db;
029
030import org.opencms.setup.CmsSetupDb;
031
032import java.io.IOException;
033import java.sql.SQLException;
034import java.util.Collections;
035import java.util.Enumeration;
036import java.util.HashMap;
037import java.util.Map;
038import java.util.Properties;
039
040/**
041 * Represent a part of the database update process.<p>
042 *
043 * @since 6.9.2
044 */
045public abstract class A_CmsUpdateDBPart implements I_CmsUpdateDBPart {
046
047    /** The connection data to use. */
048    protected Map<String, String> m_poolData;
049
050    /** A map holding all SQL queries. */
051    protected Map<String, String> m_queries;
052
053    /**
054     * Default constructor.<p>
055     */
056    public A_CmsUpdateDBPart() {
057
058        m_queries = new HashMap<String, String>();
059    }
060
061    /**
062     * @see org.opencms.setup.db.I_CmsUpdateDBPart#execute(Map)
063     */
064    public void execute(Map<String, String> dbPoolData) {
065
066        m_poolData = new HashMap<String, String>(dbPoolData);
067
068        CmsSetupDb setupDb = new CmsSetupDb(null);
069        try {
070            setupDb.setConnection(
071                m_poolData.get("driver"),
072                m_poolData.get("url"),
073                m_poolData.get("params"),
074                m_poolData.get("user"),
075                m_poolData.get("pwd"));
076
077            internalExecute(setupDb);
078        } catch (SQLException e) {
079            e.printStackTrace();
080        } finally {
081            setupDb.closeConnection();
082        }
083    }
084
085    /**
086     * Returns the database pool Data.<p>
087     *
088     * @return the database pool Data
089     */
090    public Map<String, String> getPoolData() {
091
092        return Collections.unmodifiableMap(m_poolData);
093    }
094
095    /**
096     * Searches for the SQL query with the specified key.<p>
097     *
098     * @param queryKey the SQL query key
099     * @return the the SQL query in this property list with the specified key
100     */
101    public String readQuery(String queryKey) {
102
103        String result = m_queries.get(queryKey);
104        if (result != null) {
105            result = result.replace('\t', ' ');
106            result = result.replace('\n', ' ');
107            result = result.replace('\r', ' ');
108        }
109        return result;
110    }
111
112    /**
113     * Returns the default property file location.<p>
114     *
115     * @return the default property file location
116     */
117    protected String getPropertyFileLocation() {
118
119        return getClass().getPackage().getName().replace('.', '/') + '/';
120    }
121
122    /**
123     * Does the hard work.<p>
124     *
125     * @param setupDb the db connection interface
126     *
127     * @throws SQLException if something goes wrong
128     */
129    protected abstract void internalExecute(CmsSetupDb setupDb) throws SQLException;
130
131    /**
132     * Returns the keep History parameter value.<p>
133     *
134     * @return the keep History parameter value
135     */
136    protected boolean isKeepHistory() {
137
138        return Boolean.parseBoolean(m_poolData.get("keepHistory"));
139    }
140
141    /**
142     * Loads a Java properties hash containing SQL queries.<p>
143     *
144     * @param propertyFilename the package/filename of the properties hash
145     *
146     * @throws IOException if the sql queries property file could not be read
147     */
148    protected void loadQueryProperties(String propertyFilename) throws IOException {
149
150        Properties properties = new Properties();
151        properties.load(getClass().getClassLoader().getResourceAsStream(propertyFilename));
152        @SuppressWarnings("unchecked")
153        Enumeration<String> propertyNames = (Enumeration<String>)properties.propertyNames();
154        while (propertyNames.hasMoreElements()) {
155            String propertyName = propertyNames.nextElement();
156            m_queries.put(propertyName, properties.getProperty(propertyName));
157        }
158    }
159}