From d7c4e6b22264bbd76b35fae997fb8fbfeb328d1c Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Tue, 21 May 2019 02:09:07 +0400 Subject: [PATCH 1/3] #91 base users dashboard --- .../repository/ConferenceRepository.java | 4 + .../conference/service/ConferenceService.java | 4 + .../service/ConferenceUserService.java | 2 - .../user/controller/UserMvcController.java | 5 + .../java/ru/ulstu/user/model/UserInfoNow.java | 50 ++++++++++ .../repository/UserSessionRepository.java | 3 + .../ru/ulstu/user/service/UserService.java | 37 ++++++- .../user/service/UserSessionService.java | 6 ++ .../utils/timetable/TimetableService.java | 97 +++++++++++++++++++ .../ru/ulstu/utils/timetable/model/Day.java | 27 ++++++ .../ulstu/utils/timetable/model/Lesson.java | 27 ++++++ .../ulstu/utils/timetable/model/Response.java | 16 +++ .../timetable/model/TimetableResponse.java | 22 +++++ .../ru/ulstu/utils/timetable/model/Week.java | 16 +++ .../resources/templates/users/dashboard.html | 24 +++++ .../fragments/userDashboardFragment.html | 18 ++++ 16 files changed, 355 insertions(+), 3 deletions(-) create mode 100644 src/main/java/ru/ulstu/user/model/UserInfoNow.java create mode 100644 src/main/java/ru/ulstu/utils/timetable/TimetableService.java create mode 100644 src/main/java/ru/ulstu/utils/timetable/model/Day.java create mode 100644 src/main/java/ru/ulstu/utils/timetable/model/Lesson.java create mode 100644 src/main/java/ru/ulstu/utils/timetable/model/Response.java create mode 100644 src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java create mode 100644 src/main/java/ru/ulstu/utils/timetable/model/Week.java create mode 100644 src/main/resources/templates/users/dashboard.html create mode 100644 src/main/resources/templates/users/fragments/userDashboardFragment.html diff --git a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java index 2b0a428..8f9e05f 100644 --- a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java +++ b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java @@ -29,4 +29,8 @@ public interface ConferenceRepository extends JpaRepository @Override @Query("SELECT title FROM Conference c WHERE (c.title = :name) AND (:id IS NULL OR c.id != :id) ") String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id); + + @Query("SELECT c FROM Conference c LEFT JOIN c.users u WHERE (:user IS NULL OR u.user = :user) " + + "AND (u.participation = 'INTRAMURAL') AND (c.beginDate <= CURRENT_DATE) AND (c.endDate >= CURRENT_DATE)") + Conference findActiveByUser(@Param("user") User user); } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 38ce659..1a5db52 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -306,4 +306,8 @@ public class ConferenceService extends BaseService { .filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription())) .collect(Collectors.toList())); } + + public Conference getActiveConferenceByUser(User user) { + return conferenceRepository.findActiveByUser(user); + } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceUserService.java b/src/main/java/ru/ulstu/conference/service/ConferenceUserService.java index f771b2a..16842c1 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceUserService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceUserService.java @@ -43,6 +43,4 @@ public class ConferenceUserService { newUser = conferenceUserRepository.save(newUser); return newUser; } - - } diff --git a/src/main/java/ru/ulstu/user/controller/UserMvcController.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java index 5bcbd13..d283a21 100644 --- a/src/main/java/ru/ulstu/user/controller/UserMvcController.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -48,4 +48,9 @@ public class UserMvcController extends OdinController { User user = userSessionService.getUserBySessionId(sessionId); modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); } + + @GetMapping("/dashboard") + public void getUsersDashboard(ModelMap modelMap) { + modelMap.addAttribute("users", userService.getUsersInfo()); + } } diff --git a/src/main/java/ru/ulstu/user/model/UserInfoNow.java b/src/main/java/ru/ulstu/user/model/UserInfoNow.java new file mode 100644 index 0000000..7d69c56 --- /dev/null +++ b/src/main/java/ru/ulstu/user/model/UserInfoNow.java @@ -0,0 +1,50 @@ +package ru.ulstu.user.model; + +import ru.ulstu.conference.model.Conference; +import ru.ulstu.utils.timetable.model.Lesson; + +public class UserInfoNow { + private Lesson lesson; + private Conference conference; + private User user; + private boolean isOnline; + + public UserInfoNow(Lesson lesson, Conference conference, User user, boolean isOnline) { + this.lesson = lesson; + this.conference = conference; + this.user = user; + this.isOnline = isOnline; + } + + public Lesson getLesson() { + return lesson; + } + + public void setLesson(Lesson lesson) { + this.lesson = lesson; + } + + public Conference getConference() { + return conference; + } + + public void setConference(Conference conference) { + this.conference = conference; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public boolean isOnline() { + return isOnline; + } + + public void setOnline(boolean online) { + isOnline = online; + } +} diff --git a/src/main/java/ru/ulstu/user/repository/UserSessionRepository.java b/src/main/java/ru/ulstu/user/repository/UserSessionRepository.java index b922e4f..5319245 100644 --- a/src/main/java/ru/ulstu/user/repository/UserSessionRepository.java +++ b/src/main/java/ru/ulstu/user/repository/UserSessionRepository.java @@ -1,6 +1,7 @@ package ru.ulstu.user.repository; import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserSession; import java.util.Date; @@ -10,4 +11,6 @@ public interface UserSessionRepository extends JpaRepository findAllByLogoutTimeIsNullAndLoginTimeBefore(Date date); + + List findAllByUserAndLogoutTimeIsNullAndLoginTimeBefore(User user, Date date); } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index c889caa..334fbf5 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -3,6 +3,7 @@ package ru.ulstu.user.service; import com.google.common.collect.ImmutableMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Lazy; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort; import org.springframework.mail.MailException; @@ -13,6 +14,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; +import ru.ulstu.conference.service.ConferenceService; import ru.ulstu.configuration.ApplicationProperties; import ru.ulstu.core.error.EntityIdIsNullException; import ru.ulstu.core.jpa.OffsetablePageRequest; @@ -30,6 +32,7 @@ import ru.ulstu.user.error.UserResetKeyError; import ru.ulstu.user.error.UserSendingMailException; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; +import ru.ulstu.user.model.UserInfoNow; import ru.ulstu.user.model.UserListDto; import ru.ulstu.user.model.UserResetPasswordDto; import ru.ulstu.user.model.UserRole; @@ -38,8 +41,13 @@ import ru.ulstu.user.model.UserRoleDto; import ru.ulstu.user.repository.UserRepository; import ru.ulstu.user.repository.UserRoleRepository; import ru.ulstu.user.util.UserUtils; +import ru.ulstu.utils.timetable.TimetableService; +import ru.ulstu.utils.timetable.model.Lesson; import javax.mail.MessagingException; +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; @@ -62,19 +70,27 @@ public class UserService implements UserDetailsService { private final UserMapper userMapper; private final MailService mailService; private final ApplicationProperties applicationProperties; + private final TimetableService timetableService; + private final ConferenceService conferenceService; + private final UserSessionService userSessionService; public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, UserRoleRepository userRoleRepository, UserMapper userMapper, MailService mailService, - ApplicationProperties applicationProperties) { + ApplicationProperties applicationProperties, + @Lazy ConferenceService conferenceRepository, + @Lazy UserSessionService userSessionService) throws ParseException { this.userRepository = userRepository; this.passwordEncoder = passwordEncoder; this.userRoleRepository = userRoleRepository; this.userMapper = userMapper; this.mailService = mailService; this.applicationProperties = applicationProperties; + this.conferenceService = conferenceRepository; + this.timetableService = new TimetableService(); + this.userSessionService = userSessionService; } private User getUserByEmail(String email) { @@ -341,4 +357,23 @@ public class UserService implements UserDetailsService { throw new UserSendingMailException(email); } } + + public List getUsersInfo() { + List usersInfoNow = new ArrayList<>(); + for (User user : userRepository.findAll()) { + Lesson lesson = null; + try { + lesson = timetableService.getCurrentLesson(user.getUserAbbreviate()); + } catch (IOException e) { + e.printStackTrace(); + } + usersInfoNow.add(new UserInfoNow( + lesson, + conferenceService.getActiveConferenceByUser(user), + user, + userSessionService.isOnline(user)) + ); + } + return usersInfoNow; + } } diff --git a/src/main/java/ru/ulstu/user/service/UserSessionService.java b/src/main/java/ru/ulstu/user/service/UserSessionService.java index ae289d0..17a7f5b 100644 --- a/src/main/java/ru/ulstu/user/service/UserSessionService.java +++ b/src/main/java/ru/ulstu/user/service/UserSessionService.java @@ -14,6 +14,8 @@ import ru.ulstu.user.model.UserSession; import ru.ulstu.user.model.UserSessionListDto; import ru.ulstu.user.repository.UserSessionRepository; +import java.util.Date; + import static ru.ulstu.core.util.StreamApiUtils.convert; @Service @@ -58,4 +60,8 @@ public class UserSessionService { public User getUserBySessionId(String sessionId) { return userSessionRepository.findOneBySessionId(sessionId).getUser(); } + + public boolean isOnline(User user) { + return !userSessionRepository.findAllByUserAndLogoutTimeIsNullAndLoginTimeBefore(user, new Date()).isEmpty(); + } } diff --git a/src/main/java/ru/ulstu/utils/timetable/TimetableService.java b/src/main/java/ru/ulstu/utils/timetable/TimetableService.java new file mode 100644 index 0000000..f17ea86 --- /dev/null +++ b/src/main/java/ru/ulstu/utils/timetable/TimetableService.java @@ -0,0 +1,97 @@ +package ru.ulstu.utils.timetable; + +import com.fasterxml.jackson.databind.ObjectMapper; +import ru.ulstu.utils.timetable.model.Lesson; +import ru.ulstu.utils.timetable.model.TimetableResponse; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +public class TimetableService { + private static final String TIMETABLE_URL = "http://timetable.athene.tech"; + private SimpleDateFormat lessonTimeFormat = new SimpleDateFormat("hh:mm"); + + private long[] lessonsStarts = new long[]{ + lessonTimeFormat.parse("8:00:00").getTime(), + lessonTimeFormat.parse("9:40:00").getTime(), + lessonTimeFormat.parse("11:30:00").getTime(), + lessonTimeFormat.parse("13:10:00").getTime(), + lessonTimeFormat.parse("14:50:00").getTime(), + lessonTimeFormat.parse("16:30:00").getTime(), + lessonTimeFormat.parse("18:10:00").getTime(), + }; + + public TimetableService() throws ParseException { } + + private int getCurrentDay() { + Calendar calendar = Calendar.getInstance(); + int day = calendar.get(Calendar.DAY_OF_WEEK); + return (day + 5) % 7; + } + + private int getCurrentLessonNumber() { + long lessonDuration = 90 * 60000; + Date now = new Date(); + long timeNow = now.getTime() % (24 * 60 * 60 * 1000L); + for (int i = 0; i < lessonsStarts.length; i++) { + if (timeNow > lessonsStarts[i] && timeNow < lessonsStarts[i] + lessonDuration) { + return i; + } + } + return -1; + } + + private int getCurrentWeek() { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + return (cal.get(Calendar.WEEK_OF_YEAR) + 1) % 2; + } + + private TimetableResponse getTimetableForUser(String userFIO) throws IOException { + URL url = new URL(TIMETABLE_URL + "/api/1.0/timetable?filter=" + URLEncoder.encode(userFIO, "UTF-8")); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuilder content = new StringBuilder(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + + return new ObjectMapper().readValue(content.toString(), TimetableResponse.class); + } + + public Lesson getCurrentLesson(String userFio) throws IOException { + TimetableResponse response = getTimetableForUser(userFio); + int lessonNumber = getCurrentLessonNumber(); + if (lessonNumber < 0) { + return null; + } + + List lessons = response + .getResponse() + .getWeeks() + .get(getCurrentWeek()) + .getDays() + .get(getCurrentDay()) + .getLessons() + .get(lessonNumber); + + if (lessons.size() == 0) { + return null; + } + return new ObjectMapper().convertValue(lessons.get(0), Lesson.class); + } +} diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Day.java b/src/main/java/ru/ulstu/utils/timetable/model/Day.java new file mode 100644 index 0000000..f548462 --- /dev/null +++ b/src/main/java/ru/ulstu/utils/timetable/model/Day.java @@ -0,0 +1,27 @@ +package ru.ulstu.utils.timetable.model; + +import java.util.List; + + +public class Day { + + private Integer day; + private List> lessons = null; + + public Integer getDay() { + return day; + } + + public void setDay(Integer day) { + this.day = day; + } + + public List> getLessons() { + return lessons; + } + + public void setLessons(List> lessons) { + this.lessons = lessons; + } +} + diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Lesson.java b/src/main/java/ru/ulstu/utils/timetable/model/Lesson.java new file mode 100644 index 0000000..e651e1a --- /dev/null +++ b/src/main/java/ru/ulstu/utils/timetable/model/Lesson.java @@ -0,0 +1,27 @@ +package ru.ulstu.utils.timetable.model; + +public class Lesson { + private String group; + private String nameOfLesson; + private String teacher; + private String room; + + public Lesson() { + } + + public String getGroup() { + return group; + } + + public String getNameOfLesson() { + return nameOfLesson; + } + + public String getTeacher() { + return teacher; + } + + public String getRoom() { + return room; + } +} diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Response.java b/src/main/java/ru/ulstu/utils/timetable/model/Response.java new file mode 100644 index 0000000..b5b9e97 --- /dev/null +++ b/src/main/java/ru/ulstu/utils/timetable/model/Response.java @@ -0,0 +1,16 @@ +package ru.ulstu.utils.timetable.model; + +import java.util.List; + +public class Response { + + private List weeks = null; + + public List getWeeks() { + return weeks; + } + + public void setWeeks(List weeks) { + this.weeks = weeks; + } +} diff --git a/src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java b/src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java new file mode 100644 index 0000000..118c979 --- /dev/null +++ b/src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java @@ -0,0 +1,22 @@ +package ru.ulstu.utils.timetable.model; + +public class TimetableResponse { + private Response response; + private String error; + + public Response getResponse() { + return response; + } + + public void setResponse(Response response) { + this.response = response; + } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } +} diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Week.java b/src/main/java/ru/ulstu/utils/timetable/model/Week.java new file mode 100644 index 0000000..19f3c50 --- /dev/null +++ b/src/main/java/ru/ulstu/utils/timetable/model/Week.java @@ -0,0 +1,16 @@ +package ru.ulstu.utils.timetable.model; + +import java.util.List; + +public class Week { + + private List days = null; + + public List getDays() { + return days; + } + + public void setDays(List days) { + this.days = days; + } +} diff --git a/src/main/resources/templates/users/dashboard.html b/src/main/resources/templates/users/dashboard.html new file mode 100644 index 0000000..8236734 --- /dev/null +++ b/src/main/resources/templates/users/dashboard.html @@ -0,0 +1,24 @@ + + + + + + +
+
+
+
+

Пользователи

+
+
+ +
+ +
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/users/fragments/userDashboardFragment.html b/src/main/resources/templates/users/fragments/userDashboardFragment.html new file mode 100644 index 0000000..267bbf8 --- /dev/null +++ b/src/main/resources/templates/users/fragments/userDashboardFragment.html @@ -0,0 +1,18 @@ + + + + + + +
+
+
+

+

+

+

Онлайн

+
+
+
+ + \ No newline at end of file From 193b2b3a3255f45274f299364e8c398f9f1ce3ad Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Sat, 25 May 2019 14:03:43 +0400 Subject: [PATCH 2/3] #91 using RestTemplate & show error on timetable loading exception --- .../user/controller/UserMvcController.java | 2 +- .../ru/ulstu/user/service/UserService.java | 12 +++-- .../utils/timetable/TimetableService.java | 48 ++++++++----------- .../errors/TimetableClientException.java | 7 +++ .../ru/ulstu/utils/timetable/model/Day.java | 9 ++-- .../ulstu/utils/timetable/model/Lesson.java | 5 +- .../ulstu/utils/timetable/model/Response.java | 3 +- .../timetable/model/TimetableResponse.java | 2 +- .../ru/ulstu/utils/timetable/model/Week.java | 6 ++- src/main/resources/application.properties | 4 +- .../resources/templates/users/dashboard.html | 13 ++++- 11 files changed, 63 insertions(+), 48 deletions(-) create mode 100644 src/main/java/ru/ulstu/utils/timetable/errors/TimetableClientException.java diff --git a/src/main/java/ru/ulstu/user/controller/UserMvcController.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java index d283a21..044bde8 100644 --- a/src/main/java/ru/ulstu/user/controller/UserMvcController.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -51,6 +51,6 @@ public class UserMvcController extends OdinController { @GetMapping("/dashboard") public void getUsersDashboard(ModelMap modelMap) { - modelMap.addAttribute("users", userService.getUsersInfo()); + modelMap.addAllAttributes(userService.getUsersInfo()); } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 334fbf5..b022866 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -42,10 +42,10 @@ import ru.ulstu.user.repository.UserRepository; import ru.ulstu.user.repository.UserRoleRepository; import ru.ulstu.user.util.UserUtils; import ru.ulstu.utils.timetable.TimetableService; +import ru.ulstu.utils.timetable.errors.TimetableClientException; import ru.ulstu.utils.timetable.model.Lesson; import javax.mail.MessagingException; -import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; @@ -358,14 +358,16 @@ public class UserService implements UserDetailsService { } } - public List getUsersInfo() { + public Map getUsersInfo() { List usersInfoNow = new ArrayList<>(); + String err = ""; + for (User user : userRepository.findAll()) { Lesson lesson = null; try { lesson = timetableService.getCurrentLesson(user.getUserAbbreviate()); - } catch (IOException e) { - e.printStackTrace(); + } catch (TimetableClientException e) { + err = "Не удалось загрузить расписание"; } usersInfoNow.add(new UserInfoNow( lesson, @@ -374,6 +376,6 @@ public class UserService implements UserDetailsService { userSessionService.isOnline(user)) ); } - return usersInfoNow; + return ImmutableMap.of("users", usersInfoNow, "error", err); } } diff --git a/src/main/java/ru/ulstu/utils/timetable/TimetableService.java b/src/main/java/ru/ulstu/utils/timetable/TimetableService.java index f17ea86..aa49fed 100644 --- a/src/main/java/ru/ulstu/utils/timetable/TimetableService.java +++ b/src/main/java/ru/ulstu/utils/timetable/TimetableService.java @@ -1,15 +1,12 @@ package ru.ulstu.utils.timetable; import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import ru.ulstu.utils.timetable.errors.TimetableClientException; import ru.ulstu.utils.timetable.model.Lesson; import ru.ulstu.utils.timetable.model.TimetableResponse; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -17,10 +14,10 @@ import java.util.Date; import java.util.List; public class TimetableService { - private static final String TIMETABLE_URL = "http://timetable.athene.tech"; + private static final String TIMETABLE_URL = "http://timetable.athene.tech/api/1.0/timetable?filter=%s"; private SimpleDateFormat lessonTimeFormat = new SimpleDateFormat("hh:mm"); - private long[] lessonsStarts = new long[]{ + private long[] lessonsStarts = new long[] { lessonTimeFormat.parse("8:00:00").getTime(), lessonTimeFormat.parse("9:40:00").getTime(), lessonTimeFormat.parse("11:30:00").getTime(), @@ -30,7 +27,8 @@ public class TimetableService { lessonTimeFormat.parse("18:10:00").getTime(), }; - public TimetableService() throws ParseException { } + public TimetableService() throws ParseException { + } private int getCurrentDay() { Calendar calendar = Calendar.getInstance(); @@ -42,6 +40,7 @@ public class TimetableService { long lessonDuration = 90 * 60000; Date now = new Date(); long timeNow = now.getTime() % (24 * 60 * 60 * 1000L); + for (int i = 0; i < lessonsStarts.length; i++) { if (timeNow > lessonsStarts[i] && timeNow < lessonsStarts[i] + lessonDuration) { return i; @@ -56,31 +55,26 @@ public class TimetableService { return (cal.get(Calendar.WEEK_OF_YEAR) + 1) % 2; } - private TimetableResponse getTimetableForUser(String userFIO) throws IOException { - URL url = new URL(TIMETABLE_URL + "/api/1.0/timetable?filter=" + URLEncoder.encode(userFIO, "UTF-8")); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuilder content = new StringBuilder(); - while ((inputLine = in.readLine()) != null) { - content.append(inputLine); - } - in.close(); - - return new ObjectMapper().readValue(content.toString(), TimetableResponse.class); + private TimetableResponse getTimetableForUser(String userFIO) throws RestClientException { + RestTemplate restTemplate = new RestTemplate(); + return restTemplate.getForObject(String.format(TIMETABLE_URL, userFIO), TimetableResponse.class); } - public Lesson getCurrentLesson(String userFio) throws IOException { - TimetableResponse response = getTimetableForUser(userFio); + public Lesson getCurrentLesson(String userFio) { + TimetableResponse response; + try { + response = getTimetableForUser(userFio); + } catch (RestClientException e) { + e.printStackTrace(); + throw new TimetableClientException(userFio); + } + int lessonNumber = getCurrentLessonNumber(); if (lessonNumber < 0) { return null; } - List lessons = response + List lessons = response .getResponse() .getWeeks() .get(getCurrentWeek()) diff --git a/src/main/java/ru/ulstu/utils/timetable/errors/TimetableClientException.java b/src/main/java/ru/ulstu/utils/timetable/errors/TimetableClientException.java new file mode 100644 index 0000000..4723bda --- /dev/null +++ b/src/main/java/ru/ulstu/utils/timetable/errors/TimetableClientException.java @@ -0,0 +1,7 @@ +package ru.ulstu.utils.timetable.errors; + +public class TimetableClientException extends RuntimeException { + public TimetableClientException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Day.java b/src/main/java/ru/ulstu/utils/timetable/model/Day.java index f548462..e4e37f9 100644 --- a/src/main/java/ru/ulstu/utils/timetable/model/Day.java +++ b/src/main/java/ru/ulstu/utils/timetable/model/Day.java @@ -1,12 +1,13 @@ package ru.ulstu.utils.timetable.model; +import java.util.ArrayList; import java.util.List; -public class Day { +public class Day { private Integer day; - private List> lessons = null; + private List> lessons = new ArrayList<>(); public Integer getDay() { return day; @@ -16,11 +17,11 @@ public class Day { this.day = day; } - public List> getLessons() { + public List> getLessons() { return lessons; } - public void setLessons(List> lessons) { + public void setLessons(List> lessons) { this.lessons = lessons; } } diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Lesson.java b/src/main/java/ru/ulstu/utils/timetable/model/Lesson.java index e651e1a..b1b1707 100644 --- a/src/main/java/ru/ulstu/utils/timetable/model/Lesson.java +++ b/src/main/java/ru/ulstu/utils/timetable/model/Lesson.java @@ -1,14 +1,11 @@ package ru.ulstu.utils.timetable.model; -public class Lesson { +public class Lesson { private String group; private String nameOfLesson; private String teacher; private String room; - public Lesson() { - } - public String getGroup() { return group; } diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Response.java b/src/main/java/ru/ulstu/utils/timetable/model/Response.java index b5b9e97..dfd3a84 100644 --- a/src/main/java/ru/ulstu/utils/timetable/model/Response.java +++ b/src/main/java/ru/ulstu/utils/timetable/model/Response.java @@ -1,10 +1,11 @@ package ru.ulstu.utils.timetable.model; +import java.util.ArrayList; import java.util.List; public class Response { - private List weeks = null; + private List weeks = new ArrayList<>(); public List getWeeks() { return weeks; diff --git a/src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java b/src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java index 118c979..925d9a8 100644 --- a/src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java +++ b/src/main/java/ru/ulstu/utils/timetable/model/TimetableResponse.java @@ -1,6 +1,6 @@ package ru.ulstu.utils.timetable.model; -public class TimetableResponse { +public class TimetableResponse { private Response response; private String error; diff --git a/src/main/java/ru/ulstu/utils/timetable/model/Week.java b/src/main/java/ru/ulstu/utils/timetable/model/Week.java index 19f3c50..8ea76ea 100644 --- a/src/main/java/ru/ulstu/utils/timetable/model/Week.java +++ b/src/main/java/ru/ulstu/utils/timetable/model/Week.java @@ -1,10 +1,12 @@ package ru.ulstu.utils.timetable.model; +import java.io.Serializable; +import java.util.ArrayList; import java.util.List; -public class Week { +public class Week implements Serializable { - private List days = null; + private List days = new ArrayList<>(); public List getDays() { return days; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 038ddcf..41c8ece 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -24,7 +24,7 @@ spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFact # JPA Settings spring.datasource.url=jdbc:postgresql://localhost:5432/ng-tracker spring.datasource.username=postgres -spring.datasource.password=postgres +spring.datasource.password=password spring.datasource.driverclassName=org.postgresql.Driver spring.jpa.hibernate.ddl-auto=validate # Liquibase Settings @@ -34,6 +34,6 @@ liquibase.change-log=classpath:db/changelog-master.xml # Application Settings ng-tracker.base-url=http://127.0.0.1:8080 ng-tracker.undead-user-login=admin -ng-tracker.dev-mode=true +ng-tracker.dev-mode=false ng-tracker.use-https=false ng-tracker.check-run=false \ No newline at end of file diff --git a/src/main/resources/templates/users/dashboard.html b/src/main/resources/templates/users/dashboard.html index 8236734..e5b7563 100644 --- a/src/main/resources/templates/users/dashboard.html +++ b/src/main/resources/templates/users/dashboard.html @@ -3,15 +3,18 @@ xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml"> + -

Пользователи

+
+

teste

+
@@ -19,6 +22,14 @@
+
\ No newline at end of file From f4479c2ab7d7d48806cac42361aa970b50cbd2ff Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Sat, 25 May 2019 14:11:57 +0400 Subject: [PATCH 3/3] #91 fixed app properties --- src/main/resources/application.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 41c8ece..038ddcf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -24,7 +24,7 @@ spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFact # JPA Settings spring.datasource.url=jdbc:postgresql://localhost:5432/ng-tracker spring.datasource.username=postgres -spring.datasource.password=password +spring.datasource.password=postgres spring.datasource.driverclassName=org.postgresql.Driver spring.jpa.hibernate.ddl-auto=validate # Liquibase Settings @@ -34,6 +34,6 @@ liquibase.change-log=classpath:db/changelog-master.xml # Application Settings ng-tracker.base-url=http://127.0.0.1:8080 ng-tracker.undead-user-login=admin -ng-tracker.dev-mode=false +ng-tracker.dev-mode=true ng-tracker.use-https=false ng-tracker.check-run=false \ No newline at end of file