1 5 package org.exoplatform.services.organization.hibernate; 6 7 import java.io.InputStream ; 8 import java.io.OutputStream ; 9 import java.sql.Timestamp ; 10 import java.util.Collection ; 11 import java.util.Enumeration ; 12 import java.util.Iterator ; 13 import java.util.jar.JarEntry ; 14 import java.util.jar.JarFile ; 15 import java.util.jar.JarOutputStream ; 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 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 .class); 50 } 51 52 public String getName() { return "organization" ;} 53 54 public String getDescription() { 55 return "Export User , UserProfile , Memberships of an user" ; 56 } 57 58 public void exportUserData(String userName, Object transaction, 59 OutputStream out, JarEntry entry) throws Exception { 60 Session session = (Session) transaction; 61 UserImpl user = 62 (UserImpl) service_.userQueryHandler_.findUserByName(userName, session) ; 63 Collection 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 xml = xstream_.toXML(data) ; 69 out.write(xml.getBytes()) ; 70 } 71 72 public void importUserData(String userName, Object transaction, 73 InputStream is, JarEntry entry) throws Exception { 74 Session session = (Session) transaction ; 75 removeExistingUserData(userName, session) ; 76 String xml = IOUtil.getStreamContentAsString(is) ; 77 UserBackupData data = (UserBackupData)xstream_.fromXML(xml) ; 78 UserImpl user = data.getUser() ; 79 UserProfileImpl up = data.getUserProfile() ; 80 Collection 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 transaction, JarOutputStream out) throws Exception { 88 Session session = (Session) transaction ; 89 Collection groups = session.find("from g in class org.exoplatform.services.organization.impl.GroupImpl") ; 90 Iterator i = groups.iterator() ; 91 while(i.hasNext()) { 92 GroupImpl data = (GroupImpl) i.next() ; 93 JarEntry entry = new JarEntry ("group/" + data.getId()+ ".xml") ; 94 out.putNextEntry(entry) ; 95 String xml = xstream_.toXML(data) ; 96 out.write(xml.getBytes()); 97 out.closeEntry() ; 98 } 99 100 Collection 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 entry = new JarEntry ("membership-type/" +data.getName()+ ".xml") ; 105 out.putNextEntry(entry) ; 106 String xml = xstream_.toXML(data) ; 107 out.write(xml.getBytes()); 108 out.closeEntry() ; 109 } 110 } 111 112 public void importServiceData(Object transaction, JarFile jar) throws Exception { 113 Session session = (Session) transaction ; 114 Enumeration e = jar.entries() ; 115 while(e.hasMoreElements()) { 116 JarEntry entry = (JarEntry ) e.nextElement() ; 117 InputStream is = jar.getInputStream(entry) ; 118 String xml = IOUtil.getStreamContentAsString(is) ; 119 String 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 userName, Session session) throws Exception { 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 |