KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portal > impl > PortalConfigServiceImpl


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.portal.impl;
6
7 import java.util.*;
8 import net.sf.hibernate.Hibernate;
9 import net.sf.hibernate.Session;
10 import org.exoplatform.commons.utils.PageList;
11 import org.exoplatform.services.cache.CacheService;
12 import org.exoplatform.services.cache.ExoCache;
13 import org.exoplatform.services.database.DBObjectPageList;
14 import org.exoplatform.services.database.HibernateService;
15 import org.exoplatform.services.database.ObjectQuery;
16 import org.exoplatform.services.portal.Interceptor;
17 import org.exoplatform.services.portal.PortalACL;
18 import org.exoplatform.services.portal.PortalConfigService;
19 import org.exoplatform.services.portal.Query;
20 import org.exoplatform.services.portal.impl.converter.*;
21 import org.exoplatform.services.portal.model.*;
22 import org.exoplatform.services.portletcontainer.pci.model.ExoPortletPreferences;
23 import org.exoplatform.services.portletcontainer.pci.model.Preference;
24 import com.thoughtworks.xstream.XStream;
25 import com.thoughtworks.xstream.io.xml.XppDriver;
26 /**
27  * Created by The eXo Platform SARL .
28  * Author : Tuan Nguyen
29  * tuan08@owners.sourceforge.net
30  * Date: Jun 14, 2003
31  * Time: 1:12:22 PM
32  */

33 public class PortalConfigServiceImpl implements PortalConfigService {
34     
35   static private String JavaDoc[] MAPPING =
36     { "org/exoplatform/services/portal/impl/PortalConfigData.hbm.xml",
37       "org/exoplatform/services/portal/impl/PageData.hbm.xml" ,
38       "org/exoplatform/services/portal/impl/NodeNavigationData.hbm.xml" } ;
39   
40   public static final String JavaDoc queryPageDataByOwner =
41     "from pd in class org.exoplatform.services.portal.impl.PageData where pd.owner = ? ";
42   public static final String JavaDoc queryNodeNavigationData =
43     "from nv in class org.exoplatform.services.portal.impl.NodeNavigationData where nv.owner = ? ";
44     
45   static private XStream xstream_ ;
46   
47   private PortalACL portalACL_ ;
48   private HibernateService hservice_ ;
49   private Map predefinedTemplates_ ;
50   private Interceptor interceptor_ ;
51   private ExoCache portalConfigCache_ ;
52   private ExoCache pageConfigCache_ ;
53   private ExoCache nodeNavigationCache_ ;
54   
55   public PortalConfigServiceImpl(Interceptor interceptor,
56                                  PortalACL portalACL,
57                                  CacheService cacheService ,
58                                  HibernateService hservice) throws Exception JavaDoc {
59     hservice_ = hservice ;
60     interceptor_ = interceptor ;
61     hservice.addMappingFiles(MAPPING) ;
62     portalACL_ = portalACL ;
63     portalConfigCache_ = cacheService.getCacheInstance(PortalConfig.class.getName()) ;
64     portalConfigCache_.setMaxSize(1000) ;
65     pageConfigCache_ = cacheService.getCacheInstance(Page.class.getName()) ;
66     pageConfigCache_.setMaxSize(1000) ;
67     nodeNavigationCache_ = cacheService.getCacheInstance(NodeImpl.class.getName()) ;
68     nodeNavigationCache_.setMaxSize(1000) ;
69   }
70   
71   public void initPredefinedTemplates(String JavaDoc xml) throws Exception JavaDoc {
72     predefinedTemplates_ = new HashMap() ;
73     XStream xstream = getXStreamInstance() ;
74     PageSet ps = (PageSet) xstream.fromXML(xml);
75     List pages = ps.getPages() ;
76     for (int i = 0 ; i < pages.size(); i++) {
77       Page page = (Page) pages.get(i) ;
78       predefinedTemplates_.put(page.getOwner() + ":" + page.getName(), page) ;
79     }
80   }
81   
82   public void savePortalConfig(String JavaDoc owner, String JavaDoc xml) throws Exception JavaDoc {
83     hservice_.save(new PortalConfigData(xml)) ;
84     portalConfigCache_.remove(owner) ;
85   }
86   
87   public void savePortalConfig(PortalConfig config) throws Exception JavaDoc {
88     portalConfigCache_.remove(config.getOwner()) ;
89     interceptor_.savePortalConfig(config) ;
90   }
91   
92   public PortalConfig getPortalConfig(String JavaDoc owner) throws Exception JavaDoc {
93     PortalConfig config = (PortalConfig) portalConfigCache_.get(owner) ;
94     if(config != null) return config ;
95     config = interceptor_.getPortalConfig(owner);
96     if(config != null) portalConfigCache_.put(owner, config) ;
97     return config ;
98   }
99  
100   public String JavaDoc getPortalConfigAsXmlString(String JavaDoc owner) throws Exception JavaDoc {
101     PortalConfigData data =
102         (PortalConfigData) hservice_.findOne(PortalConfigData.class, owner);
103     return data.getData() ;
104   }
105
106   public PageList findAllPortalConfigDescriptions(Query q) throws Exception JavaDoc {
107     String JavaDoc owner = q.getOwner() ;
108     if (owner == null || owner.length() == 0) {
109         owner = "%" ;
110     }
111     owner = owner.replace('*', '%') ;
112     String JavaDoc viewPermission = q.getViewPermission() ;
113     String JavaDoc editPermission = q.getEditPermission() ;
114     ObjectQuery oq = new ObjectQuery(PortalConfigDescriptionData.class);
115     oq.addLIKE("id", owner) ;
116     if(viewPermission != null && viewPermission.length() > 0) {
117       oq.addEQ("viewPermission", viewPermission) ;
118     }
119     if(editPermission != null && editPermission.length() > 0) {
120       oq.addEQ("editPermission", editPermission) ;
121     }
122     return new DBObjectPageList(hservice_, oq) ;
123   }
124   
125   public void removePortalConfig(String JavaDoc owner) throws Exception JavaDoc {
126     hservice_.remove(PortalConfigData.class, owner) ;
127     portalConfigCache_.remove(owner) ;
128   }
129   
130   public void savePage(String JavaDoc xml) throws Exception JavaDoc {
131     PageData data = new PageData(xml) ;
132     hservice_.save(data) ;
133     pageConfigCache_.remove(data.getId()) ;
134   }
135
136   public void savePage(Page page) throws Exception JavaDoc {
137     interceptor_.savePage(page) ;
138     pageConfigCache_.remove(PageData.getId(page)) ;
139   }
140   
141   public Page getPage(String JavaDoc pageId) throws Exception JavaDoc {
142     Page page = (Page)pageConfigCache_.get(pageId) ;
143     if(page != null) return page ;
144     page = interceptor_.getPage(pageId);
145     pageConfigCache_.put(pageId, page) ;
146     return page ;
147   }
148
149   public String JavaDoc getPageAsXmlString(String JavaDoc refId) throws Exception JavaDoc {
150     PageData data = (PageData) hservice_.findOne(PageData.class, refId);
151     return data.getData();
152   }
153
154   public void removePage(String JavaDoc refId) throws Exception JavaDoc {
155     hservice_.remove(PageData.class, refId) ;
156     pageConfigCache_.remove(refId) ;
157   }
158
159   public void removePageOfOwner(String JavaDoc owner) throws Exception JavaDoc {
160     Session session = hservice_.openSession();
161     session.delete(queryPageDataByOwner, owner, Hibernate.STRING);
162     session.flush() ;
163     pageConfigCache_.clear() ;
164   }
165   
166   public PageList findAllPageDescriptions(Query q) throws Exception JavaDoc {
167     String JavaDoc owner = q.getOwner() ;
168     if (owner == null || owner.length() == 0) {
169         owner = "%" ;
170     }
171     owner = owner.replace('*', '%') ;
172     String JavaDoc viewPermission = q.getViewPermission() ;
173     String JavaDoc editPermission = q.getEditPermission() ;
174     ObjectQuery oq = new ObjectQuery(PageDescriptionData.class);
175     oq.addLIKE("owner", owner) ;
176     if(viewPermission != null && viewPermission.length() > 0) {
177       oq.addEQ("viewPermission", viewPermission) ;
178     }
179     if(editPermission != null && editPermission.length() > 0) {
180       oq.addEQ("editPermission", editPermission) ;
181     }
182     return new DBObjectPageList(hservice_, oq) ;
183   }
184   //=============================================================================
185
public void saveNodeNavigation(String JavaDoc owner, Node node) throws Exception JavaDoc {
186     interceptor_.saveNodeNavigation(owner, node) ;
187     nodeNavigationCache_.remove(owner) ;
188   }
189   
190   public void removeNodeNavigation(String JavaDoc owner) throws Exception JavaDoc {
191     hservice_.remove(NodeNavigationData.class, owner) ;
192     nodeNavigationCache_.remove(owner) ;
193   }
194   
195   public Node getNodeNavigation(String JavaDoc owner) throws Exception JavaDoc {
196     Node node = (Node) nodeNavigationCache_.get(owner) ;
197     if(node == null) {
198       node = interceptor_.getNodeNavigation(owner);
199       nodeNavigationCache_.put(owner, node) ;
200     }
201     if(node == null) return null ;
202     //clone the node
203
return new NodeImpl(node, null, 0) ;
204   }
205   
206   public Node createNodeInstance() { return new NodeImpl(); }
207   //=============================================================================
208

209   public PortalACL getPortalACL() { return portalACL_ ; }
210   
211   public List getPredefinedTemplates(String JavaDoc owner) {
212     ArrayList list = new ArrayList(predefinedTemplates_.size()) ;
213     Iterator i = predefinedTemplates_.values().iterator() ;
214     while(i.hasNext()) {
215         Page page = (Page)i.next() ;
216         if(owner.equals(page.getOwner())) {
217             list.add(page) ;
218         }
219     }
220     return list ;
221   }
222   
223   public Page getPredefinedTemplate(String JavaDoc owner, String JavaDoc name) throws Exception JavaDoc {
224     Page page = (Page) predefinedTemplates_.get(owner + ":" + name) ;
225     if (page != null) page = (Page)page.softCloneObject() ;
226     return page ;
227   }
228   
229   void invalidateCache(String JavaDoc owner) throws Exception JavaDoc {
230     portalConfigCache_.remove(owner) ;
231     pageConfigCache_.clear() ;
232     nodeNavigationCache_.remove(owner) ;
233   }
234   
235  static XStream getXStreamInstance() {
236     if (xstream_ == null ) {
237       xstream_ = new XStream(new XppDriver());
238       xstream_.alias("user-portal-config", Backup.class);
239       xstream_.alias("portal-config", PortalConfig.class);
240       xstream_.alias("body", Body.class);
241       xstream_.alias("node", PageNode.class);
242       xstream_.alias("page", Page.class);
243       xstream_.alias("page-set", PageSet.class);
244       xstream_.alias("container", Container.class);
245       xstream_.alias("portlet", Portlet.class);
246       xstream_.alias("portlet-preferences", ExoPortletPreferences.class);
247       xstream_.alias("preference", Preference.class);
248       xstream_.alias("node-navigation", NodeNavigation.class);
249       xstream_.registerConverter(new PortletConverter());
250       xstream_.registerConverter(new ExoPortletPreferencesConverter());
251       xstream_.registerConverter(new PreferenceConverter());
252       xstream_.registerConverter(new ContainerConverter());
253       xstream_.registerConverter(new PageConverter());
254       xstream_.registerConverter(new PageSetConverter());
255       xstream_.registerConverter(new NodeConfigConverter());
256     }
257     return xstream_ ;
258   }
259 }
Popular Tags