1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-19 02:56:45 -06:00

Move ticks converter to api (#130)

This commit is contained in:
Mikołaj Pich 2018-06-01 12:52:03 +02:00 committed by Rafał Borcz
parent e2003e2538
commit dde5775a41
28 changed files with 195 additions and 215 deletions

View File

@ -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)
}

View File

@ -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();

View File

@ -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";

View File

@ -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<Day> getWeekTable() throws IOException, ParseException, VulcanException {
public Week<Day> getWeekTable() throws IOException, VulcanException {
return getWeekTable("");
}
public Week<Day> getWeekTable(String tick) throws IOException, ParseException, VulcanException {
Element table = snp.getSnPPageDocument(ATTENDANCE_PAGE_URL + tick)
public Week<Day> 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("<br>");
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");

View File

@ -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<ExamDay> getCurrent() throws IOException, VulcanException, ParseException {
public Week<ExamDay> getCurrent() throws IOException, VulcanException {
return getWeek("", true);
}
public Week<ExamDay> getWeek(String tick, final boolean onlyNotEmpty) throws IOException, VulcanException, ParseException {
Document examsPage = snp.getSnPPageDocument(EXAMS_PAGE_URL + tick);
public Week<ExamDay> 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<ExamDay> 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);
}
}

View File

@ -37,7 +37,8 @@ public class Day {
return dayName;
}
public void setDayName(String dayName) {
public Day setDayName(String dayName) {
this.dayName = dayName;
return this;
}
}

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.api;
package io.github.wulkanowy.api.generic;
public class Diary implements ParamItem {

View File

@ -1,6 +1,6 @@
package io.github.wulkanowy.api;
package io.github.wulkanowy.api.generic;
interface ParamItem {
public interface ParamItem {
ParamItem setId(String id);

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.api;
package io.github.wulkanowy.api.generic;
public class Semester implements ParamItem {

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.api;
package io.github.wulkanowy.api.generic;
public class Student implements ParamItem {

View File

@ -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<Grade> grades = new ArrayList<>();
public GradesList(SnP snp) {
this.snp = snp;
}
private String getGradesPageUrl() {
return GRADES_PAGE_URL;
}
public List<Grade> getAll() throws IOException, ParseException, VulcanException {
return getAll("");
}
public List<Grade> getAll(String semester) throws IOException, ParseException, VulcanException {
Document gradesPage = snp.getSnPPageDocument(getGradesPageUrl() + semester);
public List<Grade> getAll(String semester) throws IOException, VulcanException {
Document gradesPage = snp.getSnPPageDocument(GRADES_PAGE_URL + semester);
Elements gradesRows = gradesPage.select(".ocenySzczegoly-table > tbody > tr");
List<Grade> 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);
}
}

View File

@ -14,9 +14,7 @@ public class AchievementsList {
private static final String NOTES_PAGE_URL = "UwagiOsiagniecia.mvc/Wszystkie";
private SnP snp = null;
private List<String> 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<String> achievements = new ArrayList<>();
for (Element item : items) {
achievements.add(item.text());
}

View File

@ -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<Note> 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<Note> 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))

View File

@ -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;

View File

@ -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;

View File

@ -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<TimetableDay> getWeekTable() throws IOException, ParseException, VulcanException {
public Week<TimetableDay> getWeekTable() throws IOException, VulcanException {
return getWeekTable("");
}
public Week<TimetableDay> getWeekTable(final String tick) throws IOException, ParseException, VulcanException {
Element table = snp.getSnPPageDocument(TIMETABLE_PAGE_URL + tick)
public Week<TimetableDay> getWeekTable(final String date) throws IOException, VulcanException {
Element table = snp.getSnPPageDocument(TIMETABLE_PAGE_URL + getDateAsTick(date))
.select(".mainContainer .presentData").first();
List<TimetableDay> days = getDays(table.select("thead th"));
@ -44,19 +43,15 @@ public class Timetable {
.setDays(days);
}
private List<TimetableDay> getDays(Elements tableHeaderCells) throws ParseException {
private List<TimetableDay> getDays(Elements tableHeaderCells) {
List<TimetableDay> days = new ArrayList<>();
for (int i = 2; i < 7; i++) {
String[] dayHeaderCell = tableHeaderCells.get(i).html().split("<br>");
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);

View File

@ -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))
}
}

View File

@ -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;

View File

@ -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 {

View File

@ -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<Grade> list = filled.getAll();
List<Grade> 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<Grade> list = filled.getAll();
List<Grade> 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<Grade> list = filled.getAll();
List<Grade> 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<Grade> list = filled.getAll();
List<Grade> 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<Grade> list = filled.getAll();
List<Grade> 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<Grade> list = filled.getAll();
List<Grade> 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<Grade> list = filled.getAll();
List<Grade> 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<Grade> list = filled.getAll();
List<Grade> list = filled.getAll("");
Assert.assertEquals("Patryk Maciejewski", list.get(0).getTeacher());
Assert.assertEquals("Oliwia Woźniak", list.get(3).getTeacher());

View File

@ -30,8 +30,8 @@ public class NotesListTest extends StudentAndParentTestCase {
public void getDateTest() throws Exception {
List<Note> 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

View File

@ -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;

View File

@ -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<io.github.wulkanowy.api.generic.Day> weekApi = getWeekFromApi(getNormalizedDate(date));
io.github.wulkanowy.api.generic.Week<io.github.wulkanowy.api.generic.Day> 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<io.github.wulkanowy.api.generic.Day> getWeekFromApi(String date)
throws IOException, ParseException, VulcanException {
throws IOException, VulcanException {
return vulcan.getAttendanceTable().getWeekTable(date);
}

View File

@ -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<ExamDay> weekApi = getWeekFromApi(getNormalizedDate(date));
io.github.wulkanowy.api.generic.Week<ExamDay> 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<ExamDay> 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),

View File

@ -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<io.github.wulkanowy.api.timetable.TimetableDay> weekApi = getWeekFromApi(getNormalizedDate(date));
io.github.wulkanowy.api.generic.Week<io.github.wulkanowy.api.timetable.TimetableDay> 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<io.github.wulkanowy.api.timetable.TimetableDay> getWeekFromApi(String date)
throws IOException, ParseException, VulcanException {
throws IOException, VulcanException {
return vulcan.getTimetable().getWeekTable(date);
}

View File

@ -21,10 +21,10 @@ public final class DataObjectConverter {
throw new IllegalStateException("Utility class");
}
public static List<Student> studentsToStudentEntities(List<io.github.wulkanowy.api.Student> students, Long symbolId) {
public static List<Student> studentsToStudentEntities(List<io.github.wulkanowy.api.generic.Student> students, Long symbolId) {
List<Student> 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<Diary> diariesToDiaryEntities(List<io.github.wulkanowy.api.Diary> diaryList, Long studentId) {
public static List<Diary> diariesToDiaryEntities(List<io.github.wulkanowy.api.generic.Diary> diaryList, Long studentId) {
List<Diary> 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<Semester> semestersToSemesterEntities(List<io.github.wulkanowy.api.Semester> semesters, long diaryId) {
public static List<Semester> semestersToSemesterEntities(List<io.github.wulkanowy.api.generic.Semester> semesters, long diaryId) {
List<Semester> 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())

View File

@ -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<String> getMondaysFromCurrentSchoolYear() {
LocalDate startDate = LocalDate.of(getCurrentSchoolYear(), 9, 1);
LocalDate endDate = LocalDate.of(getCurrentSchoolYear() + 1, 8, 31);

View File

@ -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