1 5 package org.exoplatform.services.portal.impl; 6 7 import java.util.List ; 8 import java.io.InputStream ; 9 import net.sf.hibernate.Hibernate; 10 import net.sf.hibernate.Session; 11 import org.apache.commons.lang.StringUtils; 12 import org.exoplatform.services.database.XResources; 13 import org.exoplatform.services.organization.*; 14 import org.exoplatform.services.portal.model.Page; 15 import org.exoplatform.services.portal.model.PageSet; 16 import org.picocontainer.Startable; 17 import com.thoughtworks.xstream.XStream; 18 import org.exoplatform.commons.utils.IOUtil; 19 import org.exoplatform.container.configuration.ConfigurationManager; 20 import org.exoplatform.container.configuration.ServiceConfiguration; 21 26 public class PortalUserEventListenerImpl extends UserEventListener implements Startable { 27 private String templateConfig_; 28 private String templatePageSet_ ; 29 private String templateNavigation_ ; 30 31 private NewPortalConfig config_ ; 32 private ConfigurationManager cservice_ ; 33 private PortalConfigServiceImpl pservice_ ; 34 35 public PortalUserEventListenerImpl(ConfigurationManager cservice, 36 OrganizationService orgService, 37 PortalConfigServiceImpl pservice) throws Exception { 38 cservice_ = cservice ; 39 pservice_ = pservice ; 40 ServiceConfiguration sconf = cservice.getServiceConfiguration(getClass()) ; 41 config_ = (NewPortalConfig) sconf.getObjectParam("new.portal.configuration").getObject() ; 42 String templateUser = config_.getTemplateUser() ; 43 String templateLoc = config_.getTemplateLocation() ; 44 String xml = 45 IOUtil.getStreamContentAsString(cservice.getInputStream(config_.getTemplateLocation() + "/templates.xml")); 46 pservice.initPredefinedTemplates(xml) ; 47 48 InputStream is = cservice.getInputStream(templateLoc + "/" + templateUser + "-config.xml") ; 49 templateConfig_ = IOUtil.getStreamContentAsString(is); 50 is = cservice.getInputStream(templateLoc + "/" + templateUser + "-pages.xml") ; 51 templatePageSet_ = IOUtil.getStreamContentAsString(is); 52 is = cservice.getInputStream(templateLoc + "/" + templateUser + "-navigation.xml") ; 53 templateNavigation_ = IOUtil.getStreamContentAsString(is); 54 orgService.addUserEventListener(this) ; 55 } 56 57 public void preSave(User user, boolean isNew, XResources xresources) throws Exception { 58 if(!isNew) return ; 59 Session session = (Session) xresources.getResource(Session.class); 60 String owner = user.getUserName(); 61 String config = null ; 62 String pageSet = null ; 63 String navigation = null ; 64 if(config_.isPredefinedUser(owner)) { 65 String location = config_.getTemplateLocation() ; 66 config = 67 IOUtil.getStreamContentAsString(cservice_.getInputStream(location + "/" + owner + "-config.xml")); 68 pageSet = 69 IOUtil.getStreamContentAsString(cservice_.getInputStream(location + "/" + owner + "-pages.xml")); 70 navigation = 71 IOUtil.getStreamContentAsString(cservice_.getInputStream(location + "/" + owner + "-navigation.xml")); 72 } else { 73 config = StringUtils.replace(templateConfig_, "@owner@", owner); 74 pageSet = StringUtils.replace(templatePageSet_, "@owner@", owner); 75 navigation = StringUtils.replace(templateNavigation_, "@owner@", owner); 76 } 77 session.save(new PortalConfigData(config)); 78 session.save(new NodeNavigationData(navigation)); 79 XStream xstream = PortalConfigServiceImpl.getXStreamInstance() ; 80 PageSet ps = (PageSet) xstream.fromXML(pageSet); 81 List pages = ps.getPages() ; 82 for (int i = 0 ; i < pages.size(); i++) { 83 Page page = (Page) pages.get(i) ; 84 session.save(new PageData(page)) ; 85 } 86 } 87 88 public void preDelete(User user, XResources xresources) throws Exception { 89 Session session = (Session) xresources.getResource(Session.class); 90 String owner = user.getUserName(); 91 session.delete(PortalConfigServiceImpl.queryPageDataByOwner, owner, Hibernate.STRING); 92 session.delete(session.get(PortalConfigData.class, owner)) ; 93 session.delete(session.get(NodeNavigationData.class, owner)) ; 94 pservice_.invalidateCache(user.getUserName()) ; 95 } 96 97 public void start() { } 98 public void stop() {} 99 100 } | Popular Tags |