<?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_INSTANCE" >
            <query>
            <![CDATA[
                SELECT DISTINCT
                    i.instance_id AS INSTANCE_ID,
                    i.instance_name AS INSTANCE_NAME,
                    ps.is_currently_running AS PROGRAMSTATUS_IS_CURRENTLY_RUNNING,
                    ps.program_start_time AS PROGRAMSTATUS_PROGRAM_START_TIME,
                    ps.program_end_time AS PROGRAMSTATUS_PROGRAM_END_TIME,
                    ps.last_command_check AS PROGRAMSTATUS_LAST_COMMAND_CHECK,
                    ps.process_id AS PROGRAMSTATUS_PROCESS_ID,
                    ps.status_update_time AS PROGRAMSTATUS_STATUS_UPDATE_TIME,
                    ps.notifications_enabled AS PROGRAMSTATUS_NOTIFICATIONS,
                    ps.event_handlers_enabled AS PROGRAMSTATUS_EVENTHANDLERS,
                    ps.flap_detection_enabled AS PROGRAMSTATUS_FLAPDETECTION,
                    ps.active_service_checks_enabled AS PROGRAMSTATUS_ACTIVE_SERVICECHECKS,
                    ps.passive_service_checks_enabled AS PROGRAMSTATUS_PASSIVE_SERVICECHECKS,
                    ps.active_host_checks_enabled AS PROGRAMSTATUS_ACTIVE_HOSTCHECKS,
                    ps.passive_host_checks_enabled AS PROGRAMSTATUS_PASSIVE_HOSTCHECKS
                    FROM IcingaInstances i
                    INNER JOIN i.programstatus as ps
            ]]>
            </query>
        </dql>

        <dql name="TARGET_LOG" >
            <query>
            <![CDATA[
                SELECT
                    l.logentry_id AS LOG_ID,
                    i.instance_name AS INSTANCE_NAME,
                    l.logentry_time AS LOG_TIME,
                    l.logentry_type AS LOG_TYPE,
                    l.logentry_data AS LOG_DATA
                FROM IcingaLogentries l
                LEFT JOIN l.instance AS i
            ]]>
            </query>
        </dql>

        <dql name="TARGET_DOWNTIME">
            <query>
            <![CDATA[
                SELECT DISTINCT
                    i.instance_name AS INSTANCE_NAME,
                    d.scheduleddowntime_id   AS DOWNTIME_ID,
                    d.internal_downtime_id AS DOWNTIME_INTERNAL_DOWNTIME_ID,
                    d.downtime_type AS DOWNTIME_DOWNTIME_TYPE,
                    COALESCE(h.host_object_id,sh.host_object_id)   AS HOST_OBJECT_ID,
                    o.name1 AS HOST_NAME,
                    COALESCE(hs.current_state,shs.current_state) AS HOST_CURRENT_STATE,
                    s.service_object_id AS SERVICE_OBJECT_ID,
                    o.name2 AS SERVICE_NAME,
                    ss.current_state AS SERVICE_CURRENT_STATE,
                    d.is_fixed AS DOWNTIME_IS_FIXED,
                    d.scheduled_start_time AS DOWNTIME_SCHEDULED_START_TIME,
                    d.scheduled_end_time AS DOWNTIME_SCHEDULED_END_TIME,
                    d.author_name AS DOWNTIME_AUTHOR_NAME,
                    d.comment_data AS DOWNTIME_COMMENT_DATA,
                    d.was_started AS DOWNTIME_WAS_STARTED
               FROM IcingaScheduleddowntime d
               INNER JOIN d.instance as i
               INNER JOIN d.object as o
               LEFT JOIN o.service as s
               LEFT JOIN s.host as sh
               LEFT JOIN s.status as ss
               LEFT JOIN o.host as h
               LEFT JOIN h.status as hs
               LEFT JOIN sh.status as shs
               WHERE s.config_type = '${retained_flag}'
               OR h.config_type = '${retained_flag}'
            ]]>
            </query>

            <filter name="host_current_state" type="dql">
                <resolve>COALESCE(hs.current_state,shs.current_state)</resolve>
            </filter>

            <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="IcingaServicegroup" type="dql" affects="service">
                <leftjoin>s.servicegroups sg</leftjoin>
                <leftjoin>sg.object osg</leftjoin>

                <orwhere>
                    <![CDATA[
                        (osg.name1 IN (${credential_value})
                        OR o.objecttype_id = 1)
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaHostgroup" type="dql" affects="host">
                <leftjoin>h.hostgroups hg</leftjoin>
                <leftjoin>hg.object ohg</leftjoin>
                <leftjoin>sh.hostgroups shg</leftjoin>
                <leftjoin>shg.object oshg</leftjoin>

                <orwhere>
                    <![CDATA[
                        (ohg.name1 IN (${credential_value})
                        OR oshg.name1 IN (${credential_value}))
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaContactgroup" type="dql" affects="host">
                <leftjoin>h.contactgroups hcg</leftjoin>
                <leftjoin>sh.contactgroups shcg</leftjoin>

                <orwhere>
                    <![CDATA[
                    (hcg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    OR shcg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id}))
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaContactgroup" type="dql" affects="service">
                <leftjoin>
                    s.contactgroups scg
                </leftjoin>

                <orwhere>
                    <![CDATA[
                    (scg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    OR o.objecttype_id = 1)
                    ]]>
                </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>

	<dql name="TARGET_DOWNTIMEHISTORY">
            <query>
            <![CDATA[
                SELECT DISTINCT
                    i.instance_name AS INSTANCE_NAME,
                    d.downtimehistory_id   AS DOWNTIME_ID,
                    d.internal_downtime_id AS DOWNTIME_INTERNAL_DOWNTIME_ID,
                    d.downtime_type AS DOWNTIME_DOWNTIME_TYPE,
                    COALESCE(h.host_object_id,sh.host_object_id)   AS HOST_OBJECT_ID,
                    o.name1 AS HOST_NAME,
                    COALESCE(hs.current_state,shs.current_state) AS HOST_CURRENT_STATE,
                    s.service_object_id AS SERVICE_OBJECT_ID,
                    o.name2 AS SERVICE_NAME,
                    ss.current_state AS SERVICE_CURRENT_STATE,
                    d.is_fixed AS DOWNTIME_IS_FIXED,
                    d.scheduled_start_time AS DOWNTIME_SCHEDULED_START_TIME,
                    d.scheduled_end_time AS DOWNTIME_SCHEDULED_END_TIME,
                    d.author_name AS DOWNTIME_AUTHOR_NAME,
                    d.comment_data AS DOWNTIME_COMMENT_DATA,
                    d.was_started AS DOWNTIME_WAS_STARTED
               FROM IcingaDowntimehistory d
               INNER JOIN d.instance as i
               INNER JOIN d.object as o
               LEFT JOIN o.service as s
               LEFT JOIN s.status as ss
               LEFT JOIN o.host as h
               LEFT JOIN s.host as sh
               LEFT JOIN h.status as hs
               LEFT JOIN sh.status as shs
               WHERE (s.config_type = '${retained_flag}'
               OR h.config_type = '${retained_flag}')
               AND d.was_started = '1'
            ]]>
            </query>

            <filter name="host_current_state" type="dql">
                <resolve>COALESCE(hs.current_state,shs.current_state)</resolve>
            </filter>

            <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="IcingaServicegroup" type="dql" affects="service">
                <leftjoin>s.servicegroups sg</leftjoin>
                <leftjoin>sg.object osg</leftjoin>

                <orwhere>
                    <![CDATA[
                        (osg.name1 IN (${credential_value})
                        OR o.objecttype_id = 1)
                    ]]>
                </orwhere>
            </credential>
-
            <credential name="IcingaHostgroup" type="dql" affects="host">
                <leftjoin>h.hostgroups hg</leftjoin>
                <leftjoin>hg.object ohg</leftjoin>
                <leftjoin>sh.hostgroups shg</leftjoin>
                <leftjoin>shg.object oshg</leftjoin>

                <orwhere>
                    <![CDATA[
                        (ohg.name1 IN (${credential_value})
                        OR oshg.name1 IN (${credential_value}))
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaContactgroup" type="dql" affects="host">
                <leftjoin>h.contactgroups hcg</leftjoin>
                <leftjoin>sh.contactgroups shcg</leftjoin>

                <orwhere>
                    <![CDATA[
                    (hcg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    OR shcg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id}))
                    ]]>
                </orwhere>
            </credential>

            <credential name="IcingaContactgroup" type="dql" affects="service">
                <leftjoin>
                    s.contactgroups scg
                </leftjoin>

                <orwhere>
                    <![CDATA[
                    (scg.contactgroup_id  IN (${TARGET_CONTACTGROUPS.contactgroup_id})
                    OR o.objecttype_id = 1)
                    ]]>
                </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>

        <dql name="TARGET_STATUSMAP_HOST" base="TARGET_HOST">
            <extend>
                <!--
                 * Keep in mind, order is important.
                 * 1st Add the join to query
                 * 2nd Append a new select based on the join
                -->
                <leftjoin>
                    h.parents AS hp
                </leftjoin>

                <select>COALESCE(hp.host_object_id,-1) HOST_PARENT_OBJECT_ID</select>
            </extend>
        </dql>

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