KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > ldap > LDAPOrganizationImporterExporter


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

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