diff --git a/src/com/maxwittig/reportgenerator/builder/PlainTextReportBuilder.kt b/src/com/maxwittig/reportgenerator/builder/PlainTextReportBuilder.kt index 23c0974..c881b0b 100644 --- a/src/com/maxwittig/reportgenerator/builder/PlainTextReportBuilder.kt +++ b/src/com/maxwittig/reportgenerator/builder/PlainTextReportBuilder.kt @@ -1,15 +1,42 @@ package com.maxwittig.reportgenerator.builder import com.maxwittig.reportgenerator.models.TimekeeperTask +import com.maxwittig.reportgenerator.utils.getTimeStringFromMilliSeconds import java.util.* -class PlainTextReportBuilder(timekeeperTasks : ArrayList, reportType: ReportType) : ReportBuilder(timekeeperTasks, reportType) +class PlainTextReportBuilder(timekeeperTasks : ArrayList,val reportType: ReportType, todaysDate : Date = Date()) : ReportBuilder(timekeeperTasks, reportType, todaysDate = todaysDate) { override fun getReport() : String { val stringBuilder = StringBuilder() - //stringBuilder.append("Time worked today: " + getTotalTimeOfTasksToday()) + val taskListToUse = getTaskList() + stringBuilder.append("Time worked today: " + getTotalTimeOfTasks(dailyTasks)) stringBuilder.append("\n") - //stringBuilder.append("Projects worked on today: " + getDailyProjectTimeHashMap()) + stringBuilder.append("Projects worked on " + reportType.reportTypeName + "\n:") + stringBuilder.append(getProjectsListPlain(taskListToUse)) + return stringBuilder.toString() + } + + private fun getTaskList() : ArrayList + { + when(reportType) + { + ReportType.DAILY -> return dailyTasks + ReportType.MONTHLY -> return monthlyTasks + ReportType.YEARLY -> return yearlyTasks + ReportType.WEEKLY -> return weeklyTasks + } + } + + private fun getProjectsListPlain(taskList: ArrayList) : String + { + val stringBuilder = StringBuilder() + for(currentProject in createProjectHolder(taskList).projects) + { + stringBuilder.append(currentProject.name) + stringBuilder.append("\t") + stringBuilder.append(getTimeStringFromMilliSeconds(currentProject.totalTime*1000)) + stringBuilder.append("\n") + } return stringBuilder.toString() } diff --git a/src/com/maxwittig/reportgenerator/builder/ReportType.kt b/src/com/maxwittig/reportgenerator/builder/ReportType.kt index 8d78824..ac7f53b 100644 --- a/src/com/maxwittig/reportgenerator/builder/ReportType.kt +++ b/src/com/maxwittig/reportgenerator/builder/ReportType.kt @@ -2,6 +2,8 @@ package com.maxwittig.reportgenerator.builder import com.maxwittig.reportgenerator.utils.isLastDayOfTheMonth import com.maxwittig.reportgenerator.utils.isLastDayOfTheWeek +import com.maxwittig.reportgenerator.utils.isLastDayOfTheYear +import java.time.LocalDate enum class ReportType(val reportTypeName : String) @@ -13,19 +15,19 @@ enum class ReportType(val reportTypeName : String) YEARLY("Yearly"); companion object { - fun getCurrentReportType(weeklyEnabled : Boolean, monthlyEnabled : Boolean, yearlyEnabled : Boolean) : ReportType + fun getCurrentReportType(weeklyEnabled : Boolean, monthlyEnabled : Boolean, yearlyEnabled : Boolean, localDate: LocalDate = LocalDate.now()) : ReportType { - if(yearlyEnabled && isLastDayOfTheWeek()) + if(yearlyEnabled && isLastDayOfTheYear(now = localDate)) { return YEARLY } else - if(monthlyEnabled && isLastDayOfTheMonth()) + if(monthlyEnabled && isLastDayOfTheMonth(now = localDate)) { return MONTHLY } else - if(weeklyEnabled && isLastDayOfTheWeek()) + if(weeklyEnabled && isLastDayOfTheWeek(now = localDate)) { return WEEKLY } diff --git a/src/com/maxwittig/reportgenerator/tests/builder/HTMLReportBuilderTest.kt b/src/com/maxwittig/reportgenerator/tests/builder/HTMLReportBuilderTest.kt index 759818d..0a81485 100644 --- a/src/com/maxwittig/reportgenerator/tests/builder/HTMLReportBuilderTest.kt +++ b/src/com/maxwittig/reportgenerator/tests/builder/HTMLReportBuilderTest.kt @@ -2,32 +2,11 @@ package com.maxwittig.reportgenerator.tests.builder import com.maxwittig.reportgenerator.builder.HTMLReportBuilder import com.maxwittig.reportgenerator.builder.ReportType -import com.maxwittig.reportgenerator.models.TimekeeperTask import org.junit.Test import java.io.File -import java.text.SimpleDateFormat -import java.util.* -class HTMLReportBuilderTest +class HTMLReportBuilderTest : ReportBuilderTest() { - private val wordList : List = File("data/wordlist.txt").readText().split("\n") - - private fun getRandomTimekeeperTaskArrayList() : ArrayList - { - val completeTaskList = ArrayList() - var i = 0 - while (i < 10000) - { - i++ - val startDate = getRandomDateInTheYear() - val duration = getRandomDuration() - val endDate = Date(startDate.time + duration) - val task = TimekeeperTask(getRandomWordFromList(), getRandomWordFromList(), startDate, endDate, duration) - completeTaskList.add(task) - } - completeTaskList.sort { task1, task2 -> task1.startTime.compareTo(task2.startTime) } - return completeTaskList - } @Test fun weeklyReportTest() @@ -65,24 +44,5 @@ class HTMLReportBuilderTest File("data/testResults/yearly.html").writeText(reportBuilder.getReport()) } - private fun getRandomWordFromList() : String - { - return wordList[Random().nextInt(wordList.size)] - } - - private fun getRandomDateInTheYear() : Date - { - val year = "2017" - val format = SimpleDateFormat("yyyy") - val yearDate = format.parse(year) - val yearStartSeconds = yearDate.time - val yearLengthMilliSeconds = 31557600000 - val randomDateInMilliseconds = (Random().nextDouble()*(yearLengthMilliSeconds)).toLong() - return Date(yearStartSeconds+randomDateInMilliseconds) - } - private fun getRandomDuration() : Long - { - return (Random().nextDouble()*(86400)).toLong() - } } \ No newline at end of file diff --git a/src/com/maxwittig/reportgenerator/tests/builder/PlainTextReportBuilderTest.kt b/src/com/maxwittig/reportgenerator/tests/builder/PlainTextReportBuilderTest.kt new file mode 100644 index 0000000..2890294 --- /dev/null +++ b/src/com/maxwittig/reportgenerator/tests/builder/PlainTextReportBuilderTest.kt @@ -0,0 +1,18 @@ +package com.maxwittig.reportgenerator.tests.builder + +import com.maxwittig.reportgenerator.builder.PlainTextReportBuilder +import com.maxwittig.reportgenerator.builder.ReportType +import org.junit.Test + + +class PlainTextReportBuilderTest : ReportBuilderTest() +{ + @Test + fun dailyReportTest() + { + val tasks = getRandomTimekeeperTaskArrayList() + val reportBuilder = PlainTextReportBuilder(tasks, ReportType.DAILY, todaysDate = getRandomDateInTheYear()) + println(reportBuilder.getReport()) + + } +} \ No newline at end of file diff --git a/src/com/maxwittig/reportgenerator/tests/builder/ReportBuilderTest.kt b/src/com/maxwittig/reportgenerator/tests/builder/ReportBuilderTest.kt new file mode 100644 index 0000000..bd1d012 --- /dev/null +++ b/src/com/maxwittig/reportgenerator/tests/builder/ReportBuilderTest.kt @@ -0,0 +1,50 @@ +package com.maxwittig.reportgenerator.tests.builder + +import com.maxwittig.reportgenerator.models.TimekeeperTask +import java.io.File +import java.text.SimpleDateFormat +import java.util.* + + +abstract class ReportBuilderTest +{ + private val wordList : List = File("data/wordlist.txt").readText().split("\n") + + protected fun getRandomTimekeeperTaskArrayList() : ArrayList + { + val completeTaskList = ArrayList() + var i = 0 + while (i < 10000) + { + i++ + val startDate = getRandomDateInTheYear() + val duration = getRandomDuration() + val endDate = Date(startDate.time + duration) + val task = TimekeeperTask(getRandomWordFromList(), getRandomWordFromList(), startDate, endDate, duration) + completeTaskList.add(task) + } + completeTaskList.sort { task1, task2 -> task1.startTime.compareTo(task2.startTime) } + return completeTaskList + } + + protected fun getRandomWordFromList() : String + { + return wordList[Random().nextInt(wordList.size)] + } + + protected fun getRandomDateInTheYear() : Date + { + val year = "2017" + val format = SimpleDateFormat("yyyy") + val yearDate = format.parse(year) + val yearStartSeconds = yearDate.time + val yearLengthMilliSeconds = 31557600000 + val randomDateInMilliseconds = (Random().nextDouble()*(yearLengthMilliSeconds)).toLong() + return Date(yearStartSeconds+randomDateInMilliseconds) + } + + protected fun getRandomDuration() : Long + { + return (Random().nextDouble()*(86400)).toLong() + } +} \ No newline at end of file diff --git a/src/com/maxwittig/reportgenerator/tests/builder/ReportTypeTest.kt b/src/com/maxwittig/reportgenerator/tests/builder/ReportTypeTest.kt new file mode 100644 index 0000000..edd0a8b --- /dev/null +++ b/src/com/maxwittig/reportgenerator/tests/builder/ReportTypeTest.kt @@ -0,0 +1,22 @@ +package com.maxwittig.reportgenerator.tests.builder + +import com.maxwittig.reportgenerator.builder.ReportType +import org.junit.Assert.assertEquals +import org.junit.Test +import java.time.LocalDate + +class ReportTypeTest +{ + @Test + fun getCurrentReportTypeTest() + { + val yearlyDate = LocalDate.of(2016, 12, 31) //end of the year + val monthlyDate = LocalDate.of(2016, 11, 30) //end of the month + val sundayDate = LocalDate.of(2016, 12, 25) //sunday + val dailyDate = LocalDate.of(2016, 1, 5) //nothing special + assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = yearlyDate), ReportType.YEARLY) + assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = monthlyDate), ReportType.MONTHLY) + assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = sundayDate), ReportType.WEEKLY) + assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = dailyDate), ReportType.DAILY) + } +} \ No newline at end of file diff --git a/src/com/maxwittig/reportgenerator/utils/DateUtils.kt b/src/com/maxwittig/reportgenerator/utils/DateUtils.kt index 4a9ce6d..21ef9ee 100644 --- a/src/com/maxwittig/reportgenerator/utils/DateUtils.kt +++ b/src/com/maxwittig/reportgenerator/utils/DateUtils.kt @@ -30,22 +30,19 @@ fun isSameYear(date1 : Date, date2 : Date) : Boolean return simpleFormat.format(date1) == simpleFormat.format(date2) } -fun isLastDayOfTheMonth() : Boolean +fun isLastDayOfTheMonth(now : LocalDate = LocalDate.now()) : Boolean { - val now = LocalDate.now() val lastDay = now.with(TemporalAdjusters.lastDayOfMonth()) return now == lastDay } -fun isLastDayOfTheWeek() : Boolean +fun isLastDayOfTheWeek(now : LocalDate = LocalDate.now()) : Boolean { - val now = LocalDate.now() return now.dayOfWeek.value == 7 } -fun isLastDayOfTheYear() : Boolean +fun isLastDayOfTheYear(now : LocalDate = LocalDate.now()) : Boolean { - val now = LocalDate.now() val lastDay = now.with(TemporalAdjusters.lastDayOfYear()) return now == lastDay }