    Imports System
    Imports System.Data
    Imports System.Configuration
    Imports ASPNET.StarterKit.TimeTracker.DataAccessLayer

    Namespace ASPNET.StarterKit.TimeTracker.BusinessLogicLayer

        '*********************************************************************
        '
        ' TimeEntry Class
        '
        ' The TimeEntry class is used to represent a time entry along with its
        ' properties
        '
        '*********************************************************************

        Public Class TimeEntry
            Private _categoryID As Integer
            Private _categoryName As String
            Private _categoryShortName As String
            Private _day As String
            Private _description As String
            Private _duration As Decimal
            Private _entryDate As DateTime
            Private _entryLogID As Integer
            Private _projectID As Integer
            Private _projectName As String
            Private _userID As Integer

            Public Sub New()
            End Sub 'New

            Public Sub New(ByVal entryLogID As Integer)
                _entryLogID = entryLogID
            End Sub 'New

            Public Sub New(ByVal entryLogID As Integer, ByVal userID As Integer, ByVal ProjectID As Integer, ByVal CategoryID As Integer, ByVal EntryDate As DateTime, ByVal Description As String, ByVal Duration As Decimal)
                _entryLogID = entryLogID
                _userID = userID
                _projectID = ProjectID
                _categoryID = CategoryID
                _entryDate = EntryDate
                _description = Description
                _duration = Duration
            End Sub 'New

            Public Property CategoryID() As Integer
                Get
                    Return _categoryID
                End Get
                Set(ByVal Value As Integer)
                    _categoryID = Value
                End Set
            End Property

            Public Property CategoryName() As String
                Get
                    Return _categoryName
                End Get
                Set(ByVal Value As String)
                    _categoryName = Value
                End Set
            End Property

            Public Property CategoryShortName() As String
                Get
                    Return _categoryShortName
                End Get
                Set(ByVal Value As String)
                    _categoryShortName = Value
                End Set
            End Property

            Public Property Day() As String
                Get
                    Return _day
                End Get
                Set(ByVal Value As String)
                    _day = Value
                End Set
            End Property

            Public Property Description() As String
                Get
                    Return _description
                End Get
                Set(ByVal Value As String)
                    _description = Value
                End Set
            End Property

            Public Property Duration() As Decimal
                Get
                    Return _duration
                End Get
                Set(ByVal Value As Decimal)
                    _duration = Value
                End Set
            End Property

            Public Property EntryDate() As DateTime
                Get
                    Return _entryDate
                End Get
                Set(ByVal Value As DateTime)
                    _entryDate = Value
                End Set
            End Property

            Public Property EntryLogID() As Integer
                Get
                    Return _entryLogID
                End Get
                Set(ByVal Value As Integer)
                    _entryLogID = Value
                End Set
            End Property

            Public Property ProjectID() As Integer
                Get
                    Return _projectID
                End Get
                Set(ByVal Value As Integer)
                    _projectID = Value
                End Set
            End Property

            Public Property ProjectName() As String
                Get
                    Return _projectName
                End Get
                Set(ByVal Value As String)
                    _projectName = Value
                End Set
            End Property

            '*********************************************************************
            '
            ' FillCorrectStartEndDates Static Method
            '
            ' The TimeEntry.FillCorrectStartEndDates Method returns the first
            ' and last date of a week, given any date during the week.
            ' This method is primary used in TimeEntry page to calculate the range
            ' of a week.
            '
            '*********************************************************************

            Public Shared Sub FillCorrectStartEndDates(ByVal selectedDate As DateTime, ByRef startDate As DateTime, ByRef endDate As DateTime)
                Dim firstDayOfWeek As Integer = Convert.ToInt32(ConfigurationSettings.AppSettings(Web.Global.CfgKeyFirstDayOfWeek))

                ' Loop through 7 days of the week.
                Dim i As Integer
                For i = 0 To 6
                    ' Match first day of the week from global setting
                    If Convert.ToInt32(selectedDate.AddDays(i).DayOfWeek) = firstDayOfWeek Then
                        ' Fill in correct start and end dates
                        startDate = selectedDate.AddDays(i)
                        If i <> 0 Then
                            startDate = startDate.AddDays(-7)
                        End If
                        endDate = startDate.AddDays(6)
                        Exit For
                    End If
                Next i
            End Sub 'FillCorrectStartEndDates

            '*********************************************************************
            '
            ' GetEntries Static Method
            '
            ' The TimeEntry.GetEntries Method returns a Collection of TimeEntries from a particular user,
            ' for a specified period of time, based on the role of the current user browsing the application.
            '
            '*********************************************************************

            Public Shared Function GetEntries(ByVal queryUserID As Integer, ByVal userID As Integer, ByVal startDate As DateTime, ByVal endDate As DateTime) As TimeEntriesCollection
                Dim dsData As DataSet = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "TT_ListTimeEntries", queryUserID, userID, startDate, endDate)
                Dim entryList As New TimeEntriesCollection()

                ' Separate Data into a collection of TimeEntrys.
                Dim row As DataRow
                For Each row In dsData.Tables(0).Rows
                    Dim time As New TimeEntry()
                    time.EntryLogID = Convert.ToInt32(row("EntryLogID"))
                    time.Description = row("Description").ToString()
                    time.Duration = Convert.ToDecimal(row("Duration"))
                    time.EntryDate = Convert.ToDateTime(row("EntryDate"))
                    time.ProjectID = Convert.ToInt32(row("ProjectID"))
                    time.CategoryID = Convert.ToInt32(row("CategoryID"))
                    time.CategoryName = row("CategoryName").ToString()
                    time.ProjectName = row("ProjectName").ToString()
                    time.Day = time.EntryDate.ToString("dddd")
                    time.CategoryShortName = row("CatShortName").ToString()

                    entryList.Add(time)
                Next row
                Return entryList
            End Function 'GetEntries

            '*********************************************************************
            '
            ' GetWeek Static Method
            '
            ' The TimeEntry.GetWeek Method return a table of dates in a week,
            ' given any date during the week, this table contains 2 columns,
            ' one for Day values, and one for Date values
            ' This method is primary used in Databinding DropDowns in TimeEntry page
            ' with range of a week.
            '
            '*********************************************************************

            Public Shared Function GetWeek(ByVal selectedDate As DateTime) As DataTable
                Dim start As DateTime = DateTime.MinValue
                Dim [end] As DateTime = DateTime.MinValue
                Dim dt As New DataTable()
                dt.Columns.Add("Day")
                dt.Columns.Add("Date")

                FillCorrectStartEndDates(selectedDate, start, [end])
                Dim workRow As DataRow

                '  fill table with dates and days
                Dim i As Integer
                For i = 0 To 6
                    workRow = dt.NewRow()
                    workRow("Day") = Convert.ToDateTime(start.AddDays(i)).ToString("ddd")
                    workRow("Date") = start.AddDays(i)
                    dt.Rows.Add(workRow)
                Next i
                Return dt
            End Function 'GetWeek

            '*********************************************************************
            '
            ' Remove Static Method
            '
            ' The TimeEntry.Remove Method removes a time entry from database
            ' based on entry log ID.
            '
            '
            '*********************************************************************

            Public Shared Sub Remove(ByVal entryLogID As Integer)
                SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "TT_DeleteTimeEntry", entryLogID)
            End Sub 'Remove

            '*********************************************************************
            '
            ' Load Method
            '
            ' The TimeEntry.Load Method populates current instance of TimeEntry object with
            ' info from the database.
            '
            '*********************************************************************

            Public Sub Load()
                Dim ds As DataSet = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "TT_GetTimeEntry", _entryLogID)
                Dim row As DataRow = ds.Tables(0).Rows(0)
                _description = row("Description").ToString()
                _duration = Convert.ToDecimal(row("Duration"))
                _entryDate = Convert.ToDateTime(row("EntryDate"))
                _projectID = Convert.ToInt32(row("ProjectID"))
                _userID = Convert.ToInt32(row("UserID"))
                _categoryID = Convert.ToInt32(row("CategoryID"))
                _projectName = row("ProjectName").ToString()
            End Sub 'Load

            '*********************************************************************
            '
            ' Save Method
            '
            ' The TimeEntry.Save Method Add or update TimeEntry information
            ' in the database depending on the entryLogID.
            ' Returns True if saved successfully, false otherwise.
            '
            '*********************************************************************

            Public Function Save() As Boolean
                If _entryLogID = 0 Then
                    Return Insert()
                Else
                    Return Update()
                End If
            End Function 'Save

            Private Function Insert() As Boolean
                _entryLogID = Convert.ToInt32(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "TT_AddTimeEntry", _userID, _projectID, _categoryID, _entryDate, _description, _duration))
                Return 0 < _entryLogID
            End Function 'Insert

            Private Function Update() As Boolean
                Try
                    SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "TT_UpdateTimeEntry", _entryLogID, _userID, _projectID, _categoryID, _entryDate, _description, _duration)
                    Return True
                Catch
                End Try
            End Function 'Update
        End Class 'TimeEntry
    End Namespace 'ASPNET.StarterKit.TimeTracker.BusinessLogicLayer