KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > containers > JahiaContainerDefinition


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 //
14
// JahiaContainerDefinition
15
// EV 27.12.2000
16
//
17
// getID
18
// getJahiaID
19
// getPageDefID
20
// getName
21
// getTitle
22
// getStructure
23
// setID
24
//
25
// structureChanged( structure, jData )
26
// setStructure( structure, jData )
27
//
28

29 package org.jahia.data.containers;
30
31 import java.io.Serializable JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 import org.apache.log4j.Logger;
39 import org.jahia.content.ContainerDefinitionKey;
40 import org.jahia.content.ContentDefinition;
41 import org.jahia.content.ContentObject;
42 import org.jahia.data.fields.JahiaFieldDefinition;
43 import org.jahia.exceptions.JahiaException;
44 import org.jahia.resourcebundle.ResourceBundleMarker;
45 import org.jahia.services.containers.ContentContainer;
46 import org.jahia.services.containers.ContentContainerList;
47 import org.jahia.services.pages.ContentPage;
48 import org.jahia.services.version.ContentObjectEntryState;
49 import org.jahia.services.version.EntryLoadRequest;
50 import org.jahia.utils.LanguageCodeConverters;
51
52 /**
53  *
54  * <p>Title: A container definition defines name, title and references to
55  * structure and page references for an actual containerList. </p>
56  * <p>Description: This class is used by Jahia to determine the name of the
57  * containerList to create, the reference to the structure, its properties,
58  * etc... </p>
59  * <p>Copyright: Copyright (c) 2002</p>
60  * <p>Company: Jahia Ltd</p>
61  * @author Eric Vassalli
62  * @version 1.0
63  */

64
65 public class JahiaContainerDefinition extends ContentDefinition implements Serializable JavaDoc {
66
67     private static Logger logger = Logger.getLogger(JahiaFieldDefinition.class);
68
69     static {
70         ContentDefinition.registerType(ContainerDefinitionKey.CONTAINER_TYPE,
71                                    JahiaContainerDefinition.class.getName());
72     }
73
74     private int ID;
75     private int jahiaID;
76     private String JavaDoc name;
77
78     private ContainerEditView editView; // the edit view definition
79

80     private Properties JavaDoc ctnDefProperties = null;
81
82     /**
83      * @associates JahiaContainerSubDefinition
84      */

85     private Hashtable JavaDoc subDefs;
86
87
88
89     /***
90         * constructor
91         * EV 24.11.2000
92         *
93         */

94     public JahiaContainerDefinition( int anID,
95                                         int aJahiaID,
96                                         String JavaDoc aName,
97                                         Hashtable JavaDoc subDefinitions )
98     {
99         super(new ContainerDefinitionKey(anID));
100         this.ID = anID;
101         this.jahiaID = aJahiaID;
102         this.name = aName;
103         this.subDefs = subDefinitions;
104     } // end constructor
105

106     /**
107      * No arg constructor required for serialization support.
108      */

109     protected JahiaContainerDefinition () {
110
111     }
112
113     /***
114         * accessor methods
115         * EV 24.11.2000
116         *
117         */

118     public int getID() { return ID; }
119     public int getJahiaID() { return jahiaID; }
120     public String JavaDoc getName() { return name; }
121     public Hashtable JavaDoc getSubDefs() { return subDefs; }
122
123     public void setID( int anID ) {
124         this.ID = anID;
125         setObjectKey(new ContainerDefinitionKey(anID));
126     }
127
128     public void setName( String JavaDoc aName ) { this.name = aName; }
129
130     public static ContentDefinition getChildInstance(String JavaDoc IDInType) {
131
132         try {
133             return org.jahia.registries.JahiaContainerDefinitionsRegistry
134                     .getInstance().getDefinition(Integer.parseInt(IDInType));
135
136         } catch (JahiaException je) {
137             logger.debug("Error retrieving ContentDefinition instance for id : " + IDInType, je);
138         }
139         return null;
140     }
141
142     /**
143      * ContentDefinitionInterface implementation
144      *
145      * @param contentObject
146      * @param entryState
147      * @return
148      */

149     public String JavaDoc getTitle(ContentObject contentObject,
150                            ContentObjectEntryState entryState) {
151         String JavaDoc value = "";
152         int pageID = -1;
153
154         if ( contentObject instanceof ContentContainer ){
155             pageID = ((ContentContainer)contentObject).getPageID();
156         } else {
157             pageID = ((ContentContainerList)contentObject).getPageID();
158         }
159
160         try {
161             ContentPage contentPage = ContentPage.getPage(pageID);
162             EntryLoadRequest loadRequest = new EntryLoadRequest(entryState);
163             int pageDefID = contentPage.getPageTemplateID(loadRequest);
164             return getTitle(pageDefID,
165                             LanguageCodeConverters
166                             .languageCodeToLocale(entryState.getLanguageCode()));
167         } catch ( Throwable JavaDoc t ){
168             t.printStackTrace();
169         }
170         return value;
171     }
172
173
174     /* getTitle **************************************************************/
175     public String JavaDoc getTitle( int pageDefID ) {
176         JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID );
177         if (theSubDef != null) {
178             return theSubDef.getTitle();
179         } else {
180             return "";
181         }
182     }
183
184     public String JavaDoc getTitle( int pageDefID , Locale JavaDoc locale) {
185         String JavaDoc title = "";
186
187         title = getTitle(pageDefID);
188         ResourceBundleMarker resMarker = ResourceBundleMarker.parseMarkerValue(title);
189         if ( resMarker == null ){
190             return title;
191         }
192         try {
193             title = resMarker.getValue(locale);
194         } catch ( Throwable JavaDoc t ){
195             title = this.getName();
196         }
197         return title;
198     }
199
200     /* getStructure **********************************************************/
201     public Enumeration JavaDoc getStructure( String JavaDoc dummy, int pageDefID ) {
202         return getStructure( "", pageDefID, JahiaContainerStructure.ALL_TYPES );
203     }
204
205
206     /* getStructure (typeFlag) ***********************************************/
207     public Enumeration JavaDoc getStructure( String JavaDoc dummy, int pageDefID, int typeFlag ) {
208         Vector JavaDoc structure;
209         JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID );
210         if (theSubDef != null) {
211             structure = theSubDef.getStructure();
212         } else {
213             return (new Vector JavaDoc()).elements();
214         }
215         Vector JavaDoc out = new Vector JavaDoc();
216         for (int i=0; i < structure.size(); i++) {
217             JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.elementAt(i);
218             if ((theStructure.getObjectType() & typeFlag) != 0) {
219                 out.add( theStructure );
220             }
221         }
222         return out.elements();
223     }
224
225     /* setTitle **************************************************************/
226     public void setTitle( String JavaDoc title, int pageDefID )
227     throws JahiaException
228     {
229         logger.debug("Setting title : " + title + " for pagedef " + pageDefID );
230         JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID );
231         if (theSubDef == null) {
232             logger.debug("Creating a new list for pagedef " + pageDefID );
233             theSubDef = createSubDef( pageDefID );
234         } else {
235             logger.debug("Using pagedef " + theSubDef.getPageDefID() + " 's list" );
236         }
237         theSubDef.setTitle( title );
238     }
239
240     /* setStructure **********************************************************/
241     public void setStructure( Vector JavaDoc structure, int pageDefID )
242     throws JahiaException
243     {
244         JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID );
245         if (theSubDef == null) {
246             theSubDef = createSubDef( pageDefID );
247         }
248         theSubDef.setStructure( structure );
249     }
250
251     /* composeStructure ******************************************************/
252     public void composeStructure( Vector JavaDoc structure, int pageDefID )
253     throws JahiaException
254     {
255         JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID );
256         if (theSubDef == null) {
257             theSubDef = createSubDef( pageDefID );
258         }
259         theSubDef.composeStructure( structure );
260     }
261
262
263     /* getSubDef *************************************************************/
264     private JahiaContainerSubDefinition getSubDef( int pageDefID )
265     {
266         return (JahiaContainerSubDefinition) subDefs.get( new Integer JavaDoc(pageDefID) );
267     } // end getSubDef
268

269
270
271     /* createSubDef **********************************************************/
272     private JahiaContainerSubDefinition createSubDef( int pageDefID )
273     throws JahiaException
274     {
275         JahiaContainerSubDefinition theSubDef = new JahiaContainerSubDefinition( 0, pageDefID, "", null );
276         if (subDefs == null) {
277             subDefs = new Hashtable JavaDoc();
278         }
279         subDefs.put( new Integer JavaDoc(theSubDef.getPageDefID()), theSubDef );
280         return theSubDef;
281     } // end createSubDef
282

283
284
285     /* findFieldInStructure *************************************************/
286     public JahiaFieldDefinition findFieldInStructure( String JavaDoc fieldName, int pageDefID )
287     {
288         Enumeration JavaDoc structure = getStructure( "", pageDefID );
289         if (fieldName != null) {
290             while (structure.hasMoreElements()) {
291                 JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.nextElement();
292                 if ((theStructure.getObjectType() & JahiaContainerStructure.JAHIA_FIELD) != 0) {
293                     JahiaFieldDefinition theFieldDef = (JahiaFieldDefinition) theStructure.getObjectDef();
294                     if (fieldName.equals(theFieldDef.getName())) {
295                         return theFieldDef;
296                     }
297                 }
298             }
299         }
300         return null;
301     }
302
303
304     /* findContainerInStructure **********************************************/
305     public JahiaContainerDefinition findContainerInStructure( String JavaDoc containerName, int pageDefID )
306     {
307         Enumeration JavaDoc structure = getStructure( "", pageDefID );
308         if (containerName != null) {
309             while (structure.hasMoreElements()) {
310                 JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.nextElement();
311                 if ((theStructure.getObjectType() & JahiaContainerStructure.JAHIA_CONTAINER) != 0) {
312                     JahiaContainerDefinition theContainerDef = (JahiaContainerDefinition) theStructure.getObjectDef();
313                     if (containerName.equals(theContainerDef.getName())) {
314                         return theContainerDef;
315                     }
316                 }
317             }
318         }
319         return null;
320     }
321
322
323     /* structureChanged ******************************************************/
324     public synchronized boolean structureChanged( Vector JavaDoc struct, int pageDefID )
325     throws JahiaException
326     {
327         // gets structure
328
Vector JavaDoc structure;
329         JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID );
330         if (theSubDef != null) {
331             structure = theSubDef.getStructure();
332         } else {
333             structure = null;
334         }
335
336         if ((struct == null) || (structure == null)) {
337             // compares null
338
logger.warn(name + ":Structure non-existant in memory, must load from database");
339             return true;
340         } else if (struct.size() != structure.size()) {
341             // compares sizes
342
logger.warn(name + ":Size are not equal (cur=" +
343                                  Integer.toString(structure.size()) + ", new=" + Integer.toString(struct.size()) + ")");
344             return true;
345         } else {
346             // compares fields
347
for (int i=0; i < structure.size(); i++)
348             {
349                 JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.elementAt(i);
350                 JahiaContainerStructure aStructure = new JahiaContainerStructure(
351                                     (String JavaDoc)struct.elementAt(i), theSubDef.getID(), i, pageDefID );
352                 if (!theStructure.equals( aStructure )) {
353                     logger.warn(name + ":Size equal, fields not equal.");
354                     return true;
355                 }
356             }
357             // JahiaConsole.println("JahiaContainerDefinition.structureChanged", name + ":Size equal, fields equal.");
358
return false;
359         }
360     } // end structureChanged
361

362     public void setProperties(Properties JavaDoc newProperties) {
363         this.ctnDefProperties = newProperties;
364     }
365
366     public Properties JavaDoc getProperties() {
367         return this.ctnDefProperties;
368     }
369
370     public String JavaDoc getProperty(String JavaDoc propertyName) {
371         if (this.ctnDefProperties != null) {
372             return this.ctnDefProperties.getProperty(propertyName);
373         } else {
374             return null;
375         }
376     }
377
378     public void setProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
379         if (this.ctnDefProperties != null) {
380             this.ctnDefProperties.setProperty(propertyName, propertyValue);
381         } else {
382             logger.error("ERROR: Properties object is not defined, ignoring property insertion.");
383         }
384     }
385
386     /**
387      * Merge a set of properties passed in parameters with the internal
388      * properties set. Returns true if the resulting set of internal properties
389      * must be serialized.
390      * @param newProperties
391      * @return true if the result of the merge is a different set of properties
392      * that needs to be serialized.
393      */

394     public boolean propertiesChanged(Properties JavaDoc newProperties) {
395         boolean mustSave = false;
396         Enumeration JavaDoc newPropKeys = newProperties.keys();
397         while (newPropKeys.hasMoreElements()) {
398             String JavaDoc curNewPropName = (String JavaDoc) newPropKeys.nextElement();
399             String JavaDoc curNewPropValue = newProperties.getProperty(curNewPropName);
400             if (this.ctnDefProperties.containsKey(curNewPropName)) {
401                 String JavaDoc internalPropValue = this.ctnDefProperties.getProperty(curNewPropName);
402                 if (!internalPropValue.equals(curNewPropValue)) {
403                     // properties are not equal
404
mustSave = true;
405                 }
406             } else {
407                 // this is a new property.
408
mustSave = true;
409             }
410         }
411         return mustSave;
412     }
413
414     //--------------------------------------------------------------------------
415
/**
416      * Set the Container Edit View
417      *
418      * @param ContainerEditView
419      */

420     public void setContainerEditView(ContainerEditView anEditView)
421     {
422         this.editView = anEditView;
423     }
424
425     //--------------------------------------------------------------------------
426
/**
427      * Return the Container Edit View
428      * Can be null
429      */

430     public ContainerEditView getContainerEditView()
431     {
432         return this.editView;
433     }
434
435     /***
436         * setStructure
437         * EV 01.01.2001
438         *
439         */

440 /*
441     public synchronized void setStructure( Vector struct, int pageDefID, int jahiaID )
442     throws JahiaException
443     {
444         this.structure = new Vector();
445         for (int i=0; i < struct.size(); i++)
446         {
447             this.structure.add( new JahiaContainerStructure(
448                                 (String)struct.elementAt(i), this.getID(), i, pageDefID, jahiaID ) );
449
450         }
451     } // end setStructure
452 */

453
454
455 } // end JahiaContainerDefinition
456
Popular Tags