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

		http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0
		http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd

		http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0
		http://aries.apache.org/schemas/blueprint-ext/blueprint-ext-1.1.xsd
">

  <!-- Configuration properties -->
  <cm:property-placeholder id="properties" persistent-id="org.opennms.netmgt.sampler.persister.xml" update-strategy="none">
    <cm:default-properties>
      <cm:property name="directory" value="xml"/>
    </cm:default-properties>
  </cm:property-placeholder>

  <!-- Camel (un)marshalling DataFormats -->
  <bean id="jaxbXml" class="org.opennms.netmgt.api.sample.DataFormatUtils" factory-method="jaxbXml"/>

  <!-- Wire in a dispatcher that will route SampleSet messages into the Camel context -->
  <bean id="xmlDispatcher" class="org.opennms.netmgt.api.sample.support.DefaultSampleSetDispatcher">
    <argument value="direct:sendSampleSet"/>
  </bean>

  <service ref="xmlDispatcher" interface="org.opennms.netmgt.api.sample.SampleSetDispatcher">
    <service-properties>
      <entry key="impl" value="xml"/>
    </service-properties>
  </service>

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

    <!-- Write SampleSets to XML files -->
    <route id="sendSampleSet">
      <from uri="direct:sendSampleSet" />
      <convertBodyTo type="org.opennms.netmgt.api.sample.SampleSet" />
      <choice>
        <when>
          <simple>${body.samples.size} &gt; 0</simple>
          <!-- Prettify the timestamp into something human-readable -->
          <setHeader headerName="timestampDate">
            <simple>${body.timestampAsDate}</simple>
          </setHeader>
          <setHeader headerName="timestampString">
            <simple>${date:header.timestampDate:yyyy-MM-dd-hh:mm:ss.SSS}</simple>
          </setHeader>

          <!-- Marshal the SampleSet to XML -->
          <marshal ref="jaxbXml" />
          <!-- Sniff the agent ID out of any sample because they should be all the same -->
          <setHeader headerName="agentId">
            <xpath >/sample-set/samples/sample[1]/resource/agent/id/text()</xpath>
          </setHeader>

          <!-- Get the timestamp from the sample-set's timestamp tag -->
          <!--
          <setHeader headerName="timestampString">
            <xpath>/sample-set/timestamp/@time</xpath>
          </setHeader>
          -->

          <!-- 
            Set the filename to be something like
            2014-05-08-11:19:37.074-SNMP:192.168.1.2:161.xml
          -->
          <setHeader headerName="CamelFileName">
            <simple>${headers.timestampString}-${headers.agentId}.xml</simple>
          </setHeader>
          <to uri="file:{{directory}}" />
        </when>
      </choice>
    </route>

  </camelContext>

</blueprint>
