KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > forum > hibernate > ForumImporter


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.forum.hibernate;
6
7 import java.io.InputStream JavaDoc;
8 import java.util.Enumeration JavaDoc;
9 import java.util.jar.JarEntry JavaDoc;
10 import java.util.jar.JarFile JavaDoc;
11 import java.util.List JavaDoc ;
12 import net.sf.hibernate.Session;
13 import org.exoplatform.commons.utils.IOUtil;
14 import com.thoughtworks.xstream.XStream;
15
16 /**
17  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
18  * @since Nov 21, 2004
19  * @version $Id$
20  */

21 public class ForumImporter {
22   private Session session_ ;
23   private JarFile JavaDoc jar_ ;
24   private XStream xstream_ ;
25   
26   public ForumImporter(Session session, JarFile JavaDoc jar, XStream xstream) {
27     session_ = session ;
28     jar_ = jar ;
29     xstream_ = xstream ;
30   }
31   
32   public void importData() throws Exception JavaDoc {
33     Enumeration JavaDoc e = jar_.entries() ;
34     while(e.hasMoreElements()) {
35       JarEntry JavaDoc entry = (JarEntry JavaDoc) e.nextElement() ;
36       InputStream JavaDoc is = jar_.getInputStream(entry) ;
37       String JavaDoc xml = IOUtil.getStreamContentAsString(is) ;
38       String JavaDoc entryName = entry.getName() ;
39       if(entryName.endsWith(".category")) {
40         CategoryImpl category = (CategoryImpl)xstream_.fromXML(xml) ;
41         session_.saveOrUpdateCopy(category) ;
42       } else if(entryName.endsWith(".forum")) {
43         ForumImpl forum = (ForumImpl) xstream_.fromXML(xml) ;
44         session_.saveOrUpdateCopy(forum) ;
45       } else if(entryName.endsWith(".topic")) {
46         TopicBackup backup = (TopicBackup) xstream_.fromXML(xml) ;
47         TopicImpl topic = backup.getTopic() ;
48         List JavaDoc posts = backup.getPosts() ;
49         for(int i = 0 ; i < posts.size(); i++) {
50           PostImpl post = (PostImpl) posts.get(i) ;
51           session_.saveOrUpdateCopy(post) ;
52         }
53         session_.saveOrUpdateCopy(topic) ;
54       }
55       session_.flush() ;
56     }
57   }
58 }
59
Popular Tags