From: <fg...@us...> - 2009-03-12 11:42:31
|
Revision: 1084 http://openutils.svn.sourceforge.net/openutils/?rev=1084&view=rev Author: fgiust Date: 2009-03-12 11:42:26 +0000 (Thu, 12 Mar 2009) Log Message: ----------- new/updated tasks Modified Paths: -------------- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ModuleConfigBootstrapTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/SimpleModuleVersionHandler.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/UpdateModuleVersionTask.java Added Paths: ----------- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ConditionalTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ExistenceConditionalBootstrapTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceParagraphTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/SetNodeOrderTask.java Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ConditionalTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ConditionalTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ConditionalTask.java 2009-03-12 11:42:26 UTC (rev 1084) @@ -0,0 +1,97 @@ +/** + * 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.core.HierarchyManager; +import info.magnolia.cms.util.NodeDataUtil; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.AbstractTask; +import info.magnolia.module.delta.Task; +import info.magnolia.module.delta.TaskExecutionException; + +import java.util.List; + +import javax.jcr.RepositoryException; + + +/** + * @author molaschi + * @version $Id$ + */ +public abstract class ConditionalTask extends AbstractTask +{ + + protected String workspace; + + protected List<Task> tasks; + + protected String handle; + + /** + * @param taskName + * @param taskDescription + */ + public ConditionalTask(String workspace, String handle, List<Task> tasks) + { + super("Conditional task", "Execute task if exists " + handle + " in " + workspace); + this.tasks = tasks; + this.handle = handle; + this.workspace = workspace; + } + + /** + * {@inheritDoc} + */ + public void execute(InstallContext installContext) throws TaskExecutionException + { + HierarchyManager hm = installContext.getHierarchyManager(workspace); + try + { + if (verifyCondition(hm, handle)) + { + for (Task t : tasks) + { + t.execute(installContext); + } + } + } + catch (RepositoryException e) + { + throw new TaskExecutionException(e.getMessage(), e); + } + } + + public boolean existsNode(HierarchyManager hm, String handle) + { + return hm.isExist(handle); + } + + public boolean existsNodedata(HierarchyManager hm, String handle, String nodedata) throws RepositoryException + { + return hm.getContent(handle).hasNodeData(nodedata); + } + + public boolean nodeDataEquals(HierarchyManager hm, String handle, String nodedata, Object value) + throws RepositoryException + { + return value.equals(NodeDataUtil.getValueObject(hm.getContent(handle).getNodeData(nodedata))); + } + + public abstract boolean verifyCondition(HierarchyManager hm, String handle) throws RepositoryException; + +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ConditionalTask.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/ExistenceConditionalBootstrapTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ExistenceConditionalBootstrapTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ExistenceConditionalBootstrapTask.java 2009-03-12 11:42:26 UTC (rev 1084) @@ -0,0 +1,97 @@ +/** + * 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.core.HierarchyManager; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.BootstrapResourcesTask; + +import org.apache.commons.lang.StringUtils; + + +/** + * @author molaschi + * @version $Id$ + */ +public class ExistenceConditionalBootstrapTask extends BootstrapResourcesTask +{ + + private String workspace; + + private String folderName; + + /** + * @param name + * @param description + * @param importUUIDBehavior + */ + public ExistenceConditionalBootstrapTask(String workspace, String folderName, int importUUIDBehavior) + { + super("ExistenceConditionalBootstrap", "ExistenceConditionalBootstrap", importUUIDBehavior); + + this.workspace = workspace; + this.folderName = folderName; + } + + /** + * @param name + * @param description + */ + public ExistenceConditionalBootstrapTask(String workspace, String folderName) + { + super("ExistenceConditionalBootstrap", "ExistenceConditionalBootstrap"); + + this.workspace = workspace; + this.folderName = folderName; + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean acceptResource(InstallContext installContext, String name) + { + boolean accept = name.startsWith("/mgnl-bootstrap/" + folderName + "/" + workspace) && name.endsWith(".xml"); + + if (accept) + { + String handle = StringUtils.substringAfter(name, "/mgnl-bootstrap/" + folderName + "/" + workspace + "."); + handle = StringUtils.substringBeforeLast(handle, ".xml"); + handle = "/" + StringUtils.replace(handle, ".", "/"); + HierarchyManager hm = installContext.getHierarchyManager(workspace); + boolean alreadyExisting = hm.isExist(handle); + + if (!alreadyExisting) + { + log.info("Loading {} since no content at {}:{} has been found", new Object[]{name, workspace, handle }); + } + return !alreadyExisting; + } + return false; + } + + /** + * {@inheritDoc} + */ + @Override + protected String[] getResourcesToBootstrap(InstallContext installContext) + { + return super.getResourcesToBootstrap(installContext); + } + +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ExistenceConditionalBootstrapTask.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/ModuleConfigBootstrapTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ModuleConfigBootstrapTask.java 2009-03-12 11:23:07 UTC (rev 1083) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ModuleConfigBootstrapTask.java 2009-03-12 11:42:26 UTC (rev 1084) @@ -18,13 +18,13 @@ package it.openutils.mgnltasks; import info.magnolia.cms.core.HierarchyManager; -import info.magnolia.cms.module.ModuleUtil; -import info.magnolia.cms.module.RegisterException; import info.magnolia.module.InstallContext; import info.magnolia.module.delta.BootstrapResourcesTask; import info.magnolia.module.delta.TaskExecutionException; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import javax.jcr.RepositoryException; @@ -35,6 +35,8 @@ /** * A task to bootstrap a module. Can be used to fully re-bootstrap a custom module after an update (useful to reload * templates, dialogs, etc). This task will always ignore bootstrap files for the <code>website</code> repository. + * <strong> Please note that during an update this task will always delete the following nodes inside the affected + * module configuration: "dialogs", "templates", "paragraphs", "virtualURIMapping" </strong> * @author fgiust * @version $Id$ */ @@ -48,20 +50,33 @@ private String modulename; + private List<String> excludeRepositories = new ArrayList<String>(); + public ModuleConfigBootstrapTask(String modulename) { super("Bootstrap", "Bootstraps module configuration for " + modulename + " (will not overwrite website!)."); this.modulename = modulename; + excludeRepositories.add("website"); } + public ModuleConfigBootstrapTask(String modulename, List<String> excludeRepositories) + { + super("Bootstrap", "Bootstraps module configuration for " + modulename + " (will not overwrite website!)."); + this.modulename = modulename; + this.excludeRepositories.addAll(excludeRepositories); + } + @Override protected boolean acceptResource(InstallContext ctx, String name) { - boolean accept = name.startsWith("/mgnl-bootstrap/" + modulename + "/") - && !name.startsWith("/mgnl-bootstrap/" + modulename + "/website") - && name.endsWith(".xml"); + boolean accept = name.startsWith("/mgnl-bootstrap/" + modulename + "/") && name.endsWith(".xml"); + for (String repository : excludeRepositories) + { + accept = accept && !name.startsWith("/mgnl-bootstrap/" + modulename + "/" + repository); + } + if (accept) { log.debug("Importing file {}", name); @@ -82,8 +97,10 @@ deleteNode(installContext, "/modules/" + modulename + "/dialogs"); deleteNode(installContext, "/modules/" + modulename + "/templates"); deleteNode(installContext, "/modules/" + modulename + "/paragraphs"); - deleteNode(installContext, "/modules/" + modulename + "/virtualURIMapping"); + // no, don't delete VUMs! + // deleteNode(installContext, "/modules/" + modulename + "/virtualURIMapping"); + ModuleUtil.bootstrap(resourcesToBootstrap, false); log.info("{} bootstrap done in {} seconds", modulename, (System.currentTimeMillis() - millis) / 1000); } @@ -109,6 +126,7 @@ if (hm.isExist(nodePath)) { + log.warn("Deleting node {}", nodePath); hm.delete(nodePath); } } Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceParagraphTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceParagraphTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/ReplaceParagraphTask.java 2009-03-12 11:42:26 UTC (rev 1084) @@ -0,0 +1,93 @@ +/** + * 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.ItemType; +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 paragraph with another at startup (handy for renamed paragraphs). + * @author fgiust + * @version $Id$ + */ +public class ReplaceParagraphTask extends AbstractRepositoryTask +{ + + private final String actualTemplate; + + private final String newTemplate; + + /** + * @param actualTemplate template to be replaced + * @param newTemplate new template + */ + public ReplaceParagraphTask(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( + ItemType.CONTENTNODE.getSystemName()); + + 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/ReplaceParagraphTask.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/SetNodeOrderTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/SetNodeOrderTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/SetNodeOrderTask.java 2009-03-12 11:42:26 UTC (rev 1084) @@ -0,0 +1,111 @@ +/** + * 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.core.Content; +import info.magnolia.cms.core.ItemType; +import info.magnolia.cms.util.ContentUtil; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.AbstractRepositoryTask; +import info.magnolia.module.delta.TaskExecutionException; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import javax.jcr.RepositoryException; + + +/** + * @author molaschi + * @version $Id$ + */ +public class SetNodeOrderTask extends AbstractRepositoryTask +{ + + private String repository; + + private String parentNode; + + private String[] nodesOrder; + + /** + * @param repository repository + * @param parentNode parent node handle + * @param nodesOrder ordered node names + */ + public SetNodeOrderTask(String repository, String parentNode, String[] nodesOrder) + { + super("Set order in node " + parentNode, "Set order in node " + parentNode); + this.repository = repository; + this.parentNode = parentNode; + this.nodesOrder = nodesOrder; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + @Override + protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException + { + Content parent = installContext.getHierarchyManager(repository).getContent(parentNode); + + List<Content> children = (List<Content>) ContentUtil.getAllChildren(parent); + + if (children.isEmpty()) + { + children = (List<Content>) parent.getChildren(ItemType.CONTENTNODE); + } + + final List<String> orderedList = Arrays.asList(nodesOrder); + + Collections.sort(children, new Comparator<Content>() + { + + public int compare(Content o1, Content o2) + { + Integer index1 = orderedList.indexOf(o1.getName()); + Integer index2 = orderedList.indexOf(o2.getName()); + if (index1 < 0) + { + index1 = Integer.MAX_VALUE; + } + if (index2 < 0) + { + index2 = Integer.MAX_VALUE; + } + + return index2.compareTo(index1); + } + }); + + Content previous = null; + + for (Content content : children) + { + if (previous != null) + { + parent.orderBefore(content.getName(), previous.getName()); + } + previous = content; + } + } + +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/SetNodeOrderTask.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/SimpleModuleVersionHandler.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/SimpleModuleVersionHandler.java 2009-03-12 11:23:07 UTC (rev 1083) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/SimpleModuleVersionHandler.java 2009-03-12 11:42:26 UTC (rev 1084) @@ -34,7 +34,7 @@ /** * A base module version handler that re-bootstrap its configuration each time the version number changes. Can be used - * as is or subclassed (usually overriding getStartupTasks() for configuration tweacks). + * as is or subclassed (usually overriding getStartupTasks() for configuration tweaks). * @author fgiust * @version $Id$ */ @@ -47,6 +47,38 @@ protected Logger log = LoggerFactory.getLogger(getClass()); /** + * Repositories to exclude from bootstrap + */ + protected List<String> excludedRepositoriesFromBootstrap = new ArrayList<String>(); + + /** + * + */ + public SimpleModuleVersionHandler() + { + super(); + excludedRepositoriesFromBootstrap.add("website"); + } + + /** + * Add a repository to bootstrap exclusion list + * @param repository repository to exclude from bootstrap + */ + public void addExcludeRepositoryFromBootstrap(String repository) + { + excludedRepositoriesFromBootstrap.add(repository); + } + + /** + * Remove a repository from bootstrap exclusion list + * @param repository repository to enable for bootstrap + */ + public void removeExcludeRepositoryFromBootstrap(String repository) + { + excludedRepositoriesFromBootstrap.remove(repository); + } + + /** * {@inheritDoc} */ @SuppressWarnings("unchecked") @@ -61,7 +93,7 @@ String modulename = ctx.getCurrentModuleDefinition().getName(); - Version to = ctx.getCurrentModuleDefinition().getVersionDefinition(); + Version to = ctx.getCurrentModuleDefinition().getVersion(); List<Delta> deltas = new ArrayList<Delta>(); @@ -77,7 +109,7 @@ log.info("Updating from version {}", from); Delta delta = DeltaBuilder.update(to, "Update to current version"); - delta.getTasks().add(new ModuleConfigBootstrapTask(modulename)); + delta.getTasks().add(new ModuleConfigBootstrapTask(modulename, excludedRepositoriesFromBootstrap)); delta.getTasks().add(new UpdateModuleVersionTask()); deltas.add(delta); @@ -89,5 +121,4 @@ return deltas; } - } Modified: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/UpdateModuleVersionTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/UpdateModuleVersionTask.java 2009-03-12 11:23:07 UTC (rev 1083) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/UpdateModuleVersionTask.java 2009-03-12 11:42:26 UTC (rev 1084) @@ -1,3 +1,4 @@ + /** * Copyright Openmind http://www.openmindonline.it * @@ -57,7 +58,7 @@ final Content moduleNode = ctx.getOrCreateCurrentModuleNode(); final NodeData nodeData = NodeDataUtil.getOrCreate(moduleNode, "version"); - nodeData.setValue(ctx.getCurrentModuleDefinition().getVersionDefinition().toString()); + nodeData.setValue(ctx.getCurrentModuleDefinition().getVersion().toString()); } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |