[[cmd]]
= Command

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:

The command ("cmd") schema defines the serialized form of the __intention__ to invoke an action or to edit a property.
It can be supplemented with optional timings capturing the actual invocation of a command (introduced to support the replication of commands in a master/slave arrangement).


[NOTE]
====
Mixin actions are represented as regular actions on the mixed-in object.
In other words, the fact that the actual implementation of the action is defined by a mixin is an implementation detail only.
====

== `commandDto`

The `commandDto` root element is defined as:

[source,xml]
----
<xs:schema targetNamespace="https://causeway.apache.org/schema/cmd"              <!--.-->
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="https://causeway.apache.org/schema/cmd"
           xmlns:com="https://causeway.apache.org/schema/common">

    <xs:import namespace="https://causeway.apache.org/schema/common"             <!--.-->
               schemaLocation="../common/common-2.0.xsd"/>

    <xs:element name="commandDto">                                          <!--.-->
        <xs:complexType>
            <xs:sequence>
                <xs:element name="majorVersion" type="xs:string"            <!--.-->
                        minOccurs="1" maxOccurs="1" default="2"/>
                <xs:element name="minorVersion" type="xs:string"
                        minOccurs="1" maxOccurs="1" default="1"/>

                <xs:element name="interactionId" type="xs:string"/>         <!--.-->
                <xs:element name="username" type="xs:string"/>              <!--.-->
                <xs:element name="targets" type="com:oidsDto"/>             <!--.-->
                <xs:element name="member" type="memberDto"/>                <!--.-->
                <xs:element name="timings" type="memberDto" minOccurs="0"/> <!--.-->
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="commandsDto">                                         <!--10-->
        <xs:annotation>
            <xs:documentation>Collection of commandDto's</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence minOccurs="1" maxOccurs="unbounded">
                <xs:element ref="commandDto"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    ...
</xs:schema>
----
<1> the command schema has a namespace URI of "https://causeway.apache.org/schema/cmd".
Although URIs are not the same as URLs, you will find that the schemas are also downloadable from this location.
<2> uses complex types defined in the xref:refguide:schema:common.adoc["common" schema].
<3> definition of the `commandDto` root element.
The corresponding XML will use this as its top-level element.
<4> each instance of this schema indicates the version of the schema it is compatible with (following semantic versioning)
<5> unique identifier for the interaction that gave rise to this command.
The interaction Id can be used to correlate to the xref:refguide:schema:ixn.adoc[interaction] that executes the command, and to any xref:refguide:schema:chg.adoc[changes] to domain objects occurring as a side-effect of that interaction.
<6> the name of the user who created the command (whose intention it is to invoke the action/edit the property).
<7> the target object (or objects) to be invoked.
A bulk action will create multiple commands, each with only a single target.
(A future version of the framework may also support a single bulk command against this multiple targets, ie all-or-nothing).
<8> the `memberDto`, defined below, the captures the action/property and arguments/new value.
<9> optional timings for the invocation of a command.
<10> groups multiple commands together.

The `CommandDto` DTO corresponding to the `commandDto` root element can be marshalled to/from XML using the `CommandDtoUtils` class.
The `CommandsDto` DTO allows multiple commands to be marshalled together.
This was introduced to support replication of commands in a master/slave arrangement (eg for regression testing).


[#memberdto-and-subtypes]
== `memberDto` and subtypes

The `memberDto` complex type is an abstract type representing the intention to either invoke an action or to edit a property.
The `actionDto` and `propertyDto` are the concrete subtypes:


[source,xml]
----
<xs:schema targetNamespace="https://causeway.apache.org/schema/cmd" ...>
    ...
    <xs:complexType name="memberDto" abstract="true">                       <!--.-->
        </xs:element>
        <xs:sequence>
            <xs:element name="memberIdentifier" type="xs:string"/>          <!--.-->
            <xs:element name="logicalMemberIdentifier" type="xs:string">    <!--.-->
        </xs:sequence>
        <xs:attribute  name="interactionType" type="com:interactionType"/>  <!--.-->
    </xs:complexType>

    <xs:complexType name="actionDto">                                       <!--.-->
        <xs:complexContent>
            <xs:extension base="memberDto">
                <xs:sequence>
                    <xs:element name="parameters" type="paramsDto"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="propertyDto">                                     <!--.-->
        <xs:complexContent>
            <xs:extension base="memberDto">
                <xs:sequence>
                    <xs:element name="newValue" type="com:valueWithTypeDto"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>
----

<.> the `memberDto` is an abstract type.
I.s primary responsibility is simply to identify the member (action or property).
<.> the formal identifier (fully qualified class name + member name) of the member being interacted with (action or property).
<.> the "logical" formal identifier (object type, as per `@Named(member name) of the member being interacted with (action or property).
<.> the `interactionType` attribute indicates whether the member is an action or a property.
<.> the `actionDto` complex type captures the set of parameters (also including the argument values) with which to invoke the action.
The `paramsDto` type is defined xref:refguide:schema:cmd.adoc#ancillary-types[below].
<.> the `propertyDto` complex type captures the new value (possibly `null`) to set the property to.

In general the `logicalMemberIdentifier` should be used in preference to the `memberIdentifier` because will not (necessarily) have to change if the class is moved during a refactoring.

Note also that there is a corresponding xref:refguide:schema:ixn.adoc#memberexecutiondto[memberExecutionDto]
complex type in the "ixn" schema that is for the actual execution (capturing metrics about its execution and also the return value if an action invocation).



[#ancillary-types]
== Ancillary types

The schema also defines a small number of supporting types:

[source,xml]
----
<xs:schema targetNamespace="https://causeway.apache.org/schema/cmd" ...>
    ...
    <xs:complexType name="paramsDto">                                       <!--.-->
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="parameter" type="paramDto"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="paramDto">                                        <!--.-->
        <xs:complexContent>
            <xs:extension base="com:valueWithTypeDto">
                <xs:attribute name="name" use="required" type="xs:string"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>
----

<.> the `paramsDto` is simply the list of parameter/arguments.
<.> the `paramDto` complex type essentially combines a parameter with its corresponding argument: a named value that has a type.
It extends the `valueWithTypeDto` complex type taken from the
xref:refguide:schema:common.adoc["common"] schema.
