Skip to main content

Data Commands

A DataCommand is a form of data-input for a given DataElement. Commands can have one or more ConnectorFields, which are similar to regular Fields.

When a Command is defined, a dedicated parameter object will be generated, together with a method stub in the DataElement's bean class that allows you to implement the command.

Commands have no return type, so they are only suited for inserting data.

Starship.xml
<dataElement name="Starship">
<dataCommands>
<dataCommand name="startNewVoyage">
<!-- If hasTargetInstance is true, this command requires an instance as a target to run -->
<hasTargetInstance>true</hasTargetInstance>
<connectorFields>
<connectorField name="departure">
<fieldType>VALUE_FIELD</fieldType>
<valueField>
<valueFieldType component="" name="Date"/>
</valueField>
</connectorField>
<connectorField name="destination">
<fieldType>LINK_FIELD</fieldType>
<linkField>
<targetElement component="space" name="Planet"/>
<linkFieldType name="Ln01"/>
</linkField>
</connectorField>
</connectorFields>
</dataCommand>
</dataCommands>
</dataElement>

After expanding, the bean class will contain a method stub to implement the Command:

StarshipBean.java
public CommandResult startNewVoyage(ParameterContext<StarshipCommand.StartNewVoyage> commandParameter) {
StarshipCommand.StartNewVoyage startNewVoyageCommand = commandParameter.getValue();
CommandResult commandResult = CommandResult.success(startNewVoyageCommand);

// anchor:custom-command-startNewVoyage-before:start
// anchor:custom-command-startNewVoyage-before:end

IParameterContextFactory parameterFactory = commandParameter.getFactory();
CrudsResult<StarshipDetails> targetResult = getDetailsFromDataRef(parameterFactory.construct(startNewVoyageCommand.getTarget()));
StarshipDetails starshipDetails = targetResult.getValue();

// anchor:custom-command-startNewVoyage:start
StarshipVoyageDetails voyage = new StarshipVoyageDetails();
voyage.setStarship(starshipDetails.getDataRef());
voyage.setDeparture(startNewVoyageCommand.getDeparture());
voyage.setDestination(startNewVoyageCommand.getDestination());
CrudsResult<DataRef> result = StarshipVoyageLocalAgent.getStarshipVoyageAgent(commandParameter.getContext())
.create(voyage);
if (result.isError()) {
commandResult = CommandResult.error(startNewVoyageCommand, "Failed to create a new Voyage");
}
// anchor:custom-command-startNewVoyage:end

// anchor:custom-command-startNewVoyage-after:start
// anchor:custom-command-startNewVoyage-after:end

return commandResult;
}

Options

Option
view.buttons.commands DataElement

Adds a button for each command to the standard page (useful when prototyping).

<options>
<view.buttons.commands/>
</options>