001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools;
007:
008: import java.io.BufferedReader;
009: import java.io.InputStreamReader;
010: import java.sql.Connection;
011: import java.sql.PreparedStatement;
012: import java.sql.ResultSet;
013: import java.sql.SQLException;
014: import java.util.ArrayList;
015: import java.util.Enumeration;
016: import java.util.Hashtable;
017: import java.util.Iterator;
018: import java.util.List;
019:
020: import org.jasig.portal.PortalException;
021: import org.jasig.portal.RDBMServices;
022: import org.jasig.portal.StructureStylesheetDescription;
023: import org.jasig.portal.ThemeStylesheetDescription;
024: import org.jasig.portal.UserProfile;
025: import org.jasig.portal.layout.IUserLayoutStore;
026: import org.jasig.portal.layout.UserLayoutStoreFactory;
027: import org.jasig.portal.security.IPerson;
028: import org.jasig.portal.security.PersonFactory;
029:
030: /**
031: * ConvertProfilesToAL prepares and upgraded 2.1 database to work with
032: * Aggregated Layouts and Integrated Modes. In order to do this, it
033: * converts the existing template user profiles to use the AL/IM structure and
034: * theme, and deletes profiles for non-template users. This results in the
035: * resetting of layouts for normal users.
036: *
037: * @author Al Wold (alwold@asu.edu)
038: *
039: * @version $Revision: 35755 $
040: */
041: public class ConvertProfilesToAL {
042: private static IUserLayoutStore uls;
043:
044: public static void main(String[] args) throws Exception {
045: RDBMServices.setGetDatasourceFromJndi(false); /*don't try jndi when not in web app */
046: uls = UserLayoutStoreFactory.getUserLayoutStoreImpl();
047: BufferedReader br = new BufferedReader(new InputStreamReader(
048: System.in));
049:
050: System.out
051: .print("Please enter the max ID for a template user => ");
052: int lastTemplateUser = Integer.parseInt(br.readLine());
053:
054: Hashtable structSsList = uls.getStructureStylesheetList();
055: for (Enumeration e = structSsList.keys(); e.hasMoreElements();) {
056: Integer id = (Integer) e.nextElement();
057: StructureStylesheetDescription ssd = uls
058: .getStructureStylesheetDescription(id.intValue());
059: System.out.println(id + ": " + ssd.getStylesheetName());
060: }
061: System.out
062: .println("==================================================");
063: System.out
064: .print("Please enter the original structure stylesheet => ");
065: int simpleSsId = Integer.parseInt(br.readLine());
066: System.out
067: .print("Please enter the new structure stylesheet => ");
068: int alSsId = Integer.parseInt(br.readLine());
069:
070: Hashtable themeSsList = uls.getThemeStylesheetList();
071: for (Enumeration e = themeSsList.keys(); e.hasMoreElements();) {
072: Integer id = (Integer) e.nextElement();
073: ThemeStylesheetDescription tsd = uls
074: .getThemeStylesheetDescription(id.intValue());
075: System.out.println(id + ": " + tsd.getStylesheetName());
076: }
077: System.out
078: .println("==================================================");
079: System.out.print("Please enter the new theme stylesheet => ");
080: int imSsId = Integer.parseInt(br.readLine());
081:
082: List ids = getUserIds(lastTemplateUser);
083: // convert template users
084: for (Iterator i = ids.iterator(); i.hasNext();) {
085: int id = ((Integer) i.next()).intValue();
086: convertProfiles(id, simpleSsId, alSsId, imSsId);
087: }
088: // delete the rest
089: deleteUserProfiles(true, lastTemplateUser);
090: createTemplateProfiles(lastTemplateUser);
091: }
092:
093: public static List getUserIds(int lastTemplateUser)
094: throws PortalException {
095: List userIds = new ArrayList();
096: Connection con = RDBMServices.getConnection();
097: try {
098: String query = "SELECT USER_ID FROM UP_USER WHERE USER_ID <= ?";
099: PreparedStatement ps = con.prepareStatement(query);
100: ps.setInt(1, lastTemplateUser);
101: try {
102: ResultSet rs = ps.executeQuery();
103: try {
104: while (rs.next()) {
105: userIds.add(new Integer(rs.getInt(1)));
106: }
107: return userIds;
108: } catch (SQLException sqle) {
109: throw new PortalException(sqle);
110: } finally {
111: rs.close();
112: }
113: } catch (SQLException sqle) {
114: throw new PortalException(sqle);
115: } finally {
116: ps.close();
117: }
118: } catch (SQLException sqle) {
119: throw new PortalException(sqle);
120: } finally {
121: RDBMServices.releaseConnection(con);
122: }
123: }
124:
125: public static void deleteUserProfiles(boolean deleteLayouts,
126: int lastTemplateUser) throws Exception {
127: System.out.print("deleting user profiles...");
128: Connection con = RDBMServices.getConnection();
129: try {
130: String query1 = "DELETE FROM UP_USER_PROFILE WHERE USER_ID > ?";
131: String query2 = "DELETE FROM UP_USER_UA_MAP WHERE USER_ID > ?";
132: String query3 = "DELETE FROM UP_SS_USER_PARM WHERE USER_ID > ?";
133: String query4 = "DELETE FROM UP_SS_USER_ATTS WHERE USER_ID > ?";
134: String query5 = "DELETE FROM UP_USER_LAYOUT WHERE USER_ID > ?";
135: String query6 = "DELETE FROM UP_LAYOUT_PARAM WHERE USER_ID > ?";
136: String query7 = "DELETE FROM UP_LAYOUT_STRUCT WHERE USER_ID > ?";
137: PreparedStatement ps1 = con.prepareStatement(query1);
138: PreparedStatement ps2 = con.prepareStatement(query2);
139: PreparedStatement ps3 = con.prepareStatement(query3);
140: PreparedStatement ps4 = con.prepareStatement(query4);
141: PreparedStatement ps5 = con.prepareStatement(query5);
142: PreparedStatement ps6 = con.prepareStatement(query6);
143: PreparedStatement ps7 = con.prepareStatement(query7);
144: ps1.setInt(1, lastTemplateUser);
145: ps2.setInt(1, lastTemplateUser);
146: ps3.setInt(1, lastTemplateUser);
147: ps4.setInt(1, lastTemplateUser);
148: ps5.setInt(1, lastTemplateUser);
149: ps6.setInt(1, lastTemplateUser);
150: ps7.setInt(1, lastTemplateUser);
151: try {
152: System.out.print("Deleting profiles...");
153: ps1.executeUpdate();
154: System.out.println("done");
155: System.out.print("Deleting profile mappings...");
156: ps2.executeUpdate();
157: System.out.println("done");
158: System.out.print("Deleting structure parameters...");
159: ps3.executeUpdate();
160: System.out.println("done");
161: System.out.print("Deleting theme parameters...");
162: ps4.executeUpdate();
163: System.out.println("done");
164: if (deleteLayouts) {
165: System.out.print("Deleting layouts...");
166: ps5.executeUpdate();
167: System.out.println("done");
168: System.out.print("Deleting layout parameters...");
169: ps6.executeUpdate();
170: System.out.println("done");
171: System.out.print("Deleting layout data...");
172: ps7.executeUpdate();
173: System.out.println("done");
174: }
175: } catch (SQLException sqle) {
176: throw new PortalException(sqle);
177: } finally {
178: ps1.close();
179: ps2.close();
180: ps3.close();
181: ps4.close();
182: }
183: } catch (SQLException sqle) {
184: throw new PortalException(sqle);
185: } finally {
186: RDBMServices.releaseConnection(con);
187: }
188: System.out.println("done");
189: }
190:
191: public static void convertProfiles(int id, int simpleSsId,
192: int alSsId, int imSsId) throws Exception {
193: System.out.print("converting profiles for ID " + id + "...");
194: IPerson person = PersonFactory.createPerson();
195: person.setID(id);
196: Hashtable list = uls.getUserProfileList(person);
197: for (Enumeration e = list.keys(); e.hasMoreElements();) {
198: UserProfile profile = (UserProfile) list.get(e
199: .nextElement());
200: if (profile.getStructureStylesheetId() == simpleSsId) {
201: profile.setStructureStylesheetId(alSsId);
202: profile.setThemeStylesheetId(imSsId);
203: uls.updateUserProfile(person, profile);
204: }
205: }
206: System.out.println("done");
207: }
208:
209: public static void createTemplateProfiles(int lastTemplateUser)
210: throws PortalException {
211: System.out.println("creating template profiles...");
212: Connection con = RDBMServices.getConnection();
213: try {
214: String query = "SELECT USER_ID, USER_DFLT_USR_ID FROM UP_USER WHERE USER_ID > ?";
215: String templateQuery = "SELECT USER_ID, PROFILE_ID, PROFILE_NAME, DESCRIPTION "
216: + "FROM UP_USER_PROFILE WHERE USER_ID = ?";
217: String insert = "INSERT INTO UP_USER_PROFILE (USER_ID, PROFILE_ID, PROFILE_NAME, DESCRIPTION) "
218: + "VALUES (?, ?, ?, ?)";
219: PreparedStatement selectPs = con.prepareStatement(query);
220: PreparedStatement templatePs = con
221: .prepareStatement(templateQuery);
222: PreparedStatement insertPs = con.prepareStatement(insert);
223: selectPs.setInt(1, lastTemplateUser);
224: int templateId;
225: try {
226: ResultSet rs = selectPs.executeQuery();
227: while (rs.next()) {
228: int id = rs.getInt(1);
229: templateId = rs.getInt(2);
230: templatePs.setInt(1, templateId);
231: ResultSet rs2 = templatePs.executeQuery();
232: while (rs2.next()) {
233: insertPs.setInt(1, id);
234: insertPs.setInt(2, rs2.getInt(2));
235: insertPs.setString(3, rs2.getString(3));
236: insertPs.setString(4, rs2.getString(4));
237: insertPs.executeUpdate();
238: }
239: rs2.close();
240: }
241: } catch (SQLException sqle) {
242: throw new PortalException(sqle);
243: } finally {
244: selectPs.close();
245: templatePs.close();
246: insertPs.close();
247: }
248: } catch (SQLException sqle) {
249: throw new PortalException(sqle);
250: } finally {
251: RDBMServices.releaseConnection(con);
252: }
253: }
254: }
|