forked from github/wulkanowy-mirror
Move ticks converter to api (#130)
This commit is contained in:

committed by
Rafał Borcz

parent
e2003e2538
commit
dde5775a41
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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())
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user