1 13 package info.magnolia.cms.beans.config; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ContentHandler; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.security.AccessManager; 20 import info.magnolia.cms.security.Permission; 21 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 import javax.jcr.PathNotFoundException; 30 import javax.jcr.RepositoryException; 31 32 import org.apache.log4j.Logger; 33 34 35 39 public class Template { 40 41 44 private static Logger log = Logger.getLogger(Template.class); 45 46 private static List visibleTemplates = new ArrayList (); 47 48 private static Map cachedContent = new Hashtable (); 49 50 53 private String name; 54 55 58 private String path; 59 60 private Map alternativePaths; 61 62 65 private String type; 66 67 70 private boolean visible; 71 72 75 private String description; 76 77 private String image; 78 79 private String title; 80 81 private String location; 82 83 86 public static void init() { 87 log.info("Config : initializing Template info"); Template.cachedContent.clear(); 89 Template.visibleTemplates.clear(); 90 } 91 92 public static void update(String modulePath) { 93 HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG); 94 try { 95 log.info("Config : loading Template info - " + modulePath); Content startPage = configHierarchyManager.getContent(modulePath); 97 Collection children = startPage.getContent("Templates") .getChildren(ItemType.CONTENTNODE, ContentHandler.SORT_BY_SEQUENCE); 99 100 if ((children != null) && !(children.isEmpty())) { 101 Iterator templates = children.iterator(); 102 Template.cacheContent(templates); 103 } 104 log.info("Config : Template info loaded - " + modulePath); } 106 catch (RepositoryException re) { 107 log.error("Config : Failed to load Template info - " + modulePath); log.error(re.getMessage(), re); 109 } 110 } 111 112 public static void reload() { 113 log.info("Config : re-initializing Template info"); Template.init(); 115 update("modules/templating"); } 117 118 122 public static Iterator getAvailableTemplates() { 123 return Template.visibleTemplates.iterator(); 124 } 125 126 130 public static Iterator getAvailableTemplates(AccessManager accessManager) { 131 List templateList = new ArrayList (); 132 Iterator it = Template.visibleTemplates.iterator(); 133 while (it.hasNext()) { 134 Template template = (Template) it.next(); 135 if (accessManager.isGranted(template.getLocation(), Permission.READ)) { 136 templateList.add(template); 137 } 138 } 139 return templateList.iterator(); 140 } 141 142 146 private static void cacheContent(Iterator templates) { 147 if (templates != null) { 148 addTemplatesToCache(templates, Template.visibleTemplates); 149 } 150 } 151 152 157 private static void addTemplatesToCache(Iterator templates, List visibleTemplates) { 158 while (templates.hasNext()) { 159 Content c = (Content) templates.next(); 160 try { 161 Template ti = new Template(); 162 ti.name = c.getNodeData("name").getValue().getString(); ti.path = c.getNodeData("path").getValue().getString(); Template.addAlternativePaths(c, ti); 165 ti.type = c.getNodeData("type").getValue().getString(); ti.visible = c.getNodeData("visible").getBoolean(); ti.title = c.getNodeData("title").getString(); ti.description = c.getNodeData("description").getString(); ti.image = c.getNodeData("image").getString(); Template.cachedContent.put(ti.name, ti); 171 ti.setLocation(c.getHandle()); 172 if (ti.visible) { 173 visibleTemplates.add(ti); 174 } 175 } 176 catch (RepositoryException re) { 177 log.fatal("Failed to cache TemplateInfo"); } 179 } 180 } 181 182 187 private static void addAlternativePaths(Content node, Template ti) { 188 try { 189 Content cl = node.getContent("SubTemplates"); Iterator it = cl.getChildren().iterator(); 191 ti.alternativePaths = new Hashtable (); 192 while (it.hasNext()) { 193 Content c = (Content) it.next(); 194 ti.alternativePaths.put(c.getNodeData("extension").getString(), c.getNodeData("path").getString()); } 196 } 197 catch (PathNotFoundException e) { 198 } 200 catch (RepositoryException re) { 201 log.error("RepositoryException caught while loading alternative templates path configuration: " + re.getMessage(), re); 203 } 204 } 205 206 216 public static Template getInfo(String key) { 217 return (Template) Template.cachedContent.get(key); 218 } 219 220 223 public String getName() { 224 return this.name; 225 } 226 227 230 public String getTitle() { 231 return this.title; 232 } 233 234 237 public String getDescription() { 238 return this.description; 239 } 240 241 244 public String getPath() { 245 return this.path; 246 } 247 248 252 public String getPath(String extension) { 253 try { 254 String path = (String ) this.alternativePaths.get(extension); 255 if (path == null) { 256 return this.getPath(); 257 } 258 return path; 259 } 260 catch (Exception e) { 261 return this.getPath(); 262 } 263 } 264 265 268 public String getType() { 269 return this.type; 270 } 271 272 275 public String getImage() { 276 return this.image; 277 } 278 279 282 public boolean isVisible() { 283 return this.visible; 284 } 285 286 public String getLocation() { 287 return location; 288 } 289 290 public void setLocation(String location) { 291 this.location = location; 292 } 293 294 } 295 | Popular Tags |