KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > homepages > JahiaHomepagesBaseService


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// JahiaHomepagesBaseService
14
//
15
// NK 17.12.2001
16
//
17
package org.jahia.services.homepages;
18
19 import org.jahia.data.JahiaDOMObject;
20 import org.jahia.exceptions.JahiaException;
21 import org.jahia.exceptions.JahiaInitializationException;
22 import org.jahia.services.acl.JahiaBaseACL;
23 import org.jahia.services.sites.JahiaSite;
24 import org.jahia.settings.SettingsBean;
25
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32
33
34 /**
35  * Homepage services
36  *
37  * @author Khue ng
38  */

39 public class JahiaHomepagesBaseService extends JahiaHomepagesService {
40
41     private static org.apache.log4j.Logger logger =
42             org.apache.log4j.Logger.getLogger (JahiaHomepagesBaseService.class);
43
44     private static final String JavaDoc CLASS_NAME = JahiaHomepagesBaseService.class.getName ();
45
46     private static JahiaHomepagesBaseService instance = null;
47
48     private JahiaHomepagesPersistance hppServ;
49
50     /** list of cache, one cache for each site */
51     private Hashtable JavaDoc vCaches = null;
52
53
54     protected JahiaHomepagesBaseService () throws JahiaException {
55         logger.info (" ***** " + this.getClass ().getName () + " started *****");
56         vCaches = new Hashtable JavaDoc ();
57     }
58
59
60     public static synchronized JahiaHomepagesBaseService getInstance ()
61             throws JahiaException {
62
63         if (instance == null) {
64             instance = new JahiaHomepagesBaseService ();
65         }
66         return instance;
67     }
68
69     //--------------------------------------------------------------------------
70
/**
71      * @param JahiaPrivateSettings jSettings
72      */

73     public void init (SettingsBean jSettings)
74             throws JahiaInitializationException {
75         if (!isInitialized ()) {
76             try {
77                 loadHomepagesInCache ();
78                 mIsServiceInitialized = true;
79             } catch (Throwable JavaDoc t) {
80                 t.printStackTrace ();
81                 throw new JahiaInitializationException (CLASS_NAME + ".init: Init error");
82             }
83         }
84     }
85
86     //--------------------------------------------------------------------------
87
/**
88      * return the list of all home page
89      *
90      * @return Enumeration an enumeration of JahiaHomepage bean
91      */

92     public Enumeration JavaDoc getHomepages () throws JahiaException {
93
94         Vector JavaDoc v = new Vector JavaDoc ();
95         Enumeration JavaDoc keys = vCaches.keys ();
96         Hashtable JavaDoc hash = null;
97         while (keys.hasMoreElements ()) {
98             hash = (Hashtable JavaDoc) vCaches.get (keys.nextElement ());
99             v.addAll (hash.values ());
100         }
101
102         JahiaHomepage hp = createHomepage ("",
103                 "",
104                 "",
105                 HomepageTypes.HOMEPAGE_LINK,
106                 null,
107                 -1
108         );
109         Collections.sort (v, hp);
110
111         return v.elements ();
112     }
113
114     //--------------------------------------------------------------------------
115
/**
116      * return the list of all home pages of a site.
117      *
118      * @return Enumeration an enumeration of JahiaHomepage bean
119      */

120     public Enumeration JavaDoc getHomepages (JahiaSite site) throws JahiaException {
121
122         Vector JavaDoc v = new Vector JavaDoc ();
123         if (site == null) {
124             return v.elements ();
125         }
126         Hashtable JavaDoc hash = (Hashtable JavaDoc) vCaches.get (site.getSiteKey ());
127
128         if (hash == null) {
129             return v.elements ();
130         }
131         v.addAll (hash.values ());
132
133         JahiaHomepage hp = createHomepage ("",
134                 "",
135                 "",
136                 HomepageTypes.HOMEPAGE_LINK,
137                 null,
138                 -1
139         );
140         Collections.sort (v, hp);
141
142         return v.elements ();
143     }
144
145     //--------------------------------------------------------------------------
146
/**
147      * return a Homepage bean looking at it id
148      *
149      * @param int the JahiaHomepage id
150      *
151      * @return JahiaHomepage the JahiaHomepage bean
152      */

153     public JahiaHomepage getHomepage (int id) throws JahiaException {
154
155         // TODO : access control with ParamBean ?
156

157         Vector JavaDoc v = new Vector JavaDoc ();
158         Enumeration JavaDoc keys = vCaches.keys ();
159         Enumeration JavaDoc hpKeys = null;
160         Hashtable JavaDoc hash = null;
161         JahiaHomepage hp = null;
162         while (keys.hasMoreElements ()) {
163             hash = (Hashtable JavaDoc) vCaches.get (keys.nextElement ());
164             hp = (JahiaHomepage) hash.get (new Integer JavaDoc (id));
165             if (hp != null) {
166                 return hp;
167             }
168         }
169         // load from db
170
hp = JahiaHomepagesPersistance.getInstance ().load (id);
171         if (hp != null) {
172             hash = (Hashtable JavaDoc) vCaches.get (hp.getSiteKey ());
173             if (hash == null) {
174                 // create the cache for this site
175
hash = new Hashtable JavaDoc ();
176                 vCaches.put (hp.getSiteKey (), hash);
177             }
178             hash.put (new Integer JavaDoc (id), hp);
179         }
180         return hp;
181     }
182
183     //--------------------------------------------------------------------------
184
/**
185      * Create a new Homepage that is not saved in storage yet.
186      *
187      * @param String the home page name
188      * @param String the home page descr
189      * @param String the site key
190      * @param int type the home page type
191      * @param hashtable the props
192      * @param int aclID
193      *
194      * @return Jahiahomepage the created homepage bean
195      */

196     public JahiaHomepage createHomepage (String JavaDoc name,
197                                          String JavaDoc descr,
198                                          String JavaDoc siteKey,
199                                          int type,
200                                          Hashtable JavaDoc props,
201                                          int aclID
202                                          ) throws JahiaException {
203
204         JahiaHomepage hp;
205
206         try {
207             // define the types of the parameter of the constructor
208
Class JavaDoc theParams[] = {Class.forName ("java.lang.Integer"),
209                                  Class.forName ("java.lang.String"),
210                                  Class.forName ("java.lang.String"),
211                                  Class.forName ("java.lang.Integer"),
212                                  Class.forName ("java.lang.String"),
213                                  Class.forName ("java.util.Hashtable"),
214                                  Class.forName ("java.lang.Integer")};
215
216             // get the constructor by its name
217
// the name come from the hashtable
218
Constructor JavaDoc thisConstructor =
219                     Class.forName ((String JavaDoc) HomepageTypes.getInstance ()
220                     .getClassesNames ().get (new Integer JavaDoc (type)))
221                     .getDeclaredConstructor (theParams);
222
223
224             // the parameter values of the constructor
225
Object JavaDoc args[] = {new Integer JavaDoc (-1), // the id
226
name,
227                              descr,
228                              new Integer JavaDoc (type),
229                              siteKey,
230                              props,
231                              new Integer JavaDoc (aclID)};
232
233
234             // call the constructor
235
hp = (JahiaHomepage) thisConstructor.newInstance (args);
236
237         } catch (ClassNotFoundException JavaDoc ex) {
238             logger.error (ex);
239             throw new JahiaException (CLASS_NAME + ".createHomepage",
240                     "Class not found!",
241                     JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY);
242
243         } catch (NoSuchMethodException JavaDoc ex) {
244             logger.error (ex);
245             throw new JahiaException (CLASS_NAME + ".createHomepage",
246                     "Method not found!",
247                     JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY);
248
249         } catch (IllegalAccessException JavaDoc ex) {
250             logger.error (ex);
251             throw new JahiaException (CLASS_NAME + ".createHomepage",
252                     "Illegal Access!",
253                     JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY);
254
255         } catch (InvocationTargetException JavaDoc ex) {
256             logger.error (ex);
257             throw new JahiaException (CLASS_NAME + ".createHomepage",
258                     "InvocationTarget exception",
259                     JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY);
260
261         } catch (InstantiationException JavaDoc ex) {
262             logger.error (ex);
263             throw new JahiaException (CLASS_NAME + ".createHomepage",
264                     "Instantiation exception",
265                     JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY);
266
267         }
268
269         // return the new JahiaHomepage
270
return hp;
271
272     }
273
274     //--------------------------------------------------------------------------
275
/**
276      * delete a Homepage from storage and cache
277      *
278      * @param int the home page id
279      */

280     public void deleteHomepage (int id)
281             throws JahiaException {
282
283         // TODO : access control with ParamBean ?
284

285         JahiaHomepage hp = getHomepage (id);
286         if (hp == null)
287             return;
288
289         Hashtable JavaDoc hash = null;
290         hash = (Hashtable JavaDoc) vCaches.get (hp.getSiteKey ());
291         if (hash != null) {
292             hash.remove (new Integer JavaDoc (hp.getID ()));
293         }
294         hp.delete ();
295     }
296
297     //--------------------------------------------------------------------------
298
/**
299      * Save a Homepage. Create it if its id is <=0 and create an ACL if it is <=0.
300      * The parent ACL should be the site's acl.
301      *
302      * @param JahiaHomepage the homepage bean object
303      * @param int the parent ACL ID
304      */

305     public void saveHomepage (JahiaHomepage hp, int parentAclID)
306             throws JahiaException {
307
308         // TODO : access control with ParamBean ?
309

310         if (hp == null)
311             return;
312
313         // creates ACL, if needed
314
if (hp.getAclID () <= 0) {
315             JahiaBaseACL acl = new JahiaBaseACL ();
316             if (acl != null) {
317                 // create a new object by specifying the parent ACL ID
318
if (!acl.create (parentAclID)) {
319                     String JavaDoc message = "Could not create an ACL object for a new homepage.";
320                     logger.debug (message);
321                     throw new JahiaException (CLASS_NAME + ".saveHomepage",
322                             message,
323                             JahiaException.ACL_ERROR,
324                             JahiaException.CRITICAL_SEVERITY);
325                 } else {
326                     logger.debug ("ACL [" + acl.getID () + "] has just been created!");
327                 }
328             } else {
329                 throw new JahiaException (CLASS_NAME + ".saveHomepage",
330                         "Could not instanciate the JahiaBaseACL class",
331                         JahiaException.ACL_ERROR, JahiaException.CRITICAL_SEVERITY);
332             }
333             // End Create ACL
334
hp.setAclID (acl.getID ());
335         }
336
337         hp.save ();
338
339         Hashtable JavaDoc hash = null;
340         hash = (Hashtable JavaDoc) vCaches.get (hp.getSiteKey ());
341         if (hash == null) {
342             // create the cache for this site
343
hash = new Hashtable JavaDoc ();
344             vCaches.put (hp.getSiteKey (), hash);
345         }
346         hash.put (new Integer JavaDoc (hp.getID ()), hp);
347     }
348
349     //--------------------------------------------------------------------------
350
/**
351      * load all homepage definitions from storage in cache
352      */

353     private void loadHomepagesInCache () throws JahiaException {
354
355         if (vCaches == null)
356             vCaches = new Hashtable JavaDoc ();
357
358         Vector JavaDoc v = JahiaHomepagesPersistance.getInstance ().getHomepages ();
359         if (v == null)
360             return;
361
362         JahiaHomepage hp = null;
363         int size = v.size ();
364         Hashtable JavaDoc hash = null;
365         for (int i = 0; i < size; i++) {
366             hp = (JahiaHomepage) v.get (i);
367             hash = (Hashtable JavaDoc) vCaches.get (hp.getSiteKey ());
368             if (hash == null) {
369                 // create the cache for this site
370
hash = new Hashtable JavaDoc ();
371                 vCaches.put (hp.getSiteKey (), hash);
372             }
373             hash.put (new Integer JavaDoc (hp.getID ()), hp);
374         }
375     }
376
377     //--------------------------------------------------------------------------
378
/**
379      * returns a DOM representation of all home pages of a site
380      *
381      * @param String siteKey
382      */

383     public JahiaDOMObject getHomepageDefsAsDOM (String JavaDoc siteKey)
384             throws JahiaException {
385
386         return JahiaHomepagesPersistance.getInstance ().getHomepageDefsAsDOM (siteKey);
387
388     }
389
390     //--------------------------------------------------------------------------
391
/**
392      * returns a DOM representation of all home pages props of a site
393      *
394      * @param String siteKey
395      */

396     public JahiaDOMObject getHomepageDefPropsAsDOM (String JavaDoc siteKey)
397             throws JahiaException {
398
399         return JahiaHomepagesPersistance.getInstance ().getHomepageDefPropsAsDOM (siteKey);
400
401     }
402
403     //--------------------------------------------------------------------------
404
/**
405      * Returns a vector of all home page's Acl ID of this site
406      * Need this for site extraction
407      *
408      * @param String siteKey
409      */

410     public Vector JavaDoc getAclIDs (String JavaDoc siteKey)
411             throws JahiaException {
412         return JahiaHomepagesPersistance.getInstance ().db_get_all_acl_id (siteKey);
413     }
414
415 }
416
Popular Tags