1 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 ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 30 34 public class TemplateManager extends ObservedManager { 35 36 39 private Map cachedContent = new Hashtable (); 40 41 44 private List visibleTemplates = new ArrayList (); 45 46 49 protected void onRegister(Content node) { 50 try { 51 log.info("Config : loading Template info - " + node.getHandle()); 53 Collection children = collectChildren(node); 55 56 if ((children != null) && !(children.isEmpty())) { 57 Iterator templates = children.iterator(); 58 cacheContent(templates); 59 } 60 61 log.info("Config : Template info loaded - " + node.getHandle()); } 63 catch (Exception re) { 64 log.error("Config : Failed to load Template info - " + node.getHandle()); log.error(re.getMessage(), re); 66 } 67 68 } 69 70 protected void onClear() { 71 this.cachedContent.clear(); 72 this.visibleTemplates.clear(); 73 } 74 75 85 public Template getInfo(String key) { 86 return (Template) cachedContent.get(key); 87 } 88 89 99 public Template getInfo(String key, String 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 118 private void addTemplatesToCache(Iterator templates, List 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 []{ti.getName()})); } 131 132 } 133 } 134 135 139 private void cacheContent(Iterator templates) { 140 if (templates != null) { 141 addTemplatesToCache(templates, visibleTemplates); 142 } 143 } 144 145 151 private Collection collectChildren(Content cnt) { 152 Collection children = cnt.getChildren(ItemType.CONTENTNODE); 154 155 Collection subFolders = cnt.getChildren(ItemType.CONTENT); 157 if ((subFolders != null) && !(subFolders.isEmpty())) { 158 159 Iterator it = subFolders.iterator(); 160 while (it.hasNext()) { 161 Content subCnt = (Content) it.next(); 162 Collection 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 178 public Iterator getAvailableTemplates(AccessManager accessManager) { 179 List templateList = new ArrayList (); 180 Iterator 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 194 public Iterator getAvailableTemplates() { 195 return visibleTemplates.iterator(); 196 } 197 198 201 public static TemplateManager getInstance() { 202 return (TemplateManager) FactoryUtil.getSingleton(TemplateManager.class); 203 } 204 205 } 206 | Popular Tags |