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.update6to7;
029
030import org.opencms.setup.CmsSetupDb;
031import org.opencms.setup.db.A_CmsUpdateDBPart;
032
033import java.io.IOException;
034import java.sql.SQLException;
035import java.util.HashMap;
036import java.util.Map;
037
038/**
039 * This class upgrades the database tables containing new OU columns.<p>
040 *
041 * These tables are
042 * cms_groups
043 * cms_history_principals
044 * cms_history_projects
045 * cms_projects
046 * cms_users
047 *
048 * @since 7.0.0
049 */
050public class CmsUpdateDBUpdateOU extends A_CmsUpdateDBPart {
051
052    /** Constant for the GROUP_OU column.<p> */
053    protected static final String GROUP_OU_COLUMN = "GROUP_OU";
054
055    /** Constant for the PROJECT_OU column.<p> */
056    protected static final String PROJECT_OU_COLUMN = "PROJECT_OU";
057
058    /** Constant for the query that adds the ous to the table.<p> */
059    protected static final String QUERY_ADD_OUS_TO_TABLE = "Q_ADD_OUS_TO_TABLE";
060
061    /** Constant for the alteration of the table.<p> */
062    protected static final String QUERY_KEY_ALTER_TABLE = "Q_ALTER_TABLE_ADD_OU_COLUMN";
063
064    /** Constant for the replacement in the SQL query for the columnname.<p> */
065    protected static final String REPLACEMENT_COLUMNNAME = "${columnname}";
066
067    /** Constant for the replacement in the SQL query for the tablename.<p> */
068    protected static final String REPLACEMENT_TABLENAME = "${tablename}";
069
070    /** Constant for the CMS_BACKUP_PROJECTS table.<p> */
071    protected static final String TABLE_BACKUP_PROJECTS = "CMS_BACKUP_PROJECTS";
072
073    /** Constant for the CMS_GROUPS table.<p> */
074    protected static final String TABLE_CMS_GROUPS = "CMS_GROUPS";
075
076    /** Constant for the CMS_USERS table.<p> */
077    protected static final String TABLE_CMS_USERS = "CMS_USERS";
078
079    /** Constant for the CMS_PROJECTS table.<p> */
080    protected static final String TABLE_PROJECTS = "CMS_PROJECTS";
081
082    /** Constant for the USER_OU column.<p> */
083    protected static final String USER_OU_COLUMN = "USER_OU";
084
085    /** Constant for the SQL query properties.<p> */
086    private static final String QUERY_PROPERTY_FILE = "cms_ou_query.properties";
087
088    /**
089     * Constructor.<p>
090     *
091     * @throws IOException if the sql queries properties file could not be read
092     */
093    public CmsUpdateDBUpdateOU()
094    throws IOException {
095
096        super();
097        loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE);
098    }
099
100    /**
101     * Checks if the column USER_OU is found in the resultset.<p>
102     *
103     * @param dbCon the db connection interface
104     * @param table the table to check
105     * @param ouColumn the type of OU to find (e.g. USER_OU or GROUP_OU)
106     *
107     * @return true if the column is in the result set, false if not
108     */
109    protected boolean findOUColumn(CmsSetupDb dbCon, String table, String ouColumn) {
110
111        System.out.println(new Exception().getStackTrace()[0].toString());
112        return dbCon.hasTableOrColumn(table, ouColumn);
113    }
114
115    /**
116     * @see org.opencms.setup.db.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb)
117     */
118    @Override
119    protected void internalExecute(CmsSetupDb dbCon) {
120
121        System.out.println(new Exception().getStackTrace()[0].toString());
122
123        updateOUs(dbCon, TABLE_CMS_USERS, USER_OU_COLUMN);
124        updateOUs(dbCon, TABLE_CMS_GROUPS, GROUP_OU_COLUMN);
125        updateOUs(dbCon, TABLE_PROJECTS, PROJECT_OU_COLUMN);
126        updateOUs(dbCon, TABLE_BACKUP_PROJECTS, PROJECT_OU_COLUMN);
127    }
128
129    /**
130     * Updates the database tables with the new OUs if necessary for the given table.<p>
131     *
132     * @param dbCon the db connection interface
133     * @param table the table to update
134     * @param ouColumn the column to insert
135     *
136     * @return true if everything worked fine, false if not
137     */
138    protected int updateOUs(CmsSetupDb dbCon, String table, String ouColumn) {
139
140        System.out.println(new Exception().getStackTrace()[0].toString());
141        int result = 1;
142        try {
143
144            if (!findOUColumn(dbCon, table, ouColumn)) {
145                // Alter the table and add the OUs
146                Map<String, String> replacements = new HashMap<String, String>();
147                replacements.put(REPLACEMENT_TABLENAME, table);
148                replacements.put(REPLACEMENT_COLUMNNAME, ouColumn);
149                String alterQuery = readQuery(QUERY_KEY_ALTER_TABLE);
150
151                // Update the database and alter the table to add the OUs
152                dbCon.updateSqlStatement(alterQuery, replacements, null);
153
154                // Insert the value '/' into the OUs
155                String insertQuery = readQuery(QUERY_ADD_OUS_TO_TABLE);
156                dbCon.updateSqlStatement(insertQuery, replacements, null);
157                result = 0;
158            } else {
159                System.out.println("column " + ouColumn + " in table " + table + " already exists");
160            }
161            // Nothing needs to be done
162            result = 0;
163        } catch (SQLException e) {
164            e.printStackTrace();
165            result = 1;
166        }
167
168        return result;
169    }
170
171}