KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > pages > JahiaPageDefinition


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
// 08.05.2001 NK implements Comparator interface to be sortable
14
// 22.01.2001 NK added page def ACL
15
//
16

17 package org.jahia.services.pages;
18
19 import org.jahia.content.ContentDefinition;
20 import org.jahia.content.ContentObject;
21 import org.jahia.content.PageDefinitionKey;
22 import org.jahia.exceptions.JahiaException;
23 import org.jahia.registries.ServicesRegistry;
24 import org.jahia.services.acl.ACLNotFoundException;
25 import org.jahia.services.acl.ACLResourceInterface;
26 import org.jahia.services.acl.JahiaBaseACL;
27 import org.jahia.services.version.ContentObjectEntryState;
28 import org.jahia.services.version.EntryLoadRequest;
29
30 import java.util.Comparator JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.io.Serializable JavaDoc;
33
34
35 /**
36  * This class is used to keep the page definitions information stored
37  * in the database. Each time a page definition is loaded it's information
38  * are stored into the cache using this class. <br/>
39  * <br/>
40  * This class knows how to save it's content, but it's not an automatic process
41  * to avoid one database access for each little change made to the page data.
42  * Therefore a call to the {@link #commitChanges() commitChanges()} method is
43  * needed to save the update information to the database.
44  *
45  * @author Eric Vassalli
46  * @author Fulco Houkes
47  * @author Khue Nguyen
48  * @version 2.0
49  */

50 public class JahiaPageDefinition extends ContentDefinition implements Comparator JavaDoc,
51         ACLResourceInterface, Serializable JavaDoc {
52
53     private static org.apache.log4j.Logger logger =
54             org.apache.log4j.Logger.getLogger (JahiaPageDefinition.class);
55
56     static {
57         ContentDefinition.registerType (PageDefinitionKey.PAGE_TYPE,
58                 JahiaPageDefinition.class.getName ());
59     }
60
61     private static final String JavaDoc CLASS_NAME = JahiaPageDefinition.class.getName ();
62
63     // Properties names
64
protected static final String JavaDoc ACLID_PROP = "acl_id";
65
66     private int mID;
67     private int mSiteID;
68     private String JavaDoc mName;
69     private String JavaDoc mSourcePath; // JSP source path
70
private boolean mAvailable; // Available or not for a user in the administration manager
71
private String JavaDoc mImage;
72
73     private int mTempSiteID;
74     private String JavaDoc mTempName;
75     private String JavaDoc mTempSourcePath;
76     private boolean mTempAvailable;
77     private String JavaDoc mTempImage;
78
79     private Properties JavaDoc mProps = new Properties JavaDoc ();
80
81     private boolean mDataChanged;
82
83     //-------------------------------------------------------------------------
84
/**
85      * Default constructor.
86      *
87      * @param int ID Unique identification number of the page definition.
88      * @param int jahiaID Jahia site unique identification number
89      * @param String name Name of the page definition.
90      * @param String sourcePath Source path of the page definition
91      * @param boolean isAvailable Not available to users if false
92      * @param String image A thumbnail
93      */

94     protected JahiaPageDefinition (int ID, int siteID, String JavaDoc name,
95                                    String JavaDoc sourcePath, boolean isAvailable,
96                                    String JavaDoc image) {
97         super (new PageDefinitionKey (ID));
98         mID = ID;
99         mSiteID = siteID;
100
101         if (name == null) {
102             name = "Untitled-" + ID;
103         }
104         mName = name;
105
106         if (sourcePath == null) {
107             sourcePath = "<no source path>";
108         }
109         mSourcePath = sourcePath;
110
111         mAvailable = isAvailable;
112
113         if (image == null) {
114             image = "";
115         }
116         mImage = image;
117
118         // temp variables.
119
mTempSiteID = siteID;
120         mTempName = name;
121         mTempSourcePath = sourcePath;
122         mTempAvailable = isAvailable;
123         mTempImage = image;
124
125         mDataChanged = false;
126     }
127
128     /**
129      * @param IDInType
130      *
131      * @return
132      */

133     public static ContentDefinition getChildInstance (String JavaDoc IDInType) {
134
135         try {
136             return ServicesRegistry.getInstance ().getJahiaPageTemplateService ()
137                     .lookupPageTemplate (Integer.parseInt (IDInType));
138         } catch (JahiaException je) {
139             logger.debug ("Error retrieving ContentDefinition instance for id : " + IDInType,
140                     je);
141         }
142         return null;
143     }
144
145     //-------------------------------------------------------------------------
146
/**
147      * Commit all the previous changes to the database.
148      *
149      * @throws JahiaException Throws a JahiaException if the data could not
150      * be saved successfully into the database.
151      */

152     public void commitChanges ()
153             throws JahiaException {
154         // commit only if the data has really changed
155
if (mDataChanged) {
156
157             mSiteID = mTempSiteID;
158             mName = mTempName;
159             mSourcePath = mTempSourcePath;
160             mAvailable = mTempAvailable;
161             mImage = mTempImage;
162             ServicesRegistry.getInstance().getJahiaPageTemplateService().updatePageTemplate (this);
163             mDataChanged = false;
164         }
165     }
166
167
168     public void disableChanges () {
169         if (mDataChanged) {
170             mTempSiteID = mSiteID;
171             mTempName = mName;
172             mTempSourcePath = mSourcePath;
173             mTempAvailable = mAvailable;
174             mTempImage = mImage;
175         }
176     }
177
178     //-------------------------------------------------------------------------
179
/**
180      * Return the page definition unique identification number.
181      *
182      * @return The identification number.
183      */

184     public final int getID () {
185         return mID;
186     }
187
188
189     //-------------------------------------------------------------------------
190
/**
191      * Get the image path
192      *
193      * @return String path to the image.
194      */

195     public String JavaDoc getImage () {
196         return mImage;
197     }
198
199
200     //-------------------------------------------------------------------------
201
/**
202      * Return the site unique identification number.
203      *
204      * @return The site identification number.
205      */

206     public final int getJahiaID () {
207         return mSiteID;
208     }
209
210
211     //--------------------------------------------------------------------------
212
/**
213      * Return the page definition name.
214      *
215      * @return The page definition name.
216      */

217     public final String JavaDoc getName () {
218         return mName;
219     }
220
221     /**
222      * @param contentObject
223      * @param entryState
224      *
225      * @return
226      */

227     public String JavaDoc getTitle (ContentObject contentObject,
228                             ContentObjectEntryState entryState) {
229         try {
230             return ((ContentPage) contentObject)
231                     .getTitle (new EntryLoadRequest (entryState));
232         } catch (Throwable JavaDoc t) {
233             logger.debug ("Exception retrieving Definition Title", t);
234         }
235         return null;
236     }
237
238     //-------------------------------------------------------------------------
239
/**
240      * Return the JSP file associated with the page definition.
241      *
242      * @return The source path.
243      */

244     public final String JavaDoc getSourcePath () {
245         return mSourcePath;
246     }
247
248
249     //-------------------------------------------------------------------------
250
/**
251      * Get the Available status
252      *
253      * @return Return true if the page definition is available or false if
254      * it's not.
255      */

256     public final boolean isAvailable () {
257         return mAvailable;
258     }
259
260     //-------------------------------------------------------------------------
261
/**
262      * Set the image's path
263      *
264      * @param value The new image path.
265      */

266     public void setImage (String JavaDoc value) {
267         mTempImage = value;
268         mDataChanged = true;
269     }
270
271
272     //-------------------------------------------------------------------------
273
/**
274      * Set the Jahia ID.
275      *
276      * @param value The new Jahia ID.
277      */

278     public void setJahiaID (int value) {
279         mTempSiteID = value;
280         mDataChanged = true;
281     }
282
283
284     //-------------------------------------------------------------------------
285
/**
286      * Set the page definition name.
287      *
288      * @param value The new page definition name.
289      */

290     public void setName (String JavaDoc value) {
291         mTempName = value;
292         mDataChanged = true;
293     }
294
295     //-------------------------------------------------------------------------
296
/**
297      * Set the source path.
298      *
299      * @param value The new source path.
300      */

301     public void setSourcePath (String JavaDoc value) {
302         mTempSourcePath = value;
303         mDataChanged = true;
304     }
305
306     //-------------------------------------------------------------------------
307
/**
308      * Set the available status.
309      *
310      * @param value Set the new available status. True the Definition page
311      * will be available, false it won't.
312      */

313     public void setAvailable (boolean value) {
314         mTempAvailable = value;
315         mDataChanged = true;
316     }
317
318     //-------------------------------------------------------------------------
319
/**
320      * Return the ACL object.
321      *
322      * @return Return the ACL.
323      */

324     public final JahiaBaseACL getACL () {
325
326         if (mProps == null)
327             return null;
328
329         String JavaDoc value = mProps.getProperty (ACLID_PROP);
330         if (value == null)
331             return null;
332
333         JahiaBaseACL acl = null;
334         try {
335             acl = new JahiaBaseACL (Integer.parseInt (value));
336         } catch (ACLNotFoundException ex) {
337             ex.printStackTrace ();
338         } catch (Throwable JavaDoc t) {
339             t.printStackTrace ();
340         }
341         return acl;
342     }
343
344     //-------------------------------------------------------------------------
345
/**
346      * Return the ACL id.
347      *
348      * @return int the ACL id.
349      */

350     public final int getAclID () {
351
352         if (mProps == null)
353             return -1;
354
355         String JavaDoc value = mProps.getProperty (ACLID_PROP);
356         if (value == null)
357             return -1;
358
359         JahiaBaseACL acl = null;
360         try {
361             acl = new JahiaBaseACL (Integer.parseInt (value));
362             return acl.getID ();
363         } catch (ACLNotFoundException ex) {
364             ex.printStackTrace ();
365         } catch (Throwable JavaDoc t) {
366             t.printStackTrace ();
367         }
368         return -1;
369     }
370
371     //-------------------------------------------------------------------------
372
/**
373      * Set the acl id.
374      *
375      * @param int id, the acl id
376      */

377     protected final void setACL (int id) {
378         if (mProps == null) {
379             logger.debug ("ACL cannot be set because properties is null");
380             return;
381         }
382         mProps.setProperty (ACLID_PROP, String.valueOf (id));
383         mDataChanged = true;
384     }
385
386     //-------------------------------------------------------------------------
387
/**
388      * Compare between two objects, sort by their name
389      *
390      * @param Object
391      * @param Object
392      *
393      * @author NK
394      */

395     public int compare (Object JavaDoc c1, Object JavaDoc c2) throws ClassCastException JavaDoc {
396
397         return ((JahiaPageDefinition) c1)
398                 .getName ().toLowerCase ()
399                 .compareTo (((JahiaPageDefinition) c2).getName ().toLowerCase ());
400
401     }
402
403
404
405
406     //--------------------------------------------------------------------------
407
/**
408      * Set the properties
409      *
410      * @param Properties properties
411      */

412     public void setProperties (Properties JavaDoc props) {
413         mProps = props;
414     }
415
416     //--------------------------------------------------------------------------
417
/**
418      * Return the properties
419      *
420      * @return Properties the properties
421      */

422     public Properties JavaDoc getProperties () {
423         return mProps;
424     }
425
426     //--------------------------------------------------------------------------
427
/**
428      * Return a property as String
429      *
430      * @param String , the property name, null if not found
431      */

432     public String JavaDoc getProperty (String JavaDoc name) {
433         if (mProps != null) {
434             return mProps.getProperty (name);
435         } else {
436             return null;
437         }
438     }
439
440     //--------------------------------------------------------------------------
441
/**
442      * Set a property
443      *
444      * @param String , the property name
445      * @param String , the property value as String
446      */

447     public void setProperty (String JavaDoc name, String JavaDoc value) {
448         if (mProps != null) {
449             mProps.setProperty (name, value);
450         }
451     }
452
453
454 } // end JahiaPageDefinition
455
Popular Tags