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.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import org.jasig.portal.RDBMServices;
018: import org.jasig.portal.UserProfile;
019: import org.jasig.portal.layout.IUserLayoutStore;
020: import org.jasig.portal.layout.alm.AggregatedLayout;
021: import org.jasig.portal.layout.alm.AggregatedLayoutManager;
022: import org.jasig.portal.layout.alm.AggregatedUserLayoutStore;
023: import org.jasig.portal.layout.simple.RDBMUserLayoutStore;
024: import org.jasig.portal.security.IPerson;
025: import org.jasig.portal.security.PersonFactory;
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Node;
028: import org.w3c.dom.NodeList;
029:
030: /**
031: * Converts Simple Layouts to AggregatedLayout / IntegratedMode.
032: * A new profile is also created.
033: * To use the new layout, an administrator must update the UP_USER_UA_MAP table
034: * to map the appropriate media to the new profile
035: * @author Susan Bramhall, susan.bramhall@yale.edu
036: * @version $Revision: 35735 $
037: */
038:
039: public class SimpleLayout2ALIM {
040: public static void main(String[] args) {
041: RDBMServices.setGetDatasourceFromJndi(false); /*don't try jndi when not in web app */
042:
043: if (args.length < 1) {
044: System.out
045: .println("Usage \"SimpleLayout2ALIM [-all] <userid> [<profileid>]\" \n profileid defaults to 1 ");
046: return;
047: }
048:
049: SimpleLayout2ALIM converter = new SimpleLayout2ALIM();
050:
051: if ("-all".equals(args[0])) {
052: converter.convertAllLayouts();
053: } else {
054: int uid = Integer.parseInt(args[0]);
055: int simpleProfileId = 1;
056: if (args.length >= 2) {
057: simpleProfileId = Integer.parseInt(args[1]);
058: }
059: converter.convertLayout(uid, simpleProfileId);
060: }
061: }
062:
063: public SimpleLayout2ALIM() {
064: }
065:
066: public void convertAllLayouts() {
067: StringBuffer sql = new StringBuffer();
068: sql.append("select distinct p.user_id, p.profile_id ");
069: sql.append("from up_user_profile p, up_layout_struct s ");
070: sql
071: .append("where p.user_id = s.user_id and p.layout_id = s.layout_id");
072: List userProfiles = new ArrayList();
073: // db objects.
074: Connection conn = null;
075: PreparedStatement pstmt = null;
076: ResultSet rs = null;
077:
078: int userId;
079: int profileId;
080:
081: try {
082: conn = RDBMServices.getConnection();
083: pstmt = conn.prepareStatement(sql.toString());
084: rs = pstmt.executeQuery();
085: while (rs.next()) {
086: userId = Integer.parseInt(rs.getString("user_id"));
087: profileId = Integer
088: .parseInt(rs.getString("profile_id"));
089: userProfiles
090: .add(new UserProfileInfo(userId, profileId));
091: }
092: } catch (Exception e) {
093: e.printStackTrace();
094: } finally {
095: RDBMServices.closeResultSet(rs);
096: RDBMServices.closeStatement(pstmt);
097: RDBMServices.releaseConnection(conn);
098: rs = null;
099: pstmt = null;
100: conn = null;
101: }
102:
103: UserProfileInfo upinfo;
104: Iterator itr = userProfiles.iterator();
105: while (itr.hasNext()) {
106: upinfo = (UserProfileInfo) itr.next();
107: convertLayout(upinfo.getUserId(), upinfo.getProfileId());
108: }
109: }
110:
111: public void convertLayout(int uid, int simpleProfileId) {
112:
113: final int NEW_LAYOUT_ID = 1; /* id to use when creating new layout */
114: UserProfile simpleProfile = null;
115: UserProfile ALProfile = null;
116: IUserLayoutStore uls = null;
117: int structSsId = 0, themeSsId = 0;
118: int ALProfileId = 0;
119: Connection con = null;
120: Statement qstmt = null;
121: ResultSet rs = null;
122:
123: IPerson user = PersonFactory.createPerson();
124: user.setID(uid);
125:
126: try {
127: // read in the simple layout
128: uls = new RDBMUserLayoutStore();
129: simpleProfile = uls.getUserProfileById(user,
130: simpleProfileId);
131: Document ul = uls.getUserLayout(user, simpleProfile);
132: // wow - that was easy. Now need to get rid of initial letters in IDs.
133: stripNodes(ul.getChildNodes().item(0));
134:
135: // create a profile for the new layout
136:
137: try {
138: con = RDBMServices.getConnection();
139: qstmt = con.createStatement();
140:
141: rs = qstmt
142: .executeQuery("select ss_id from up_ss_struct where ss_uri like "
143: + "'%org/jasig/portal/layout/AL_TabColumn/AL_TabColumn.xsl'");
144: if (rs.next()) {
145: structSsId = rs.getInt(1);
146: } else {
147: System.out
148: .println("No AL structure stylesheet found. \n Layout for user "
149: + uid + " not converted.");
150: con.rollback();
151: return;
152: }
153: rs.close();
154: rs = qstmt
155: .executeQuery("select ss_id from up_ss_theme where ss_uri like "
156: + "'%org/jasig/portal/layout/AL_TabColumn/integratedModes/integratedModes.xsl'");
157: if (rs.next()) {
158: themeSsId = rs.getInt(1);
159: } else {
160: System.out
161: .println("No IM theme stylesheet found. \n Layout for user "
162: + uid + " not converted.");
163: con.rollback();
164: return;
165: }
166: rs.close();
167:
168: // create a new profile id
169: //rs = qstmt.executeQuery("select max(profile_id) from UP_USER_PROFILE where user_ID ="+ uid);
170: //if (rs.next()) ALProfileId = rs.getInt(1)+1;
171:
172: ALProfile = new UserProfile(ALProfileId, "AL",
173: "AL Profile", NEW_LAYOUT_ID, structSsId,
174: themeSsId);
175:
176: } catch (SQLException se) {
177: System.err
178: .println("Error creating new profile for user "
179: + uid);
180: se.printStackTrace();
181: } finally {
182: RDBMServices.closeResultSet(rs);
183: RDBMServices.closeStatement(qstmt);
184: RDBMServices.releaseConnection(con);
185: }
186:
187: // new AggregatedLayoutManager to save layout
188: AggregatedLayoutManager alm = new AggregatedLayoutManager(
189: user, ALProfile);
190: // new AggregatedLayout with root node only to get started
191: AggregatedLayout al = new AggregatedLayout(
192: "userLayoutRootNode", alm);
193: // Give it to the layout manager
194: alm.setUserLayout(al);
195: // Initialize the layout store
196: AggregatedUserLayoutStore als = new AggregatedUserLayoutStore();
197: // Set the layout manager to use the new store
198: alm.setLayoutStore(als);
199: // set the layout to the DOM created from the old simple manager
200: alm.setUserLayoutDOM(ul);
201: // persist the new layout to the store
202: // creates new layout with id 1
203: alm.saveUserLayout();
204: // add the new profile
205: als.addUserProfile(user, ALProfile);
206: } catch (Exception e) {
207: System.out
208: .println("Error saving aggregated layout for user_id "
209: + uid);
210: e.printStackTrace();
211: }
212: System.out.println("Saved aggregated layout for user_id " + uid
213: + " and new profile with id="
214: + ALProfile.getProfileId());
215: return;
216: }
217:
218: private void stripNodes(Node node) {
219: NodeList nlist = node.getChildNodes();
220: for (int i = 0; i < nlist.getLength(); i++) {
221: if (nlist.item(i).getAttributes().getNamedItem("ID") != null) {
222: String oldId = nlist.item(i).getAttributes()
223: .getNamedItem("ID").getNodeValue();
224: Node attrNode = nlist.item(i).getAttributes()
225: .getNamedItem("ID");
226: attrNode.setNodeValue(oldId.substring(1));
227: }
228: stripNodes(nlist.item(i));
229: }
230: }
231:
232: private class UserProfileInfo {
233: int userId;
234: int profileId;
235:
236: public UserProfileInfo(int userId, int profileId) {
237: this .userId = userId;
238: this .profileId = profileId;
239: }
240:
241: public int getUserId() {
242: return userId;
243: }
244:
245: public int getProfileId() {
246: return profileId;
247: }
248: }
249: }
|