//
// Licensed 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.
//

=== Built-in conditions

Apache Unomi comes with an extensive collection of built-in conditions. Instead of detailing them one by one you will
find here an overview of what a JSON condition descriptor looks like:

[source,json]
----
{
  "metadata": {
    "id": "booleanCondition",
    "name": "booleanCondition",
    "description": "",
    "systemTags": [
      "profileTags",
      "logical",
      "condition",
      "profileCondition",
      "eventCondition",
      "sessionCondition",
      "sourceEventCondition"
    ],
    "readOnly": true
  },
  "conditionEvaluator": "booleanConditionEvaluator",
  "queryBuilder": "booleanConditionESQueryBuilder",
  "parameters": [
    {
      "id": "operator",
      "type": "String",
      "multivalued": false,
      "defaultValue": "and"
    },
    {
      "id": "subConditions",
      "type": "Condition",
      "multivalued": true
    }
  ]
}
----

Note that conditions have two important identifiers:

- conditionEvaluator
- queryBuilder

This is because conditions can either be used to build queries or to evaluate a condition in real time. When implementing
a new condition type, both implementations much be provided. Here's an example an OSGi Blueprint registration for the
above condition descriptor:

From https://github.com/apache/unomi/blob/master/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml

[source,xml]
----
...
    <service
            interface="org.apache.unomi.persistence.elasticsearch.conditions.ConditionESQueryBuilder">
        <service-properties>
            <entry key="queryBuilderId" value="booleanConditionESQueryBuilder"/>
        </service-properties>
        <bean class="org.apache.unomi.plugins.baseplugin.conditions.BooleanConditionESQueryBuilder"/>
    </service>
...
    <!-- Condition evaluators -->
    <service interface="org.apache.unomi.persistence.elasticsearch.conditions.ConditionEvaluator">
        <service-properties>
            <entry key="conditionEvaluatorId" value="booleanConditionEvaluator"/>
        </service-properties>
        <bean class="org.apache.unomi.plugins.baseplugin.conditions.BooleanConditionEvaluator"/>
    </service>
...
----

As you can see two Java classes are used to build a single condition. You don't need to understand all these details in
order to use conditions, but this might be interesting to know if you're interested in building your own condition
implementations. For more details on building your own custom plugins/extensions, please refer to the corresponding
sections.

==== Existing condition descriptors

Here is a non-exhaustive list of conditions built into Apache Unomi. Feel free to browse the source code if you want to
discover more. But the list below should get you started with the most useful conditions:

- https://github.com/apache/unomi/tree/master/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions

Of course it is also possible to build your own custom conditions by developing custom Unomi plugins/extensions.

You will also note that some conditions can re-use a `parentCondition`. This is a way to inherit from another condition
type to make them more specific.