Imports System.Net
Imports System.Web.Http

Imports C1.Olap
Imports System.Data
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters
Imports LightSwitchApplication

Public Class OlapController
    Inherits ApiController

    Public Function GetValues() As String
        Using context As ServerApplicationContext = ServerApplicationContext.CreateContext()
            GetValues = context.Application.User.FullName
        End Using
    End Function

    Public Function GetValues(ByVal row As String, ByVal column As String, ByVal value As String, ByVal totals As String) As String
        Using context As ServerApplicationContext = ServerApplicationContext.CreateContext()
            Dim engine = New C1OlapEngine
            engine.ShowKeyColumns = True

            Select Case totals
                Case "rows"
                    engine.ShowTotalsRows = ShowTotals.GrandTotals
                    engine.ShowTotalsColumns = ShowTotals.None

                Case "columns"
                    engine.ShowTotalsRows = ShowTotals.None
                    engine.ShowTotalsColumns = ShowTotals.GrandTotals

                Case "all"
                    engine.ShowTotalsRows = ShowTotals.GrandTotals
                    engine.ShowTotalsColumns = ShowTotals.GrandTotals

                Case Else
                    engine.ShowTotalsRows = ShowTotals.None
                    engine.ShowTotalsColumns = ShowTotals.None
            End Select

            ' TODO: Replace the data source and collection names with actual entities from your DataWorkspace
            engine.DataSource = context.DataWorkspace.MyDataSource.MyCollection.GetQuery().Execute()

            If Not IsNothing(row) Then
                engine.RowFields.Add(New String() {row})
            End If

            If Not IsNothing(column) Then
                engine.ColumnFields.Add(New String() {column})
            End If

            If Not IsNothing(value) Then
                engine.ValueFields.Add(New String() {value})
            End If

            engine.Update()

            For Each c As DataColumn In engine.OlapTable.Columns
                Dim key As C1OlapKey = engine.GetOlapKey(c)

                If Not IsNothing(key) Then
                    Dim name As String = key.GetFormattedValue(0)

                    If Not String.IsNullOrEmpty(name) Then
                        c.ColumnName = name

                    ElseIf engine.ShowTotalsColumns = ShowTotals.None Then
                        c.ColumnName = key.ValueField.Caption

                    Else
                        c.ColumnName = "Total"
                    End If
                End If
            Next

            If engine.ShowTotalsRows <> ShowTotals.None Then
                For Each r As DataRow In engine.OlapTable.Rows
                    Dim key As C1OlapKey = engine.GetOlapKey(r)

                    If key.Values.Length = 0 Then
                        r(0) = "Total"
                    End If
                Next
            End If

            GetValues = JsonConvert.SerializeObject(engine.OlapTable, New DataTableConverter)
        End Using
    End Function

End Class
