From: <fg...@us...> - 2011-11-19 17:10:42
|
Revision: 3693 http://openutils.svn.sourceforge.net/openutils/?rev=3693&view=rev Author: fgiust Date: 2011-11-19 17:10:34 +0000 (Sat, 19 Nov 2011) Log Message: ----------- MGNLUTILS-32 Add paragraphs auto-generation feature to ExtendedTemplate Modified Paths: -------------- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/ExtendedTemplate.java Added Paths: ----------- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/AutoGenerateTemplateModel.java Added: trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/AutoGenerateTemplateModel.java =================================================================== --- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/AutoGenerateTemplateModel.java (rev 0) +++ trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/AutoGenerateTemplateModel.java 2011-11-19 17:10:34 UTC (rev 3693) @@ -0,0 +1,104 @@ +/** + * + * Generic utilities for Magnolia CMS (http://www.openmindlab.com/lab/products/mgnlutils.html) + * Copyright(C) 2009-2011, Openmind S.r.l. http://www.openmindonline.it + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package it.openutils.mgnlutils.templating; + +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.ItemType; +import info.magnolia.cms.util.ContentUtil; +import info.magnolia.context.MgnlContext; +import info.magnolia.module.templating.RenderingModel; +import info.magnolia.module.templating.RenderingModelImpl; + +import javax.jcr.RepositoryException; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Model class for auto-generating paragraphs. See {@link ExtendedTemplate} for details. + * @author fgiust + * @version $Id$ + */ +public class AutoGenerateTemplateModel extends RenderingModelImpl<ExtendedTemplate> +{ + + private static Logger log = LoggerFactory.getLogger(AutoGenerateTemplateModel.class); + + /** + * @param content + * @param definition + * @param parent + */ + public AutoGenerateTemplateModel(Content content, ExtendedTemplate definition, RenderingModel parent) + { + super(content, definition, parent); + } + + public String execute() + { + final ExtendedTemplate templateDef = this.getDefinition(); + MgnlContext.doInSystemContext(new MgnlContext.VoidOp() + { + + public void doExec() + { + createMainArea(templateDef); + } + }); + return super.execute(); + } + + private void createMainArea(ExtendedTemplate templateDef) + { + String autogenerate = templateDef.getAutogenerate(); + + if (StringUtils.isEmpty(autogenerate)) + { + return; + } + + String[] autogeneratelist = StringUtils.split(autogenerate, ","); + + for (String autogeneratepar : autogeneratelist) + { + String[] parDef = StringUtils.split(autogeneratepar, "="); + if (parDef != null && parDef.length == 2) + { + Content paragraph; + try + { + paragraph = ContentUtil.createPath(content, parDef[0], ItemType.CONTENTNODE, true); + if (StringUtils.isEmpty(paragraph.getTemplate())) + { + paragraph.getMetaData().setTemplate(parDef[1]); + paragraph.save(); + } + } + + catch (RepositoryException e) + { + log.error("Cannot create auto-generated paragraph " + autogeneratepar, e); + } + } + } + } +} Property changes on: trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/AutoGenerateTemplateModel.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/ExtendedTemplate.java =================================================================== --- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/ExtendedTemplate.java 2011-11-06 18:15:15 UTC (rev 3692) +++ trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/templating/ExtendedTemplate.java 2011-11-19 17:10:34 UTC (rev 3693) @@ -43,6 +43,13 @@ * <li>A comma separated list of required parent templates (e.g. "home" to make the template available only if one of * its ancestor has the "home" template set.</li> * </ul> + * <p> + * Also allows the configuration of paragraphs to be auto-generated using the "autogenerate" and model class properties: + * </p> + * <ul> + * <li>autogenerate="path1=paragraph1,path2=paragraph2,..." (e.g. "main-column/singleton=p-text")</li> + * <li>modelClass=it.openutils.mgnlutils.templating.AutoGenerateTemplateModel</li> + * </ul> * @author fgiust * @version $Id$ */ @@ -57,12 +64,32 @@ private Set<String> repositories = new HashSet<String>(); + private String autogenerate; + /** * Logger. */ private Logger log = LoggerFactory.getLogger(ExtendedTemplate.class); /** + * Returns the autogenerate. + * @return the autogenerate + */ + public String getAutogenerate() + { + return autogenerate; + } + + /** + * Sets the autogenerate. + * @param autogenerate the autogenerate to set + */ + public void setAutogenerate(String autogenerate) + { + this.autogenerate = autogenerate; + } + + /** * Sets the parentPath. * @param parentPath the parentPath to set */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |