<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations xmlns:view="http://icinga.org/icinga/config/global/api/views/1.0"
    xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0"
>
    <ae:configuration>
        <dql name="TARGET_NOTIFICATIONS" >
            <query>
            <![CDATA[
            SELECT
            n.notification_id AS NOTIFICATION_ID,
            n.notification_type AS NOTIFICATION_TYPE,
            c.name1 AS NOTIFICATION_CONTACT,
            o.name1 AS HOST_NAME,
            o.name2 AS SERVICE_NAME,
            n.state AS NOTIFICATION_STATE,
            n.start_time  AS NOTIFICATION_STARTTIME,
            n.output AS NOTIFICATION_OUTPUT,
            co.name1 as COMMAND_NAME
            FROM IcingaNotifications n
            INNER JOIN n.object o
            LEFT JOIN o.service s
            LEFT JOIN o.host h
            LEFT JOIN s.host sh

            LEFT JOIN n.contactnotifications cn
            LEFT JOIN cn.notificationmethod cnm
            LEFT JOIN cnm.command cnc
            LEFT JOIN cnc.object co

            LEFT JOIN cn.contactobject c
            ORDER BY notification_id DESC
            ]]>
            </query>

            <credential name="IcingaHost" type="MultiLike" affects="host">
                <parameter name="target">IcingaHost</parameter>
                <parameter name="column">o.name1</parameter>
            </credential>

            <credential name="IcingaService" type="MultiLike" affects="service">
                <parameter name="target">IcingaService</parameter>
                <parameter name="column">o.name2</parameter>
                <!-- In mixed views we allow the service to be null => its a host -->
                <parameter name="ornull">true</parameter>
            </credential>

            <credential name="IcingaContactgroup" type="dql" affects="both">
                <leftjoin>s.contactgroups sc</leftjoin>
                <leftjoin>h.contactgroups hc</leftjoin>
            </credential>

            <credential name="IcingaContactgroup" type="custom" affects="host">
             <![CDATA[
                WHERE  (
                    hc.contactgroup_id IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    OR
                    sc.contactgroup_id IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                )
            ]]>
            </credential>

            <credential name="IcingaContactgroup" type="custom" affects="service">
             <![CDATA[
                WHERE  (
                    hc.contactgroup_id IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    OR
                    sc.contactgroup_id IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                )
            ]]>
            </credential>

            <credential name="IcingaServicegroup" type="dql" affects="service">
                <leftjoin>s.servicegroups sg</leftjoin>
                <leftjoin>sg.object sgo</leftjoin>
                <orwhere>
                    (o.objecttype_id = 1 OR sgo.name1 IN (${credential_value}))
                </orwhere>
            </credential>

            <credential name="IcingaHostgroup" type="dql" affects="host">
                <leftjoin>h.hostgroups hg</leftjoin>
                <leftjoin>hg.object hgo</leftjoin>

                <leftjoin>sh.hostgroups chg</leftjoin>
                <leftjoin>chg.object chgo</leftjoin>

                <orwhere>
                    (
                        (o.objecttype_id=1 AND hgo.name1 IN (${credential_value}))
                        OR (o.objecttype_id=2 AND chgo.name1 IN (${credential_value}))
                    )
                </orwhere>
            </credential>

            <credential name="IcingaHostCustomVariablePair" type="CustomVariable" affects="host">
                <parameter name="alias">h</parameter>
                <parameter name="target">host</parameter>
            </credential>
            <credential name="IcingaHostCustomVariablePair" type="CustomVariable" affects="host">
                <parameter name="alias">sh</parameter>
                <parameter name="target">host</parameter>
            </credential>

            <credential name="IcingaServiceCustomVariablePair" type="CustomVariable" affects="service">
                <parameter name="alias">s</parameter>
                <parameter name="target">service</parameter>
            </credential>

        </dql>
    </ae:configuration>
</ae:configurations>
