From: <fg...@us...> - 2008-07-24 08:30:40
|
Revision: 892 http://openutils.svn.sourceforge.net/openutils/?rev=892&view=rev Author: fgiust Date: 2008-07-24 08:30:43 +0000 (Thu, 24 Jul 2008) Log Message: ----------- new tasks for checking/replacing templates Modified Paths: -------------- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/DisableSubscribersTask.java trunk/openutils-mgnltasks/src/site/changes/changes.xml Added Paths: ----------- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/BaseCheckMissingTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingParagraphsTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingTemplatesTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceTemplateTask.java Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/BaseCheckMissingTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/BaseCheckMissingTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/BaseCheckMissingTask.java 2008-07-24 08:30:43 UTC (rev 892) @@ -0,0 +1,157 @@ +/** + * Copyright Openmind 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.mgnltasks; + +import info.magnolia.cms.beans.config.ContentRepository; +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.core.search.Query; +import info.magnolia.cms.core.search.QueryManager; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.AbstractRepositoryTask; +import info.magnolia.module.delta.TaskExecutionException; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import javax.jcr.RepositoryException; +import javax.jcr.query.InvalidQueryException; + +import org.apache.commons.lang.StringUtils; + + +/** + * An abstract task that check for invalid templates/paragraphs. Should not be used directly, see + * {@link CheckMissingTemplatesTask} and {@link CheckMissingParagraphsTask} instead. + * @author fgiust + * @version $Id: $ + */ +public abstract class BaseCheckMissingTask extends AbstractRepositoryTask +{ + + private final String templateOrParagraph; + + private final String nodetype; + + /** + * @param templateOrParagraph "template" or "paragraph" + * @param nodetype node type foc checked nodes + */ + public BaseCheckMissingTask(String templateOrParagraph, String nodetype) + { + super("Check " + templateOrParagraph + "s task", "Checking pages configured with missing " + + templateOrParagraph + + "s"); + this.templateOrParagraph = templateOrParagraph; + this.nodetype = nodetype; + + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + @Override + protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException + { + + QueryManager configQueryManager = installContext + .getHierarchyManager(ContentRepository.CONFIG) + .getQueryManager(); + + Collection<Content> templates = configQueryManager.createQuery( + "//modules/*/" + templateOrParagraph + "s/*", + Query.XPATH).execute().getContent("mgnl:contentNode"); + + List<String> templ = new ArrayList<String>(); + Iterator<Content> availableTemplates = templates.iterator(); + while (availableTemplates.hasNext()) + { + Content template = availableTemplates.next(); + templ.add(template.getName()); + } + + checkInvalidPages(installContext, templ); + + } + + /** + * @param installContext + * @param templates + * @throws RepositoryException + * @throws InvalidQueryException + */ + @SuppressWarnings("unchecked") + private void checkInvalidPages(InstallContext installContext, List<String> templates) throws RepositoryException, + InvalidQueryException + { + HierarchyManager hm = installContext.getHierarchyManager(ContentRepository.WEBSITE); + + QueryManager qm = hm.getQueryManager(); + + StringBuilder query = new StringBuilder("//*[jcr:primaryType='" + + this.nodetype + + "' and MetaData/mgnl:template and not("); + + Iterator<String> nameIterator = templates.iterator(); + while (nameIterator.hasNext()) + { + String template = nameIterator.next(); + + query.append("MetaData/mgnl:template='"); + query.append(template); + query.append("'"); + if (nameIterator.hasNext()) + { + query.append(" or "); + } + } + + query.append(")]"); + + String queryAAsString = query.toString(); + + log.debug("Running query: {}", queryAAsString); + + Collection<Content> nodes = qm.createQuery(queryAAsString, Query.XPATH).execute().getContent(this.nodetype); + + int count = 0; + StringBuilder sb = new StringBuilder(); + + for (Content page : nodes) + { + String template = page.getMetaData().getTemplate(); + + if (StringUtils.isNotEmpty(template)) + { + count++; + sb.append(page.getHandle()); + sb.append(" "); + sb.append(template); + sb.append("\n"); + } + } + + if (count > 0) + { + log.error("Found {} pages with invalid templates:\n{}", count, sb.toString()); + } + } +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/BaseCheckMissingTask.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingParagraphsTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingParagraphsTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingParagraphsTask.java 2008-07-24 08:30:43 UTC (rev 892) @@ -0,0 +1,35 @@ +/** + * Copyright Openmind 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.mgnltasks; + +/** + * A task that checks for pages that contains invalid (missing) paragraphs and logs the result as an error on startup. + * Please note that this task collect the paragraph list extracting any direct subnode of the "paragraphs" nodes in each + * installed module, and not using ParagraphManager (not yet initialized at startup). + * @author fgiust + * @version $Id: $ + */ +public class CheckMissingParagraphsTask extends BaseCheckMissingTask +{ + + public CheckMissingParagraphsTask() + { + super("paragraph", "mgnl:contentNode"); + } + +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingParagraphsTask.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingTemplatesTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingTemplatesTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingTemplatesTask.java 2008-07-24 08:30:43 UTC (rev 892) @@ -0,0 +1,35 @@ +/** + * Copyright Openmind 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.mgnltasks; + +/** + * A task that checks for pages that contains invalid (missing) templates and logs the result as an error on startup. + * Please note that this task collect the template list extracting any direct subnode of the "templates" nodes in each + * installed module, and not using TemplateManager (not yet initialized at startup). + * @author fgiust + * @version $Id: $ + */ +public class CheckMissingTemplatesTask extends BaseCheckMissingTask +{ + + public CheckMissingTemplatesTask() + { + super("template", "mgnl:content"); + } + +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CheckMissingTemplatesTask.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/DisableSubscribersTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/DisableSubscribersTask.java 2008-07-21 13:27:33 UTC (rev 891) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/DisableSubscribersTask.java 2008-07-24 08:30:43 UTC (rev 892) @@ -17,6 +17,7 @@ */ package it.openutils.mgnltasks; +import info.magnolia.cms.beans.config.ContentRepository; import info.magnolia.cms.core.Content; import info.magnolia.cms.core.HierarchyManager; import info.magnolia.cms.core.NodeData; @@ -53,7 +54,7 @@ protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException { - HierarchyManager hm = installContext.getHierarchyManager("config"); + HierarchyManager hm = installContext.getHierarchyManager(ContentRepository.CONFIG); if (hm.isExist(subscribersPath)) { Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceTemplateTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceTemplateTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceTemplateTask.java 2008-07-24 08:30:43 UTC (rev 892) @@ -0,0 +1,91 @@ +/** + * Copyright Openmind 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.mgnltasks; + +import info.magnolia.cms.beans.config.ContentRepository; +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.core.search.Query; +import info.magnolia.cms.core.search.QueryManager; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.AbstractRepositoryTask; +import info.magnolia.module.delta.TaskExecutionException; + +import java.util.Collection; + +import javax.jcr.RepositoryException; + + +/** + * A task that replace any occurrence of a given template with another at startup (handy for renamed templates). + * @author fgiust + * @version $Id: $ + */ +public class ReplaceTemplateTask extends AbstractRepositoryTask +{ + + private final String actualTemplate; + + private final String newTemplate; + + /** + * @param actualTemplate template to be replaced + * @param newTemplate new template + */ + public ReplaceTemplateTask(String actualTemplate, String newTemplate) + { + super("Replacing template " + actualTemplate + " with " + newTemplate, "Replacing template " + + actualTemplate + + " with " + + newTemplate); + this.actualTemplate = actualTemplate; + this.newTemplate = newTemplate; + + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + @Override + protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException + { + + HierarchyManager hm = installContext.getHierarchyManager(ContentRepository.WEBSITE); + + QueryManager qm = hm.getQueryManager(); + + StringBuilder query = new StringBuilder("//*[MetaData/mgnl:template ='"); + query.append(actualTemplate); + query.append("']"); + + String queryAAsString = query.toString(); + + log.debug("Running query: {}", queryAAsString); + + Collection<Content> nodes = qm.createQuery(queryAAsString, Query.XPATH).execute().getContent(); + + for (Content page : nodes) + { + log.warn("Replacing template " + page.getMetaData().getTemplate() + " with {} in {}", newTemplate, page + .getHandle()); + page.getMetaData().setTemplate(newTemplate); + } + } + +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceTemplateTask.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnltasks/src/site/changes/changes.xml =================================================================== --- trunk/openutils-mgnltasks/src/site/changes/changes.xml 2008-07-21 13:27:33 UTC (rev 891) +++ trunk/openutils-mgnltasks/src/site/changes/changes.xml 2008-07-24 08:30:43 UTC (rev 892) @@ -8,6 +8,10 @@ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> </properties> <body> + <release version="3.5.4" date="2008-07-24" description=""> + <action type="add" dev="fgiust">new tasks: CheckMissingTemplatesTask, CheckMissingParagraphsTask, + ReplaceTemplateTask</action> + </release> <release version="3.5.3" date="2008-06-24" description=""> <action type="add" dev="fgiust">new CheckAndCreateRoleTask and CheckAndCreateGroupTask</action> <action type="update" dev="fgiust">AnonymousUserSetupTask now also takes care of changing This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |