001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration;
023:
024: /**
025: * @author <a href="mailto:boleslaw.dawidowicz@jboss.org">Boleslaw Dawidowicz</a>
026: * @version $Revision: 8784 $
027: */
028: public class Migrator {
029: /* //MigrationModule20_22 module20_22 = new MigrationModule20_22();
030: Configuration fromConfiguration;
031: Configuration toConfiguration;
032:
033: public Migrator()
034: {
035: }
036:
037: **
038: * Initial set up - retrieve config files path from system properties and create SessionFactories
039: *
040: public void setUp()
041: {
042: System.out.println("Migration: Setting up configuration...");
043: //Get config files path
044: String oldSchemaConfig = System.getProperty("org.jboss.portal.migration.schema20.cfg");
045: String newSchemaConfig = System.getProperty("org.jboss.portal.migration.schema22.cfg");
046: String oldSchemaMapping = System.getProperty("org.jboss.portal.migration.schema20.mapping");
047: String newSchemaMapping = System.getProperty("org.jboss.portal.migration.schema22.mapping");
048: if (oldSchemaConfig == null || newSchemaConfig == null)
049: {
050: System.out.println("No config params provided");
051: return;
052: }
053: System.out.println("Migrator working");
054: System.out.println("Old schema config: " + oldSchemaConfig);
055: System.out.println("New schema config: " + newSchemaConfig);
056:
057: setUp(oldSchemaConfig, newSchemaConfig, oldSchemaMapping, newSchemaMapping);
058:
059:
060: }
061:
062: **
063: * Initial set up - create SessionFactories
064: *
065: public void setUp(String oldSchemaConfig, String newSchemaConfig, String oldSchemaMapping, String newSchemaMapping)
066: {
067: //Set up SessionFactories
068: SessionFactory sf1 = null;
069: SessionFactory sf2 = null;
070:
071: try
072: {
073: Configuration cfgOld = new Configuration();
074: cfgOld.configure(new File(oldSchemaConfig));
075: cfgOld.addFile(new File(oldSchemaMapping));
076: Configuration cfgNew = new Configuration();
077: cfgNew.configure(new File(newSchemaConfig));
078: cfgNew.addFile(new File(newSchemaMapping));
079: sf1 = cfgOld.buildSessionFactory();
080: sf2 = cfgNew.buildSessionFactory();
081:
082: fromConfiguration = cfgOld;
083: toConfiguration = cfgNew;
084: }
085: catch (Throwable e)
086: {
087: System.out.println("dabum Error: " + e);
088: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
089: System.exit(-1);
090: }
091:
092:
093: module20_22.setFromSessionFactory(sf1);
094: module20_22.setToSessionFactory(sf2);
095: }
096:
097: public void dropSchema20() throws Exception
098: {
099: module20_22.nextFromSession();
100: //module20_22.nextToSession();
101:
102: SchemaExport export = new SchemaExport(fromConfiguration);
103: export.drop(false, true);
104: }
105:
106: public void dropSchema22() throws Exception
107: {
108: //module20_22.nextFromSession();
109: module20_22.nextToSession();
110:
111: SchemaExport export = new SchemaExport(toConfiguration);
112: export.drop(false, true);
113: }
114:
115: public void exportSchema20() throws Exception
116: {
117: module20_22.nextFromSession();
118: //module20_22.nextToSession();
119:
120: SchemaExport export = new SchemaExport(fromConfiguration);
121: export.create(false, true);
122: }
123:
124: public void exportSchema22() throws Exception
125: {
126: //module20_22.nextFromSession();
127: module20_22.nextToSession();
128:
129: SchemaExport export = new SchemaExport(toConfiguration);
130: export.create(false, true);
131: }
132:
133:
134: **
135: * migrate whole users set - only users objects without roles and prefSets
136: *
137: public void migrateUsers() throws Exception
138: {
139: System.out.println("Migration: migrating users table...");
140: //make new tx and session
141: module20_22.nextFromSession();
142: module20_22.nextToSession();
143:
144: //get set of user id's
145: Set users20Ids = Finder.findUsers20Ids(getF);
146: Hibernate.initialize(users20Ids);
147: //in this approach we get users one by one in separete Transactions
148: // to avoid OOME
149: module20_22.nextFromSession();
150:
151: for (Iterator i = users20Ids.iterator(); i.hasNext();)
152: {
153: Integer u20id = (Integer)i.next();
154: User20Impl user20Impl = module20_22.findUser20ById(u20id);
155: User22Impl user22Impl = MigrationUtils.cloneUserTo22(user20Impl);
156: module20_22.getCurrentToSession().save(user22Impl);
157:
158: module20_22.nextToSession();
159: module20_22.nextFromSession();
160: }
161: }
162:
163: **
164: * migrate whole roles set
165: *
166: public void migrateRoles() throws Exception
167: {
168: System.out.println("Migration: migrating roles table...");
169: **
170: * We read all roles into memory as there aren't meny roles in system usually.
171: * Well... a lot less then users for sure.
172: *
173:
174: //make new tx and session
175: module20_22.nextFromSession();
176: module20_22.nextToSession();
177:
178: Set roles20 = module20_22.findRoles20();
179:
180:
181: for (Iterator i = roles20.iterator(); i.hasNext();)
182: {
183: Role20Impl role20Impl = (Role20Impl)i.next();
184: Role22Impl role22Impl = MigrationUtils.cloneRoleTo22(role20Impl);
185: //module20_22.getCurrentToSession().replicate(role22Impl.getClass().getName(), role22Impl, ReplicationMode.OVERWRITE);
186: module20_22.getCurrentToSession().save(role22Impl);
187: }
188: module20_22.nextToSession();
189: }
190:
191: **
192: * migrate relations between users and roles
193: *
194: public void migrateUserRoleRelations() throws Exception
195: {
196: System.out.println("Migration: migrating users in roles membership...");
197: //make new tx and session
198: module20_22.nextFromSession();
199: module20_22.nextToSession();
200:
201: //get set of user id's
202: Set users20Ids = module20_22.findUsers20Ids();
203: Hibernate.initialize(users20Ids);
204: //in this approach we get users one by one in separete Transactions
205: // to avoid OOME
206: module20_22.nextFromSession();
207:
208: for (Iterator i = users20Ids.iterator(); i.hasNext();)
209: {
210: Integer u20id = (Integer)i.next();
211: User20Impl user20Impl = module20_22.findUser20ById(u20id);
212: Set roles = user20Impl.getRoles();
213: User22Impl user22Impl = module20_22.findUser22ById(new Long(u20id.longValue()));
214: for (Iterator j = roles.iterator(); j.hasNext();)
215: {
216: Role20Impl role20Impl = (Role20Impl)j.next();
217: String r20Name = role20Impl.getName();
218: Role22Impl role22Impl = module20_22.findRole22ByName(r20Name);
219: user22Impl.getRoles().add(role22Impl);
220: role22Impl.users.add(user22Impl);
221: }
222: //User22Impl user22Impl = MigrationUtils.cloneUserTo22(user20Impl);
223: //module20_22.getCurrentToSession().save(user22Impl);
224:
225: module20_22.nextToSession();
226: module20_22.nextFromSession();
227: }
228: }
229:
230:
231: **
232: * migrate relations between users and roles
233: *
234: public void migrateUserPrefSet() throws Exception
235: {
236: System.out.println("Migration: migrating users preferences...");
237: //make new tx and session
238: module20_22.nextFromSession();
239: module20_22.nextToSession();
240:
241: //get set of user id's
242: Set users20Ids = module20_22.findUsers20Ids();
243: Hibernate.initialize(users20Ids);
244: //in this approach we get users one by one in separete Transactions
245: // to avoid OOME
246: module20_22.nextFromSession();
247:
248: for (Iterator i = users20Ids.iterator(); i.hasNext();)
249: {
250: Integer u20id = (Integer)i.next();
251: User20Impl user20Impl = module20_22.findUser20ById(u20id);
252: UserPrefSet20 prefSet20 = user20Impl.preferenceSet20;
253: User22Impl user22Impl = module20_22.findUser22ById(new Long(u20id.longValue()));
254:
255: if (prefSet20 != null)
256: {
257: Map newPrefMap = new HashMap();
258: MigrationUtils.parseUserPrefSet20To22(prefSet20, newPrefMap, null, false);
259: //user22Impl.prefMap = newPrefMap;
260: UserPreferencesGroup22Impl prefGroup22Impl = new UserPreferencesGroup22Impl(user22Impl.getUserName());
261: prefGroup22Impl.prefMap = newPrefMap;
262: module20_22.getCurrentToSession().save(prefGroup22Impl);
263:
264: }
265: //User22Impl user22Impl = MigrationUtils.cloneUserTo22(user20Impl);
266: //module20_22.getCurrentToSession().save(user22Impl);
267:
268: module20_22.nextToSession();
269: module20_22.nextFromSession();
270: }
271: }
272:
273:
274: public void disconnect() throws Exception
275: {
276: module20_22.tearDown();
277: }
278:
279:
280: public static void main(String[] args) throws Exception
281: {
282:
283:
284: Migrator migrator = new Migrator();
285:
286: **
287: * If this is run by ant task paths to config files should be retrieved from
288: * sys properties
289: *
290: if (System.getProperty("org.jboss.portal.migration.runAsTask", "false").equals("false"))
291: {
292: migrator.setUp("schema20/hibernate.cfg.xml",
293: "schema22/hibernate.cfg.xml",
294: "schema20/domain.hbm.xml",
295: "schema22/domain.hbm.xml");
296: }
297: else
298: {
299: migrator.setUp();
300: }
301:
302: migrator.dropSchema22();
303: migrator.exportSchema22();
304: migrator.migrateUsers();
305: migrator.migrateRoles();
306: migrator.migrateUserRoleRelations();
307: migrator.migrateUserPrefSet();
308: migrator.disconnect();
309:
310:
311:
312:
313: }*/
314: }
|