KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > content > ContentDefinition


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 package org.jahia.content;
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.jahia.services.version.ContentObjectEntryState;
20 import org.jahia.services.version.EntryLoadRequest;
21 import java.io.Serializable JavaDoc;
22
23 /**
24  * Abstract implementation for Content Definition
25  *
26  * @author Khue Nguyen
27  */

28 public abstract class ContentDefinition implements Serializable JavaDoc {
29
30     private static org.apache.log4j.Logger logger =
31         org.apache.log4j.Logger.getLogger(ContentDefinition.class);
32
33     private static Map JavaDoc keyTypeClassNames = new HashMap JavaDoc();
34     private ObjectKey objectKey;
35
36     protected ContentDefinition(ObjectKey objectKey) {
37         this.objectKey = objectKey;
38     }
39
40     /**
41      * No arg constructor required for serialization support.
42      */

43     protected ContentDefinition() {
44     }
45
46     /**
47      * This method is reserved for class that derive from this one so that
48      * they can update their object key notably when they get an ID assigned
49      * from the database when the object is first created.
50      * @param objectKey the new object key to set.
51      */

52     protected void setObjectKey(ObjectKey objectKey) {
53         this.objectKey = objectKey;
54     }
55
56     /**
57      * Registers a new type for a sub class of ContentDefinition in the factory
58      * hash table. Use this if you want to be able to create instance of the
59      * new class by using a complete content object key. The new sub class MUST
60      * implement a static method with the following signature :
61      * public static Content getChildInstance(String IDInType);
62      *
63      * @param type the name of the type, usually defined as a constant in the
64      * equivalent ObjectKey sub class (such as ContentPageKey)
65      * @param className the name of the class. Can be obtained with a called
66      * similar to ContentObject.class.getName() (replacing the ContentObject
67      * with the subclass of course.
68      */

69     public static void registerType(String JavaDoc type, String JavaDoc className) {
70         keyTypeClassNames.put ( type, className );
71     }
72
73     /**
74      * Removes a type registration. See the registerType method for more details
75      * @param type the name of the type to unregister
76      */

77     public static void unregisterType(String JavaDoc type) {
78         keyTypeClassNames.remove(type);
79     }
80
81     /**
82      * Returns the object key that fully identifies the content object within
83      * a Jahia system.
84      * @return an ObjectKey class that uniquely identifies the object.
85      */

86     public ObjectKey getObjectKey() {
87         return objectKey;
88     }
89
90     /**
91      * Instance generator. Build an instance of the appropriate
92      * class corresponding to the DefinitionKey passed described.
93      *
94      * @param objectKey an ContentDefinitionKey instance for the Content Definition we want to retrieve
95      * an instance of.
96      * @returns a ContentDefinition sub class instance that corresponds to the given
97      * object key.
98      *
99      * @param objectKey , a ContentDefinitionKey subclass
100      * @param loadRequest
101      * @return
102      * @throws ClassNotFoundException
103      */

104     public static ContentDefinition getInstance (ObjectKey objectKey)
105     throws ClassNotFoundException JavaDoc {
106
107         ContentDefinition contentDefinition = null;
108         String JavaDoc type = objectKey.getType();
109         String JavaDoc idStr = objectKey.getIDInType();
110         if (!keyTypeClassNames.containsKey(type)) {
111             throw new ClassNotFoundException JavaDoc("No class defined for type [" +
112                                              type + "]");
113         }
114         try {
115             Class JavaDoc childClass = Class.forName( (String JavaDoc) keyTypeClassNames.
116                                              get(type));
117             Class JavaDoc[] childClassParameters = new Class JavaDoc[1];
118             childClassParameters[0] = String JavaDoc.class;
119             java.lang.reflect.Method JavaDoc childClassMethod = childClass.
120                 getMethod("getChildInstance", childClassParameters);
121             Object JavaDoc[] args = new Object JavaDoc[1];
122             args[0] = idStr;
123             contentDefinition = (ContentDefinition) childClassMethod.invoke(null, args);
124         } catch (ClassNotFoundException JavaDoc cnfe) {
125             logger.error("Error while creating instance of object " +
126                          objectKey, cnfe);
127         } catch (NoSuchMethodException JavaDoc nsme) {
128             logger.error("Error while creating instance of object " +
129                          objectKey, nsme);
130         } catch (SecurityException JavaDoc se) {
131             logger.error("Error while creating instance of object " +
132                          objectKey, se);
133         } catch (IllegalAccessException JavaDoc iae) {
134             logger.error("Error while creating instance of object " +
135                          objectKey, iae);
136         } catch (IllegalArgumentException JavaDoc iae2) {
137             logger.error("Error while creating instance of object " +
138                          objectKey, iae2);
139         } catch (InvocationTargetException JavaDoc ite) {
140             logger.error("Error while creating instance of object " +
141                          objectKey, ite);
142             logger.error(
143                 "Error while creating instance of object " + objectKey +
144                 ", target exception="
145                 , ite.getTargetException());
146         }
147         return contentDefinition;
148     }
149
150     /**
151      * Return the human readable title of this Content Definition for Content Object using this Definition
152      *
153      * @param contentObject, the contextual Content Object
154      *
155      * @param contentObject
156      * @param entryState
157      * @return
158      */

159     public abstract String JavaDoc getTitle(ContentObject contentObject,
160                                     ContentObjectEntryState entryState)
161         throws ClassNotFoundException JavaDoc;
162
163     /**
164      * @return a String containing the name of the definition, which must be
165      * unique in a site.
166      */

167     public abstract String JavaDoc getName();
168
169     /**
170      * Return the human readable title of this Content Definition for Content Object using this Definition
171      *
172      * @param contentObject, the contextual Content Object
173      *
174      * @param contentObject
175      * @param entryState
176      * @return
177      */

178     public static String JavaDoc getObjectTitle(ContentObject contentObject,
179                                   ContentObjectEntryState entryState)
180     throws ClassNotFoundException JavaDoc{
181
182         String JavaDoc title = "";
183         try {
184             ContentDefinition definition =
185                        ContentDefinition.getInstance(
186                        contentObject.getDefinitionKey(new EntryLoadRequest(entryState)));
187            title = definition.getTitle(contentObject,entryState);
188         } catch ( Throwable JavaDoc t ){
189
190         }
191         return title;
192     }
193 }
194
Popular Tags