1 13 package info.magnolia.module.templating; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.beans.config.Paragraph; 17 import info.magnolia.cms.beans.config.Template; 18 import info.magnolia.cms.core.Content; 19 import info.magnolia.cms.core.HierarchyManager; 20 import info.magnolia.cms.core.ItemType; 21 import info.magnolia.cms.module.Module; 22 import info.magnolia.cms.module.ModuleConfig; 23 import info.magnolia.cms.module.RegisterException; 24 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.jar.JarFile ; 28 29 import javax.jcr.RepositoryException; 30 import javax.jcr.observation.Event; 31 import javax.jcr.observation.EventIterator; 32 import javax.jcr.observation.EventListener; 33 import javax.jcr.observation.ObservationManager; 34 35 import org.apache.log4j.Logger; 36 37 38 44 public class Engine implements Module { 45 46 49 private static Logger log = Logger.getLogger(Engine.class); 50 51 54 private static final String ATTRIBUTE_BASE_PATH = "basePath"; 56 59 protected String moduleName; 60 61 64 protected String basePath; 65 66 71 public void register(String moduleName, String version, Content moduleNode, JarFile jar, int registerState) 72 throws RegisterException { 73 } 75 76 79 public void init(ModuleConfig config) { 80 this.moduleName = config.getModuleName(); 81 this.basePath = (String ) config.getInitParameters().get(ATTRIBUTE_BASE_PATH); 82 83 85 Store.getInstance().setStore(config.getLocalStore()); 86 87 log.info("Module: " + this.moduleName); log.info(this.moduleName + ": updating Template list"); Template.update(this.basePath); 90 log.info(this.moduleName + ": updating Paragraph list"); registerParagraphs(); 92 93 registerEventListeners(); 94 } 95 96 99 public void destroy() { 100 } 103 104 107 private void registerEventListeners() { 108 109 registerEventListeners("/" + this.basePath + "/Paragraphs", new EventListener() { 112 public void onEvent(EventIterator iterator) { 113 registerParagraphs(); 115 } 116 }); 117 118 registerEventListeners("/" + this.basePath + "/Templates", new EventListener() { 121 public void onEvent(EventIterator iterator) { 122 Template.reload(); 124 } 125 }); 126 } 127 128 133 private void registerEventListeners(String observationPath, EventListener listener) { 134 135 log.info("Registering event listener for path [" + observationPath + "]"); 137 try { 138 139 ObservationManager observationManager = ContentRepository 140 .getHierarchyManager(ContentRepository.CONFIG) 141 .getWorkspace() 142 .getObservationManager(); 143 144 observationManager.addEventListener(listener, Event.NODE_ADDED 145 | Event.PROPERTY_ADDED 146 | Event.PROPERTY_CHANGED, observationPath, true, null, null, false); 147 } 148 catch (RepositoryException e) { 149 log.error("Unable to add event listeners for " + observationPath, e); } 151 152 } 153 154 157 protected void registerParagraphs() { 158 161 log.info(this.moduleName + ": initializing Paragraph info"); HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG); 163 try { 164 log.info(this.moduleName + ": loading Paragraph info - " + this.basePath); Content startPage = configHierarchyManager.getContent(this.basePath); 166 Content paragraphDefinition = startPage.getContent("Paragraphs"); 168 cacheParagraphsContent(paragraphDefinition); 169 log.info(this.moduleName + ": Paragraph info loaded - " + this.basePath); } 171 catch (RepositoryException re) { 172 log.error(this.moduleName + ": Failed to load Paragraph info - " + this.basePath); log.error(re.getMessage(), re); 174 } 175 } 176 177 181 private void addParagraphsToCache(Iterator paragraphs) { 182 while (paragraphs.hasNext()) { 183 Content c = (Content) paragraphs.next(); 184 Paragraph pi = Paragraph.addParagraphToCache(c, this.basePath); 185 186 if (pi.getDialogContent() != null) { 188 info.magnolia.module.admininterface.Store.getInstance().registerParagraphDialogHandler( 189 pi.getName(), 190 pi.getDialogContent()); 191 } 192 } 193 } 194 195 200 private void cacheParagraphsContent(Content content) { 201 Collection contentNodes = content.getChildren(ItemType.CONTENTNODE); 202 Iterator definitions = contentNodes.iterator(); 203 addParagraphsToCache(definitions); 204 Collection subDefinitions = content.getChildren(ItemType.CONTENT); 205 Iterator it = subDefinitions.iterator(); 206 while (it.hasNext()) { 207 Content c = (Content) it.next(); 208 cacheParagraphsContent(c); 209 } 210 } 211 212 } | Popular Tags |