#91 using RestTemplate & show error on timetable loading exception
This commit is contained in:
parent
505a054cc8
commit
193b2b3a32
@ -51,6 +51,6 @@ public class UserMvcController extends OdinController<UserListDto, UserDto> {
|
|||||||
|
|
||||||
@GetMapping("/dashboard")
|
@GetMapping("/dashboard")
|
||||||
public void getUsersDashboard(ModelMap modelMap) {
|
public void getUsersDashboard(ModelMap modelMap) {
|
||||||
modelMap.addAttribute("users", userService.getUsersInfo());
|
modelMap.addAllAttributes(userService.getUsersInfo());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,10 +42,10 @@ import ru.ulstu.user.repository.UserRepository;
|
|||||||
import ru.ulstu.user.repository.UserRoleRepository;
|
import ru.ulstu.user.repository.UserRoleRepository;
|
||||||
import ru.ulstu.user.util.UserUtils;
|
import ru.ulstu.user.util.UserUtils;
|
||||||
import ru.ulstu.utils.timetable.TimetableService;
|
import ru.ulstu.utils.timetable.TimetableService;
|
||||||
|
import ru.ulstu.utils.timetable.errors.TimetableClientException;
|
||||||
import ru.ulstu.utils.timetable.model.Lesson;
|
import ru.ulstu.utils.timetable.model.Lesson;
|
||||||
|
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import java.io.IOException;
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -358,14 +358,16 @@ public class UserService implements UserDetailsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<UserInfoNow> getUsersInfo() {
|
public Map<String, Object> getUsersInfo() {
|
||||||
List<UserInfoNow> usersInfoNow = new ArrayList<>();
|
List<UserInfoNow> usersInfoNow = new ArrayList<>();
|
||||||
|
String err = "";
|
||||||
|
|
||||||
for (User user : userRepository.findAll()) {
|
for (User user : userRepository.findAll()) {
|
||||||
Lesson lesson = null;
|
Lesson lesson = null;
|
||||||
try {
|
try {
|
||||||
lesson = timetableService.getCurrentLesson(user.getUserAbbreviate());
|
lesson = timetableService.getCurrentLesson(user.getUserAbbreviate());
|
||||||
} catch (IOException e) {
|
} catch (TimetableClientException e) {
|
||||||
e.printStackTrace();
|
err = "Не удалось загрузить расписание";
|
||||||
}
|
}
|
||||||
usersInfoNow.add(new UserInfoNow(
|
usersInfoNow.add(new UserInfoNow(
|
||||||
lesson,
|
lesson,
|
||||||
@ -374,6 +376,6 @@ public class UserService implements UserDetailsService {
|
|||||||
userSessionService.isOnline(user))
|
userSessionService.isOnline(user))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return usersInfoNow;
|
return ImmutableMap.of("users", usersInfoNow, "error", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,12 @@
|
|||||||
package ru.ulstu.utils.timetable;
|
package ru.ulstu.utils.timetable;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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.Lesson;
|
||||||
import ru.ulstu.utils.timetable.model.TimetableResponse;
|
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.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@ -17,7 +14,7 @@ import java.util.Date;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TimetableService {
|
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 SimpleDateFormat lessonTimeFormat = new SimpleDateFormat("hh:mm");
|
||||||
|
|
||||||
private long[] lessonsStarts = new long[] {
|
private long[] lessonsStarts = new long[] {
|
||||||
@ -30,7 +27,8 @@ public class TimetableService {
|
|||||||
lessonTimeFormat.parse("18:10:00").getTime(),
|
lessonTimeFormat.parse("18:10:00").getTime(),
|
||||||
};
|
};
|
||||||
|
|
||||||
public TimetableService() throws ParseException { }
|
public TimetableService() throws ParseException {
|
||||||
|
}
|
||||||
|
|
||||||
private int getCurrentDay() {
|
private int getCurrentDay() {
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
@ -42,6 +40,7 @@ public class TimetableService {
|
|||||||
long lessonDuration = 90 * 60000;
|
long lessonDuration = 90 * 60000;
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
long timeNow = now.getTime() % (24 * 60 * 60 * 1000L);
|
long timeNow = now.getTime() % (24 * 60 * 60 * 1000L);
|
||||||
|
|
||||||
for (int i = 0; i < lessonsStarts.length; i++) {
|
for (int i = 0; i < lessonsStarts.length; i++) {
|
||||||
if (timeNow > lessonsStarts[i] && timeNow < lessonsStarts[i] + lessonDuration) {
|
if (timeNow > lessonsStarts[i] && timeNow < lessonsStarts[i] + lessonDuration) {
|
||||||
return i;
|
return i;
|
||||||
@ -56,31 +55,26 @@ public class TimetableService {
|
|||||||
return (cal.get(Calendar.WEEK_OF_YEAR) + 1) % 2;
|
return (cal.get(Calendar.WEEK_OF_YEAR) + 1) % 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TimetableResponse getTimetableForUser(String userFIO) throws IOException {
|
private TimetableResponse getTimetableForUser(String userFIO) throws RestClientException {
|
||||||
URL url = new URL(TIMETABLE_URL + "/api/1.0/timetable?filter=" + URLEncoder.encode(userFIO, "UTF-8"));
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
return restTemplate.getForObject(String.format(TIMETABLE_URL, userFIO), TimetableResponse.class);
|
||||||
con.setRequestMethod("GET");
|
}
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(
|
public Lesson getCurrentLesson(String userFio) {
|
||||||
new InputStreamReader(con.getInputStream()));
|
TimetableResponse response;
|
||||||
String inputLine;
|
try {
|
||||||
StringBuilder content = new StringBuilder();
|
response = getTimetableForUser(userFio);
|
||||||
while ((inputLine = in.readLine()) != null) {
|
} catch (RestClientException e) {
|
||||||
content.append(inputLine);
|
e.printStackTrace();
|
||||||
}
|
throw new TimetableClientException(userFio);
|
||||||
in.close();
|
|
||||||
|
|
||||||
return new ObjectMapper().readValue(content.toString(), TimetableResponse.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Lesson getCurrentLesson(String userFio) throws IOException {
|
|
||||||
TimetableResponse response = getTimetableForUser(userFio);
|
|
||||||
int lessonNumber = getCurrentLessonNumber();
|
int lessonNumber = getCurrentLessonNumber();
|
||||||
if (lessonNumber < 0) {
|
if (lessonNumber < 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Object> lessons = response
|
List<Lesson> lessons = response
|
||||||
.getResponse()
|
.getResponse()
|
||||||
.getWeeks()
|
.getWeeks()
|
||||||
.get(getCurrentWeek())
|
.get(getCurrentWeek())
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulstu.utils.timetable.errors;
|
||||||
|
|
||||||
|
public class TimetableClientException extends RuntimeException {
|
||||||
|
public TimetableClientException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,13 @@
|
|||||||
package ru.ulstu.utils.timetable.model;
|
package ru.ulstu.utils.timetable.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class Day {
|
public class Day {
|
||||||
|
|
||||||
private Integer day;
|
private Integer day;
|
||||||
private List<List<Object>> lessons = null;
|
private List<List<Lesson>> lessons = new ArrayList<>();
|
||||||
|
|
||||||
public Integer getDay() {
|
public Integer getDay() {
|
||||||
return day;
|
return day;
|
||||||
@ -16,11 +17,11 @@ public class Day {
|
|||||||
this.day = day;
|
this.day = day;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<List<Object>> getLessons() {
|
public List<List<Lesson>> getLessons() {
|
||||||
return lessons;
|
return lessons;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLessons(List<List<Object>> lessons) {
|
public void setLessons(List<List<Lesson>> lessons) {
|
||||||
this.lessons = lessons;
|
this.lessons = lessons;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,6 @@ public class Lesson {
|
|||||||
private String teacher;
|
private String teacher;
|
||||||
private String room;
|
private String room;
|
||||||
|
|
||||||
public Lesson() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getGroup() {
|
public String getGroup() {
|
||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package ru.ulstu.utils.timetable.model;
|
package ru.ulstu.utils.timetable.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Response {
|
public class Response {
|
||||||
|
|
||||||
private List<Week> weeks = null;
|
private List<Week> weeks = new ArrayList<>();
|
||||||
|
|
||||||
public List<Week> getWeeks() {
|
public List<Week> getWeeks() {
|
||||||
return weeks;
|
return weeks;
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package ru.ulstu.utils.timetable.model;
|
package ru.ulstu.utils.timetable.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Week {
|
public class Week implements Serializable {
|
||||||
|
|
||||||
private List<Day> days = null;
|
private List<Day> days = new ArrayList<>();
|
||||||
|
|
||||||
public List<Day> getDays() {
|
public List<Day> getDays() {
|
||||||
return days;
|
return days;
|
||||||
|
@ -24,7 +24,7 @@ spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFact
|
|||||||
# JPA Settings
|
# JPA Settings
|
||||||
spring.datasource.url=jdbc:postgresql://localhost:5432/ng-tracker
|
spring.datasource.url=jdbc:postgresql://localhost:5432/ng-tracker
|
||||||
spring.datasource.username=postgres
|
spring.datasource.username=postgres
|
||||||
spring.datasource.password=postgres
|
spring.datasource.password=password
|
||||||
spring.datasource.driverclassName=org.postgresql.Driver
|
spring.datasource.driverclassName=org.postgresql.Driver
|
||||||
spring.jpa.hibernate.ddl-auto=validate
|
spring.jpa.hibernate.ddl-auto=validate
|
||||||
# Liquibase Settings
|
# Liquibase Settings
|
||||||
@ -34,6 +34,6 @@ liquibase.change-log=classpath:db/changelog-master.xml
|
|||||||
# Application Settings
|
# Application Settings
|
||||||
ng-tracker.base-url=http://127.0.0.1:8080
|
ng-tracker.base-url=http://127.0.0.1:8080
|
||||||
ng-tracker.undead-user-login=admin
|
ng-tracker.undead-user-login=admin
|
||||||
ng-tracker.dev-mode=true
|
ng-tracker.dev-mode=false
|
||||||
ng-tracker.use-https=false
|
ng-tracker.use-https=false
|
||||||
ng-tracker.check-run=false
|
ng-tracker.check-run=false
|
@ -3,15 +3,18 @@
|
|||||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml">
|
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
<head>
|
<head>
|
||||||
|
<script src="/js/core.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div class="container" layout:fragment="content">
|
<div class="container" layout:fragment="content">
|
||||||
<section id="services">
|
<section id="services">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="col-lg-12 text-center">
|
<div class="col-lg-12 text-center">
|
||||||
<h2 class="section-heading text-uppercase">Пользователи</h2>
|
<h2 class="section-heading text-uppercase">Пользователи</h2>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="text-center">
|
||||||
|
<h2 th:text="${error}">teste</h2>
|
||||||
|
</div>
|
||||||
<div class="row justify-content-center" id="dashboard">
|
<div class="row justify-content-center" id="dashboard">
|
||||||
<th:block th:each="user : ${users}">
|
<th:block th:each="user : ${users}">
|
||||||
<div th:replace="users/fragments/userDashboardFragment :: userDashboard(user=${user})"/>
|
<div th:replace="users/fragments/userDashboardFragment :: userDashboard(user=${user})"/>
|
||||||
@ -19,6 +22,14 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
<script th:inline="javascript">
|
||||||
|
/*<![CDATA[*/
|
||||||
|
$(document).ready(function () {
|
||||||
|
var error = /*[[${error}]]*/
|
||||||
|
showFeedbackMessage(error, MessageTypesEnum.WARNING)
|
||||||
|
});
|
||||||
|
/*]]>*/
|
||||||
|
</script>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user