KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
8 import java.util.jar.JarEntry JavaDoc;
9 import java.util.jar.JarOutputStream JavaDoc;
10 import com.thoughtworks.xstream.XStream;
11 import net.sf.hibernate.Hibernate;
12 import net.sf.hibernate.Query;
13 import net.sf.hibernate.Session;
14
15 /**
16  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
17  * @since Nov 21, 2004
18  * @version $Id$
19  */

20 public class ForumExporter {
21   private Session session_ ;
22   private JarOutputStream JavaDoc out_ ;
23   private XStream xstream_ ;
24   
25   public ForumExporter(Session session, JarOutputStream JavaDoc out, XStream xstream) {
26     session_ = session ;
27     out_ = out ;
28     xstream_ = xstream ;
29   }
30   
31   public void export() throws Exception JavaDoc {
32     Query q = session_.createQuery(ForumServiceContainerImpl.queryForumOwners);
33     List JavaDoc owners = q.list() ;
34     for(int i = 0; i < owners.size(); i++ ) {
35       String JavaDoc owner = (String JavaDoc) owners.get(i) ;
36       export(owner) ;
37     }
38   }
39   
40   public void export(String JavaDoc owner) throws Exception JavaDoc {
41     List JavaDoc categories = session_.find(ForumServiceImpl.queryCategoriesByOwner, owner, Hibernate.STRING );
42     for(int i = 0; i < categories.size(); i++) {
43       CategoryImpl cat = (CategoryImpl) categories.get(i) ;
44       exportCategory(owner, cat) ;
45     }
46   }
47   
48   public void exportCategory(String JavaDoc path ,CategoryImpl category) throws Exception JavaDoc {
49     String JavaDoc categoryPath = path + "/" + category.getId() ;
50     JarEntry JavaDoc entry = new JarEntry JavaDoc(categoryPath + "/" + category.getId() + ".category") ;
51     out_.putNextEntry(entry) ;
52     String JavaDoc xml = xstream_.toXML(category) ;
53     out_.write(xml.getBytes());
54     out_.closeEntry() ;
55     List JavaDoc forums = session_.find(ForumServiceImpl.queryForumsByCategory, category.getId(), Hibernate.STRING );
56     for(int i = 0; i < forums.size(); i++) {
57       ForumImpl forum = (ForumImpl) forums.get(i) ;
58       exportForum(categoryPath ,forum) ;
59     }
60   }
61   
62   public void exportForum(String JavaDoc path ,ForumImpl forum) throws Exception JavaDoc {
63     String JavaDoc forumPath = path + "/" +forum.getId();
64     JarEntry JavaDoc entry = new JarEntry JavaDoc(forumPath+ "/" + forum.getId() + ".forum") ;
65     out_.putNextEntry(entry) ;
66     String JavaDoc xml = xstream_.toXML(forum) ;
67     out_.write(xml.getBytes());
68     out_.closeEntry() ;
69     String JavaDoc queryTopicsByForum =
70       "from topic in class org.exoplatform.services.communication.forum.hibernate.TopicImpl " +
71       "where topic.forumId = ?";
72     List JavaDoc topics = session_.find(queryTopicsByForum, forum.getId(), Hibernate.STRING );
73     for(int i = 0; i < topics.size(); i++) {
74       TopicImpl topic = (TopicImpl) topics.get(i) ;
75       exportTopic(forumPath, topic) ;
76     }
77   }
78   
79   public void exportTopic(String JavaDoc path ,TopicImpl topic) throws Exception JavaDoc {
80     JarEntry JavaDoc entry = new JarEntry JavaDoc(path + "/" + topic.getId() + ".topic") ;
81     out_.putNextEntry(entry) ;
82     List JavaDoc posts = session_.find(ForumServiceImpl.queryPostsByTopic, topic.getId(), Hibernate.STRING) ;
83     TopicBackup backup = new TopicBackup(topic, posts) ;
84     String JavaDoc xml = xstream_.toXML(backup) ;
85     out_.write(xml.getBytes());
86     out_.closeEntry() ;
87   }
88 }
89
Popular Tags