KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > hibernate > HibernateOrganizationImporterExporter


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.organization.hibernate;
6
7 import java.io.InputStream JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.sql.Timestamp JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.jar.JarEntry JavaDoc;
14 import java.util.jar.JarFile JavaDoc;
15 import java.util.jar.JarOutputStream JavaDoc;
16
17 import net.sf.hibernate.Session;
18
19 import org.exoplatform.commons.utils.IOUtil;
20 import org.exoplatform.services.backup.ImporterExporter;
21 import org.exoplatform.services.organization.impl.GroupImpl;
22 import org.exoplatform.services.organization.impl.MembershipImpl;
23 import org.exoplatform.services.organization.impl.MembershipTypeImpl;
24 import org.exoplatform.services.organization.impl.UserBackupData;
25 import org.exoplatform.services.organization.impl.UserImpl;
26 import org.exoplatform.services.organization.impl.UserProfileImpl;
27
28 import com.thoughtworks.xstream.XStream;
29 import com.thoughtworks.xstream.io.xml.XppDriver;
30
31
32 /**
33  * May 27, 2004
34  * @author: Tuan Nguyen
35  * @email: tuan08@users.sourceforge.net
36  * @version: $ID$
37  **/

38 public class HibernateOrganizationImporterExporter implements ImporterExporter {
39   private XStream xstream_ ;
40   private OrganizationServiceImpl service_ ;
41
42   public HibernateOrganizationImporterExporter(OrganizationServiceImpl service) {
43     service_ = service ;
44     xstream_ = new XStream(new XppDriver());
45     xstream_.alias("userData", UserBackupData.class);
46     xstream_.alias("membership", MembershipImpl.class);
47     xstream_.alias("membershipType", MembershipTypeImpl.class);
48     xstream_.alias("group", GroupImpl.class);
49     xstream_.alias("date", Timestamp JavaDoc.class);
50   }
51   
52   public String JavaDoc getName() { return "organization" ;}
53   
54   public String JavaDoc getDescription() {
55     return "Export User , UserProfile , Memberships of an user" ;
56   }
57
58   public void exportUserData(String JavaDoc userName, Object JavaDoc transaction,
59                              OutputStream JavaDoc out, JarEntry JavaDoc entry) throws Exception JavaDoc {
60     Session session = (Session) transaction;
61     UserImpl user =
62       (UserImpl) service_.userQueryHandler_.findUserByName(userName, session) ;
63     Collection JavaDoc memberships =
64       service_.membershipQueryHandler_.findMembershipsByUser(userName, session) ;
65     UserProfileImpl up =
66       (UserProfileImpl)service_.userProfileQueryHandler_.findUserProfileByName(userName, session) ;
67     UserBackupData data = new UserBackupData(user, up, memberships) ;
68     String JavaDoc xml = xstream_.toXML(data) ;
69     out.write(xml.getBytes()) ;
70   }
71
72   public void importUserData(String JavaDoc userName, Object JavaDoc transaction,
73                              InputStream JavaDoc is, JarEntry JavaDoc entry) throws Exception JavaDoc {
74     Session session = (Session) transaction ;
75     removeExistingUserData(userName, session) ;
76     String JavaDoc xml = IOUtil.getStreamContentAsString(is) ;
77     UserBackupData data = (UserBackupData)xstream_.fromXML(xml) ;
78     UserImpl user = data.getUser() ;
79     UserProfileImpl up = data.getUserProfile() ;
80     Collection JavaDoc memberships = data.getMemberships() ;
81  
82     service_.userQueryHandler_.createUserEntry(user, session) ;
83     service_.membershipQueryHandler_.createMembershipEntries(memberships, session) ;
84     service_.userProfileQueryHandler_.createUserProfileEntry(up, session) ;
85   }
86
87   public void exportServiceData(Object JavaDoc transaction, JarOutputStream JavaDoc out) throws Exception JavaDoc {
88     Session session = (Session) transaction ;
89     Collection JavaDoc groups = session.find("from g in class org.exoplatform.services.organization.impl.GroupImpl") ;
90     Iterator JavaDoc i = groups.iterator() ;
91     while(i.hasNext()) {
92       GroupImpl data = (GroupImpl) i.next() ;
93       JarEntry JavaDoc entry = new JarEntry JavaDoc("group/" + data.getId()+ ".xml") ;
94       out.putNextEntry(entry) ;
95       String JavaDoc xml = xstream_.toXML(data) ;
96       out.write(xml.getBytes());
97       out.closeEntry() ;
98     }
99  
100     Collection JavaDoc membershipTypes = session.find("from m in class org.exoplatform.services.organization.impl.MembershipTypeImpl") ;
101     i = membershipTypes.iterator() ;
102     while(i.hasNext()) {
103       MembershipTypeImpl data = (MembershipTypeImpl) i.next() ;
104       JarEntry JavaDoc entry = new JarEntry JavaDoc("membership-type/" +data.getName()+ ".xml") ;
105       out.putNextEntry(entry) ;
106       String JavaDoc xml = xstream_.toXML(data) ;
107       out.write(xml.getBytes());
108       out.closeEntry() ;
109     }
110   }
111
112   public void importServiceData(Object JavaDoc transaction, JarFile JavaDoc jar) throws Exception JavaDoc {
113     Session session = (Session) transaction ;
114     Enumeration JavaDoc e = jar.entries() ;
115     while(e.hasMoreElements()) {
116         JarEntry JavaDoc entry = (JarEntry JavaDoc) e.nextElement() ;
117         InputStream JavaDoc is = jar.getInputStream(entry) ;
118         String JavaDoc xml = IOUtil.getStreamContentAsString(is) ;
119         String JavaDoc entryName = entry.getName() ;
120         if(entryName.startsWith("group")) {
121             GroupImpl data = (GroupImpl)xstream_.fromXML(xml) ;
122             GroupQueryHandler.removeGroupEntry(data.getGroupName(), session) ;
123             session.save(data) ;
124         } else if(entryName.startsWith("membership-type")) {
125             MembershipTypeImpl data = (MembershipTypeImpl) xstream_.fromXML(xml) ;
126             MembershipTypeQueryHandler.removeMembershipTypeEntry(data.getName(), session) ;
127             session.save(data) ;
128         }
129     }
130   }
131
132   private void removeExistingUserData(String JavaDoc userName, Session session) throws Exception JavaDoc {
133     UserImpl user =
134       (UserImpl) service_.userQueryHandler_.findUserByName(userName, session) ;
135     if (user != null) {
136        service_.userQueryHandler_.removeUserEntry(user, session) ;
137        MembershipQueryHandler.removeMembershipEntriesOfUser(userName, session) ;
138        UserProfileQueryHandler.removeUserProfileEntry(userName, session) ;
139     }
140   }
141 }
Popular Tags