Quintiq file version 2.0
#parent: #root
Method ApplyConfigurationQuery (
  KpiQuery kpiQuery
) id:Method_Kpi_frmKpiQueryCompare_ApplyConfigurationQuery
{
  #keys: '[146874.0.1482143428]'
  Body:
  [*
    // Applies the configuration query from a KpiQuery to radio button groups, List, and selector.
    label := select( KpiTracker, Label, l, l.Name() = kpiQuery.SeriesSelection() );
    dhCompareLabel.Data( guard( label, null( KpiLabel ) ) );
    dhTimetype.Data( kpiQuery.Timetype() );
    
    // Flush dataholder.
    dhFilterLabels.Data().Flush();
    dhCompareLabels.Data().Flush();
    
    traverse( kpiQuery.LabelFilters().Tokenize( "," ), Elements, lv )
    {
      labelValue := select( KpiTracker, Label.Value, labelvalue, labelvalue.Value() = lv );
      if ( not isnull( labelValue ) )
      {
        if( labelValue.Label() = dhCompareLabel.Data() )
        {
          dhCompareLabels.Data().Add( labelValue);
        }
        else
        {
          dhFilterLabels.Data().Add( labelValue );
        }
      }
    }
    
    // Flush dataholder before insert Kpi selection
    dhMetaDatas.Data().Flush();
    
    traverse( kpiQuery.MetaDatas().Tokenize( "," ), Elements, md )
    {
      metaData := select( KpiTracker, MetaData, metadata, metadata.DisplayName() = md );
      if ( not isnull( metaData ) )
      {
        dhMetaDatas.Data().Add( metaData );
      }
    }
    
    
    // Refresh data - Queries the KPI Tracker for the KPI compare data
    // Do this because the DataHolder has independency on drop down selector and is not reflect the changes immediately.
    compareLabel := dhCompareLabel.Data();
    compareLabels := selectset( dhCompareLabels.Data(), Elements, val, 
                                val.Label() = compareLabel );
    
    fixedLabels := selectset( dhFilterLabels.Data(), Elements, val, 
                              val.Label() <> compareLabel );
                              
    
    set := Kpi_GlobalState.QueryDataFromServer( compareLabels, fixedLabels );
    
    Kpi_GlobalState.PropagateLabel( set, guard( compareLabel.Name(), "" ) );
    Kpi_GlobalState.PropagateTimeStamp( set, dhTimetype.Data() );
    
    // Set relations
    traverse( set, SnapshotData.Value, v )
    {
      v.UI_CompareLabel( relset, select( compareLabels, Elements, s, s.Value() = v.UI_LabelValue() ) );
      v.UI_Kpi( relset, select( KpiTracker, MetaData, k, k.Name() = v.Name() ) );
    }
    
    dhSnapshotData.Data( &set );
  *]
}
