<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~~~~~~licensing~~~~~~
  permission-soap
  ==========
  Copyright (C) 2020 - 2023 EmDev LLC
  ==========
  You may not use this file except in accordance with the License Terms of the Copyright
  Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
  rights to the Software and any copies are the property of the Copyright Holder. Unless
  it is explicitly allowed the Copyright Holder, the User is prohibited from using the
  Software for commercial purposes to provide services to third parties.
  
  The Copyright Holder hereby declares that the Software is provided on an "AS IS".
  Under no circumstances does the Copyright Holder guarantee or promise that the
  Software provided by him will be suitable or not suitable for the specific purposes
  of the User, that the Software will meet all commercial and personal subjective
  expectations of the User, that the Software will work properly, without technical
  errors, quickly and uninterruptedly.
  
  Under no circumstances shall the Copyright Holder or its Affiliates is not liable
  to the User for any direct or indirect losses of the User, his expenses or actual
  damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
  or damage to data, property, etc.
  ~~~~~~/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"
           xmlns:cxf="http://cxf.apache.org/blueprint/core"
           xmlns:camelcxf="http://camel.apache.org/schema/blueprint/cxf"
           xmlns:p="http://www.entaxy.ru/permission/"
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
             ">

    <cm:property-placeholder persistent-id="ru.entaxy.esb.system.permission" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="endpoint.address" value="/permission-management"/>
        </cm:default-properties>
    </cm:property-placeholder>

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

    <reference id="serviceInterceptor"
               interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=service)"
               timeout="30000"/>
    <reference id="authInterceptor" interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=authentication)"/>

    <camelcxf:cxfEndpoint id="permission-endpoint"
                          address="${endpoint.address}"
                          endpointName="p:permission-serviceSOAP"
                          serviceName="p:permission-service"
                          wsdlURL="wsdl/permission-service.wsdl">
        <camelcxf:properties>
            <entry key="dataFormat" value="PAYLOAD"/>
            <entry key="mtom-enabled" value="false"/>
        </camelcxf:properties>
        <camelcxf:inInterceptors>
            <ref component-id="authInterceptor"/>
            <ref component-id="serviceInterceptor"/>
        </camelcxf:inInterceptors>
    </camelcxf:cxfEndpoint>

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

        <route id="permission-endpoint" streamCache="true">
            <from uri="cxf:bean:permission-endpoint"/>
            <log message="${body}" loggingLevel="INFO"/>

            <toD uri="direct:${headers.operationName}"/>
        </route>

        <route id="check">
            <from uri="direct:check"/>
            <doTry>
                <to uri="direct:preparePermissionCommon"/>

                <to uri="bean:permissionService?method=existByAllParameters(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

                <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
                <setBody>
                    <simple>&lt;response xmlns="http://www.entaxy.ru/permission/"&gt;${body}&lt;/response&gt;</simple>
                </setBody>
                <doCatch>
                    <exception>java.lang.Exception</exception>
                    <log message="${exception.message}" loggingLevel="ERROR"/>
                    <log message="${exception.stacktrace}" loggingLevel="DEBUG"/>
                    <setBody>
                        <constant>&lt;response xmlns="http://www.entaxy.ru/permission/"&gt;false&lt;/response&gt;
                        </constant>
                    </setBody>
                </doCatch>
            </doTry>
        </route>

        <route id="get">
            <from uri="direct:get"/>

            <setProperty name="permissionId">
                <xpath resultType="long">//p:permissionId</xpath>
            </setProperty>

            <to uri="bean:permissionService?method=get(${exchangeProperty.permissionId})"/>

            <log message="\r\nGet permission ${body}\r\n" loggingLevel="DEBUG"/>
            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>

            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="getByAllParams">
            <from uri="direct:getByAllParams"/>
            <to uri="direct:preparePermissionCommon"/>

            <to uri="bean:permissionService?method=getByAllParameters(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>
            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="create">
            <from uri="direct:create"/>
            <to uri="direct:preparePermissionCommon"/>

            <to uri="bean:permissionService?method=add(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>
            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="update">
            <from uri="direct:update"/>
            <setProperty name="permissionId">
                <xpath resultType="long">//p:permission/p:permissionId</xpath>
            </setProperty>
            <setProperty name="objectId">
                <xpath resultType="long">//p:permission/p:objectId</xpath>
            </setProperty>
            <setProperty name="objectType">
                <xpath resultType="String">//p:permission/p:objectType</xpath>
            </setProperty>
            <setProperty name="subjectId">
                <xpath resultType="String">//p:permission/p:subjectId</xpath>
            </setProperty>
            <setProperty name="subjectType">
                <xpath resultType="String">//p:permission/p:subjectType</xpath>
            </setProperty>
            <setProperty name="action">
                <xpath resultType="String">//p:permission/p:action</xpath>
            </setProperty>

            <to uri="bean:permissionService?method=update(${exchangeProperty.permissionId}, ${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>
            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="delete">
            <from uri="direct:delete"/>
            <to uri="direct:preparePermissionCommon"/>

            <to uri="bean:permissionService?method=remove(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
             	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <setBody>
                <simple>&lt;response xmlns="http://www.entaxy.ru/permission/"&gt;true&lt;/response&gt;</simple>
            </setBody>
        </route>


        <route>
            <from uri="direct:modelToProperties"/>
            <setProperty name="permissionId">
                <simple>${body.id}</simple>
            </setProperty>
            <setProperty name="objectId">
                <simple>${body.objectId}</simple>
            </setProperty>
            <setProperty name="objectType">
                <simple>${body.objectType}</simple>
            </setProperty>
            <setProperty name="subjectId">
                <simple>${body.subjectId}</simple>
            </setProperty>
            <setProperty name="subjectType">
                <simple>${body.subjectType}</simple>
            </setProperty>
            <setProperty name="action">
                <simple>${body.action}</simple>
            </setProperty>
        </route>

        <route>
            <from uri="direct:modelToHeaders"/>
            <setHeader name="model_permissionId">
                <simple>${body.id}</simple>
            </setHeader>
            <setHeader name="model_objectId">
                <simple>${body.objectId}</simple>
            </setHeader>
            <setHeader name="model_objectType">
                <simple>${body.objectType}</simple>
            </setHeader>
            <setHeader name="model_subjectId">
                <simple>${body.subjectId}</simple>
            </setHeader>
            <setHeader name="model_subjectType">
                <simple>${body.subjectType}</simple>
            </setHeader>
            <setHeader name="model_action">
                <simple>${body.action}</simple>
            </setHeader>
        </route>

        <route>
            <from uri="direct:preparePermissionCommon"/>
            <setProperty name="objectId">
                <xpath resultType="long">//p:permissionCommon/p:objectId</xpath>
            </setProperty>
            <setProperty name="objectType">
                <xpath resultType="String">//p:permissionCommon/p:objectType</xpath>
            </setProperty>
            <setProperty name="subjectId">
                <xpath resultType="String">//p:permissionCommon/p:subjectId</xpath>
            </setProperty>
            <setProperty name="subjectType">
                <xpath resultType="String">//p:permissionCommon/p:subjectType</xpath>
            </setProperty>
            <setProperty name="action">
                <xpath resultType="String">//p:permissionCommon/p:action</xpath>
            </setProperty>

            <log message="\r\nobjectId ${exchangeProperty.objectId}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\nobjectType ${exchangeProperty.objectType}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\nsubjectId ${exchangeProperty.subjectId}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\nsubjectType ${exchangeProperty.subjectType}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\naction ${exchangeProperty.action}\r\n" loggingLevel="DEBUG"/>
        </route>
    </camelContext>
</blueprint>
