KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > TemplateManager


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.config;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.security.AccessManager;
18 import info.magnolia.cms.security.Permission;
19 import info.magnolia.cms.util.FactoryUtil;
20
21 import java.text.MessageFormat JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29
30 /**
31  * Manages the templates of the system
32  * @author philipp
33  */

34 public class TemplateManager extends ObservedManager {
35
36     /**
37      * The cached templates
38      */

39     private Map JavaDoc cachedContent = new Hashtable JavaDoc();
40
41     /**
42      * The templates visible in the templates selection
43      */

44     private List JavaDoc visibleTemplates = new ArrayList JavaDoc();
45
46     /**
47      * Called by the ObervedManager
48      */

49     protected void onRegister(Content node) {
50         try {
51             log.info("Config : loading Template info - " + node.getHandle()); //$NON-NLS-1$
52

53             // It makes possibly to use templates defined within subfolders of /module/templating/Templates
54
Collection JavaDoc children = collectChildren(node);
55
56             if ((children != null) && !(children.isEmpty())) {
57                 Iterator JavaDoc templates = children.iterator();
58                 cacheContent(templates);
59             }
60
61             log.info("Config : Template info loaded - " + node.getHandle()); //$NON-NLS-1$
62
}
63         catch (Exception JavaDoc re) {
64             log.error("Config : Failed to load Template info - " + node.getHandle()); //$NON-NLS-1$
65
log.error(re.getMessage(), re);
66         }
67
68     }
69
70     protected void onClear() {
71         this.cachedContent.clear();
72         this.visibleTemplates.clear();
73     }
74
75     /**
76      * Returns the cached content of the requested template. TemplateInfo properties:
77      * <ol>
78      * <li> title - title describing template</li>
79      * <li> type - jsp / servlet</li>
80      * <li> path - jsp / servlet path</li>
81      * <li> description - description of a template</li>
82      * </ol>
83      * @return TemplateInfo
84      */

85     public Template getInfo(String JavaDoc key) {
86         return (Template) cachedContent.get(key);
87     }
88
89     /**
90      * Returns the cached content of the requested template. TemplateInfo properties:
91      * <ol>
92      * <li> title - title describing template</li>
93      * <li> type - jsp / servlet</li>
94      * <li> path - jsp / servlet path</li>
95      * <li> description - description of a template</li>
96      * </ol>
97      * @return TemplateInfo
98      */

99     public Template getInfo(String JavaDoc key, String JavaDoc extension) {
100         Template template = (Template) cachedContent.get(key);
101
102         if (template == null) {
103             return null;
104         }
105         Template subtemplate = template.getSubTemplate(extension);
106         if (subtemplate != null) {
107             return subtemplate;
108         }
109
110         return template;
111     }
112
113     /**
114      * Adds templates definition to TemplatesInfo cache.
115      * @param templates iterator as read from the repository
116      * @param visibleTemplates List in with all visible templates will be added
117      */

118     private void addTemplatesToCache(Iterator JavaDoc templates, List JavaDoc visibleTemplates) {
119         while (templates.hasNext()) {
120             Content c = (Content) templates.next();
121
122             Template ti = new Template(c);
123             cachedContent.put(ti.getName(), ti);
124             if (ti.isVisible()) {
125                 visibleTemplates.add(ti);
126             }
127
128             if (log.isDebugEnabled()) {
129                 log.debug(MessageFormat.format("Registering template [{0}]", new Object JavaDoc[]{ti.getName()})); //$NON-NLS-1$
130
}
131
132         }
133     }
134
135     /**
136      * Load content of this template info page in a hash table caching at the system load, this will save lot of time on
137      * every request while matching template info.
138      */

139     private void cacheContent(Iterator JavaDoc templates) {
140         if (templates != null) {
141             addTemplatesToCache(templates, visibleTemplates);
142         }
143     }
144
145     /**
146      * Recursive search for content nodes contains template data (looks up subfolders)
147      * @author <a HREF="mailto:tm@touk.pl">Tomasz Mazan</a>
148      * @param cnt current folder to look for template's nodes
149      * @return collection of template's content nodes from current folder and descendants
150      */

151     private Collection JavaDoc collectChildren(Content cnt) {
152         // Collect template's content node - children of current node
153
Collection JavaDoc children = cnt.getChildren(ItemType.CONTENTNODE);
154
155         // Look into subfolders
156
Collection JavaDoc subFolders = cnt.getChildren(ItemType.CONTENT);
157         if ((subFolders != null) && !(subFolders.isEmpty())) {
158
159             Iterator JavaDoc it = subFolders.iterator();
160             while (it.hasNext()) {
161                 Content subCnt = (Content) it.next();
162                 Collection JavaDoc grandChildren = collectChildren(subCnt);
163
164                 if ((grandChildren != null) && !(grandChildren.isEmpty())) {
165                     children.addAll(grandChildren);
166                 }
167             }
168
169         }
170
171         return children;
172     }
173
174     /**
175      * Get templates collection after access control filter applied using specified AccessManager
176      * @return Collection list containing templates as Template objects
177      */

178     public Iterator JavaDoc getAvailableTemplates(AccessManager accessManager) {
179         List JavaDoc templateList = new ArrayList JavaDoc();
180         Iterator JavaDoc it = visibleTemplates.iterator();
181         while (it.hasNext()) {
182             Template template = (Template) it.next();
183             if (accessManager.isGranted(template.getLocation(), Permission.READ)) {
184                 templateList.add(template);
185             }
186         }
187         return templateList.iterator();
188     }
189
190     /**
191      * Get templates collection.
192      * @return Collection list containing templates as Template objects
193      */

194     public Iterator JavaDoc getAvailableTemplates() {
195         return visibleTemplates.iterator();
196     }
197
198     /**
199      * @return Returns the instance.
200      */

201     public static TemplateManager getInstance() {
202         return (TemplateManager) FactoryUtil.getSingleton(TemplateManager.class);
203     }
204
205 }
206
Popular Tags