From dde5775a4147c0e329fa18ddbd5aa2d5d8cb8041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Fri, 1 Jun 2018 12:52:03 +0200 Subject: [PATCH] Move ticks converter to api (#130) --- .../io/github/wulkanowy/api/DateTimeUtils.kt | 49 +++++++++++++++++ .../java/io/github/wulkanowy/api/SnP.java | 5 ++ .../wulkanowy/api/StudentAndParent.java | 5 ++ .../api/attendance/AttendanceTable.java | 26 ++++----- .../github/wulkanowy/api/exams/ExamsWeek.java | 20 +++---- .../io/github/wulkanowy/api/generic/Day.java | 3 +- .../wulkanowy/api/{ => generic}/Diary.java | 2 +- .../api/{ => generic}/ParamItem.java | 4 +- .../wulkanowy/api/{ => generic}/Semester.java | 2 +- .../wulkanowy/api/{ => generic}/Student.java | 2 +- .../wulkanowy/api/grades/GradesList.java | 34 +++--------- .../wulkanowy/api/notes/AchievementsList.java | 6 +-- .../github/wulkanowy/api/notes/NotesList.java | 10 ++-- .../wulkanowy/api/school/SchoolInfo.java | 2 +- .../wulkanowy/api/school/TeachersInfo.java | 2 +- .../wulkanowy/api/timetable/Timetable.java | 21 +++----- .../github/wulkanowy/api/DateTimeUtilsTest.kt | 53 ++++++++++++++++++ .../wulkanowy/api/StudentAndParentTest.java | 2 + .../api/StudentAndParentTestCase.java | 2 + .../wulkanowy/api/grades/GradesListTest.java | 18 +++---- .../wulkanowy/api/notes/NotesListTest.java | 4 +- .../data/db/dao/migrations/Migration23.java | 2 +- .../wulkanowy/data/sync/AttendanceSync.java | 12 ++--- .../github/wulkanowy/data/sync/ExamsSync.java | 13 ++--- .../wulkanowy/data/sync/TimetableSync.java | 12 ++--- .../wulkanowy/utils/DataObjectConverter.java | 12 ++--- .../io/github/wulkanowy/utils/TimeUtils.java | 33 ------------ .../github/wulkanowy/utils/TimeUtilsTest.java | 54 +------------------ 28 files changed, 195 insertions(+), 215 deletions(-) create mode 100644 api/src/main/java/io/github/wulkanowy/api/DateTimeUtils.kt rename api/src/main/java/io/github/wulkanowy/api/{ => generic}/Diary.java (93%) rename api/src/main/java/io/github/wulkanowy/api/{ => generic}/ParamItem.java (67%) rename api/src/main/java/io/github/wulkanowy/api/{ => generic}/Semester.java (93%) rename api/src/main/java/io/github/wulkanowy/api/{ => generic}/Student.java (93%) create mode 100644 api/src/test/java/io/github/wulkanowy/api/DateTimeUtilsTest.kt diff --git a/api/src/main/java/io/github/wulkanowy/api/DateTimeUtils.kt b/api/src/main/java/io/github/wulkanowy/api/DateTimeUtils.kt new file mode 100644 index 000000000..bad4e97df --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/DateTimeUtils.kt @@ -0,0 +1,49 @@ +package io.github.wulkanowy.api + +import java.text.SimpleDateFormat +import java.util.* + +const val LOG_DATE_PATTERN = "dd.MM.yyyy" +const val API_DATE_PATTERN = "yyyy-MM-dd" + +const val TICKS_AT_EPOCH = 621355968000000000L +const val TICKS_PER_MILLISECOND = 10000 + +fun getFormattedDate(date: String): String { + return getFormattedDate(date, API_DATE_PATTERN) +} + +fun getFormattedDate(date: String, format: String): String { + val sdf = SimpleDateFormat(LOG_DATE_PATTERN, Locale.ROOT) + val d = sdf.parse(date) + sdf.applyPattern(format) + + return sdf.format(d) +} + +fun getDateAsTick(dateString: String?): String { + if (dateString.isNullOrEmpty()) { + return "" + } + + return getDateAsTick(dateString as String, API_DATE_PATTERN).toString() +} + +fun getDateAsTick(dateString: String, dateFormat: String): Long { + val format = SimpleDateFormat(dateFormat, Locale.ROOT) + format.timeZone = TimeZone.getTimeZone("UTC") + val dateObject = format.parse(dateString) + + return getDateAsTick(dateObject) +} + +fun getDateAsTick(date: Date): Long { + val calendar = Calendar.getInstance() + calendar.time = date + + return calendar.timeInMillis * TICKS_PER_MILLISECOND + TICKS_AT_EPOCH +} + +fun getDate(netTicks: Long): Date { + return Date((netTicks - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND) +} diff --git a/api/src/main/java/io/github/wulkanowy/api/SnP.java b/api/src/main/java/io/github/wulkanowy/api/SnP.java index 7f074071c..6747faf0e 100644 --- a/api/src/main/java/io/github/wulkanowy/api/SnP.java +++ b/api/src/main/java/io/github/wulkanowy/api/SnP.java @@ -6,6 +6,11 @@ import org.jsoup.nodes.Element; import java.io.IOException; import java.util.List; +import io.github.wulkanowy.api.generic.Diary; +import io.github.wulkanowy.api.generic.ParamItem; +import io.github.wulkanowy.api.generic.Semester; +import io.github.wulkanowy.api.generic.Student; + public interface SnP { String getSchoolID(); diff --git a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java index 16ee002e0..a4b8fefa0 100644 --- a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -11,6 +11,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import io.github.wulkanowy.api.generic.Diary; +import io.github.wulkanowy.api.generic.ParamItem; +import io.github.wulkanowy.api.generic.Semester; +import io.github.wulkanowy.api.generic.Student; + public class StudentAndParent implements SnP { private static final String START_PAGE_URL = "{schema}://uonetplus.{host}/{symbol}/Start.mvc/Index"; diff --git a/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java b/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java index d81a30b2f..7caa70d80 100644 --- a/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java +++ b/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java @@ -4,12 +4,8 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.Locale; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; @@ -17,6 +13,9 @@ import io.github.wulkanowy.api.generic.Day; import io.github.wulkanowy.api.generic.Lesson; import io.github.wulkanowy.api.generic.Week; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getDateAsTick; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getFormattedDate; + public class AttendanceTable { private final static String ATTENDANCE_PAGE_URL = "Frekwencja.mvc?data="; @@ -27,13 +26,12 @@ public class AttendanceTable { this.snp = snp; } - public Week getWeekTable() throws IOException, ParseException, VulcanException { + public Week getWeekTable() throws IOException, VulcanException { return getWeekTable(""); } - public Week getWeekTable(String tick) throws IOException, ParseException, VulcanException { - Element table = snp.getSnPPageDocument(ATTENDANCE_PAGE_URL + tick) - + public Week getWeekTable(String date) throws IOException, VulcanException { + Element table = snp.getSnPPageDocument(ATTENDANCE_PAGE_URL + getDateAsTick(date)) .select(".mainContainer .presentData").first(); Elements headerCells = table.select("thead th"); @@ -42,14 +40,10 @@ public class AttendanceTable { for (int i = 1; i < headerCells.size(); i++) { String[] dayHeaderCell = headerCells.get(i).html().split("
"); - SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - Date d = sdf.parse(dayHeaderCell[1].trim()); - sdf.applyPattern("yyyy-MM-dd"); - - Day day = new Day(); - day.setDayName(dayHeaderCell[0]); - day.setDate(sdf.format(d)); - days.add(day); + days.add(new Day() + .setDayName(dayHeaderCell[0]) + .setDate(getFormattedDate(dayHeaderCell[1].trim())) + ); } Elements hoursInDays = table.select("tbody tr"); diff --git a/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java b/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java index c111ce848..391056ac9 100644 --- a/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java +++ b/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java @@ -6,17 +6,16 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.Locale; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Week; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getDateAsTick; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getFormattedDate; + public class ExamsWeek { private static final String EXAMS_PAGE_URL = "Sprawdziany.mvc/Terminarz?rodzajWidoku=2&data="; @@ -27,12 +26,12 @@ public class ExamsWeek { this.snp = snp; } - public Week getCurrent() throws IOException, VulcanException, ParseException { + public Week getCurrent() throws IOException, VulcanException { return getWeek("", true); } - public Week getWeek(String tick, final boolean onlyNotEmpty) throws IOException, VulcanException, ParseException { - Document examsPage = snp.getSnPPageDocument(EXAMS_PAGE_URL + tick); + public Week getWeek(String date, final boolean onlyNotEmpty) throws IOException, VulcanException { + Document examsPage = snp.getSnPPageDocument(EXAMS_PAGE_URL + getDateAsTick(date)); Elements examsDays = examsPage.select(".mainContainer > div:not(.navigation)"); List days = new ArrayList<>(); @@ -71,11 +70,4 @@ public class ExamsWeek { .first().text().split(" ")[1])) .setDays(days); } - - private String getFormattedDate(String date) throws ParseException { - SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - Date d = sdf.parse(date); - sdf.applyPattern("yyyy-MM-dd"); - return sdf.format(d); - } } diff --git a/api/src/main/java/io/github/wulkanowy/api/generic/Day.java b/api/src/main/java/io/github/wulkanowy/api/generic/Day.java index 5b9086a37..fbb5ee0a7 100644 --- a/api/src/main/java/io/github/wulkanowy/api/generic/Day.java +++ b/api/src/main/java/io/github/wulkanowy/api/generic/Day.java @@ -37,7 +37,8 @@ public class Day { return dayName; } - public void setDayName(String dayName) { + public Day setDayName(String dayName) { this.dayName = dayName; + return this; } } diff --git a/api/src/main/java/io/github/wulkanowy/api/Diary.java b/api/src/main/java/io/github/wulkanowy/api/generic/Diary.java similarity index 93% rename from api/src/main/java/io/github/wulkanowy/api/Diary.java rename to api/src/main/java/io/github/wulkanowy/api/generic/Diary.java index d7d09f9ce..5c7c8593b 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Diary.java +++ b/api/src/main/java/io/github/wulkanowy/api/generic/Diary.java @@ -1,4 +1,4 @@ -package io.github.wulkanowy.api; +package io.github.wulkanowy.api.generic; public class Diary implements ParamItem { diff --git a/api/src/main/java/io/github/wulkanowy/api/ParamItem.java b/api/src/main/java/io/github/wulkanowy/api/generic/ParamItem.java similarity index 67% rename from api/src/main/java/io/github/wulkanowy/api/ParamItem.java rename to api/src/main/java/io/github/wulkanowy/api/generic/ParamItem.java index c830467ad..e7edfbf48 100644 --- a/api/src/main/java/io/github/wulkanowy/api/ParamItem.java +++ b/api/src/main/java/io/github/wulkanowy/api/generic/ParamItem.java @@ -1,6 +1,6 @@ -package io.github.wulkanowy.api; +package io.github.wulkanowy.api.generic; -interface ParamItem { +public interface ParamItem { ParamItem setId(String id); diff --git a/api/src/main/java/io/github/wulkanowy/api/Semester.java b/api/src/main/java/io/github/wulkanowy/api/generic/Semester.java similarity index 93% rename from api/src/main/java/io/github/wulkanowy/api/Semester.java rename to api/src/main/java/io/github/wulkanowy/api/generic/Semester.java index 31230f012..db4a724dd 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Semester.java +++ b/api/src/main/java/io/github/wulkanowy/api/generic/Semester.java @@ -1,4 +1,4 @@ -package io.github.wulkanowy.api; +package io.github.wulkanowy.api.generic; public class Semester implements ParamItem { diff --git a/api/src/main/java/io/github/wulkanowy/api/Student.java b/api/src/main/java/io/github/wulkanowy/api/generic/Student.java similarity index 93% rename from api/src/main/java/io/github/wulkanowy/api/Student.java rename to api/src/main/java/io/github/wulkanowy/api/generic/Student.java index 8b22f1d83..4ed5dd373 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Student.java +++ b/api/src/main/java/io/github/wulkanowy/api/generic/Student.java @@ -1,4 +1,4 @@ -package io.github.wulkanowy.api; +package io.github.wulkanowy.api.generic; public class Student implements ParamItem { diff --git a/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java b/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java index 936c1533b..cb9fe4657 100644 --- a/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java +++ b/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java @@ -5,42 +5,32 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getFormattedDate; + public class GradesList { private static final String GRADES_PAGE_URL = "Oceny/Wszystkie?details=2&okres="; private SnP snp; - private List grades = new ArrayList<>(); - public GradesList(SnP snp) { this.snp = snp; } - private String getGradesPageUrl() { - return GRADES_PAGE_URL; - } - - public List getAll() throws IOException, ParseException, VulcanException { - return getAll(""); - } - - public List getAll(String semester) throws IOException, ParseException, VulcanException { - Document gradesPage = snp.getSnPPageDocument(getGradesPageUrl() + semester); + public List getAll(String semester) throws IOException, VulcanException { + Document gradesPage = snp.getSnPPageDocument(GRADES_PAGE_URL + semester); Elements gradesRows = gradesPage.select(".ocenySzczegoly-table > tbody > tr"); + List grades = new ArrayList<>(); + for (Element row : gradesRows) { if ("Brak ocen".equals(row.select("td:nth-child(2)").text())) { continue; @@ -52,13 +42,13 @@ public class GradesList { return grades; } - private Grade getGrade(Element row) throws ParseException { + private Grade getGrade(Element row) { String descriptions = row.select("td:nth-child(3)").text(); String symbol = descriptions.split(", ")[0]; String description = descriptions.replaceFirst(Pattern.quote(symbol), "").replaceFirst(", ", ""); String color = getColor(row.select("td:nth-child(2) span.ocenaCzastkowa").attr("style")); - String date = formatDate(row.select("td:nth-child(5)").text()); + String date = getFormattedDate(row.select("td:nth-child(5)").text()); return new Grade() .setSubject(row.select("td:nth-child(1)").text()) @@ -82,12 +72,4 @@ public class GradesList { return color; } - - private String formatDate(String date) throws ParseException { - SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - Date d = sdf.parse(date); - sdf.applyPattern("yyyy-MM-dd"); - - return sdf.format(d); - } } diff --git a/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java b/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java index 11d013429..346df832e 100644 --- a/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java +++ b/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java @@ -14,9 +14,7 @@ public class AchievementsList { private static final String NOTES_PAGE_URL = "UwagiOsiagniecia.mvc/Wszystkie"; - private SnP snp = null; - - private List achievements = new ArrayList<>(); + private SnP snp; public AchievementsList(SnP snp) { this.snp = snp; @@ -27,6 +25,8 @@ public class AchievementsList { .select(".mainContainer > div").get(1); Elements items = pageFragment.select("article"); + List achievements = new ArrayList<>(); + for (Element item : items) { achievements.add(item.text()); } diff --git a/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java b/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java index 0c8a30b6b..54f57edf4 100644 --- a/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java +++ b/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java @@ -10,13 +10,13 @@ import java.util.List; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getFormattedDate; + public class NotesList { private static final String NOTES_PAGE_URL = "UwagiOsiagniecia.mvc/Wszystkie"; - private SnP snp = null; - - private List notes = new ArrayList<>(); + private SnP snp; public NotesList(SnP snp) { this.snp = snp; @@ -28,10 +28,12 @@ public class NotesList { Elements items = pageFragment.select("article"); Elements dates = pageFragment.select("h2"); + List notes = new ArrayList<>(); + int index = 0; for (Element item : items) { notes.add(new Note() - .setDate(dates.get(index++).text()) + .setDate(getFormattedDate(dates.get(index++).text())) .setTeacher(snp.getRowDataChildValue(item, 1)) .setCategory(snp.getRowDataChildValue(item, 2)) .setContent(snp.getRowDataChildValue(item, 3)) diff --git a/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java b/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java index 51a7278bd..f0dcd43de 100644 --- a/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java +++ b/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java @@ -11,7 +11,7 @@ public class SchoolInfo { private static final String SCHOOL_PAGE_URL = "Szkola.mvc/Nauczyciele"; - private SnP snp = null; + private SnP snp; public SchoolInfo(SnP snp) { this.snp = snp; diff --git a/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java b/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java index bbf5f5d7f..ec8429bd9 100644 --- a/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java +++ b/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java @@ -15,7 +15,7 @@ public class TeachersInfo { private static final String SCHOOL_PAGE_URL = "Szkola.mvc/Nauczyciele"; - private SnP snp = null; + private SnP snp; public TeachersInfo(SnP snp) { this.snp = snp; diff --git a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java index 4668149c3..8a2b9e14e 100644 --- a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java +++ b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java @@ -5,18 +5,17 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.Locale; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Lesson; import io.github.wulkanowy.api.generic.Week; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getFormattedDate; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getDateAsTick; + public class Timetable { private static final String TIMETABLE_PAGE_URL = "Lekcja.mvc/PlanZajec?data="; @@ -27,12 +26,12 @@ public class Timetable { this.snp = snp; } - public Week getWeekTable() throws IOException, ParseException, VulcanException { + public Week getWeekTable() throws IOException, VulcanException { return getWeekTable(""); } - public Week getWeekTable(final String tick) throws IOException, ParseException, VulcanException { - Element table = snp.getSnPPageDocument(TIMETABLE_PAGE_URL + tick) + public Week getWeekTable(final String date) throws IOException, VulcanException { + Element table = snp.getSnPPageDocument(TIMETABLE_PAGE_URL + getDateAsTick(date)) .select(".mainContainer .presentData").first(); List days = getDays(table.select("thead th")); @@ -44,19 +43,15 @@ public class Timetable { .setDays(days); } - private List getDays(Elements tableHeaderCells) throws ParseException { + private List getDays(Elements tableHeaderCells) { List days = new ArrayList<>(); for (int i = 2; i < 7; i++) { String[] dayHeaderCell = tableHeaderCells.get(i).html().split("
"); - SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - Date d = sdf.parse(dayHeaderCell[1].trim()); - sdf.applyPattern("yyyy-MM-dd"); - TimetableDay day = new TimetableDay(); day.setDayName(dayHeaderCell[0]); - day.setDate(sdf.format(d)); + day.setDate(getFormattedDate(dayHeaderCell[1].trim())); if (tableHeaderCells.get(i).hasClass("free-day")) { day.setFreeDay(true); diff --git a/api/src/test/java/io/github/wulkanowy/api/DateTimeUtilsTest.kt b/api/src/test/java/io/github/wulkanowy/api/DateTimeUtilsTest.kt new file mode 100644 index 000000000..1105b2a72 --- /dev/null +++ b/api/src/test/java/io/github/wulkanowy/api/DateTimeUtilsTest.kt @@ -0,0 +1,53 @@ +package io.github.wulkanowy.api + +import org.junit.Assert +import org.junit.Test +import java.text.ParseException +import java.text.SimpleDateFormat +import java.util.* + +class DateTimeUtilsTest { + + @Test + fun getTicksDateObjectTest() { + val format = SimpleDateFormat("dd.MM.yyyy", Locale.ROOT) + format.timeZone = TimeZone.getTimeZone("UTC") + val date = format.parse("31.07.2017") + + Assert.assertEquals(636370560000000000L, getDateAsTick(date)) + + val calendar = Calendar.getInstance() + calendar.time = date + calendar.add(Calendar.DAY_OF_YEAR, -14) + val dateTwoWeekBefore = calendar.time + + Assert.assertEquals(636358464000000000L, getDateAsTick(dateTwoWeekBefore)) + } + + @Test(expected = ParseException::class) + fun getTicsStringInvalidFormatTest() { + Assert.assertEquals(636370560000000000L, getDateAsTick("31.07.2017", "dd.MMM.yyyy")) + } + + @Test + fun getTicsStringFormatTest() { + Assert.assertEquals(636370560000000000L, getDateAsTick("31.07.2017", "dd.MM.yyyy")) + } + + @Test + fun getTicsStringTest() { + Assert.assertEquals("636370560000000000", getDateAsTick("2017-07-31")) + Assert.assertEquals("636334272000000000", getDateAsTick("2017-06-19")) + Assert.assertEquals("636189120000000000", getDateAsTick("2017-01-02")) + Assert.assertEquals("636080256000000000", getDateAsTick("2016-08-29")) + } + + @Test + fun getDateTest() { + val format = SimpleDateFormat("dd.MM.yyyy", Locale.ROOT) + format.timeZone = TimeZone.getTimeZone("UTC") + val date = format.parse("31.07.2017") + + Assert.assertEquals(date, getDate(636370560000000000L)) + } +} diff --git a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java index 387d18f58..1b6f7a91b 100644 --- a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java @@ -11,6 +11,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import io.github.wulkanowy.api.generic.Semester; + public class StudentAndParentTest { private Client client; diff --git a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java index 8d32291fe..764e8009e 100644 --- a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java +++ b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java @@ -5,6 +5,8 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.mockito.Mockito; +import io.github.wulkanowy.api.generic.Semester; + public abstract class StudentAndParentTestCase { protected StudentAndParent getSnp(String fixtureFileName) throws Exception { diff --git a/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java b/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java index 6eff84c95..deab7617c 100644 --- a/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java @@ -19,12 +19,12 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getAllTest() throws Exception { - Assert.assertEquals(7, filled.getAll().size()); // 2 items are skipped + Assert.assertEquals(7, filled.getAll("").size()); // 2 items are skipped } @Test public void getSubjectTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("Zajęcia z wychowawcą", list.get(0).getSubject()); Assert.assertEquals("Język angielski", list.get(3).getSubject()); @@ -34,7 +34,7 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getValueTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("5", list.get(0).getValue()); Assert.assertEquals("5", list.get(3).getValue()); @@ -44,7 +44,7 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getColorTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("000000", list.get(0).getColor()); Assert.assertEquals("1289F7", list.get(3).getColor()); @@ -54,7 +54,7 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getSymbolTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("A1", list.get(0).getSymbol()); Assert.assertEquals("BW3", list.get(3).getSymbol()); @@ -65,7 +65,7 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getDescriptionTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("Dzień Kobiet w naszej klasie", list.get(0).getDescription()); Assert.assertEquals("Writing", list.get(3).getDescription()); @@ -76,7 +76,7 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getWeightTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("1,00", list.get(0).getWeight()); Assert.assertEquals("3,00", list.get(3).getWeight()); @@ -86,7 +86,7 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getDateTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("2017-03-21", list.get(0).getDate()); Assert.assertEquals("2017-06-02", list.get(3).getDate()); @@ -96,7 +96,7 @@ public class GradesListTest extends StudentAndParentTestCase { @Test public void getTeacherTest() throws Exception { - List list = filled.getAll(); + List list = filled.getAll(""); Assert.assertEquals("Patryk Maciejewski", list.get(0).getTeacher()); Assert.assertEquals("Oliwia Woźniak", list.get(3).getTeacher()); diff --git a/api/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java b/api/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java index d76c06486..304eb01a8 100644 --- a/api/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java @@ -30,8 +30,8 @@ public class NotesListTest extends StudentAndParentTestCase { public void getDateTest() throws Exception { List filledList = filled.getAllNotes(); - Assert.assertEquals("06.06.2017", filledList.get(0).getDate()); - Assert.assertEquals("01.10.2016", filledList.get(2).getDate()); + Assert.assertEquals("2017-06-06", filledList.get(0).getDate()); + Assert.assertEquals("2016-10-01", filledList.get(2).getDate()); } @Test diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/migrations/Migration23.java b/app/src/main/java/io/github/wulkanowy/data/db/dao/migrations/Migration23.java index b32c6261b..97b575a02 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/migrations/Migration23.java +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/migrations/Migration23.java @@ -9,7 +9,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import io.github.wulkanowy.api.Diary; +import io.github.wulkanowy.api.generic.Diary; import io.github.wulkanowy.api.Vulcan; import io.github.wulkanowy.data.db.dao.DbHelper; import io.github.wulkanowy.data.db.shared.SharedPrefContract; diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/AttendanceSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/AttendanceSync.java index 59a6e1a3b..9f379be60 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/AttendanceSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/AttendanceSync.java @@ -1,7 +1,6 @@ package io.github.wulkanowy.data.sync; import java.io.IOException; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; @@ -20,7 +19,6 @@ import io.github.wulkanowy.data.db.dao.entities.Week; import io.github.wulkanowy.data.db.dao.entities.WeekDao; import io.github.wulkanowy.utils.DataObjectConverter; import io.github.wulkanowy.utils.LogUtils; -import io.github.wulkanowy.utils.TimeUtils; @Singleton public class AttendanceSync { @@ -37,10 +35,10 @@ public class AttendanceSync { this.vulcan = vulcan; } - public void syncAttendance(long diaryId, String date) throws IOException, ParseException, VulcanException { + public void syncAttendance(long diaryId, String date) throws IOException, VulcanException { this.diaryId = diaryId; - io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(getNormalizedDate(date)); + io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(date); Week weekDb = getWeekFromDb(weekApi.getStartDayDate()); long weekId = updateWeekInDb(weekDb, weekApi); @@ -52,12 +50,8 @@ public class AttendanceSync { LogUtils.debug("Synchronization attendance lessons (amount = " + lessonList.size() + ")"); } - private String getNormalizedDate(String date) throws ParseException { - return null != date ? String.valueOf(TimeUtils.getNetTicks(date)) : ""; - } - private io.github.wulkanowy.api.generic.Week getWeekFromApi(String date) - throws IOException, ParseException, VulcanException { + throws IOException, VulcanException { return vulcan.getAttendanceTable().getWeekTable(date); } diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/ExamsSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/ExamsSync.java index 53470882d..85b4dac40 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/ExamsSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/ExamsSync.java @@ -1,7 +1,6 @@ package io.github.wulkanowy.data.sync; import java.io.IOException; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; @@ -19,7 +18,6 @@ import io.github.wulkanowy.data.db.dao.entities.Week; import io.github.wulkanowy.data.db.dao.entities.WeekDao; import io.github.wulkanowy.utils.DataObjectConverter; import io.github.wulkanowy.utils.LogUtils; -import io.github.wulkanowy.utils.TimeUtils; public class ExamsSync { @@ -35,11 +33,10 @@ public class ExamsSync { this.vulcan = vulcan; } - public void syncExams(long diaryId, String date) throws IOException, VulcanException, - ParseException { + public void syncExams(long diaryId, String date) throws IOException, VulcanException { this.diaryId = diaryId; - io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(getNormalizedDate(date)); + io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(date); Week weekDb = getWeekFromDb(weekApi.getStartDayDate()); long weekId = updateWeekInDb(weekDb, weekApi); @@ -59,7 +56,7 @@ public class ExamsSync { } private io.github.wulkanowy.api.generic.Week getWeekFromApi(String date) - throws VulcanException, IOException, ParseException { + throws VulcanException, IOException { return vulcan.getExamsList().getWeek(date, true); } @@ -77,10 +74,6 @@ public class ExamsSync { return daoSession.getWeekDao().insert(weekApiEntity); } - private String getNormalizedDate(String date) throws ParseException { - return null != date ? String.valueOf(TimeUtils.getNetTicks(date)) : ""; - } - private Day getDayFromDb(String date, long weekId) { return daoSession.getDayDao().queryBuilder().where( DayDao.Properties.WeekId.eq(weekId), diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/TimetableSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/TimetableSync.java index 3fe22afff..309403c88 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/TimetableSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/TimetableSync.java @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.sync; import org.apache.commons.collections4.CollectionUtils; import java.io.IOException; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; @@ -22,7 +21,6 @@ import io.github.wulkanowy.data.db.dao.entities.Week; import io.github.wulkanowy.data.db.dao.entities.WeekDao; import io.github.wulkanowy.utils.DataObjectConverter; import io.github.wulkanowy.utils.LogUtils; -import io.github.wulkanowy.utils.TimeUtils; @Singleton public class TimetableSync { @@ -39,10 +37,10 @@ public class TimetableSync { this.vulcan = vulcan; } - public void syncTimetable(long diaryId, String date) throws IOException, ParseException, VulcanException { + public void syncTimetable(long diaryId, String date) throws IOException, VulcanException { this.diaryId = diaryId; - io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(getNormalizedDate(date)); + io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(date); Week weekDb = getWeekFromDb(weekApi.getStartDayDate()); long weekId = updateWeekInDb(weekDb, weekApi); @@ -54,12 +52,8 @@ public class TimetableSync { LogUtils.debug("Synchronization timetable lessons (amount = " + lessonList.size() + ")"); } - private String getNormalizedDate(String date) throws ParseException { - return null != date ? String.valueOf(TimeUtils.getNetTicks(date)) : ""; - } - private io.github.wulkanowy.api.generic.Week getWeekFromApi(String date) - throws IOException, ParseException, VulcanException { + throws IOException, VulcanException { return vulcan.getTimetable().getWeekTable(date); } diff --git a/app/src/main/java/io/github/wulkanowy/utils/DataObjectConverter.java b/app/src/main/java/io/github/wulkanowy/utils/DataObjectConverter.java index 3f733ad6d..1747fc265 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/DataObjectConverter.java +++ b/app/src/main/java/io/github/wulkanowy/utils/DataObjectConverter.java @@ -21,10 +21,10 @@ public final class DataObjectConverter { throw new IllegalStateException("Utility class"); } - public static List studentsToStudentEntities(List students, Long symbolId) { + public static List studentsToStudentEntities(List students, Long symbolId) { List studentList = new ArrayList<>(); - for (io.github.wulkanowy.api.Student student : students) { + for (io.github.wulkanowy.api.generic.Student student : students) { studentList.add(new Student() .setName(student.getName()) .setCurrent(student.isCurrent()) @@ -36,10 +36,10 @@ public final class DataObjectConverter { return studentList; } - public static List diariesToDiaryEntities(List diaryList, Long studentId) { + public static List diariesToDiaryEntities(List diaryList, Long studentId) { List diaryEntityList = new ArrayList<>(); - for (io.github.wulkanowy.api.Diary diary : diaryList) { + for (io.github.wulkanowy.api.generic.Diary diary : diaryList) { diaryEntityList.add(new Diary() .setStudentId(studentId) .setValue(diary.getId()) @@ -50,10 +50,10 @@ public final class DataObjectConverter { return diaryEntityList; } - public static List semestersToSemesterEntities(List semesters, long diaryId) { + public static List semestersToSemesterEntities(List semesters, long diaryId) { List semesterList = new ArrayList<>(); - for (io.github.wulkanowy.api.Semester semester : semesters) { + for (io.github.wulkanowy.api.generic.Semester semester : semesters) { semesterList.add(new Semester() .setDiaryId(diaryId) .setName(semester.getName()) diff --git a/app/src/main/java/io/github/wulkanowy/utils/TimeUtils.java b/app/src/main/java/io/github/wulkanowy/utils/TimeUtils.java index eaed222e2..a305bc6d1 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/TimeUtils.java +++ b/app/src/main/java/io/github/wulkanowy/utils/TimeUtils.java @@ -4,54 +4,21 @@ import org.threeten.bp.DayOfWeek; import org.threeten.bp.LocalDate; import org.threeten.bp.format.DateTimeFormatter; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; import java.util.List; -import java.util.Locale; -import java.util.TimeZone; public final class TimeUtils { - private static final long TICKS_AT_EPOCH = 621355968000000000L; - - private static final long TICKS_PER_MILLISECOND = 10000; - private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(AppConstant.DATE_PATTERN); private TimeUtils() { throw new IllegalStateException("Utility class"); } - public static long getNetTicks(Date date) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - - return (calendar.getTimeInMillis() * TICKS_PER_MILLISECOND) + TICKS_AT_EPOCH; - } - - public static long getNetTicks(String dateString) throws ParseException { - return getNetTicks(dateString, AppConstant.DATE_PATTERN); - } - - public static long getNetTicks(String dateString, String dateFormat) throws ParseException { - SimpleDateFormat format = new SimpleDateFormat(dateFormat, Locale.ROOT); - format.setTimeZone(TimeZone.getTimeZone("UTC")); - Date dateObject = format.parse(dateString); - - return getNetTicks(dateObject); - } - public static LocalDate getParsedDate(String dateString, String dateFormat) { return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat)); } - public static Date getDate(long netTicks) { - return new Date((netTicks - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND); - } - public static List getMondaysFromCurrentSchoolYear() { LocalDate startDate = LocalDate.of(getCurrentSchoolYear(), 9, 1); LocalDate endDate = LocalDate.of(getCurrentSchoolYear() + 1, 8, 31); diff --git a/app/src/test/java/io/github/wulkanowy/utils/TimeUtilsTest.java b/app/src/test/java/io/github/wulkanowy/utils/TimeUtilsTest.java index 41e606891..e216392e7 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/TimeUtilsTest.java +++ b/app/src/test/java/io/github/wulkanowy/utils/TimeUtilsTest.java @@ -4,62 +4,12 @@ import org.junit.Assert; import org.junit.Test; import org.threeten.bp.LocalDate; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Locale; -import java.util.TimeZone; - public class TimeUtilsTest { - @Test - public void getTicksDateObjectTest() throws Exception { - SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - format.setTimeZone(TimeZone.getTimeZone("UTC")); - Date date = format.parse("31.07.2017"); - - Assert.assertEquals(636370560000000000L, TimeUtils.getNetTicks(date)); - - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - calendar.add(Calendar.DAY_OF_YEAR, -14); - Date dateTwoWeekBefore = calendar.getTime(); - - Assert.assertEquals(636358464000000000L, TimeUtils.getNetTicks(dateTwoWeekBefore)); - } - - @Test(expected = ParseException.class) - public void getTicsStringInvalidFormatTest() throws Exception { - Assert.assertEquals(636370560000000000L, TimeUtils.getNetTicks("31.07.2017", "dd.MMM.yyyy")); - } - - @Test - public void getTicsStringFormatTest() throws Exception { - Assert.assertEquals(636370560000000000L, TimeUtils.getNetTicks("31.07.2017", "dd.MM.yyyy")); - } - - @Test - public void getTicsStringTest() throws Exception { - Assert.assertEquals(636370560000000000L, TimeUtils.getNetTicks("2017-07-31")); - Assert.assertEquals(636334272000000000L, TimeUtils.getNetTicks("2017-06-19")); - Assert.assertEquals(636189120000000000L, TimeUtils.getNetTicks("2017-01-02")); - Assert.assertEquals(636080256000000000L, TimeUtils.getNetTicks("2016-08-29")); - } - @Test public void getParsedDateTest() { - Assert.assertEquals(LocalDate.of(1970, 1, 1), TimeUtils.getParsedDate("1970-01-01", "yyyy-MM-dd")); - } - - @Test - public void getDateTest() throws Exception { - DateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - format.setTimeZone(TimeZone.getTimeZone("UTC")); - Date date = format.parse("31.07.2017"); - - Assert.assertEquals(date, TimeUtils.getDate(636370560000000000L)); + Assert.assertEquals(LocalDate.of(1970, 1, 1), + TimeUtils.getParsedDate("1970-01-01", "yyyy-MM-dd")); } @Test