Merge branch 'dev' of gitlab.com:romanov73/ng-tracker into 98-project-tasks

This commit is contained in:
a.vasin 2019-05-28 00:41:15 +04:00
commit 8d1e410e83
17 changed files with 724 additions and 357 deletions

View File

@ -29,4 +29,8 @@ public interface ConferenceRepository extends JpaRepository<Conference, Integer>
@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);
}

View File

@ -313,4 +313,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);
}
}

View File

@ -43,6 +43,4 @@ public class ConferenceUserService {
newUser = conferenceUserRepository.save(newUser);
return newUser;
}
}

View File

@ -48,4 +48,9 @@ public class UserMvcController extends OdinController<UserListDto, UserDto> {
User user = userSessionService.getUserBySessionId(sessionId);
modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto));
}
@GetMapping("/dashboard")
public void getUsersDashboard(ModelMap modelMap) {
modelMap.addAllAttributes(userService.getUsersInfo());
}
}

View File

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

View File

@ -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<UserSession, Intege
UserSession findOneBySessionId(String sessionId);
List<UserSession> findAllByLogoutTimeIsNullAndLoginTimeBefore(Date date);
List<UserSession> findAllByUserAndLogoutTimeIsNullAndLoginTimeBefore(User user, Date date);
}

View File

@ -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.errors.TimetableClientException;
import ru.ulstu.utils.timetable.model.Lesson;
import javax.mail.MessagingException;
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) {
@ -352,4 +368,25 @@ public class UserService implements UserDetailsService {
public User findOneByLoginIgnoreCase(String login) {
return userRepository.findOneByLoginIgnoreCase(login);
}
public Map<String, Object> getUsersInfo() {
List<UserInfoNow> usersInfoNow = new ArrayList<>();
String err = "";
for (User user : userRepository.findAll()) {
Lesson lesson = null;
try {
lesson = timetableService.getCurrentLesson(user.getUserAbbreviate());
} catch (TimetableClientException e) {
err = "Не удалось загрузить расписание";
}
usersInfoNow.add(new UserInfoNow(
lesson,
conferenceService.getActiveConferenceByUser(user),
user,
userSessionService.isOnline(user))
);
}
return ImmutableMap.of("users", usersInfoNow, "error", err);
}
}

View File

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

View File

@ -0,0 +1,91 @@
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.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/api/1.0/timetable?filter=%s";
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 RestClientException {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject(String.format(TIMETABLE_URL, userFIO), TimetableResponse.class);
}
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<Lesson> 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);
}
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.utils.timetable.errors;
public class TimetableClientException extends RuntimeException {
public TimetableClientException(String message) {
super(message);
}
}

View File

@ -0,0 +1,28 @@
package ru.ulstu.utils.timetable.model;
import java.util.ArrayList;
import java.util.List;
public class Day {
private Integer day;
private List<List<Lesson>> lessons = new ArrayList<>();
public Integer getDay() {
return day;
}
public void setDay(Integer day) {
this.day = day;
}
public List<List<Lesson>> getLessons() {
return lessons;
}
public void setLessons(List<List<Lesson>> lessons) {
this.lessons = lessons;
}
}

View File

@ -0,0 +1,24 @@
package ru.ulstu.utils.timetable.model;
public class Lesson {
private String group;
private String nameOfLesson;
private String teacher;
private String room;
public String getGroup() {
return group;
}
public String getNameOfLesson() {
return nameOfLesson;
}
public String getTeacher() {
return teacher;
}
public String getRoom() {
return room;
}
}

View File

@ -0,0 +1,17 @@
package ru.ulstu.utils.timetable.model;
import java.util.ArrayList;
import java.util.List;
public class Response {
private List<Week> weeks = new ArrayList<>();
public List<Week> getWeeks() {
return weeks;
}
public void setWeeks(List<Week> weeks) {
this.weeks = weeks;
}
}

View File

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

View File

@ -0,0 +1,18 @@
package ru.ulstu.utils.timetable.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Week implements Serializable {
private List<Day> days = new ArrayList<>();
public List<Day> getDays() {
return days;
}
public void setDays(List<Day> days) {
this.days = days;
}
}

View File

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<script src="/js/core.js"></script>
</head>
<body>
<div class="container" layout:fragment="content">
<section id="services">
<div class="container">
<div class="col-lg-12 text-center">
<h2 class="section-heading text-uppercase">Пользователи</h2>
</div>
<div class="text-center">
<h2 th:text="${error}">teste</h2>
</div>
<div class="row justify-content-center" id="dashboard">
<th:block th:each="user : ${users}">
<div th:replace="users/fragments/userDashboardFragment :: userDashboard(user=${user})"/>
</th:block>
</div>
</div>
</section>
<script th:inline="javascript">
/*<![CDATA[*/
$(document).ready(function () {
var error = /*[[${error}]]*/
showFeedbackMessage(error, MessageTypesEnum.WARNING)
});
/*]]>*/
</script>
</div>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headerfiles">
<meta charset="UTF-8"/>
</head>
<body>
<div th:fragment="userDashboard (user)" class="col-12 col-sm-12 col-md-12 col-lg-4 col-xl-3 dashboard-card">
<div class="row">
<div class="col col-10">
<b><p th:text="${user.user.lastName} + ' ' + ${user.user.firstName} + ' ' + ${user.user.patronymic}"></p></b>
<i><p th:if="${user.conference != null}" th:text="'Сейчас на конференции ' + ${user.conference.title}"></p></i>
<i><p th:if="${user.lesson != null}" th:text="'Сейчас на паре ' + ${user.lesson.nameOfLesson} + ' в аудитории ' + ${user.lesson.room}"></p></i>
<p th:if="${user.isOnline()}">Онлайн</p>
</div>
</div>
</div>
</body>
</html>