001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-setup/org/opencms/setup/update6to7/generic/CmsUpdateDBUpdateOU.java,v $
003: * Date : $Date: 2008-02-27 12:05:35 $
004: * Version: $Revision: 1.2 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.setup.update6to7.generic;
033:
034: import org.opencms.setup.CmsSetupDb;
035: import org.opencms.setup.update6to7.A_CmsUpdateDBPart;
036:
037: import java.io.IOException;
038: import java.sql.SQLException;
039: import java.util.HashMap;
040: import java.util.Map;
041:
042: /**
043: * This class upgrades the database tables containing new OU columns.<p>
044: *
045: * These tables are
046: * cms_groups
047: * cms_history_principals
048: * cms_history_projects
049: * cms_projects
050: * cms_users
051: *
052: * @author Roland Metzler
053: *
054: * @version $Revision: 1.2 $
055: *
056: * @since 7.0.0
057: */
058: public class CmsUpdateDBUpdateOU extends A_CmsUpdateDBPart {
059:
060: /** Constant for the GROUP_OU column.<p> */
061: protected static final String GROUP_OU_COLUMN = "GROUP_OU";
062:
063: /** Constant for the PROJECT_OU column.<p> */
064: protected static final String PROJECT_OU_COLUMN = "PROJECT_OU";
065:
066: /** Constant for the query that adds the ous to the table.<p> */
067: protected static final String QUERY_ADD_OUS_TO_TABLE = "Q_ADD_OUS_TO_TABLE";
068:
069: /** Constant for the alteration of the table.<p> */
070: protected static final String QUERY_KEY_ALTER_TABLE = "Q_ALTER_TABLE_ADD_OU_COLUMN";
071:
072: /** Constant for the replacement in the SQL query for the columnname.<p> */
073: protected static final String REPLACEMENT_COLUMNNAME = "${columnname}";
074:
075: /** Constant for the replacement in the SQL query for the tablename.<p> */
076: protected static final String REPLACEMENT_TABLENAME = "${tablename}";
077:
078: /** Constant for the CMS_BACKUP_PROJECTS table.<p> */
079: protected static final String TABLE_BACKUP_PROJECTS = "CMS_BACKUP_PROJECTS";
080:
081: /** Constant for the CMS_GROUPS table.<p> */
082: protected static final String TABLE_CMS_GROUPS = "CMS_GROUPS";
083:
084: /** Constant for the CMS_USERS table.<p> */
085: protected static final String TABLE_CMS_USERS = "CMS_USERS";
086:
087: /** Constant for the CMS_PROJECTS table.<p> */
088: protected static final String TABLE_PROJECTS = "CMS_PROJECTS";
089:
090: /** Constant for the USER_OU column.<p> */
091: protected static final String USER_OU_COLUMN = "USER_OU";
092:
093: /** Constant for the SQL query properties.<p> */
094: private static final String QUERY_PROPERTY_FILE = "generic/cms_ou_query.properties";
095:
096: /**
097: * Constructor.<p>
098: *
099: * @throws IOException if the sql queries properties file could not be read
100: */
101: public CmsUpdateDBUpdateOU() throws IOException {
102:
103: super ();
104: loadQueryProperties(QUERY_PROPERTIES_PREFIX
105: + QUERY_PROPERTY_FILE);
106: }
107:
108: /**
109: * Checks if the column USER_OU is found in the resultset.<p>
110: *
111: * @param dbCon the db connection interface
112: * @param table the table to check
113: * @param ouColumn the type of OU to find (e.g. USER_OU or GROUP_OU)
114: *
115: * @return true if the column is in the result set, false if not
116: */
117: protected boolean findOUColumn(CmsSetupDb dbCon, String table,
118: String ouColumn) {
119:
120: System.out.println(new Exception().getStackTrace()[0]
121: .toString());
122: return dbCon.hasTableOrColumn(table, ouColumn);
123: }
124:
125: /**
126: * @see org.opencms.setup.update6to7.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb)
127: */
128: protected void internalExecute(CmsSetupDb dbCon) {
129:
130: System.out.println(new Exception().getStackTrace()[0]
131: .toString());
132:
133: updateOUs(dbCon, TABLE_CMS_USERS, USER_OU_COLUMN);
134: updateOUs(dbCon, TABLE_CMS_GROUPS, GROUP_OU_COLUMN);
135: updateOUs(dbCon, TABLE_PROJECTS, PROJECT_OU_COLUMN);
136: updateOUs(dbCon, TABLE_BACKUP_PROJECTS, PROJECT_OU_COLUMN);
137: }
138:
139: /**
140: * Updates the database tables with the new OUs if necessary for the given table.<p>
141: *
142: * @param dbCon the db connection interface
143: * @param table the table to update
144: * @param ouColumn the column to insert
145: *
146: * @return true if everything worked fine, false if not
147: */
148: protected int updateOUs(CmsSetupDb dbCon, String table,
149: String ouColumn) {
150:
151: System.out.println(new Exception().getStackTrace()[0]
152: .toString());
153: int result = 1;
154: try {
155:
156: if (!findOUColumn(dbCon, table, ouColumn)) {
157: // Alter the table and add the OUs
158: Map replacements = new HashMap();
159: replacements.put(REPLACEMENT_TABLENAME, table);
160: replacements.put(REPLACEMENT_COLUMNNAME, ouColumn);
161: String alterQuery = readQuery(QUERY_KEY_ALTER_TABLE);
162:
163: // Update the database and alter the table to add the OUs
164: dbCon
165: .updateSqlStatement(alterQuery, replacements,
166: null);
167:
168: // Insert the value '/' into the OUs
169: String insertQuery = readQuery(QUERY_ADD_OUS_TO_TABLE);
170: dbCon.updateSqlStatement(insertQuery, replacements,
171: null);
172: result = 0;
173: } else {
174: System.out.println("column " + ouColumn + " in table "
175: + table + " already exists");
176: }
177: // Nothing needs to be done
178: result = 0;
179: } catch (SQLException e) {
180: e.printStackTrace();
181: result = 1;
182: }
183:
184: return result;
185: }
186:
187: }
|