1 5 package org.exoplatform.services.communication.forum.hibernate; 6 7 import java.util.List ; 8 import java.util.jar.JarEntry ; 9 import java.util.jar.JarOutputStream ; 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 20 public class ForumExporter { 21 private Session session_ ; 22 private JarOutputStream out_ ; 23 private XStream xstream_ ; 24 25 public ForumExporter(Session session, JarOutputStream out, XStream xstream) { 26 session_ = session ; 27 out_ = out ; 28 xstream_ = xstream ; 29 } 30 31 public void export() throws Exception { 32 Query q = session_.createQuery(ForumServiceContainerImpl.queryForumOwners); 33 List owners = q.list() ; 34 for(int i = 0; i < owners.size(); i++ ) { 35 String owner = (String ) owners.get(i) ; 36 export(owner) ; 37 } 38 } 39 40 public void export(String owner) throws Exception { 41 List 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 path ,CategoryImpl category) throws Exception { 49 String categoryPath = path + "/" + category.getId() ; 50 JarEntry entry = new JarEntry (categoryPath + "/" + category.getId() + ".category") ; 51 out_.putNextEntry(entry) ; 52 String xml = xstream_.toXML(category) ; 53 out_.write(xml.getBytes()); 54 out_.closeEntry() ; 55 List 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 path ,ForumImpl forum) throws Exception { 63 String forumPath = path + "/" +forum.getId(); 64 JarEntry entry = new JarEntry (forumPath+ "/" + forum.getId() + ".forum") ; 65 out_.putNextEntry(entry) ; 66 String xml = xstream_.toXML(forum) ; 67 out_.write(xml.getBytes()); 68 out_.closeEntry() ; 69 String queryTopicsByForum = 70 "from topic in class org.exoplatform.services.communication.forum.hibernate.TopicImpl " + 71 "where topic.forumId = ?"; 72 List 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 path ,TopicImpl topic) throws Exception { 80 JarEntry entry = new JarEntry (path + "/" + topic.getId() + ".topic") ; 81 out_.putNextEntry(entry) ; 82 List posts = session_.find(ForumServiceImpl.queryPostsByTopic, topic.getId(), Hibernate.STRING) ; 83 TopicBackup backup = new TopicBackup(topic, posts) ; 84 String xml = xstream_.toXML(backup) ; 85 out_.write(xml.getBytes()); 86 out_.closeEntry() ; 87 } 88 } 89 | Popular Tags |