1 5 package org.exoplatform.services.communication.forum.hibernate; 6 7 import java.io.InputStream ; 8 import java.util.Enumeration ; 9 import java.util.jar.JarEntry ; 10 import java.util.jar.JarFile ; 11 import java.util.List ; 12 import net.sf.hibernate.Session; 13 import org.exoplatform.commons.utils.IOUtil; 14 import com.thoughtworks.xstream.XStream; 15 16 21 public class ForumImporter { 22 private Session session_ ; 23 private JarFile jar_ ; 24 private XStream xstream_ ; 25 26 public ForumImporter(Session session, JarFile jar, XStream xstream) { 27 session_ = session ; 28 jar_ = jar ; 29 xstream_ = xstream ; 30 } 31 32 public void importData() throws Exception { 33 Enumeration e = jar_.entries() ; 34 while(e.hasMoreElements()) { 35 JarEntry entry = (JarEntry ) e.nextElement() ; 36 InputStream is = jar_.getInputStream(entry) ; 37 String xml = IOUtil.getStreamContentAsString(is) ; 38 String 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 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 |