<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~~~~~~licensing~~~~~~
  permission-handler
  ==========
  Copyright (C) 2020 - 2021 EmDev LLC
  ==========
  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.
  ~~~~~~/licensing~~~~~~
  -->

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <cm:property-placeholder
            persistent-id="ru.entaxy.esb.system.permission.handler" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="mode.dev" value="false"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <reference id="permissionService"
               interface="ru.entaxy.esb.system.core.permission.jpa.PermissionService"
               timeout="30000"
               availability="mandatory"/>

    <service ref="permissionChecker"
             interface="ru.entaxy.esb.system.core.permission.handler.PermissionChecker"/>

    <bean id="permissionChecker"
          class="ru.entaxy.esb.system.core.permission.handler.PermissionCheckerImpl">
        <property name="permissionService" ref="permissionService"/>
    </bean>

    <camelContext id="permission-handler-context" xmlns="http://camel.apache.org/schema/blueprint">

        <route id="test-permission" autoStartup="{{mode.dev}}">
            <from uri="timer:test-permission?period=60&amp;repeatCount=1"/>

            <to uri="bean:permissionService?method=add(1, 'system', '2', 'system', 'send')"/>
            <log message="CREATE PERMISSION RECORD ${body}"/>

            <to uri="bean:permissionService?method=getByAllParameters(1, 'system', '2', 'system', 'send')"/>
            <log message="${body}"/>
            <choice>
                <when>
                    <simple>${body} != null</simple>
                    <log message="GET EXISTING RECORD: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="GET EXISTING RECORD: TEST FAILED"/>
                </otherwise>
            </choice>

            <doTry>
                <to uri="bean:permissionService?method=getByAllParameters(1, '4321', '4321', '4321', '4321')"/>
                <log message="GET NOT EXISTING RECORD: TEST FAILED"/>
                <doCatch>
                    <exception>java.lang.Exception</exception>
                    <log message="GET NOT EXISTING RECORD: TEST PASSED"/>
                </doCatch>
            </doTry>

            <to uri="bean:permissionService?method=remove(1, 'system', '2', 'system', 'send')"/>

            <log message="DELETE PERMISSION RECORD ${body}"/>
        </route>

        <route id="test-permission-component" autoStartup="{{mode.dev}}">
            <from uri="timer:test-has-permission?period=60&amp;repeatCount=1&amp;delay=30000"/>

            <toD uri="permission:create?objectId=2&amp;objectType=system&amp;subjectId=3&amp;subjectType=system&amp;action=default"/>
            <log message="Create"/>
            <choice>
                <when>
                    <simple>${body} != null</simple>
                    <log message="CREATE ${body}: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="CREATE ${body}: TEST FAILED"/>
                </otherwise>
            </choice>

            <toD uri="permission:check?objectId=2&amp;objectType=system&amp;subjectId=3&amp;subjectType=system&amp;action=default"/>
            <choice>
                <when>
                    <simple>${body}</simple>
                    <log message="CHECK ${body}: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="CHECK ${body}: TEST FAILED"/>
                </otherwise>
            </choice>

            <toD uri="permission:create?objectId=2&amp;objectType=system&amp;subjectId=4&amp;subjectType=system&amp;action=send"/>
            <choice>
                <when>
                    <simple>${body} != null</simple>
                    <log message="CREATE ${body}: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="CREATE ${body}: TEST FAILED"/>
                </otherwise>
            </choice>

            <toD uri="permission:check?objectId=2&amp;objectType=system&amp;subjectId=4&amp;subjectType=system&amp;action=send"/>
            <choice>
                <when>
                    <simple>${body}</simple>
                    <log message="CHECK ${body}: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="CHECK ${body}: TEST FAILED"/>
                </otherwise>
            </choice>

            <toD uri="permission:getByAllParams?objectId=2&amp;objectType=system&amp;subjectId=3&amp;subjectType=system&amp;action=default"/>
            <log message="getByAllParams objectId=2&amp;objectType=system&amp;subjectId=3&amp;subjectType=system&amp;action=default"/>
            <choice>
                <when>
                    <simple>${body} != null</simple>
                    <log message="GET ${body}: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="GET ${body}: TEST FAILED"/>
                </otherwise>
            </choice>


            <toD uri="permission:delete?objectId=2&amp;objectType=system&amp;subjectId=3&amp;subjectType=system&amp;action=default"/>
            <log message="DELETE ${body}"/>

            <toD uri="permission:delete?objectId=2&amp;objectType=system&amp;subjectId=4&amp;subjectType=system&amp;action=send"/>
            <log message="DELETE ${body}"/>

            <toD uri="permission:check?objectId=2&amp;objectType=system&amp;subjectId=3&amp;subjectType=system&amp;action=default"/>
            <choice>
                <when>
                    <simple>${body} == false</simple>
                    <log message="CHECK DELETED ${body}: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="CHECK DELETED ${body}: TEST FAILED"/>
                </otherwise>
            </choice>

            <toD uri="permission:check?objectId=2&amp;objectType=system&amp;subjectId=4&amp;subjectType=system&amp;action=send"/>
            <choice>
                <when>
                    <simple>${body} == false</simple>
                    <log message="CHECK DELETED ${body}: TEST PASSED"/>
                </when>
                <otherwise>
                    <log message="CHECK DELETED ${body}: TEST FAILED"/>
                </otherwise>
            </choice>

        </route>

    </camelContext>

</blueprint>
