Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: DOCU-1967

...

  • Independent of the UI
  • Back-end executable
  • Executed by UI (for example by an action), Web services or other modules such as the Workflow and Scheduler

You can write your own custom commands and execute them on-demand or scheduled.

Commands consist of a command definition and Java business logic. Since the business logic is written in Java, you can perform basically any task that Java allows.

...

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue
Mgnl f
ui-admincentral
 

Mgnl f
commands

 

Mgnl f
default

 


Mgnl n
markAsDeleted

 

Mgnl p
class

info.magnolia.commands.impl.MarkNodeAsDeletedCommand

Mgnl p
enabled

true

Mgnl n
export

 


Mgnl p
class

info.magnolia.importexport.command.JcrExportCommand

Mgnl n
import

 


Mgnl p
class

info.magnolia.importexport.command.JcrImportCommand

Mgnl n
delete

 

Mgnl n
deactivate


Mgnl p
class

info.magnolia.module.activation.commands.DeactivationCommand

Mgnl p
enabled

true

Mgnl n
delete


Mgnl p
class

info.magnolia.commands.impl.DeleteCommand

Mgnl p
enabled

true

Mgnl n
restorePreviousVersion


Mgnl p
class

info.magnolia.commands.impl.RestorePreviousVersionCommand

Mgnl p
enabled

true

Mgnl n
exportYaml


Mgnl p
class

info.magnolia.commands.ExportJcrNodeToYamlCommand

...

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue
Mgnl f
activation
 

Mgnl f
commands

 

Mgnl f
default

 


Mgnl n
activate

 


Mgnl f
versioned

 

Mgnl n
activate

 


Mgnl n
deactivate

 

The advantage of using a catalog is that you can have identically named commands. You might want two activate commands – one for pages and another for assets. To call a command that resides in a catalog, use the catalog name followed by a hyphen, then the command name. For example, you would distinguish the two activate commands by calling one with workflow-activate and the other with default-activate.

Catalog names have to be unique across the system. Magnolia will not merge commands with same name from various catalogs. It will load commands only from one of them. Since the ui-admininterface module has a default catalog, the name is already taken. If you created a default catalog the names would conflict and the system would not load your command. Choose a catalog name that characterizes its commands, such as "messaging" for commands that send emails and notifications. See Querying for catalogs and commands how to check for currently used names.

...

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue
Mgnl f
activation
 

Mgnl f
commands

 


Mgnl f
default

 

Mgnl n
activate

 


Mgnl p
class

info.magnolia.module.activation.commands.ActivationCommand

Mgnl n
deactivate

 

Mgnl f
versioned

 


Mgnl n
activate

 


Mgnl n
version

 

Mgnl p
class

info.magnolia.commands.impl.VersionCommand

Mgnl p
enabled

true

Mgnl n
activate

 


Mgnl p
class

info.magnolia.commands.DelegateCommand

Mgnl p
commandName

default-activate

Mgnl n
deactivate

 

...


Executing commands with actions

In the UI commands are executed by actions. Here's an example action definition for the activate action in the Pages app. You can find it in Configuration > -> /modules/pages/apps/pages/subApps/browser/actions/activate. See Dialog action definition for the properties and their values.

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
 
Node nameValue

Mgnl f
modules

 


Mgnl f
pages


Mgnl f
apps

 

Mgnl n
pages

 


Mgnl n
subApps

 

Mgnl n
browser

 


Mgnl n
actions

 


Mgnl n
activate

 

Mgnl p
catalog

website

Mgnl p
class

info.magnolia.ui.framework.action.ActivationActionDefinition

Mgnl p
command

activate

Mgnl p
icon

icon-publish

...

  • params: Parameters passed to the command. Depends on the command. For example, the activate command expects to receive a repository name and a content path as parameters.
  • catalog: Name of the catalog where your command resides.
  • command: Name of the command.
  • description: Describes the job, for example "Send an email message on the hour"
  • cron: Schedule that indicates the execution time, written as a CRON expression. For example 0 0 1 5 11 ? 2010 means "run on November 5 at 01:00 am". Cronmaker is a useful tool for building expressions.
  • active: Set value to true to activate the job.
Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue
Mgnl f
scheduler
 

Mgnl f
config

 

Mgnl f
jobs

 


Mgnl n
demo

 


Mgnl n
params

 

Mgnl p
path

/news

Mgnl p
repository

website

Mgnl p
active

 false

Mgnl p
catalog

 website

Mgnl p
command

 activate

Mgnl p
cron

 0 0 * * * *

Mgnl p
description

 activate each hour the page news.html

...

To query for existing commands:

  1. Go to Tools > JCR QueriesTools.
  2. In the Query area tab, select config repository workspace, sql query language and nt:base result item type.
  3. Paste the following query into the Statement box: "select * from nt:base where jcr:path like '%/commands/%'".
  4. Click Execute.

The result looks like this. Path notation: /modules/moduleName/commands/catalog/command.

Code Block
xml
xml
71101 nodes returned in 24ms
/modules/publishing-core/commands/default
/modules/publishing-core/commands/default/publish
/modules/activationpublishing-core/commands/default/unpublish
/modules/activationpublishing-core/commands/default/activate
/modules/activationpublishing-core/commands/default/deactivate
/modules/activationpublishing-core/commands/versioned
/modules/activationpublishing-core/commands/versioned/activatepublish
/modules/activationpublishing-core/commands/versioned/activatepublish/version
/modules/activationpublishing-core/commands/versioned/activatepublish/activatepublish
/modules/activationpublishing-core/commands/versioned/deactivateunpublish
/modules/activationpublishing-core/commands/versioned/deactivateunpublish/version
/modules/publishing-core/commands/versioned/unpublish/unpublish
/modules/publishing-core/commands/versioned/activate
/modules/activationpublishing-core/commands/versioned/deactivate/deactivate
/modules/ui-admincentral/commands/default
/modules/ui-admincentral/commands/default/markAsDeleted
/modules/ui-admincentral/commands/default/export
/modules/ui-admincentral/commands/default/import
/modules/ui-admincentral/commands/default/delete
/modules/cache/commands/cache
/modules/cache/commands/cache/flushAll
....

/modules/rest-services/rest-endpoints/commands/enabledCommands
/modules/rest-services/rest-endpoints/commands/enabledCommands/activate
/modules/rest-services/rest-endpoints/commands/enabledCommands/activate/access
/modules/rest-services/rest-endpoints/commands/enabledCommands/activate/access/roles
/modules/rest-services/rest-endpoints/commands/enabledCommands/markAsDeleted
/modules/rest-services/rest-endpoints/commands/enabledCommands/markAsDeleted/access
/modules/rest-services/rest-endpoints/commands/enabledCommands/markAsDeleted/access/roles
/modules/rest-services/rest-endpoints/commands/enabledCommands/backup
/modules/rest-services/rest-endpoints/commands/enabledCommands/backup/access
/modules/rest-services/rest-endpoints/commands/enabledCommands/backup/access/roles
/modules/ui-framework/commands/default
/modules/ui-framework/commands/default/importZip
....

Executing a command in the Groovy console

Use the Groovy console to execute command business logic manually, in ad hoc fashion. This is a quick way to test a custom command before you compile it as a Java class. Both of the two options below publish the page /travel/about/careers .

...

You can test it by making a small change on the careers page, executing the Groovy commands, and viewing the modified history page on the public instance. 

  • Open the Groovy app.
  • Issue the following commands in the console:

    • Option A - Calling the info.magnolia.commands.CommandsManager.executeCommand():

      Code Block
      java
      java
      map  = new java.util.LinkedHashMap<String, String>()
      map.put("path", "/travel/about/careers")
      map.put("repository", "website")
      cm = info.magnolia.commands.CommandsManager.getInstance()
      cm.executeCommand('default','publish',map)
      
    • Option B - Passing a SimpleContextobject to the execute() method:

      Code Block
      java
      java
      cm = info.magnolia.commands.CommandsManager.getInstance()
      command = cm.getCommand('activate')

...

    • 
      command.setRepository('website')

...

    • 
      command.setPath('/travel/about/careers')

...

    • 
      command.setRecursive(true)
      command.execute(

...

    • new info.magnolia.context.SimpleContext())
      

This example activates the page /travel/about/careers. You can test it by making a small change on the careers page, executing the Groovy commands, and viewing the modified history page on the public instance.

The execute() method is passed a Magnolia context instance ctx which is an implicit object the console always puts at your disposal.

 

Executing a command as a Groovy script

...

You can write your own custom commands and execute them either on demand or scheduled. For example, you could execute the standard Magnolia sendMail command when a page is activated. This way you can send an email notification to a user who needs to review the page.

Definition

To create a command definition:

  1. Create a commands folder in the configuration hierarchy of your module.

  2. Create a catalog subfolder under the commands folder. Choose a catalog name that characterizes its commands, such as messaging for commands that send emails and notifications.

  3. Create a content node under the catalog. This is the name of your command in Magnolia. If you did not create a catalog make sure that your command name is unique across Magnolia.
  4. Create a class property. Set its value to the fully-qualified name of the Java class that contains the command logic such as info.magnolia.module.mail.commands.MailCommand. The Java class should reside inside the module JAR.
Advanced Tables - Table Plus
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue

Mgnl f
modules

 


Mgnl f
myModule

 


Mgnl f
commands

 


Mgnl f
myCatalog

 


Mgnl n
myCommand

 


Mgnl p
class

com.example.modules.acme.commands.MyCommand

...

Code Block
languagejava
titleMyCommand.java
public class MyCommand extends BaseRepositoryCommand {
  public boolean execute(Context context) {
    // Your command logic goes here.
  }
}

See the commands in the  info.magnolia.commands.impl  package for examples.