Quintiq file version 2.0
#parent: #root
Method ConvertNvtToShadowSnapshotSetData (
  NamedValueTree nvt
) as shadow[KpiSnapshotSetData] id:Method_GlobalStateKpiTracker_ConvertNvtToShadowSnapshotSetData
{
  #keys: '[136398.1.783148550]'
  Body:
  [*
    // Converts the query-result NamedValueTree to a shadow-KpiSnapshotSetData enclosure.
    set := KpiTracker.KpiSnapshotSetData( relshadow );
    
    // Define handles.
    setDataHandle  := nvt.GetHandle( "KpiSnapshotSetData" );
    snapshotHandle := nvt.GetHandle( "KpiSnapshotData" );
    kpivalueHandle := nvt.GetHandle( "KpiValueData" );
    labelHandle    := nvt.GetHandle( "KpiLabelData" );
    serverDtHandle := nvt.GetHandle( "ServerDateTime" );
    planDtHandle   := nvt.GetHandle( "PlanDateTime" );
    nameHandle     := nvt.GetHandle( 'Name' );
    valueHandle    := nvt.GetHandle( 'Value' );
    retentionHandle:= nvt.GetHandle( 'RetentionPolicyIdentification' );
    sourceDatasetMDSIDHandle := nvt.GetHandle( 'SourceDatasetMDSID' );
    
    // Get to the real root (the setData).
    root           := nvt.Root();
    setData        := root.Child( setDataHandle );
    
    // Each child of setData is a SnapshotData.
    traverse( setData.Children( snapshotHandle ), Elements, snapshotNvt )
    {
      snapshot := set.SnapshotData( relnew );
      snapshot.ServerDateTime ( snapshotNvt.Child( serverDtHandle ).GetValueAsDateTime() );
      snapshot.PlanDateTime   ( snapshotNvt.Child( planDtHandle ).GetValueAsDateTime() );
      snapshot.RetentionPolicyIdentification( snapshotNvt.Child( retentionHandle ).GetValueAsString() );
      snapshot.SourceDatasetMDSID( snapshotNvt.Child( sourceDatasetMDSIDHandle ).GetValueAsKey() );
    
      // Labels
      labels := construct( Strings );
      labeldata := selectsortedset( snapshotNvt.Children( labelHandle ), Elements, label, true, label.Child( nameHandle ).GetValueAsString() );
      traverse( labeldata, Elements, labelNvt )
      {
        label := snapshot.Label( relnew );
        label.Name( labelNvt.Child( nameHandle ).GetValueAsString() );
        label.Value( labelNvt.Child( valueHandle ).GetValueAsString() );
        labels.Add( label.Name() + ": " + label.Value() );
      }
      
      // Kpi values
      traverse( snapshotNvt.Children( kpivalueHandle ), Elements, valueNvt )
      {
        val := snapshot.Value( relnew );
        val.Name( valueNvt.Child( nameHandle ).GetValueAsString() );
        val.Value( valueNvt.Child( valueHandle ).GetValueAsReal() );
        val.UI_SnapshotLabels( labels.ToString( ";" ) );
      }
    
    }
    
    return &set;
  *]
  ReturnsOwning: true
}
