# ---------------------------------------------------------------------------
# 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.
# ---------------------------------------------------------------------------
apiVersion: camel.apache.org/v1
kind: Kamelet
metadata:
  name: mariadb-source
  annotations:
    camel.apache.org/kamelet.support.level: "Stable"
    camel.apache.org/catalog.version: "4.10.2"
    camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,[encoded data, 7560 characters]"
    camel.apache.org/provider: "Apache Software Foundation"
    camel.apache.org/kamelet.group: "SQL"
    camel.apache.org/kamelet.namespace: "Database"
  labels:
    camel.apache.org/kamelet.type: "source"
spec:
  definition:
    title: "MariaDB Source"
    description: |-
      Query data from a MariaDB Database.

      In your Pipe file, you must explicitly declare the MariaDB Server driver dependency in spec->integration->dependencies
      
      - "mvn:org.mariadb.jdbc:mariadb-java-client:<version>"
    required:
      - serverName
      - username
      - password
      - query
      - databaseName
    type: object
    properties:
      serverName:
        title: Server Name
        description: The server name for the data source.
        type: string
        example: localhost
      serverPort:
        title: Server Port
        description: The server port for the data source.
        type: string
        default: 3306
      username:
        title: Username
        description: The username to access a secured MariaDB Database.
        type: string
        x-descriptors:
        - urn:camel:group:credentials
      password:
        title: Password
        description: The password to access a secured MariaDB Database.
        type: string
        format: password
        x-descriptors:
        - urn:camel:group:credentials
      query:
        title: Query
        description: The query to execute against the MariaDB Database.
        type: string
        example: 'INSERT INTO accounts (username,city) VALUES (:#username,:#city)'
      databaseName:
        title: Database Name
        description: The name of the MariaDB Database.
        type: string
      consumedQuery:
        title: Consumed Query
        description: A query to run on a tuple consumed.
        type: string
        example: 'DELETE FROM accounts where user_id = :#user_id'
      delay:
        title: Delay
        description: The number of milliseconds before the next poll
        type: integer
        default: 500
  types:
    in:
      mediaType: application/json
  dependencies:
  - "camel:jackson"
  - "camel:kamelet"
  - "camel:sql"
  - "mvn:org.apache.commons:commons-dbcp2:2.13.0"
  template:
    beans:
      - name: local-sql-mariadb-source
        type: "#class:org.apache.camel.component.sql.SqlComponent"
        properties:
          autowiredEnabled: "false"
      - name: dsBean
        type: "#class:org.apache.commons.dbcp2.BasicDataSource"
        properties:
          username: '{{username}}'
          password: '{{password}}'
          url: 'jdbc:mariadb://{{serverName}}:{{serverPort}}/{{databaseName}}'
          driverClassName: 'org.mariadb.jdbc.Driver'
    from:
      uri: "{{local-sql-mariadb-source}}:{{query}}"
      parameters:
        dataSource: "#bean:{{dsBean}}"
        onConsume: "{{?consumedQuery}}"
        delay: "{{delay}}"
      steps:
      - marshal:
          json: 
            library: Jackson
      - to: "kamelet:sink"