KickJava   Java API By Example, From Geeks To Geeks.

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


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
// NK - 17 Dec. 2001 :
14
//
15

16 package org.jahia.services.homepages;
17
18 import java.util.Hashtable JavaDoc;
19
20 import org.jahia.exceptions.JahiaException;
21
22
23 /**
24  * A JahiaHomepageCopy holds information used to define homepage of type Copy.
25  * Home page definition of this type holds extra information like the original page to duplicate,
26  * the parent page id ( where to insert the duplicated page ), and a user or group reference
27  * used to duplicate right for the new user or group.
28  *
29  * @author Khue ng
30  * @see HomepageTypes.HOMEPAGE_COPY
31  * @version 1.0
32  */

33 public final class JahiaHomepageCopy extends JahiaHomepage
34 {
35     private static String JavaDoc CLASS_NAME = JahiaHomepageCopy.class.getName();
36     
37     // the page to copy
38
private static final String JavaDoc PAGEID = "pageid";
39     
40     // use this user to duplicate exact right for the new group or user.
41
private static final String JavaDoc USER_REF = "user_ref";
42     
43     // use this group to duplicate exact right for the new group or user.
44
private static final String JavaDoc GROUP_REF = "group_ref";
45     
46     // where to insert the duplicated page ( subtree )
47
private static final String JavaDoc PARENT_PAGEID = "parent_pageid";
48
49     // how deep to copy the subtree
50
private static final String JavaDoc COPY_DEPTH = "copy_depth";
51     
52     private JahiaHomepagesPersistance hpp;
53
54
55     /**
56      * Constructor
57      *
58      * @param Integer id, the unique identifier
59      * @param String name, the name
60      * @param String descr, the descr
61      * @param Integer type, the type
62      * @param String sitekey, the site key
63      * @param Hashtable props, the properties
64      * @param Integer aclID, the acl
65      */

66     JahiaHomepageCopy( Integer JavaDoc id,
67                         String JavaDoc name,
68                         String JavaDoc descr,
69                         Integer JavaDoc type,
70                         String JavaDoc siteKey,
71                         Hashtable JavaDoc props,
72                         Integer JavaDoc aclID ){
73     
74         super(id,name,descr,type,siteKey,props,aclID);
75     }
76
77
78     //-------------------------------------------------------------------------
79
/**
80      * Return the internal page id that refers to a Jahia Page.
81      *
82      * @return int the internal page id or -1 if not defined.
83      */

84     public int getPageID(){
85         Integer JavaDoc pageID = (Integer JavaDoc)props.get(PAGEID);
86         if ( pageID == null ){
87             return -1;
88         }
89         return pageID.intValue();
90     }
91     
92     //-------------------------------------------------------------------------
93
/**
94      * Set the page ID
95      *
96      * @param int the id
97      */

98     public void setPageID(int id){
99         props.put(PAGEID,new Integer JavaDoc(id));
100     }
101
102     //-------------------------------------------------------------------------
103
/**
104      * Return the user ref.
105      *
106      * @return String the user ref's key.
107      */

108     public String JavaDoc getUserRef(){
109         return (String JavaDoc)props.get(USER_REF);
110     }
111     
112     //-------------------------------------------------------------------------
113
/**
114      * Set the user ref
115      *
116      * @param String the user ref's key
117      */

118     public void setUserRef(String JavaDoc userKey) throws JahiaException{
119         props.put(USER_REF,userKey);
120     }
121
122     //-------------------------------------------------------------------------
123
/**
124      * Return the group ref.
125      *
126      * @return String the group ref's key.
127      */

128     public String JavaDoc getGroupRef(){
129         return (String JavaDoc)props.get(GROUP_REF);
130     }
131     
132     //-------------------------------------------------------------------------
133
/**
134      * Set the group ref
135      *
136      * @param String the group ref's key
137      */

138     public void setGroupRef(String JavaDoc groupKey) throws JahiaException{
139         props.put(GROUP_REF,groupKey);
140     }
141
142     //-------------------------------------------------------------------------
143
/**
144      * Return the id of the page to use as parent for the new homepage.
145      *
146      * @return int the parent page id, -1 if not defined.
147      */

148     public int getParentPageID(){
149         Integer JavaDoc id = (Integer JavaDoc)props.get(PARENT_PAGEID);
150         if ( id == null ){
151             return -1;
152         }
153         return id.intValue();
154     }
155     
156     //-------------------------------------------------------------------------
157
/**
158      * Set the parent page ID
159      *
160      * @param int the parent page id
161      */

162     public void setParentPageID(int pageID) throws JahiaException{
163         props.put(PARENT_PAGEID,new Integer JavaDoc(pageID));
164     }
165
166     //-------------------------------------------------------------------------
167
/**
168      * Return the copy depth.
169      * 0 = all tree.
170      * -1 = undefined.
171      * @return int the copy depth, -1 if not defined.
172      */

173     public int getCopyDepth(){
174         Integer JavaDoc depth = (Integer JavaDoc)props.get(COPY_DEPTH);
175         if ( depth == null ){
176             return -1;
177         }
178         return depth.intValue();
179     }
180
181     //-------------------------------------------------------------------------
182
/**
183      * Set the copy depth:
184      * 0, the entire tree.
185      * 1, only the original page.
186      * n, how deep to copy the subtree.
187      *
188      * @param int the copy mode
189      */

190     public void setCopyDepth(int depth) throws JahiaException{
191         if ( depth < 0 ){
192             throw new JahiaException( CLASS_NAME+".setCopyDepth()",
193                                         "Copy depth value must be bigger or equals to 0",
194                                         JahiaException.DATA_ERROR,
195                                         JahiaException.ERROR_SEVERITY);
196         }
197         
198         props.put(COPY_DEPTH,new Integer JavaDoc(depth));
199     }
200
201     //--------------------------------------------------------------------------
202
/**
203      * Save its state.
204      *
205      */

206     void save()
207     throws JahiaException {
208         
209         JahiaHomepagesPersistance.getInstance().save(this);
210         saveProperties();
211     }
212
213     //--------------------------------------------------------------------------
214
/**
215      * Delete.
216      *
217      */

218     void delete()
219     throws JahiaException {
220
221         JahiaHomepagesPersistance.getInstance().delete(getID());
222         deleteProperties();
223     }
224
225     //-------------------------------------------------------------------------
226
/**
227      * Load extra properties from storage
228      *
229      */

230     void loadProperties() throws JahiaException{
231         
232         if ( props == null )
233             props = new Hashtable JavaDoc();
234         
235         String JavaDoc value = null;
236
237         value = JahiaHomepagesPersistance.getInstance().getProperty(this,USER_REF);
238         if ( value != null )
239             props.put(USER_REF,value);
240
241         value = JahiaHomepagesPersistance.getInstance().getProperty(this,GROUP_REF);
242         if ( value != null )
243             props.put(GROUP_REF,value);
244
245         value = JahiaHomepagesPersistance.getInstance().getProperty(this,PAGEID);
246         if ( value != null ){
247             try {
248                 props.put(PAGEID,new Integer JavaDoc(value));
249             } catch ( Throwable JavaDoc t ){
250                 t.printStackTrace();
251             }
252         }
253
254         value = JahiaHomepagesPersistance.getInstance().getProperty(this,PARENT_PAGEID);
255         if ( value != null ){
256             try {
257                 props.put(PARENT_PAGEID,new Integer JavaDoc(value));
258             } catch ( Throwable JavaDoc t ){
259                 t.printStackTrace();
260             }
261         }
262
263         value = JahiaHomepagesPersistance.getInstance().getProperty(this,COPY_DEPTH);
264         if ( value != null ){
265             try {
266                 props.put(COPY_DEPTH,new Integer JavaDoc(value));
267             } catch ( Throwable JavaDoc t ){
268                 t.printStackTrace();
269             }
270         }
271
272     }
273
274     //-------------------------------------------------------------------------
275
/**
276      * Save extra properties in storage
277      *
278      */

279     void saveProperties() throws JahiaException{
280         
281         deleteProperties();
282         
283         String JavaDoc value = null;
284         
285         value = getUserRef();
286         if ( value != null )
287             JahiaHomepagesPersistance.getInstance().addProperty(this,USER_REF,value);
288
289         value = getGroupRef();
290         if ( value != null )
291             JahiaHomepagesPersistance.getInstance().addProperty(this,GROUP_REF,value);
292
293         value = Integer.toString(getPageID());
294         if ( value != null )
295             JahiaHomepagesPersistance.getInstance().addProperty(this,PAGEID,value);
296
297         value = Integer.toString(getParentPageID());
298         if ( value != null )
299             JahiaHomepagesPersistance.getInstance().addProperty(this,PARENT_PAGEID,value);
300
301         value = Integer.toString(getCopyDepth());
302         if ( value != null )
303             JahiaHomepagesPersistance.getInstance().addProperty(this,COPY_DEPTH,value);
304
305     }
306
307     //-------------------------------------------------------------------------
308
/**
309      * Delete extra properties in storage
310      *
311      */

312     void deleteProperties() throws JahiaException{
313         
314         JahiaHomepagesPersistance.getInstance().deleteProperties(this);
315     }
316
317
318     //--------------------------------------------------------------------------
319
/**
320      * Return a string representation of the home page and it's internal state.
321      *
322      * @return A string representation of this home page.
323      */

324     public String JavaDoc toString (){
325         
326         StringBuffer JavaDoc buff= new StringBuffer JavaDoc("String rep. of a ");
327         buff.append(CLASS_NAME);
328         buff.append(" bean :\n");
329         buff.append(" id :");
330         buff.append(getID());
331         // TODO . complete
332
return buff.toString();
333         
334     }
335
336     //--------------------------------------------------------------------------
337
/**
338      * Return a clone.
339      *
340      * @return Object the clone.
341      */

342     public Object JavaDoc clone (){
343         
344         Hashtable JavaDoc hash = null;
345         if ( getProperties() != null ){
346             hash = new Hashtable JavaDoc();
347             if ( props.get(PAGEID) != null )
348                 hash.put(PAGEID,new Integer JavaDoc(getPageID()));
349             if ( props.get(PARENT_PAGEID) != null )
350                 hash.put(PARENT_PAGEID,new Integer JavaDoc(getParentPageID()));
351             if ( props.get(COPY_DEPTH) != null )
352                 hash.put(COPY_DEPTH,new Integer JavaDoc(getCopyDepth()));
353             if ( props.get(USER_REF) != null )
354                 hash.put(USER_REF,getUserRef());
355             if ( props.get(GROUP_REF) != null )
356                 hash.put(GROUP_REF,getGroupRef());
357
358         }
359         
360         JahiaHomepageCopy clone =
361                 new JahiaHomepageCopy( new Integer JavaDoc(getID()),
362                                         getName(),
363                                         getDescr(),
364                                         new Integer JavaDoc(getType()),
365                                         getSiteKey(),
366                                         hash,
367                                         new Integer JavaDoc(getAclID()) );
368         return clone;
369     }
370
371
372 }
373
Popular Tags