Writing Updatorscript - binding a Java Class executing the the delete task

Hello dear community colleagues. I am writing a script for deleting channels. As I see in the old Implementations, there was a way to bind a JavaClass to the Script update method and make a method call like this.

@Override
boolean doUpdate(Node node) throws RepositoryException {
    BaseChannelCreator baseChannelCreator = new BaseChannelCreator()
    return baseChannelCreator.create(node, parametersMap)
}

This was written a long time ago. I tried the same in the new implementation for deleting channels, because I would like to write the Implementation in java and not .groovy. What am I doing wrong?

Here the code in the Updatortemplate:

boolean doUpdate(Node node) {
log.debug “Updating node ${node.path}”
ChannelRemover channelRemover = new ChannelRemover()
channelRemover.remove(node, parametersMap)
return false;
}

Here the output:

ERROR 2024-04-17 12:48:03 Cannot run updater: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
updater: 28: unable to resolve class ChannelRemover
@ line 28, column 20.
ChannelRemover channelRemover = new ChannelRemover()

Questions:

  1. Do I have an possibility to bind a Java Class into the UpdatorTemplate?
  2. If yes, what am I doing wrong?

I would appreciate your answers to solve my problem.

With kind regards,
Petra

if your xpath is targeting channel nodes, you can use:

boolean doUpdate(Node node) {
log.debug "Deleting channel node ${node.path}"
node.remove()
return true;
}

Hello machak, I want to write the logic in the ChannelRemover.java in which I want to clean channel settings in hst:hst, hippo:namespaces, hippo:configuration and the content/documents of the channel which I want to delete. Due to this fact of deleting different nodes at different places I would prefer writing it in Java and not Groovy. I would like to process the node as parameter in the remove(node, parametersMap) method of the ChannelRemover.java. Is it possible?

You can write all your logic in Java and call it from Groovy. Groovy runs on the JVM and has access to the same classloader…sort of, it’s a actually a special classloader to prevent some obvious avenues for mistakes and abuse.

You might be better off getting the ChannelService and using the deleteChannel method.

Great, Jasper, so it is possible. What can be the reason of updater: 28: unable to resolve class ChannelRemover?

What I did since now:

  1. I created a groovy Script in the BRX system updator
  2. I adjusted the doUpdate method in the script to call the ChannelRemover.java remove method

boolean doUpdate(Node node) {
log.debug “Updating node ${node.path}”
ChannelRemover channelRemover = new ChannelRemover()
channelRemover.remove(node, parametersMap)
return false;
}

Thank you Jasper, new for me, I will use it.

ChannelRemover does have to be in the cms project for this to work and you need to have an include statement in the class. Groovy is almost identical to Java in many ways, such that if you just write Java odds are it will work as a Groovy script.

Also, in regards to the channel service, you shouldn’t instantiate it yourself, get it from the service registry. I think the following may work:

final ChannelService channelService = HippoServiceRegistry.getService(PlatformServices.class).getChannelService();

1 Like

Ok, I saw, I forgot the import statement of the class in the groovy script. I added it. Now I would like to have the option of logging or debugging. Either of both is fine for me. When I have the log.info in the update methode and logging target repository, I should see this in the BRX output console or not?

boolean doUpdate(Node node) {
log.info “Updating node ${node.path}”

ChannelRemover channelRemover = new ChannelRemover()
channelRemover.remove(node, parametersMap)
return false;
}

Without logging or debugging possibility no chance to write such a script.

I just get this output:
INFO 2024-04-17 14:28:22 Executing updater ChannelRemoveUpdator
INFO 2024-04-17 14:28:22 Finished executing updater ChannelRemoveUpdator

My question is which magic behiond gives me a node. Before in other tasks I was always using the xpath for the groovy scripts. Now I use the option updator in BRX

Debugging in Groovy is challenging. The log should output to the console or the log files. If you are using Java classes in your Groovy you can of course set a breakpoint in those. Also, Java classes can be unit tested. See this

boolean doUpdate(Node node) {
log.info “Updating node ${node.path}”

It seems like the do update method was not called.

Because the output is:

INFO 2024-04-17 14:28:22 Executing updater ChannelRemoveUpdator
INFO 2024-04-17 14:28:22 Finished executing updater ChannelRemoveUpdator

ok, I still have to define xpath … I see, this is the magic I was looking for. Thank you for your help. So let`s start.

1 Like