view artifacts/doc/conf/meta-data.xml @ 5983:0c63ce5a7e74

Use name id as username in saml.User. The old WAS-only code used the user-name typed in by the user when logging in. When logging in via SAML single sign on, we must extract the user name from the SAML ticket and the name ID should basically be what the user types in when authenticating.
author Bernhard Herzog <bh@intevation.de>
date Fri, 10 May 2013 19:03:48 +0200
parents f33d717aae5c
children a3060bfd28cf
line wrap: on
line source
<?xml version="1.0" encoding="UTF-8"?>
<dc:template xmlns:dc="http://www.intevation.org/2011/Datacage">
<datacage>
  <dc:comment>
    Statement to load data from wsts.
  </dc:comment>
  <dc:macro name="SQL-wst_columns_statement">
    <dc:statement>
        SELECT wst_columns.id          AS prot_column_id,
               wst_columns.name        AS prot_column_name,
               wst_columns.position    AS prot_rel_pos,
               wst_columns.description AS info,
               wst_ranges.a            AS deffrom,
               wst_ranges.b            AS defto
        FROM wst_columns JOIN wst_ranges
            ON wst_columns.id = wst_ranges.wst_column_id
        WHERE wst_columns.wst_id = ${prot_id}
            AND NOT((${tokm} &lt; wst_ranges.a) OR (${fromkm} &gt; wst_ranges.b))
        ORDER by wst_columns.position
    </dc:statement>
  </dc:macro>

  <dc:comment>
    Load user specific distance information from artifact.
  </dc:comment>
  <dc:macro name="user-range">
    <dc:choose>
      <dc:when test="dc:contains($parameters, 'user-id')">
        <dc:context connection="user">
        <dc:statement>
          SELECT COALESCE(ld_mode, '')      AS ldm,
                 COALESCE(ld_locations, '') AS ldl,
                 COALESCE(ld_from, '')      AS ldf,
                 COALESCE(ld_to, '')        AS ldt
          FROM master_artifacts_range
          WHERE gid = CAST(${artifact-id} AS uuid)
        </dc:statement>
        <dc:for-each>
          <dc:variable name="fromkm" type="number" expr="dc:fromValue($ldm, $ldl, $ldf)"/>
          <dc:variable name="tokm" type="number" expr="dc:toValue($ldm, $ldl, $ldt)"/>
          <dc:macro-body/>
        </dc:for-each>
        </dc:context>
      </dc:when>
      <dc:otherwise>
        <dc:variable name="fromkm" type="number" expr="dc:fromValue('', '', '')"/>
        <dc:variable name="tokm" type="number" expr="dc:toValue('', '', '')"/>
        <dc:macro-body/>
      </dc:otherwise>
    </dc:choose>
  </dc:macro>

  <dc:comment>
    System part. Load data for the given river.
  </dc:comment>
  <dc:macro name="load-system">
    <dc:context connection="system">
      <dc:statement>
        SELECT id AS river_id, name AS river_name FROM rivers
        WHERE lower(name) LIKE lower(${river})
      </dc:statement>
      <dc:for-each>

        <dc:comment>
          Base-data macros (mostly data imported from wst-files).
        </dc:comment>
        <dc:macro name="basedata_0">
          <dc:call-macro name="user-range">
            <dc:comment comment=" BASEDATA ---------------------------"/>
            <basedata>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 0 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <basedata name="{$prot_description}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="base_data-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwqkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </basedata>
                </dc:for-each>
              </dc:context>
            </basedata>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_0_wq">
          <dc:call-macro name="user-range">
            <dc:comment comment=" BASEDATA ---------------------------"/>
            <basedata>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 0 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <basedata name="{$prot_description}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="base_data-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="wqinterpol"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </basedata>
                </dc:for-each>
              </dc:context>
            </basedata>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_1_additionals_marks">
          <dc:call-macro name="user-range">
            <dc:comment comment=".ZUS -------------------------------"/>
            <additionals>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 1 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <additional name="{$prot_description}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="additionalsmarks-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context></additional>
                </dc:for-each>
              </dc:context>
            </additionals>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_1_additionals">
          <dc:call-macro name="user-range">
            <dc:comment comment=".ZUS -------------------------------"/>
            <additionals>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 1 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
		  <additional name="{dc:replace($prot_description, 'Zus.Längsschnitte/', '')}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="additionals-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </additional>
                </dc:for-each>
              </dc:context>
            </additionals>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_1_additionals-relative_point">
          <dc:call-macro name="user-range">
            <dc:comment comment=".ZUS -------------------------------"/>
            <additionals>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 1 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <relativepoint name="{$prot_description}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="additionals-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </relativepoint>
                </dc:for-each>
              </dc:context>
            </additionals>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_2_fixations_wqkms">
          <dc:call-macro name="user-range">
            <fixations>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 2 AND river_id = ${river_id}
                  ORDER BY prot_description
                </dc:statement>
                <dc:for-each>
                  <fixation name="{dc:replace($prot_description, 'Fixierungen/', '')}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="fixations-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="wqinterpol"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </fixation>
                </dc:for-each>
              </dc:context>
            </fixations>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_2_fixations">
          <dc:call-macro name="user-range">
            <fixations>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 2 AND river_id = ${river_id}
                  ORDER BY prot_description
                </dc:statement>
                <dc:for-each>
                  <fixation name="{dc:replace($prot_description, 'Fixierungen/', '')}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="fixations-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwqkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </fixation>
                </dc:for-each>
              </dc:context>
            </fixations>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_2_fixations_relative_point">
          <dc:call-macro name="user-range">
            <fixations>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 2 AND river_id = ${river_id}
                  ORDER BY prot_description
                </dc:statement>
                <dc:for-each>
                  <relativepoint name="{dc:replace($prot_description, 'Fixierungen/', '')}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="fixations-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </relativepoint>
                </dc:for-each>
              </dc:context>
            </fixations>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_3_officials">
          <dc:call-macro name="user-range">
            <dc:comment comment=".wst -------------------------------"/>
            <officiallines>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 3 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <official name="{$prot_description}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="additionals-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwqkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </official>
                </dc:for-each>
              </dc:context>
            </officiallines>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_4_heightmarks-points-relative_points">
          <dc:call-macro name="user-range">
            <heightmarks>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 4 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <relativepoint name="{$prot_description}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="heightmarks_points-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </relativepoint>
                </dc:for-each>
              </dc:context>
            </heightmarks>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_4_heightmarks-points">
          <dc:call-macro name="user-range">
            <heightmarks>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 4 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <heightmark name="{dc:replace($prot_description, 'HW-Marken/', '')}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="heightmarks_points-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </heightmark>
                </dc:for-each>
              </dc:context>
            </heightmarks>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_4_heightmarks-wq">
          <dc:call-macro name="user-range">
            <heightmarks>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 4 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <heightmark name="{$prot_description}">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="heightmarks_annotations-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="wqinterpol"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </heightmark>
                </dc:for-each>
              </dc:context>
            </heightmarks>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_5_flood-protections_relative_points">
          <dc:call-macro name="user-range">
            <flood_protections id="flood-protections-{$river_id}">
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 5 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <relativepoint name="{$prot_description}"
                                 db-id="{$prot_id}"
                                 factory="staticwkms">
                  <columns>
                      <dc:context>
                        <dc:call-macro name="SQL-wst_columns_statement"/>
                        <dc:for-each>
                          <column name="{$prot_column_name}"
                                  ids="flood_protection-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                  factory="staticwkms"
                                  info="{$info} [km {$deffrom} - {$defto}]"/>
                        </dc:for-each>
                      </dc:context>
                    </columns>
                  </relativepoint>
                </dc:for-each>
              </dc:context></flood_protections>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="basedata_5_flood-protections">
          <dc:call-macro name="user-range">
            <flood_protections id="flood-protections-{$river_id}">
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS prot_id,
                         description AS prot_description
                  FROM wsts WHERE kind = 5 AND river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <flood_protection name="{dc:replace($prot_description, 'HW-Schutzanlagen/', '')}"
                                    db-id="{$prot_id}"
                                    factory="staticwkms">
                    <dc:context>
                      <dc:call-macro name="SQL-wst_columns_statement"/>
                      <dc:for-each>
                        <column name="{$prot_column_name}"
                                ids="flood_protection-wstv-{$prot_rel_pos}-{$prot_id}:OUT={$OUT}"
                                factory="staticwkms"
                                info="{$info} [km {$deffrom} - {$defto}]"/>
                      </dc:for-each>
                    </dc:context>
                  </flood_protection>
                </dc:for-each>
              </dc:context>
            </flood_protections>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="mainvalues">
          <wmainvalue factory="mainvalue" ids="{$river_id}:w:OUT={$OUT}"/>
          <qmainvalue factory="mainvalue" ids="{$river_id}:q:OUT={$OUT}"/>
        </dc:macro>

        <dc:macro name="qsectors">
          <qsector factory="qsectors" ids="{$river_id}:OUT={$OUT}"/>
        </dc:macro>

        <dc:macro name="annotations">
          <annotation factory="annotations" ids="{$river_id}:OUT={$OUT}"/>
        </dc:macro>

        <dc:macro name="annotations_per_type">
          <annotations>
            <dc:context>
              <all_annotations factory="annotations" ids="{$river_id}:OUT={$OUT}"/>
              <dc:statement>
                SELECT id          AS anno_id,
                       name AS anno_description
                FROM annotation_types
              </dc:statement>
              <dc:for-each>
                <annotation name="{$anno_description}"
                            factory="annotations"
                            ids="{$river_id}:{$anno_description}:OUT={$OUT}"/>
             </dc:for-each>
            </dc:context>
          </annotations>
        </dc:macro>

        <dc:macro name="cross_sections">
          <dc:call-macro name="user-range">
            <cross_sections id="flood-protections-{$river_id}">
              <dc:context connection="system">
                <dc:statement>
                  SELECT DISTINCT
                         cs.id          AS prot_id,
                         cs.description AS prot_description
                  FROM cross_sections cs
                    JOIN cross_section_lines csl ON csl.cross_section_id = cs.id
                  WHERE cs.river_id = ${river_id}
                    AND csl.km BETWEEN ${fromkm} AND ${tokm}
                </dc:statement>
                <dc:for-each>
                  <cross_section name="{$prot_description}"
                                 ids="{$prot_id}:OUT={$OUT}"
                                 factory="crosssections"/>
                </dc:for-each>
              </dc:context>
            </cross_sections>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="hyks">
          <dc:call-macro name="user-range">
            <hyks id="hyk-{$river_id}">
              <dc:context connection="system">
                <dc:statement>
                  SELECT DISTINCT
                         h.id          AS hyk_id,
                         h.description AS hyk_description
                  FROM hyks h
                    JOIN hyk_entries he ON he.hyk_id = h.id
                  WHERE river_id = ${river_id}
                    AND he.km BETWEEN ${fromkm} AND ${tokm}
                </dc:statement>
                <dc:for-each>
                  <hyk name="{$hyk_description}" 
                    ids="{$hyk_id}:OUT={$OUT}"
                    factory="hyk"/>
                </dc:for-each>
              </dc:context></hyks>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="flow_velocity_measurements">
          <dc:call-macro name="user-range">
            <flowvelocitymeasurement>
              <dc:context connection="system">
                <dc:statement>
                  SELECT id          AS fvmid,
                         description AS fvmd
                  FROM flow_velocity_measurements WHERE river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <flow_velocity_measurement name="{$fvmd}"
                                             ids="{$fvmid}:OUT={$OUT}"
                                             factory="flowvelocity">
                    <dc:context>
                      <dc:statement>
                        SELECT id, description, station, datetime, v, w, q
                        FROM flow_velocity_measure_values
                        WHERE measurements_id = ${fvmid}
                          AND station BETWEEN ${fromkm} AND ${tokm}
                      </dc:statement>
                      <dc:for-each>
                        <measurement_value name="{$description} - {$station} - {$datetime}"
                                           ids="{$id}:OUT={$OUT}"
                                           factory="flowvelocity"/>
                      </dc:for-each>
                    </dc:context>
                  </flow_velocity_measurement>
                </dc:for-each>
              </dc:context>
            </flowvelocitymeasurement>
          </dc:call-macro>
        </dc:macro>

        <dc:macro name="sounding-width">
          <soundings_width>
            <dc:context>
              <dc:statement>
                  SELECT id          AS bedh_id,
                         year        AS bedh_year,
                         description AS bedh_descr
                  FROM bed_height_single WHERE river_id = ${river_id}
              </dc:statement>
              <dc:for-each>
                <height factory="bedheight"
                        ids="bedheight-singlevalues-{$bedh_id}-{$bedh_year}:OUT={$OUT}"
                        description="{$bedh_descr}"/>
              </dc:for-each>
            </dc:context>
          </soundings_width>
        </dc:macro>

        <dc:macro name="morph_width">
          <morph_width>
            <dc:context>
              <dc:statement>
                SELECT id   AS width_id
                FROM morphologic_width
                WHERE river_id = ${river_id}
              </dc:statement>
              <dc:for-each>
                <dc:context>
                  <dc:statement>
                    SELECT min(station) AS from,
                           max(station) AS to
                    FROM morphologic_width_values
                    WHERE morphologic_width_id = ${width_id}
                  </dc:statement>
                  <dc:for-each>
                    <morphologic-width name="{$from} - {$to}"
                                       ids="{$width_id}:OUT={$OUT}"
                                       factory="morph-width"/>
                  </dc:for-each>
                </dc:context>
              </dc:for-each>
            </dc:context>
          </morph_width>
        </dc:macro>

        <dc:macro name="longitudinal-section-prototype">
          <dc:call-macro name="basedata_0"/>
          <dc:call-macro name="basedata_1_additionals"/>
          <dc:comment comment=" FIXATIONS ---------------------------"/>
          <dc:call-macro name="basedata_2_fixations"/>
          <dc:comment comment=" HOEHENMARKEN ---------------------------"/>
          <dc:call-macro name="basedata_4_heightmarks-points"/>
          <dc:comment comment=" AMTL LINIEN  ---------------------------"/>
          <dc:call-macro name="basedata_3_officials"/>
          <dc:call-macro name="basedata_5_flood-protections"/>
          <dc:call-macro name="annotations_per_type"/>
        </dc:macro>

        <dc:macro name="discharge_table_gauge">
          <discharge_table_nn>
            <discharge_table_gauge>
              <dc:context>
                <dc:statement>
                  SELECT id   AS gauge_id,
                  name AS gauge_name
                  FROM gauges WHERE river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <gauge name="{$gauge_name}"
                    db-id="{$gauge_id}"
                    factory="gaugedischarge"
                    from="{$g_start}"
                    to="{$g_stop}"
                    ids="{$gauge_name}:OUT={$OUT}"/>
                </dc:for-each>
              </dc:context>
            </discharge_table_gauge>
          </discharge_table_nn>
        </dc:macro>

        <dc:macro name="discharge_computed">
          <discharge_table_nn>
            <discharge_table_gauge>
              <dc:context>
                <dc:statement>
                  SELECT id   AS gauge_id,
                  name AS gauge_name
                  FROM gauges WHERE river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <gauge name="{$gauge_name}"
                    db-id="{$gauge_id}"
                    factory="gaugedischarge"
                    from="{$g_start}"
                    to="{$g_stop}"
                    ids="{$gauge_name}:OUT={$OUT}"/>
                  <dc:comment>
                    <!--
                    <gauge>
                      <dc:attribute name="name" value="${gauge_name}"/>
                      <dc:attribute name="db-id" value="${gauge_id}"/>
                      <dc:context>
                        <dc:statement>
                          SELECT description   AS gauge_desc,
                                 d.id          AS discharge_id,
                                 ti.start_time AS g_start,
                                 ti.stop_time  AS g_stop
                          FROM discharge_tables d JOIN time_intervals ti
                          ON d.time_interval_id = ti.id
                          WHERE d.gauge_id = ${gauge_id} AND d.kind = 1
                        </dc:statement>
                        <dc:for-each>
                          <historical>
                            <dc:attribute name="name" value="${gauge_desc}"/>
                            <dc:attribute name="factory" value="gaugedischarge"/>
                            <dc:attribute name="from" value="${g_start}"/>
                            <dc:attribute name="to" value="${g_stop}"/>
                            <dc:attribute name="ids" value="${discharge_id}-${g_start}-${g_stop}:OUT=${out}"/>
                          </historical>
                        </dc:for-each>
                      </dc:context>
                    </gauge>
                  -->
                  </dc:comment>
                </dc:for-each>
              </dc:context>
            </discharge_table_gauge>
          </discharge_table_nn>
        </dc:macro>

        <dc:macro name="discharge_fix_wq">
          <discharge_table_nn>
            <discharge_table_gauge>
              <dc:context>
                <dc:statement>
                  SELECT id   AS gauge_id,
                         name AS gauge_name
                  FROM gauges WHERE river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <gauge name="{$gauge_name}"
                         db-id="{$gauge_id}"
                         factory="gaugedischarge"
                         from="{$g_start}"
                         to="{$g_stop}"
                         ids="{$gauge_name}:OUT={$OUT}"/>
                </dc:for-each>
              </dc:context>
            </discharge_table_gauge>
          </discharge_table_nn>
        </dc:macro>

        <dc:choose>
          <dc:when test="dc:contains($parameters, 'recommended')">
            <dc:comment>
              Recommendations (client shall load immediately).
            </dc:comment>
            <dc:iterate var="out" container="artifact-outs">
              <dc:choose>
                <dc:when test="$out = 'w_differences'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'discharge_longitudinal_section'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'historical_discharge_wq'">
                  <dc:call-macro name="mainvalues"/>
                </dc:when>
                <dc:when test="$out = 'cross_section'">
                  <dc:call-macro name="cross_sections"/>
                  <dc:call-macro name="hyks"/>
                </dc:when>
                <dc:when test="$out = 'computed_discharge_curve'">
                  <dc:call-macro name="mainvalues"/>
                  <dc:call-macro name="basedata_2_fixations_wqkms"/>
                </dc:when>
                <dc:when test="$out = 'duration_curve'">
                  <dc:call-macro name="mainvalues"/>
                </dc:when>
                <dc:when test="$out = 'reference_curve'">
                  <dc:call-macro name="annotations"/>
                  <dc:call-macro name="mainvalues"/>
                </dc:when>
                <dc:when test="$out = 'fix_wq_curve'">
                  <dc:call-macro name="qsectors"/>
                </dc:when>
                <dc:when test="$out = 'longitudinal_section'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'fix_longitudinal_section_curve'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'bed_difference_epoch'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'bed_difference_year'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'bed_difference_height_year'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'floodmap'">
                  <dc:call-macro name="flood-map-recommended"/>
                </dc:when>
                <dc:when test="$out = 'floodmap-hws'">
                  <dc:call-macro name="flood-map-recommended"/>
                </dc:when>
                <dc:when test="$out = 'minfo-heights'">
                  <dc:call-macro name="minfo-heights"/>
                  <dc:macro name="minfo-heights">
                    <bedheights>
                      <dc:call-macro name="bed-heights-single"/>
                      <dc:call-macro name="bed-heights-epoch"/>
                    </bedheights>
                  </dc:macro>
                </dc:when>
                <dc:when test="$out = 'minfo-heights-epoch'">
                  <bedheights>
                    <dc:call-macro name="bed-heights-epoch"/>
                  </bedheights>
                </dc:when>
              </dc:choose>
            </dc:iterate>
          </dc:when>
          <dc:otherwise>
            <dc:comment>
              Non - Recommendations.
            </dc:comment>
            <dc:iterate var="out" container="artifact-outs">
              <dc:choose>
                <dc:when test="$out = 'cross_section'">
                  <dc:call-macro name="basedata_0"/>
                  <dc:call-macro name="basedata_1_additionals"/>
                  <dc:call-macro name="basedata_2_fixations"/>
                  <dc:call-macro name="basedata_3_officials"/>
                  <dc:call-macro name="basedata_4_heightmarks-points"/>
                  <dc:call-macro name="cross_sections"/>
                  <dc:call-macro name="hyks"/>
                </dc:when>
                <dc:when test="$out = 'longitudinal_section'">
                  <dc:call-macro name="longitudinal-section-prototype"/>
                </dc:when>
                <dc:when test="$out = 'w_differences'">
                  <dc:call-macro name="longitudinal-section-prototype"/>
                </dc:when>
                <dc:when test="$out = 'discharge_longitudinal_section'">
                  <dc:call-macro name="longitudinal-section-prototype"/>
                </dc:when>
                <dc:when test="$out = 'duration_curve'">
                  <dc:call-macro name="mainvalues"/>
                  <dc:call-macro name="basedata_2_fixations_relative_point"/>
                  <dc:call-macro name="basedata_4_heightmarks-points-relative_points"/>
                  <dc:call-macro name="basedata_5_flood-protections_relative_points"/>
                </dc:when>
                <dc:when test="$out = 'reference_curve'">
                  <dc:call-macro name="annotations"/>
                  <dc:call-macro name="basedata_1_additionals-relative_point"/>
                  <dc:comment comment=" FIXATIONS ---------------------------"/>
                  <dc:call-macro name="basedata_2_fixations_relative_point"/>
                  <dc:comment comment=" HOEHENMARKEN ---------------------------"/>
                  <dc:call-macro name="basedata_4_heightmarks-points-relative_points"/>
                  <dc:call-macro name="basedata_5_flood-protections_relative_points"/>
                </dc:when>
                <dc:when test="$out = 'fix_wq_curve'">
                  <dc:call-macro name="basedata_0_wq"/>
                  <dc:call-macro name="basedata_1_additionals_marks"/>
                  <dc:call-macro name="basedata_2_fixations_wqkms"/>
                  <dc:call-macro name="basedata_3_officials"/>
                  <dc:call-macro name="basedata_4_heightmarks-points"/>
                  <dc:call-macro name="basedata_5_flood-protections_relative_points"/>
                  <dc:call-macro name="discharge_table_gauge"/>
                  <dc:call-macro name="discharge_fix_wq"/>
                </dc:when>
                <dc:when test="$out = 'fix_deltawt_curve'">
                </dc:when>
                <dc:when test="$out = 'fix_longitudinal_section_curve'">
                  <dc:call-macro name="annotations"/>
                </dc:when>
                <dc:when test="$out = 'map'">
                  <dc:call-macro name="flood-map-complete"/>
                </dc:when>
                <dc:when test="$out = 'flow_velocity'">
                  <dc:call-macro name="annotations_per_type"/>
                  <dc:call-macro name="flow_velocity_measurements"/>
                </dc:when>
                <dc:when test="$out = 'bed_longitudinal_section'">
                  <dc:call-macro name="annotations_per_type"/>
                </dc:when>
                <dc:when test="$out = 'sedimentload_ls'">
                  <dc:call-macro name="annotations_per_type"/>
                  <dc:call-macro name="morph_width"/>
                </dc:when>
                <dc:comment>
                  MINFO bedheight middle
                </dc:comment>
                <dc:when test="$out = 'bedheight_middle'">
                  <dc:call-macro name="sounding-width"/>
                  <dc:call-macro name="basedata_0"/>
                  <dc:call-macro name="basedata_1_additionals"/>
                  <dc:call-macro name="basedata_2_fixations"/>
                  <dc:call-macro name="basedata_3_officials"/>
                  <dc:call-macro name="annotations_per_type"/>
                </dc:when>
                <dc:when test="$out = 'bed_difference_year'">
                  <dc:call-macro name="basedata_0"/>
                  <dc:call-macro name="basedata_1_additionals"/>
                  <dc:call-macro name="basedata_2_fixations"/>
                  <dc:call-macro name="basedata_3_officials"/>
                  <dc:call-macro name="annotations_per_type"/>
                  <dc:call-macro name="morph_width"/>
                </dc:when>
                <dc:when test="$out = 'bed_difference_epoch'">
                  <dc:call-macro name="basedata_0"/>
                  <dc:call-macro name="basedata_1_additionals"/>
                  <dc:call-macro name="basedata_2_fixations"/>
                  <dc:call-macro name="basedata_3_officials"/>
                  <dc:call-macro name="annotations_per_type"/>
                  <dc:call-macro name="morph_width"/>
                </dc:when>
                <dc:when test="$out = 'floodmap'">
                  <dc:choose>
                    <dc:when test="dc:contains($parameters, 'dem')">
                      <dc:call-macro name="flood-map-dem"/>
                    </dc:when>
                    <dc:when test="dc:contains($parameters, 'hws')">
                      <hws>
                        <dc:call-macro name="flood-map-hws-lines"/>
                        <dc:call-macro name="flood-map-hws-points"/>
                      </hws>
                    </dc:when>
                    <dc:otherwise>
                      <dc:call-macro name="flood-map-complete"/>
                    </dc:otherwise>
                  </dc:choose>
                </dc:when>
                <dc:when test="$out = 'floodmap-hws'">
                  <dc:choose>
                    <dc:when test="dc:contains($parameters, 'dem')">
                      <dc:call-macro name="flood-map-dem"/>
                    </dc:when>
                    <dc:when test="dc:contains($parameters, 'hws')">
                      <hws>
                        <dc:call-macro name="flood-map-hws-lines"/>
                        <dc:call-macro name="flood-map-hws-points"/>
                      </hws>
                    </dc:when>
                    <dc:otherwise>
                      <dc:call-macro name="flood-map-complete"/>
                    </dc:otherwise>
                  </dc:choose>
                </dc:when>
                <dc:when test="$out = 'computed_discharge_curve'">
                  <dc:call-macro name="discharge_computed"/>
                  <dc:call-macro name="basedata_2_fixations_wqkms"/>

                  <dc:call-macro name="basedata_5_flood-protections"/>

                  <!-- former waterlevels -->
                  <dc:call-macro name="basedata_0"/>

                  <dc:call-macro name="basedata_1_additionals"/>

                  <!-- former flood-water-marks -->
                  <dc:call-macro name="basedata_4_heightmarks-points"/>
                  <computed_discharge_curve>
                    <dc:call-macro name="mainvalues"/>
                  </computed_discharge_curve>
                </dc:when>
                <dc:when test="$out = 'minfo-heights'">
                  <dc:call-macro name="minfo-heights"/>
                  <dc:macro name="minfo-heights">
                    <bedheights>
                      <dc:call-macro name="bed-heights-single"/>
                      <dc:call-macro name="bed-heights-epoch"/>
                    </bedheights>
                  </dc:macro>
                </dc:when>
                <dc:when test="$out = 'minfo-heights-epoch'">
                  <bedheights>
                    <dc:call-macro name="bed-heights-epoch"/>
                  </bedheights>
                </dc:when>
              </dc:choose>
            </dc:iterate>
          </dc:otherwise>
        </dc:choose>


        <dc:if test="dc:contains($artifact-outs, 'floodmap') or dc:contains($artifact-outs, 'floodmap-hws')">
          <dc:macro name="flood-map-recommended">
            <dc:comment>
               FIXME: Following two macros look identical to me.
            </dc:comment>
            <kilometrage>
              <riveraxis factory="riveraxis" ids="{$river_id}:OUT={$OUT}"/>
            </kilometrage>
            <rastermap>
              <background factory="wmsbackground" ids="{$river_id}:OUT={$OUT}"/>
            </rastermap>
          </dc:macro>

          <dc:macro name="flood-map-dem">
            <dems>
              <dc:context>
                <dc:statement>
                  SELECT d.id    AS dem_id,
                         r.a     AS dem_lower,
                         r.b     AS dem_upper,
                         d.name  AS name,
                         t.start_time AS start_time,
                         t.stop_time AS stop_time,
                         'Projektion: ' || d.projection || '$' ||
                         'Rasterweite: '   || d.resolution || 'm$' ||
                         'Format: '  || d.format || '$' ||
                         'Zeitraum: '
                         AS info
                         FROM dem d
                         JOIN ranges r ON d.range_id = r.id
                         LEFT JOIN time_intervals t ON d.time_interval_id = t.id
                         WHERE d.river_id = ${river_id}
                </dc:statement>
                <dc:for-each>
                  <dem factory="demfactory" 
                    ids="{$dem_id}:OUT={$OUT}"
                    name="{$name}"
                    info="{dc:replace($info, '$', '&lt;BR&gt;')}{dc:date-format('yyyy', $start_time)} - {dc:date-format('yyyy', $stop_time)}"/>
                </dc:for-each>
              </dc:context>
            </dems>
          </dc:macro>

          <dc:macro name="filter_hws_ddg">
            <dc:macro name="durchlass_damm_graben">
              <dc:macro name="ddg_factory">
                <dc:for-each>
                  <hws factory="hwsfactory" name="{$hws_name}"/>
                </dc:for-each>
              </dc:macro>

              <dc:filter expr="$hws_kind=1">
                <dc:if test="dc:has-result()">
                  <Durchlass><dc:call-macro name="ddg_factory"/></Durchlass>
                </dc:if>
              </dc:filter>

              <dc:filter expr="$hws_kind=2">
                <dc:if test="dc:has-result()">
                  <Damm><dc:call-macro name="ddg_factory"/></Damm>
                </dc:if>
              </dc:filter>

              <dc:filter expr="$hws_kind=3">
                <dc:if test="dc:has-result()">
                  <Graben><dc:call-macro name="ddg_factory"/></Graben>
                </dc:if>
              </dc:filter>
            </dc:macro>

            <dc:filter expr="$hws_official=1">
              <dc:if test="dc:has-result()">
                <official>
                  <dc:call-macro name="durchlass_damm_graben"/>
                </official>
              </dc:if>
            </dc:filter>

            <dc:filter expr="$hws_official=0">
              <dc:if test="dc:has-result()">
                <inofficial>
                  <dc:call-macro name="durchlass_damm_graben"/>
                </inofficial>
              </dc:if>
            </dc:filter>
          </dc:macro>

          <dc:macro name="flood-map-hws-lines">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                       name     AS hws_name,
                       official AS hws_official,
                       kind_id  AS hws_kind
                FROM hws_lines
                WHERE river_id = ${river_id}
              </dc:statement>
              <dc:if test="dc:has-result()">
                <lines>
                  <dc:call-macro name="filter_hws_ddg"/>
                </lines>
              </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-hws-points">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                       name     AS hws_name,
                       official AS hws_official,
                       kind_id  AS hws_kind
                FROM hws_points
                WHERE river_id = ${river_id}
              </dc:statement>
              <dc:if test="dc:has-result()">
                <points>
                  <dc:call-macro name="filter_hws_ddg"/>
                </points>
              </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-km">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT 1 FROM river_axes_km WHERE river_id = ${river_id}
              </dc:statement>
               <dc:for-each>
                  <kilometrage factory="wmskmfactory" ids="{$river_id}:OUT={$OUT}"/>
              </dc:for-each>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-qps">
            <dc:context>
              <dc:comment>Grab only the actual first</dc:comment>
              <dc:statement>
                SELECT DISTINCT
                cs.kind_id AS kind_id,
                ck.name AS kind_name
                FROM cross_section_tracks cs
                JOIN cross_section_track_kinds ck on cs.kind_id = ck.id
                WHERE river_id = ${river_id}
                AND kind_id=1
              </dc:statement>
              <dc:if test="dc:has-result()">
                <dc:for-each>
                  <actual description="{$kind_name}"
                          factory="wmsqpsfactory"
                          ids="{$river_id};{$kind_name};{$kind_id}:OUT={$OUT}"/>
                </dc:for-each>
              </dc:if>
            </dc:context>
            <dc:context>
              <dc:comment>Now the other tracks</dc:comment>
              <dc:statement>
                SELECT DISTINCT
                cs.kind_id AS kind_id,
                ck.name AS kind_name,
                cs.name AS layer_name
                FROM cross_section_tracks cs
                JOIN cross_section_track_kinds ck on cs.kind_id = ck.id
                WHERE river_id = ${river_id}
                AND kind_id=0
              </dc:statement>
              <dc:if test="dc:has-result()">
                <other>
                  <dc:for-each>
                    <misc-qps description="{$layer_name}"
                              factory="wmsqpsfactory"
                              ids="{$river_id};{$layer_name};{$kind_id}:OUT={$OUT}"/>
                  </dc:for-each>
                </other>
              </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-riveraxis">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                ax.kind_id AS kind_id,
                ak.name AS kind_name
                FROM river_axes ax
                JOIN axis_kinds ak on ax.kind_id = ak.id
                WHERE river_id = ${river_id}
                AND kind_id=1
              </dc:statement>
              <dc:if test="dc:has-result()">
                <dc:for-each>
                  <actual description="{$kind_name}"
                    ids="{$river_id};{$kind_name};{$kind_id}:OUT={$OUT}"
                          factory="riveraxis"/>
                </dc:for-each>
              </dc:if>
            </dc:context>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                ak.name AS kind_name,
                ax.kind_id AS kind_id,
                ax.name AS layer_name
                FROM river_axes ax
                JOIN axis_kinds ak on ax.kind_id = ak.id
                WHERE river_id = ${river_id}
                AND kind_id &lt;&gt; 1
              </dc:statement>
              <dc:if test="dc:has-result()">
                <other>
                  <dc:for-each>
                    <misc-axis description="{$layer_name}"
                               ids="{$river_id};{$layer_name};{$kind_id}:OUT={$OUT}"
                               factory="riveraxis"/>
                  </dc:for-each>
                </other>
              </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-hydr-boundaries-state">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                name
                FROM hydr_boundaries
                WHERE river_id = ${river_id}
                AND kind = 2
              </dc:statement>
              <dc:for-each>
                <line factory="wmshydrboundariesfactory" 
                      ids="{$river_id};{$name};2:OUT={$OUT}"
                      name="{$name}"/>
              </dc:for-each>
            </dc:context>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                name
                FROM hydr_boundaries_poly
                WHERE river_id = ${river_id}
                AND kind = 2
              </dc:statement>
              <dc:for-each>
                <line factory="wmshydrboundariespolyfactory"
                      ids="{$river_id};{$name};2:OUT={$OUT}"
                      name="{$name}"/>
              </dc:for-each>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-hydr-boundaries-lines">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                name
                FROM hydr_boundaries
                WHERE river_id = ${river_id}
                AND kind = 1
              </dc:statement>
              <dc:comment> What about all other line kinds?</dc:comment>
              <dc:if test="dc:has-result()">
                <lines>
                  <dc:for-each>
                    <line factory="wmshydrboundariesfactory"
                          ids="{$river_id};{$name};1:OUT={$OUT}"
                          name="{$name}"/>
                  </dc:for-each>
                </lines>
              </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-hydr-boundaries-poly">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                b.sectie AS sectie_id,
                sk.name AS sectie
                FROM hydr_boundaries_poly b
                JOIN sectie_kinds sk ON b.sectie = sk.id
                WHERE b.river_id = ${river_id}
                AND b.kind = 1
              </dc:statement>
              <dc:if test="dc:has-result()">
                <sobek_areas>
                  <dc:for-each>
                    <boundary name="{$sectie}"
                              factory="wmshydrboundariespolyfactory"
                              ids="{$river_id};{$sectie};1;{$sectie_id};-1:OUT={$OUT}"/>
                  </dc:for-each>
                </sobek_areas>
              </dc:if>
            </dc:context>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                b.sobek AS sobek_id,
                sk.name AS sobek
                FROM hydr_boundaries_poly b
                JOIN sobek_kinds sk ON b.sobek = sk.id
                WHERE b.river_id = ${river_id}
                AND b.kind = 1
              </dc:statement>
              <dc:if test="dc:has-result()">
                <sobek_flooded>
                  <dc:for-each>
                    <boundary name="{$sobek}"
                              factory="wmshydrboundariespolyfactory"
                              ids="{$river_id};{$sobek};1;-1;{$sobek_id}:OUT={$OUT}"/>
                  </dc:for-each>
                </sobek_flooded>
              </dc:if>
            </dc:context>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                b.name AS name
                FROM hydr_boundaries_poly b
                WHERE b.river_id = ${river_id}
                AND b.kind = 1
                AND b.sobek IS NULL
                AND b.sectie is NULL
              </dc:statement>
              <dc:for-each>
                <boundary name="{$name}"
                          factory="wmshydrboundariespolyfactory"
                          ids="{$river_id};{$name}:OUT={$OUT}"/>
              </dc:for-each>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-hydr-boundaries">
            <bfg_model>
              <areas>
                <dc:call-macro name="flood-map-hydr-boundaries-poly"/>
              </areas>
              <dc:call-macro name="flood-map-hydr-boundaries-lines"/>
            </bfg_model>
            <federal>
              <dc:call-macro name="flood-map-hydr-boundaries-state"/>
            </federal>
          </dc:macro>

          <dc:macro name="flood-map-floodplain">
            <floodplain>
              <dc:context>
                <dc:statement>
                  SELECT DISTINCT
                  fp.kind_id AS kind_id,
                  flk.name AS kind_name
                  FROM floodplain fp
                  JOIN floodplain_kinds flk on fp.kind_id = flk.id
                  WHERE river_id = ${river_id}
                  AND kind_id=1
                </dc:statement>
                <dc:if test="dc:has-result()">
                  <dc:for-each>
                    <floody factory="wmsfloodplainfactory"
                      description="{$kind_name}"
                      ids="{$river_id};{$kind_name};{$kind_id}:OUT={$OUT}"/>
                  </dc:for-each>
                </dc:if>
              </dc:context>
              <dc:context>
                <dc:statement>
                  SELECT DISTINCT
                  flk.name AS kind_name,
                  fp.kind_id AS kind_id,
                  fp.name AS layer_name
                  FROM floodplain fp
                  JOIN floodplain_kinds flk on fp.kind_id = flk.id
                  WHERE river_id = ${river_id}
                  AND kind_id &lt;&gt; 1
                </dc:statement>
                <dc:if test="dc:has-result()">
                  <other>
                    <dc:for-each>
                      <floody factory="wmsfloodplainfactory"
                        description="{$layer_name}"
                        ids="{$river_id};{$layer_name};{$kind_id}:OUT={$OUT}"/>
                    </dc:for-each>
                  </other>
                </dc:if>
              </dc:context>
            </floodplain>
          </dc:macro>

          <dc:macro name="hwslines_by_kind">
            <dc:comment>
              Call from a context where fed_name hws_kind hws_name and river_id is
              availble
            </dc:comment>

            <dc:macro name="hwslines_by_kind_factory">
              <dc:for-each>
                <hws factory="wmshwslinesfactory"
                     ids="{$river_id};{$hws_name}:OUT={$OUT}"
                     name="{$hws_name}"/>
              </dc:for-each>
            </dc:macro>

            <dc:filter expr="$hws_kind=1">
              <dc:if test="dc:has-result()">
                <Durchlass>
                  <dc:call-macro name="hwslines_by_kind_factory"/>
                </Durchlass>
              </dc:if>
            </dc:filter>

            <dc:filter expr="$hws_kind=2">
              <dc:if test="dc:has-result()">
                <Damm>
                  <dc:call-macro name="hwslines_by_kind_factory"/>
                </Damm>
              </dc:if>
            </dc:filter>

            <dc:filter expr="$hws_kind=3">
              <dc:if test="dc:has-result()">
                <Graben>
                  <dc:call-macro name="hwslines_by_kind_factory"/>
                </Graben>
              </dc:if>
            </dc:filter>
          </dc:macro>

          <dc:macro name="hwslines">
            <hws_lines>
              <official>
                <dc:context>
                  <dc:statement>
                    SELECT DISTINCT
                    fs.name AS fed_name,
                    fs.id AS fed_id
                    FROM hws_lines hws
                    JOIN fed_states fs ON hws.fed_state_id = fs.id
                    WHERE river_id = ${river_id}
                    AND hws.official=1
                  </dc:statement>
                  <dc:for-each>
                    <dc:context>
                      <dc:statement>
                        SELECT DISTINCT
                        name AS hws_name,
                        kind_id AS hws_kind
                        FROM hws_lines
                        WHERE river_id = ${river_id}
                        AND official=1
                        AND fed_state_id = ${fed_id} ORDER BY name
                      </dc:statement>
                      <fedstate description="{$fed_name}">
                        <dc:call-macro name="hwslines_by_kind"/>
                      </fedstate>
                    </dc:context>
                  </dc:for-each>
                </dc:context>
                <dc:context>
                  <dc:statement>
                    SELECT distinct
                    name AS hws_name,
                    kind_id AS hws_kind
                    FROM hws_lines
                    WHERE river_id = ${river_id}
                    AND official=1
                    AND fed_state_id IS NULL
                    ORDER BY name
                  </dc:statement>
                  <hws_fed_unknown>
                    <dc:call-macro name="hwslines_by_kind"/>
                  </hws_fed_unknown>
                </dc:context>
              </official>
              <inofficial>
                <dc:context>
                  <dc:statement>
                    SELECT DISTINCT
                    fs.name AS fed_name,
                    fs.id AS fed_id
                    FROM hws_lines hws
                    JOIN fed_states fs ON hws.fed_state_id = fs.id
                    WHERE river_id = ${river_id}
                    AND hws.official=0
                  </dc:statement>
                  <dc:for-each>
                    <dc:context>
                      <dc:statement>
                        SELECT DISTINCT
                        name AS hws_name,
                        kind_id AS hws_kind
                        FROM hws_lines
                        WHERE river_id = ${river_id}
                        AND official=0
                        AND fed_state_id = ${fed_id} ORDER BY name
                      </dc:statement>
                      <fedstate description="{$fed_name}">
                        <dc:call-macro name="hwslines_by_kind"/>
                      </fedstate>
                    </dc:context>
                  </dc:for-each>
                </dc:context>
                <dc:context>
                  <dc:statement>
                    SELECT distinct
                    name AS hws_name,
                    kind_id AS hws_kind
                    FROM hws_lines
                    WHERE river_id = ${river_id}
                    AND official=0
                    AND fed_state_id IS NULL ORDER BY name
                  </dc:statement>
                  <hws_fed_unknown>
                    <dc:call-macro name="hwslines_by_kind"/>
                  </hws_fed_unknown>
                </dc:context>
              </inofficial>
            </hws_lines>
          </dc:macro>

          <dc:macro name="hwspoints_by_kind">
            <dc:comment>
              Call from a context where fed_name hws_kind hws_name and river_id is
              availble
            </dc:comment>

            <dc:macro name="hwspoints_by_kind_factory">
              <dc:for-each>
                <hws factory="wmshwspointsfactory"
                     ids="{$river_id};{$hws_name}:OUT={$OUT}"
                     name="{$hws_name}"/>
              </dc:for-each>
            </dc:macro>

            <dc:filter expr="$hws_kind=1">
              <dc:if test="dc:has-result()">
                <Durchlass>
                  <dc:call-macro name="hwspoints_by_kind_factory"/>
                </Durchlass>
              </dc:if>
            </dc:filter>

            <dc:filter expr="$hws_kind=2">
              <dc:if test="dc:has-result()">
                <Damm>
                  <dc:call-macro name="hwspoints_by_kind_factory"/>
                </Damm>
              </dc:if>
            </dc:filter>

            <dc:filter expr="$hws_kind=3">
              <dc:if test="dc:has-result()">
                <Graben>
                  <dc:call-macro name="hwspoints_by_kind_factory"/>
                </Graben>
              </dc:if>
            </dc:filter>
          </dc:macro>

          <dc:macro name="hwspoints">
            <hws_points>
              <official>
                <dc:context>
                  <dc:statement>
                    SELECT DISTINCT
                    fs.name AS fed_name,
                    fs.id AS fed_id
                    FROM hws_points hws
                    JOIN fed_states fs ON hws.fed_state_id = fs.id
                    WHERE river_id = ${river_id}
                    AND hws.official=1
                  </dc:statement>
                  <dc:for-each>
                    <dc:context>
                      <dc:statement>
                        SELECT DISTINCT
                        name AS hws_name,
                        kind_id AS hws_kind
                        FROM hws_points
                        WHERE river_id = ${river_id}
                        AND official=1
                        AND fed_state_id = ${fed_id} ORDER BY name
                      </dc:statement>
                      <fedstate description="{$fed_name}">
                        <dc:call-macro name="hwspoints_by_kind"/>
                      </fedstate>
                    </dc:context>
                  </dc:for-each>
                </dc:context>
                <dc:context>
                  <dc:statement>
                    SELECT distinct
                    name AS hws_name,
                    kind_id AS hws_kind
                    FROM hws_points
                    WHERE river_id = ${river_id}
                    AND official=1
                    AND fed_state_id IS NULL
                    ORDER BY name
                  </dc:statement>
                  <hws_fed_unknown>
                    <dc:call-macro name="hwspoints_by_kind"/>
                  </hws_fed_unknown>
                </dc:context>
              </official>
              <inofficial>
                <dc:context>
                  <dc:statement>
                    SELECT DISTINCT
                    fs.name AS fed_name,
                    fs.id AS fed_id
                    FROM hws_points hws
                    JOIN fed_states fs ON hws.fed_state_id = fs.id
                    WHERE river_id = ${river_id}
                    AND hws.official=0
                  </dc:statement>
                  <dc:for-each>
                    <dc:context>
                      <dc:statement>
                        SELECT DISTINCT
                        name AS hws_name,
                        kind_id AS hws_kind
                        FROM hws_points
                        WHERE river_id = ${river_id}
                        AND official=0
                        AND fed_state_id = ${fed_id} ORDER BY name
                      </dc:statement>
                      <fedstate description="{$fed_name}">
                        <dc:call-macro name="hwspoints_by_kind"/>
                      </fedstate>
                    </dc:context>
                  </dc:for-each>
                </dc:context>
                <dc:context>
                  <dc:statement>
                    SELECT distinct
                    name AS hws_name,
                    kind_id AS hws_kind
                    FROM hws_points
                    WHERE river_id = ${river_id}
                    AND official=0
                    AND fed_state_id IS NULL ORDER BY name
                  </dc:statement>
                  <hws_fed_unknown>
                    <dc:call-macro name="hwspoints_by_kind"/>
                  </hws_fed_unknown>
                </dc:context>
              </inofficial>
            </hws_points>
          </dc:macro>

          <dc:macro name="flood-map-buildings">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                b.kind_id AS building_kind_id,
                bk.name AS building_kind
                FROM buildings b
                JOIN building_kinds bk ON b.kind_id = bk.id
                WHERE b.river_id = ${river_id}
                AND b.kind_id &lt;&gt; 0
              </dc:statement>
              <dc:for-each>
                <buildings description="{$building_kind}"
                           factory="wmsbuildingsfactory"
                           ids="{$river_id};{$building_kind};{$building_kind_id}:OUT={$OUT}"/>
              </dc:for-each>
            </dc:context>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                b.name AS building_name,
                bk.name AS building_kind,
                b.kind_id AS building_kind_id
                FROM buildings b
                JOIN building_kinds bk ON b.kind_id = bk.id
                WHERE river_id = ${river_id}
                AND kind_id = 0
              </dc:statement>
              <dc:if test="dc:has-result()">
                <other>
                  <dc:for-each>
                    <buildings description="{$building_name}"
                      factory="wmsbuildingsfactory"
                      ids="{$river_id};{$building_name}:OUT={$OUT}"/>
                  </dc:for-each>
                </other>
              </dc:if>
            </dc:context>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                j.kind_id AS jetty_kind_id,
                jk.name AS jetty_kind
                FROM jetties j
                JOIN jetty_kinds jk ON j.kind_id = jk.id
                WHERE river_id = ${river_id}
              </dc:statement>
              <dc:if test="dc:has-result()">
                <jetties>
                  <dc:for-each>
                    <jetty description="{$jetty_kind}"
                           factory="wmsjettiesfactory"
                           ids="{$river_id};{$jetty_kind};{$jetty_kind_id}:OUT={$OUT}"/>
                  </dc:for-each>
                </jetties>
              </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-fixpoints">
            <dc:context>
              <dc:statement>
                SELECT name AS name
                FROM fixpoints WHERE river_id = ${river_id} GROUP BY name
              </dc:statement>
              <dc:for-each>
                <fixpoints factory="wmsfixpointsfactory"
                  ids="{$river_id};{$name}:OUT={$OUT}"/>
              </dc:for-each>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-gaugelocations">
            <gauge_points factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelpunkte;Pegelpunkte (WSV):OUT={$OUT}"/>
            <gauge_names factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelnamen;Pegelnamen (WSV):OUT={$OUT}"/>
            <gauge_level factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelwasserstand;Aktueller Wasserstand (WSV):OUT={$OUT}"/>
            <gauge_tendency factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;TendenzWasserstand;Tendenz des Wasserstands (WSV):OUT={$OUT}"/>
          </dc:macro>

          <dc:macro name="flood-map-uesk">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT 1 from floodmaps where river_id = ${river_id}
              </dc:statement>
              <dc:if test="dc:has-result()">
              <uesk>
                <calculations>
                  <dc:context>
                    <dc:statement>
                      SELECT DISTINCT 1 from floodmaps where river_id = ${river_id} 
                      AND (kind = 112 OR kind = 111)
                    </dc:statement>
                    <dc:if test="dc:has-result()">
                      <current>
                        <dc:context>
                          <dc:statement>
                            SELECT DISTINCT name AS name
                            FROM floodmaps
                            WHERE river_id = ${river_id} AND kind = 111
                          </dc:statement>
                          <dc:if test="dc:has-result()">
                            <bfg>
                              <dc:for-each>
                                <floodmaps factory="wmsfloodmapsfactory"
                                  ids="{$river_id};{$name}:OUT={$OUT}"
                                  name="{$name}"/>
                              </dc:for-each>
                            </bfg>
                          </dc:if>
                        </dc:context>
                        <dc:context>
                          <dc:statement>
                            SELECT DISTINCT name AS name
                            FROM floodmaps
                            WHERE river_id = ${river_id} AND kind = 112
                          </dc:statement>
                          <dc:if test="dc:has-result()">
                            <federal>
                              <dc:for-each>
                                <floodmaps factory="wmsfloodmapsfactory"
                                  ids="{$river_id};{$name}:OUT={$OUT}"
                                  name="{$name}"/>
                              </dc:for-each>
                            </federal>
                          </dc:if>
                        </dc:context>
                      </current>
                    </dc:if>
                  </dc:context>
                  <dc:context>
                    <dc:statement>
                      SELECT DISTINCT 1 from floodmaps where river_id = ${river_id}
                      AND (kind = 122 OR kind = 121)
                    </dc:statement>
                    <dc:if test="dc:has-result()">
                      <potential>
                        <dc:context>
                          <dc:statement>
                            SELECT DISTINCT name AS name
                            FROM floodmaps
                            WHERE river_id = ${river_id} AND kind = 121
                          </dc:statement>
                          <dc:if test="dc:has-result()">
                            <bfg>
                              <dc:for-each>
                                <floodmaps factory="wmsfloodmapsfactory"
                                  ids="{$river_id};{$name}:OUT={$OUT}"
                                  name="{$name}"/>
                              </dc:for-each>
                            </bfg>
                          </dc:if>
                        </dc:context>
                        <dc:context>
                          <dc:statement>
                            SELECT DISTINCT
                            name AS name
                            FROM floodmaps
                            WHERE river_id = ${river_id} AND kind = 122
                          </dc:statement>
                          <dc:if test="dc:has-result()">
                            <federal>
                              <dc:for-each>
                                <floodmaps factory="wmsfloodmapsfactory"
                                  ids="{$river_id};{$name}:OUT={$OUT}"
                                  name="{$name}"/>
                              </dc:for-each>
                            </federal>
                          </dc:if>
                        </dc:context>
                      </potential>
                    </dc:if>
                  </dc:context>
                </calculations>
                <dc:context>
                  <dc:statement>
                    SELECT DISTINCT
                    source AS source
                    FROM floodmaps
                    WHERE river_id = ${river_id} AND kind = 200
                  </dc:statement>
                  <dc:if test="dc:has-result()">
                    <measurements>
                      <dc:for-each>
                        <year name="{$source}">
                          <dc:context>
                            <dc:statement>
                              SELECT DISTINCT
                              name AS name
                              FROM floodmaps
                              WHERE river_id = ${river_id} AND kind = 200 AND source =
                              ${source}
                            </dc:statement>
                            <dc:for-each>
                              <floodmaps factory="wmsfloodmapsfactory"
                                ids="{$river_id};{$name}:OUT={$OUT}"
                                name="{$name}"/>
                            </dc:for-each>
                          </dc:context>
                        </year>
                      </dc:for-each>
                    </measurements>
                  </dc:if>
                </dc:context>
              </uesk>
            </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-routing">
            <qps>
              <dc:call-macro name="flood-map-qps"/>
            </qps>
            <dc:call-macro name="flood-map-fixpoints"/>
            <dc:call-macro name="flood-map-km"/>
            <axis>
              <dc:call-macro name="flood-map-riveraxis"/>
            </axis>
          </dc:macro>

          <dc:macro name="floodmarks">
            <dc:context>
              <dc:statement>
                SELECT DISTINCT
                coalesce(CAST (year AS VARCHAR(64)), 'Unbekanntes Jahr') as year
                FROM flood_marks
                WHERE river_id = ${river_id}
              </dc:statement>
              <dc:if test="dc:has-result()">
                <floodmarks>
                  <dc:for-each>
                    <floodmark name="{$year}"
                      factory="wmsfloodmarkfactory"
                      ids="{$river_id};{$year};{$year}:OUT={$OUT}"/>
                  </dc:for-each>
                </floodmarks>
              </dc:if>
            </dc:context>
          </dc:macro>

          <dc:macro name="flood-map-complete">
            <buildings>
              <dc:call-macro name="flood-map-buildings"/>
          </buildings>
          <catchments>
              <catchment_wms factory="externalwmsfactory"
                  ids="http://geoportal.bafg.de/wmsproxy/INSPIRE/DrainageBasin;HY.PHYSICALWATERS.CATCHMENTS;Einzugsgebiete (WMS):OUT={$OUT}" />
              <gaugelocations>
                  <dc:call-macro name="flood-map-gaugelocations"/>
              </gaugelocations>
          </catchments>
            <dc:call-macro name="floodmarks"/>
            <hws>
              <dc:call-macro name="hwslines"/>
              <dc:call-macro name="hwspoints"/>
            </hws>
            <route_data>
              <dc:call-macro name="flood-map-routing"/>
            </route_data>
            <hydrboundaries>
              <dc:call-macro name="flood-map-floodplain"/>
              <dc:call-macro name="flood-map-hydr-boundaries"/>
            </hydrboundaries>
            <dc:call-macro name="flood-map-uesk"/>
          </dc:macro>

        </dc:if>

        <dc:macro name="bed-heights-single">
          <single>
            <dc:context>
              <dc:statement>
                SELECT id          AS bedh_id,
                       year        AS bedh_year,
                       description AS bedh_descr
                FROM bed_height_single WHERE river_id = ${river_id}
              </dc:statement>
              <dc:for-each>
                <height factory="bedheight"
                        ids="bedheight-single-{$bedh_id}-{$bedh_year}:OUT={$OUT}"
                        description="{$bedh_descr}"/>
              </dc:for-each>
            </dc:context>
          </single>
        </dc:macro>

        <dc:macro name="bed-heights-epoch">
          <epoch>
            <dc:context>
              <dc:statement>
                SELECT id               AS bedh_id,
                       time_interval_id AS bedh_interval_id,
                       description      AS bedh_descr
                FROM bed_height_epoch WHERE river_id = ${river_id}
              </dc:statement>
              <dc:for-each>
                <height factory="bedheight"
                        ids="bedheight-epoch-{$bedh_id}-{$bedh_interval_id}:OUT={$OUT}"
                        description="{$bedh_descr}"/>
              </dc:for-each>
            </dc:context>
          </epoch>
        </dc:macro>

      </dc:for-each>
    </dc:context>
  </dc:macro>

  <dc:choose>
    <dc:comment>
      User specific part
      ------------------
    </dc:comment>
    <dc:when test="dc:contains($parameters, 'user-id') and
      not(dc:contains($artifact-outs, 'floodmap-hws'))">

      <old_calculations>

        <dc:comment><!-- <dc:macro name="load-user">--></dc:comment>
        <dc:call-macro name="user-range">
          <dc:context connection="user">
            <dc:comment> Get the user and collection-id. </dc:comment>
            <dc:statement>
              SELECT u.id AS user_id, c.id AS collection_id, c.name AS collection_name
              FROM collections c JOIN users u ON c.user_id = u.id
              WHERE u.gid = CAST(${user-id} AS uuid)
              ORDER BY c.creation DESC
            </dc:statement>

            <dc:macro name="range-filter">
              <dc:statement>
                SELECT m.id       AS a_id,
                       m.state    AS a_state,
                       m.gid      AS a_gid,
                       m.creation AS a_creation,
                       COALESCE(ld_mode, '')      AS ld_m,
                       COALESCE(ld_locations, '') AS ld_l,
                       COALESCE(ld_from, '')      AS ld_f,
                       COALESCE(ld_to, '')        AS ld_t
                FROM   master_artifacts_range m
                WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
                AND EXISTS (
                    SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
              </dc:statement>
              <dc:for-each>
                <dc:variable name="from" type="number" expr="dc:fromValue($ld_m, $ld_l, $ld_f)"/>
                <dc:variable name="to" type="number" expr="dc:toValue($ld_m, $ld_l, $ld_t)"/>
                <dc:if test="($from &gt;= $fromkm and $from &lt;= $tokm) or ($to &lt;= $tokm and $to &gt;= $fromkm) or ($from &lt;= $fromkm and $to &gt;= $tokm)">
                  <dc:macro-body/>
                </dc:if>
              </dc:for-each>
            </dc:macro>

            <!-- OFFICIAL LINES -->
            <dc:macro name="officiallines_user">
              <dc:comment comment=".wst -------------------------------"/>
              <officiallines>
                <dc:for-each>
                  <dc:context>
                    <dc:statement>
                      SELECT m.id AS a_id,
                      m.state AS a_state,
                      m.gid AS a_gid,
                      m.creation AS a_creation,
                      ardg.v AS gaugy,
                      arv.v AS wqsingle
                      FROM   master_artifacts m,
                      artifact_data ardg,
                      artifact_data arv
                      WHERE  m.collection_id = ${collection_id}
                      AND m.gid = CAST(${artifact-id} AS uuid)
                      AND ardg.artifact_id = m.id
                      AND ardg.k = 'ld_gaugename'
                      AND arv.artifact_id = m.id
                      AND arv.k = 'wq_single'
                      AND EXISTS (
                      SELECT id
                      FROM artifact_data ad
                      WHERE ad.artifact_id = m.id
                      AND k = 'river'
                      AND v = ${river})
                    </dc:statement>
                    <dc:for-each>
                      <dc:context connection="system">
                        <dc:statement>
                          SELECT ol.wst_id AS wstid, ol.wst_column_pos AS wstcolpos, ol.name AS olname, ol.value AS oval
                          FROM official_q_values ol
                          WHERE ol.value = CAST(${wqsingle} AS NUMERIC(10,2)) AND ol.gauge_name = ${gaugy}
                        </dc:statement>
                        <dc:for-each>
                          <dc:element name="${olname}">
                            <dc:attribute name="name" value="${olname}"/>
                            <dc:attribute name="ids" value="additionals-wstv-${wstcolpos}-${wstid}:OUT=${out}"/>
                            <dc:attribute name="factory" value="staticwkms"/>
                          </dc:element>
                        </dc:for-each>
                      </dc:context>
                    </dc:for-each>
                  </dc:context>
                </dc:for-each>
              </officiallines>
            </dc:macro>
            <!-- END OFFICIAL LINES -->

            <dc:comment>
              SHOW W-DIFFERENCES
            </dc:comment>

            <dc:macro name="differences">
              <differences>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT a.gid AS aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description AS facet_description
                          FROM outs AS o, facets AS f, artifacts AS a
                          WHERE f.name = 'w_differences' and f.out_id = o.id and o.artifact_id = ${a_id} and a.id = ${a_id}
                        </dc:statement>
                        <dc:for-each>
                          <dc:element name="${facet_name}">
                            <dc:attribute name="description" value="${facet_description}"/>
                            <dc:attribute name="factory" value="winfo"/>
                            <dc:attribute name="artifact-id" value="${aid}"/>
                            <dc:attribute name="ids" value="${aid}:OUT=${out}"/>
                            <dc:attribute name="out" value="w_differences"/>
                          </dc:element>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </differences>
            </dc:macro>

            <dc:comment>
              SHOW REFERENCE CURVE
            </dc:comment>

            <dc:macro name="reference-curves">
              <reference_curves>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="user-range">
                      <dc:context>
                        <dc:statement>
                          SELECT a.gid AS aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description AS facet_description
                          FROM outs AS o, facets AS f, artifacts AS a
                          WHERE f.name = 'reference_curve' and f.out_id = o.id and o.artifact_id = ${a_id} and a.id = ${a_id}
                        </dc:statement>
                        <dc:for-each>
                          <dc:element name="${facet_name}">
                            <dc:attribute name="description" value="${facet_description}"/>
                            <dc:attribute name="factory" value="winfo"/>
                            <dc:attribute name="artifact-id" value="${aid}"/>
                            <dc:attribute name="ids" value="${aid}:OUT=${out}"/>
                            <dc:attribute name="out" value="reference_curve"/>
                          </dc:element>
                        </dc:for-each>
                      </dc:context>
                     </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </reference_curves>
            </dc:macro>

            <dc:comment>
              SHOW COMPUTED DISCHARGE CURVES
            </dc:comment>

            <dc:macro name="computed-discharge-curve">
              <computed_discharge_curves>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT a.gid AS aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description AS facet_description
                          FROM outs AS o, facets AS f, artifacts AS a
                          WHERE f.name = 'computed_discharge_curve.q' and f.out_id = o.id and o.artifact_id = ${a_id} and a.id = ${a_id}
                        </dc:statement>
                        <dc:for-each>
                          <dc:element name="${facet_name}">
                            <dc:attribute name="description" value="${facet_description}"/>
                            <dc:attribute name="factory" value="winfo"/>
                            <dc:attribute name="artifact-id" value="${aid}"/>
                            <dc:attribute name="ids" value="${aid}:OUT=${out}"/>
                            <dc:attribute name="out" value="computed_discharge_curve"/>
                          </dc:element>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </computed_discharge_curves>
            </dc:macro>

            <dc:comment>
              CROSS SECTION
            </dc:comment>

            <dc:macro name="waterlevels">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'cross_section'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id}
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <longitudinal_section_columns description="{$river} {$a_creation}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="winfo"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="cross_section"/>
                                </dc:element>
                              </dc:for-each>
                            </longitudinal_section_columns>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:macro name="longitudinal">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'longitudinal_section'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id}
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <longitudinal_section_columns description="{$river} {$a_creation}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="winfo"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </longitudinal_section_columns>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:macro name="longitudinal-section">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'fix_longitudinal_section_curve'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:comment><!-- average und deviation ls_0 . ls_1 ...--></dc:comment>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id} AND ( 
                                  name LIKE 'fix_deviation_ls%'       OR 
                                  name LIKE 'fix_sector_average_ls%'  OR 
                                  name LIKE 'fix_analysis_events_ls%' OR 
                                  name LIKE 'fix_reference_events_ls%' )
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <waterlevels description="{$river} {$a_creation} {$collection_name}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="fixanalysis"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="fix_longitudinal_section_curve"/>
                                </dc:element>
                              </dc:for-each>
                            </waterlevels>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:macro name="delta-wt">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'fix_deltawt_curve'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id} and (
                                name LIKE 'fix_sector_average_dwt%' OR
                                name LIKE 'fix_deviation_dwt%'      OR
                                name = 'fix_analysis_events_dwt'    OR
                                name = 'fix_reference_events_dwt'   OR
                                name = 'fix_analysis_periods_dwt' )
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <waterlevels description="{$river} {$a_creation} {$collection_name}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="fixanalysis"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="fix_deltawt_curve"/>
                                </dc:element>
                              </dc:for-each>
                            </waterlevels>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:macro name="delta-wt-ls">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'fix_deltawt_curve'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id} and (
                                name LIKE 'fix_sector_average_dwt%' OR
                                name LIKE 'fix_deviation_dwt%')
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <waterlevels description="{$river} {$a_creation} {$collection_name}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="fixanalysis"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="fix_deltawt_curve"/>
                                </dc:element>
                              </dc:for-each>
                            </waterlevels>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:macro name="fix-derivate-curve">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'fix_derivate_curve'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id} and name = 'fix_derivate_curve'
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <waterlevels description="{$river} {$a_creation} {$collection_name}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="fixanalysis"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="fix_derivate_curve"/>
                                </dc:element>
                              </dc:for-each>
                            </waterlevels>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:macro name="fix-wq-curve">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'fix_wq_curve'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id} and (
                                name LIKE 'fix_sector_average_wq%'  OR
                                name = 'fix_wq_curve'               OR
                                name LIKE 'fix_analysis_events_wq%' OR
                                name LIKE 'fix_reference_events_wq%' )
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <waterlevels description="{$river} {$a_creation} {$collection_name}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="fixanalysis"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="fix_wq_curve"/>
                                </dc:element>
                              </dc:for-each>
                            </waterlevels>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:macro name="duration-curve">
              <computed_discharge_curves>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT a.gid         AS aid,
                                 f.id          AS fid,
                                 f.name        AS facet_name,
                                 f.num         AS facet_num,
                                 f.description AS facet_description
                          FROM outs AS o, facets AS f, artifacts AS a
                          WHERE
                              (f.name = 'duration_curve.q' OR f.name = 'duration_curve.w') AND 
                              f.out_id = o.id         AND
                              o.artifact_id = ${a_id} AND
                              a.id = ${a_id}
                        </dc:statement>
                        <dc:for-each>
                          <dc:element name="${facet_name}">
                            <dc:attribute name="description" value="${facet_description}"/>
                            <dc:attribute name="factory" value="winfo"/>
                            <dc:attribute name="artifact-id" value="${aid}"/>
                            <dc:attribute name="ids" value="${aid}:OUT=${out}"/>
                            <dc:attribute name="out" value="duration_curve"/>
                          </dc:element>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </computed_discharge_curves>
            </dc:macro>

            <dc:comment>
               WATERLEVELS - ONLY SHOW Ws
            </dc:comment>

            <dc:comment><!-- TODO doesnt work nicely for fix/wq-diags. --></dc:comment>

            <dc:macro name="waterlevels-fix">
              <waterlevels>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS out_id
                          FROM outs
                          WHERE artifact_id = ${a_id} AND name = 'longitudinal_section'
                        </dc:statement>
                        <dc:for-each>
                          <dc:context>
                            <dc:statement>
                              SELECT name AS facet_name, num AS facet_num, description AS facet_description
                              FROM facets
                              WHERE out_id = ${out_id} and name = 'longitudinal_section.w'
                              ORDER BY num ASC, name DESC
                            </dc:statement>
                            <waterlevels description="{$river} {$a_creation} {$collection_name}">
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                                  <dc:attribute name="factory" value="winfo"/>
                                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                                  <dc:attribute name="out" value="longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </waterlevels>
                          </dc:context>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels>
            </dc:macro>

            <dc:comment>
              SHOW FLOODMAPS
            </dc:comment>

            <dc:macro name="flood-map">
              <floodmap>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT a.gid         AS aid,
                                 f.id          AS fid,
                                 f.name        AS facet_name,
                                 f.num         AS facet_num,
                                 f.description AS facet_description
                          FROM outs AS o, facets AS f, artifacts AS a
                          WHERE f.name = 'floodmap.wsplgen' AND
                                f.out_id = o.id             AND
                                o.artifact_id = ${a_id}     AND
                                a.id = ${a_id}
                        </dc:statement>
                        <dc:for-each>
                          <dc:element name="${facet_name}">
                            <dc:attribute name="description" value="${facet_description}"/>
                            <dc:attribute name="factory" value="winfo"/>
                            <dc:attribute name="artifact-id" value="${aid}"/>
                            <dc:attribute name="ids" value="${aid}:OUT=${out}"/>
                            <dc:attribute name="out" value="floodmap"/>
                          </dc:element>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </floodmap>
            </dc:macro>

            <dc:comment>
              MINFO bedheight middle
            </dc:comment>

            <dc:macro name="bed-height">
              <fix_vollmer_wq_curve>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT a.gid         AS aid,
                                 f.id          AS fid,
                                 f.name        AS facet_name,
                                 f.num         AS facet_num,
                                 f.description AS facet_description
                          FROM outs AS o, facets AS f, artifacts AS a
                          WHERE (
                              f.name = 'longitudinal_section.w' OR 
                              f.name = 'heightmarks_points'
                          ) AND f.out_id = o.id AND o.artifact_id = ${a_id} AND a.id = ${a_id}
                        </dc:statement>
                        <fix_vollmer_wq_curve description="{$river} {$a_creation} {$collection_name}">
                          <dc:for-each>
                            <dc:element name="${facet_name}">
                              <dc:attribute name="description" value="${facet_description}"/>
                              <dc:attribute name="factory" value="fixanalysis"/>
                              <dc:attribute name="artifact-id" value="${aid}"/>
                              <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                              <dc:attribute name="out" value="longitudinal_section"/>
                            </dc:element>
                          </dc:for-each>
                        </fix_vollmer_wq_curve>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </fix_vollmer_wq_curve>
            </dc:macro>

            <dc:macro name="floodmap-hws-user">
              <dc:context>
                <dc:statement>
                  SELECT id AS out_id
                  FROM outs
                  WHERE artifact_id = ${a_id} AND name = 'floodmap'
                </dc:statement>
                <dc:for-each>
                  <dc:context>
                    <dc:statement>
                      SELECT name        AS facet_name,
                             num         AS facet_num,
                             description AS facet_description
                      FROM facets
                      WHERE out_id = ${out_id} AND name = 'floodmap.usershape'
                      ORDER BY num ASC, name DESC
                    </dc:statement>
                    <own-hws>
                      <dc:for-each>
                        <dc:element name="${facet_name}">
                          <dc:attribute name="description" value="${facet_description}"/>
                          <dc:attribute name="ids" value="${facet_num}:OUT=${out}"/>
                          <dc:attribute name="factory" value="winfo"/>
                          <dc:attribute name="artifact-id" value="${a_gid}"/>
                          <dc:attribute name="out" value="floodmap"/>
                        </dc:element>
                      </dc:for-each>
                    </own-hws>
                  </dc:context>
                </dc:for-each>
              </dc:context>
            </dc:macro>

            <dc:macro name="bedquality-bed">
              <bed_quality_bed>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS oid
                          FROM outs AS o
                          WHERE
                            o.artifact_id = ${a_id} AND
                            o.name='bed_longitudinal_section'
                        </dc:statement>
                        <dc:for-each>
                          <quality-bed description="{$river} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE
                                  (f.name = 'bed_longitudinal_section.bed_diameter_toplayer'
                                   OR f.name = 'bed_longitudinal_section.bed_diameter_sublayer') AND
                                   f.out_id = o.id         AND
                                   o.artifact_id = ${a_id} AND
                                   a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="minfo"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:attribute name="out" value="bed_longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </quality-bed>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </bed_quality_bed>
            </dc:macro>

            <dc:macro name="bedquality-load">
              <bed_quality_load>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS oid
                          FROM outs AS o
                          WHERE
                            o.artifact_id = ${a_id} AND
                            o.name='bed_longitudinal_section'
                        </dc:statement>
                        <dc:for-each>
                          <quality-load description="{$river} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE
                                   f.name = 'bed_longitudinal_section.bedload_diameter' AND
                                   f.out_id = o.id         AND
                                   o.artifact_id = ${a_id} AND
                                   a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="minfo"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:attribute name="out" value="bed_longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </quality-load>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </bed_quality_load>
            </dc:macro>

            <dc:macro name="bedquality-density">
              <bed_quality_density>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                    <dc:context>
                      <dc:statement>
                        SELECT id AS oid
                        FROM outs AS o
                        WHERE
                          o.artifact_id = ${a_id} AND
                          o.name='bed_longitudinal_section'
                      </dc:statement>
                      <dc:for-each>
                        <density description="{$river} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE
                                   (f.name = 'bed_longitudinal_section.sediment_density_toplayer' OR
                                    f.name = 'bed_longitudinal_section.sediment_density_sublayer') AND
                                   f.out_id = o.id         AND
                                   o.artifact_id = ${a_id} AND
                                   a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="minfo"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:attribute name="out" value="bed_longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </density>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </bed_quality_density>
            </dc:macro>

            <dc:macro name="bedquality-porosity">
              <bed_quality_porosity>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS oid
                          FROM outs AS o
                          WHERE
                            o.artifact_id = ${a_id} AND
                            o.name='bed_longitudinal_section'
                        </dc:statement>
                        <dc:for-each>
                          <porosity description="{$river} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE
                                   (f.name = 'bed_longitudinal_section.porosity_toplayer' OR
                                    f.name = 'bed_longitudinal_section.porosity_sublayer') AND
                                   f.out_id = o.id         AND
                                   o.artifact_id = ${a_id} AND
                                   a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="minfo"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:attribute name="out" value="bed_longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </porosity>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </bed_quality_porosity>
            </dc:macro>

            <dc:macro name="flow-velocity">
              <flow-velocity>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS oid
                          FROM outs AS o
                          WHERE
                            o.artifact_id = ${a_id} and o.name='flow_velocity'
                        </dc:statement>
                        <dc:for-each>
                          <flow description="{$river} {$a_creation} {$collection_name}">
                          <dc:context>
                            <dc:statement>
                              SELECT a.gid         AS aid,
                                     f.id          AS fid,
                                     f.name        AS facet_name,
                                     f.num         AS facet_num,
                                     f.description AS facet_description
                              FROM outs AS o, facets AS f, artifacts AS a
                              WHERE
                                (f.name = 'flow_velocity.totalchannel' OR
                                 f.name = 'flow_velocity.mainchannel' OR
                                 f.name = 'flow_velocity.totalchannel.filtered' OR
                                 f.name = 'flow_velocity.mainchannel.filtered') AND
                                 f.out_id = o.id         AND
                                 o.artifact_id = ${a_id} AND
                                 a.id = ${a_id}
                            </dc:statement>
                            <dc:for-each>
                              <dc:element name="${facet_name}">
                                <dc:attribute name="factory" value="minfo"/>
                                <dc:attribute name="description" value="${facet_description}"/>
                                <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                <dc:attribute name="artifact-id" value="${aid}"/>
                                <dc:attribute name="out" value="flow_velocity"/>
                              </dc:element>
                            </dc:for-each>
                          </dc:context>
                        </flow>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </flow-velocity>
            </dc:macro>

            <dc:macro name="sediment-load">
              <sediment-load>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS oid
                          FROM outs AS o
                          WHERE
                            o.artifact_id = ${a_id} and o.name='sedimentload_ls'
                        </dc:statement>
                        <dc:for-each>
                          <load description="{$river} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE f.name LIKE 'sedimentload%' AND
                                      f.out_id = o.id         AND
                                      o.artifact_id = ${a_id} AND
                                      a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="minfo"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:attribute name="out" value="sedimentload_ls"/>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </load>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </sediment-load>
            </dc:macro>

            <dc:macro name="bedheight-differences">
              <bedheight-differences>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS oid,
                                 name AS o_name
                          FROM outs AS o
                          WHERE
                            o.artifact_id = ${a_id} AND
                            (o.name = 'bed_difference_year' OR
                             o.name = 'bed_differnece_epoch')
                        </dc:statement>
                        <dc:for-each>
                          <difference description="{$river} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE (f.name LIKE 'bedheight_difference.year%' OR
                                       f.name LIKE 'bedheight_difference.epoch%') AND
                                      f.out_id = o.id         AND
                                      o.artifact_id = ${a_id} AND
                                      a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="minfo"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:choose>
                                    <dc:when test="dc:contains($o_name, 'bed_difference_year')">
                                      <dc:attribute name="out" value="bed_difference_year"/>
                                    </dc:when>
                                    <dc:otherwise>
                                      <dc:attribute name="out" value="bed_difference_epoch"/>
                                    </dc:otherwise>
                                  </dc:choose>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </difference>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </bedheight-differences>
            </dc:macro>

            <dc:macro name="fixings-year">
              <fixings>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT ou.id AS oid
                          FROM outs AS ou, artifacts AS ar
                          WHERE
                            ar.state = 'state.fix.vollmer.compute' AND
                            ou.name = 'longitudinal_section'
                        </dc:statement>
                        <dc:for-each>
                          <fix description="{$river} {$a_id} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE f.name = 'longitudinal_section.w' AND
                                      f.out_id = o.id         AND
                                      o.artifact_id = ${a_id} AND
                                      a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="fixanalysis"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:attribute name="out" value="longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </fix>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </fixings>
            </dc:macro>

            <dc:macro name="waterlevels-discharge">
              <waterlevels-discharge>
                <dc:for-each>
                  <dc:context>
                    <dc:call-macro name="range-filter">
                      <dc:context>
                        <dc:statement>
                          SELECT id AS oid
                          FROM outs AS o
                          WHERE
                            o.artifact_id = ${a_id} AND
                            o.name = 'discharge_longitudinal_section'
                        </dc:statement>
                        <dc:for-each>
                          <discharge description="{$oid} {$river} {$a_id} {$a_creation} {$collection_name}">
                            <dc:context>
                              <dc:statement>
                                SELECT a.gid         AS aid,
                                       f.id          AS fid,
                                       f.name        AS facet_name,
                                       f.num         AS facet_num,
                                       f.description AS facet_description
                                FROM outs AS o, facets AS f, artifacts AS a
                                WHERE f.name = 'discharge_longitudinal_section.w' AND
                                      f.out_id = o.id         AND
                                      o.artifact_id = ${a_id} AND
                                      a.id = ${a_id}
                              </dc:statement>
                              <dc:for-each>
                                <dc:element name="${facet_name}">
                                  <dc:attribute name="factory" value="winfo"/>
                                  <dc:attribute name="description" value="${facet_description}"/>
                                  <dc:attribute name="ids" value="${facet_num}-${facet_name}:OUT=${out}"/>
                                  <dc:attribute name="artifact-id" value="${aid}"/>
                                  <dc:attribute name="out" value="longitudinal_section"/>
                                </dc:element>
                              </dc:for-each>
                            </dc:context>
                          </discharge>
                        </dc:for-each>
                      </dc:context>
                    </dc:call-macro>
                  </dc:context>
                </dc:for-each>
              </waterlevels-discharge>
            </dc:macro>

            <dc:iterate var="out" container="artifact-outs">
              <dc:choose>
                <dc:when test="$out = 'longitudinal_section'">
                  <dc:call-macro name="longitudinal"/>
                  <dc:call-macro name="differences"/>
                  <dc:call-macro name="officiallines_user"/>
                </dc:when>
                <dc:when test="$out = 'discharge_longitudinal_section'">
                  <dc:call-macro name="longitudinal"/>
                  <dc:call-macro name="differences"/>
                </dc:when>
                <dc:when test="$out = 'w_differences'">
                  <dc:call-macro name="longitudinal"/>
                  <dc:call-macro name="differences"/>
                </dc:when>
                <dc:when test="$out = 'fix_deltawt_curve'">
                  <dc:call-macro name="delta-wt"/>
                </dc:when>
                <dc:when test="$out = 'reference_curve'">
                  <dc:call-macro name="reference-curves"/>
                </dc:when>
                <dc:when test="$out = 'computed_discharge_curve'">
                  <dc:call-macro name="computed-discharge-curve"/>
                </dc:when>
                <dc:when test="$out = 'cross_section'">
                  <dc:call-macro name="waterlevels"/>
                </dc:when>
                <dc:when test="$out = 'fix_longitudinal_section_curve'">
                  <dc:call-macro name="longitudinal-section"/>
                </dc:when>
                <dc:when test="$out = 'fix_derivate_curve'">
                  <dc:call-macro name="fix-derivate-curve"/>
                </dc:when>
                <dc:when test="$out = 'fix_wq_curve'">
                  <dc:call-macro name="fix-wq-curve"/>
                </dc:when>
                <dc:when test="$out = 'duration_curve'">
                  <dc:call-macro name="duration-curve"/>
                </dc:when>
                <dc:when test="$out = 'waterlevels'">
                  <!-- base data -->
                  <dc:call-macro name="basedata_0"/>
                  <!-- extra-longitudinal-sections -->
                  <dc:call-macro name="basedata_1_additionals"/>
                  <!-- fixations -->
                  <dc:call-macro name="basedata_2_fixations"/>
                  <!-- flood water marks-->
                  <dc:call-macro name="basedata_4_heightmarks-points"/>
                  <!-- flood protection -->
                  <dc:call-macro name="basedata_5_flood-protections"/>
                  <dc:call-macro name="waterlevels-fix"/>
                </dc:when>
                <dc:when test="$out = 'fix_wq_curve'">
                  <dc:call-macro name="waterlevels-fix"/>
                </dc:when>
                <dc:when test="$out = 'floodmap'">
                  <dc:call-macro name="flood-map"/>
                </dc:when>
                <dc:when test="$out = 'map'">
                  <dc:call-macro name="flood-map"/>
                </dc:when>
                <dc:when test="$out = 'bedheight_middle'">
                  <dc:call-macro name="waterlevels-discharge"/>
                  <dc:call-macro name="waterlevels-fix"/>
                </dc:when>
                <dc:when test="$out = 'floodmap-hws'">
                  <dc:call-macro name="floodmap-hws-user"/>
                </dc:when>
                <dc:when test="$out = 'flow_velocity'">
                  <dc:call-macro name="bedquality-bed"/>
                  <dc:call-macro name="bedquality-load"/>
                </dc:when>
                <dc:when test="$out = 'bed_longitudinal_section'">
                  <dc:call-macro name="bedquality-bed"/>
                  <dc:call-macro name="bedquality-load"/>
                  <dc:call-macro name="bedquality-density"/>
                  <dc:call-macro name="bedquality-porosity"/>
                </dc:when>
                <dc:when test="$out = 'sedimentload_ls'">
                  <dc:call-macro name="differences"/>
                  <dc:call-macro name="bedheight-differences"/>
                  <dc:call-macro name="flow-velocity"/>
                  <dc:call-macro name="sediment-load"/>
                </dc:when>
                <dc:when test="$out = 'bed_difference_year'">
                  <dc:call-macro name="waterlevels-discharge"/>
                  <dc:call-macro name="bedheight-differences"/>
                  <dc:call-macro name="differences"/>
                  <dc:call-macro name="waterlevels-fix"/>
                  <dc:call-macro name="delta-wt-ls"/>
                </dc:when>
                <dc:when test="$out = 'bed_difference_epoch'">
                  <dc:call-macro name="waterlevels-discharge"/>
                  <dc:call-macro name="bedheight-differences"/>
                  <dc:call-macro name="differences"/>
                  <dc:call-macro name="waterlevels-fix"/>
                  <dc:call-macro name="delta-wt-ls"/>
                </dc:when>
              </dc:choose>
            </dc:iterate>
          </dc:context>
        </dc:call-macro>

      </old_calculations>

      <dc:if test="dc:contains($parameters,'load-system')">
        <dc:comment>
          Include System specific part when 'load-system' is in parameters.
          -----------------------------------------------------------------
        </dc:comment>
        <dc:call-macro name="load-system"/>
      </dc:if>
    </dc:when>

    <dc:otherwise>
      <dc:comment>
        Include System specific part only if no user ID is given.
        ---------------------------------------------------------
      </dc:comment>
      <dc:call-macro name="load-system"/>
    </dc:otherwise>
  </dc:choose>

</datacage>
</dc:template>

http://dive4elements.wald.intevation.org