|
| 1 | +package com.merkle.oss.magnolia.setup.task.common; |
| 2 | + |
| 3 | +import info.magnolia.commands.MgnlCommand; |
| 4 | +import info.magnolia.jcr.nodebuilder.NodeOperation; |
| 5 | +import info.magnolia.jcr.nodebuilder.task.ErrorHandling; |
| 6 | +import info.magnolia.jcr.util.NodeTypes; |
| 7 | +import info.magnolia.module.InstallContext; |
| 8 | +import info.magnolia.repository.RepositoryConstants; |
| 9 | + |
| 10 | +import java.lang.invoke.MethodHandles; |
| 11 | +import java.text.MessageFormat; |
| 12 | + |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import com.merkle.oss.magnolia.powernode.NodeOperationFactory; |
| 17 | +import com.merkle.oss.magnolia.setup.task.nodebuilder.AbstractPathNodeBuilderTask; |
| 18 | + |
| 19 | +/** |
| 20 | + * Base class for command install tasks. |
| 21 | + */ |
| 22 | +public abstract class AbstractInstallCommandTask extends AbstractPathNodeBuilderTask { |
| 23 | + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 24 | + |
| 25 | + public static final String MODULE_PATH = "modules/{0}"; |
| 26 | + |
| 27 | + private final NodeOperationFactory ops; |
| 28 | + private final String catalog; |
| 29 | + private final String name; |
| 30 | + private final Class<? extends MgnlCommand> clazz; |
| 31 | + |
| 32 | + protected AbstractInstallCommandTask( |
| 33 | + final NodeOperationFactory nodeOperationFactory, |
| 34 | + final String catalog, |
| 35 | + final String name, |
| 36 | + final Class<? extends MgnlCommand> clazz |
| 37 | + ) { |
| 38 | + super("Install command " + name, "Install command " + name + " in catalog " + catalog, ErrorHandling.strict, RepositoryConstants.CONFIG); |
| 39 | + this.ops = nodeOperationFactory; |
| 40 | + this.catalog = catalog; |
| 41 | + this.name = name; |
| 42 | + this.clazz = clazz; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected NodeOperation[] getNodeOperations(final InstallContext ctx) { |
| 47 | + final String moduleName = ctx.getCurrentModuleDefinition().getName(); |
| 48 | + final String modulePath = MessageFormat.format(MODULE_PATH, moduleName); |
| 49 | + LOG.info("installing command '{}' for module {}", name, modulePath); |
| 50 | + return new NodeOperation[]{ |
| 51 | + ops.getOrAddNode(modulePath, NodeTypes.Content.NAME).then( |
| 52 | + ops.getOrAddNode("commands", NodeTypes.Content.NAME).then( |
| 53 | + ops.getOrAddNode(catalog, NodeTypes.Content.NAME).then( |
| 54 | + ops.getOrAddContentNode(name).then( |
| 55 | + ops.setClassProperty(clazz) |
| 56 | + ) |
| 57 | + ) |
| 58 | + ) |
| 59 | + ) |
| 60 | + }; |
| 61 | + } |
| 62 | +} |
0 commit comments