1. Introduction

In this tutorial you will learn how to extend your Command by adding Command Services that are made available by the executing Engine.

This guide merely demonstrates how to write a Command. For more information on how to use the Command in the apps, refer to this guide.

2. Prerequisites

To complete this guide you need:

  • Roughly 10 minutes

  • JDK 11+ installed with JAVA_HOME configured appropriately

  • An IDE (we recommend IntelliJ)

  • Some kind of build tool (like gradle, maven or ant)

  • Basic understanding of Commands and the respective annotations

  • Preferably the completion of the step-by-step guide

3. What is a Command Service?

Services mostly handle tasks that are relevant to the executing environment in some way, for example the IPersistenceService, which is responsible for accessing data.

A Command Service is a special kind of service which is only available to Commands. They can be used to extract relevant business logic or make use the Engines services.

3.1. Available Services

When executing your command in the Workflow Executor, the following services will always be available.

  • IJsonMapper: Handles the conversion between java objects and JSON and resolves json paths

  • ICommandRestService: Provides web targets which can be used to perform REST actions

  • ICommandServiceProvider: Collection of all available Command Services that a Command may use

4. Example Implementation

In our case, we want to develop a simple Command which can resolve a JSON path within a given object.

4.1. Building the Command

The basic structure of a Command should be known, and will in our case look like the following code snippet.

/**
 * @workflowDocu Simple command that resolves a given JSON path for the
 * given JSON string.
 */
@JavaCommand
@CommandGroup("org.example")
public class ResolveJsonPath {

	/**
         * @workflowDocu The JSON string to look in
         */
	@Persistent
	private String jsonString; (1)

	/**
         * @workflowDocu The JSON path to search
         */
	@Persistent
	private String jsonPath; (2)

	/**
         * @workflowDocu Result of the operation
         */
	@Persistent(scope = FieldScope.WRITE_ONLY)
	private String resolvedPath; (3)

	@Run
	public void resolveJsonPath() {
		// TODO: Fetch the JSON path
	}
}
1 Our first input parameter will be the JSON string that we want to traverse
2 Our second input is the JSON path which we will try to resolve in the given JSON string
3 The output will be the result of our operation

Resolving a JSON path by hand would be too much work for ourselves, but we also want to keep our implementation inline with the Engine. In order to achieve that we use the IJsonMapper, which already is a service of the Engine.

4.2. Building the Command Service

/**
 * Service class which provides JSON path functionality.
 */
public class JsonPathService implements ICommandService { (1)
	@InjectService (2)
	private IJsonMapper jsonMapper;

    /**
     * Resolves a given JSON path in the given object using the Engine's json mapper.
     *
     * @param object Object to look in
     * @param path Path to look for
     * @return Resolved JSON path
     */
    (3)
    public String fetchJsonPath(final String object, final String path) {
		return this.jsonMapper.resolvePathIn(object, path.split("\\."));
	}
}
1 Every Command Service needs to implement the marker ICommandService
2 Inside the scope of a Command Service we can freely inject services of the Engine into our service by using the @InjectService annotation
3 Our implementation merely delegates the call to the Engines IJsonMapper.
Due to the nature of services being interfaces, are dependent on your runtime. For example, when running the Command in the workflow executor the IJsonMapper functions as a bridge to Jackson and may not handle the complete JSON path syntax.

4.3. Extending the Command

After building our Command Service we can now extend our Command using the @CommandService annotation. In order to do so we must create a new resource folder META-INF\services. There, we create a new file called “de.asap.pak.core.commandservices.api.ICommandService” with one line “org.example.JsonPathService”.

Make sure your file has no extension (.txt etc)
/**
 * @workflowDocu Simple command that resolves a given JSON path for the
 * given JSON string.
 */
@JavaCommand
@CommandGroup("org.example")
public class ResolveJsonPath {
	/**
         * @workflowDocu Service which provides the ability to resolve JSON paths
         */
	@CommandService (1)
	private JsonPathService jsonService;

	/**
         * @workflowDocu The JSON string to look in
         */
	@Persistent
	private String jsonString;

	/**
         * @workflowDocu The JSON path to search
         */
	@Persistent
	private String jsonPath;

	/**
         * @workflowDocu Result of the operation
         */
	@Persistent(scope = FieldScope.WRITE_ONLY)
	private String resolvedPath;

	@Run
	public void resolveJsonPath() {
		this.resolvedPath = this.jsonService.fetchJsonPath(this.jsonString, this.jsonPath);
	}
}
1 Our Service will be injected into our Command using the @CommandService annotation

5. What to do next?

After creating the Command, you might be curious on how to use it in one of our apps, for more information, see this guide.