From ed7707c4d8cf9511250e5ce6fc1f48106c9f7d39 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Wed, 5 Jun 2019 10:53:30 +0400 Subject: [PATCH] #93 users activity by date --- .../ru/ulstu/paper/service/PaperService.java | 5 ++ .../project/controller/ProjectController.java | 2 + .../ulstu/project/service/ProjectService.java | 6 ++ .../ulstu/user/controller/UserController.java | 10 +++ .../user/controller/UserMvcController.java | 11 +++ .../java/ru/ulstu/user/model/Activity.java | 80 +++++++++++++++++++ .../user/repository/ActivityRepository.java | 19 +++++ .../ru/ulstu/user/service/UserService.java | 41 +++++++++- src/main/resources/application.properties | 6 +- .../db/changelog-20190603_000002-schema.xml | 17 ++++ src/main/resources/db/changelog-master.xml | 1 + .../resources/templates/users/activities.html | 47 +++++++++++ 12 files changed, 240 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ru/ulstu/user/model/Activity.java create mode 100644 src/main/java/ru/ulstu/user/repository/ActivityRepository.java create mode 100644 src/main/resources/db/changelog-20190603_000002-schema.xml create mode 100644 src/main/resources/templates/users/activities.html diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 2393af4..013ce72 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -21,6 +21,7 @@ import ru.ulstu.user.service.UserService; import java.io.IOException; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashSet; @@ -197,6 +198,10 @@ public class PaperService { if (paper.getStatus() != oldStatus) { paperNotificationService.statusChangeNotification(paper, oldStatus); + + if (paper.getStatus() == Paper.PaperStatus.ON_PREPARATION) { + userService.createActivityTookToWork(new ArrayList<>(paper.getUsers())); + } } return paper.getId(); diff --git a/src/main/java/ru/ulstu/project/controller/ProjectController.java b/src/main/java/ru/ulstu/project/controller/ProjectController.java index 88f0c7a..a344f75 100644 --- a/src/main/java/ru/ulstu/project/controller/ProjectController.java +++ b/src/main/java/ru/ulstu/project/controller/ProjectController.java @@ -13,6 +13,8 @@ import ru.ulstu.deadline.model.Deadline; import ru.ulstu.project.model.Project; import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.service.ProjectService; +import ru.ulstu.user.error.UserNotActivatedException; +import ru.ulstu.user.error.UserNotFoundException; import ru.ulstu.user.model.User; import springfox.documentation.annotations.ApiIgnore; diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index fe8813c..a8444a6 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -64,6 +64,9 @@ public class ProjectService { public Project create(ProjectDto projectDto) throws IOException { Project newProject = copyFromDto(new Project(), projectDto); newProject = projectRepository.save(newProject); + if (projectDto.getStatus() == Project.ProjectStatus.IN_WORK) { + userService.createActivityTookToWork(newProject.getExecutors()); + } return newProject; } @@ -74,6 +77,9 @@ public class ProjectService { fileService.deleteFile(project.getApplication()); } projectRepository.save(copyFromDto(project, projectDto)); + if (projectDto.getStatus() == Project.ProjectStatus.IN_WORK) { + userService.createActivityTookToWork(project.getExecutors()); + } return project; } diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index c40ed8f..6cabd99 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -2,6 +2,7 @@ package ru.ulstu.user.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.security.access.annotation.Secured; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.DeleteMapping; @@ -34,6 +35,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.validation.Valid; +import java.util.Date; +import java.util.List; import java.util.Map; import static ru.ulstu.user.controller.UserController.URL; @@ -171,4 +174,11 @@ public class UserController extends OdinController { public void inviteUser(@RequestParam("email") String email) { userService.inviteUser(email); } + + @GetMapping("/activities") + public Response> getActivitiesList(@RequestParam("userId") Integer userId, + @RequestParam("dateFrom") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateFrom, + @RequestParam("dateTo") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateTo) { + return new Response<>(userService.getUsersActivity(userId, dateFrom, dateTo)); + } } diff --git a/src/main/java/ru/ulstu/user/controller/UserMvcController.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java index 044bde8..de0592e 100644 --- a/src/main/java/ru/ulstu/user/controller/UserMvcController.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import ru.ulstu.configuration.Constants; @@ -17,6 +18,7 @@ import ru.ulstu.user.service.UserSessionService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import java.util.List; @Controller @RequestMapping(value = "/users") @@ -53,4 +55,13 @@ public class UserMvcController extends OdinController { public void getUsersDashboard(ModelMap modelMap) { modelMap.addAllAttributes(userService.getUsersInfo()); } + + @ModelAttribute("allUsers") + public List getAllUsers() { + return userService.findAll(); + } + + @GetMapping("/activities") + public void getActivities() { + } } diff --git a/src/main/java/ru/ulstu/user/model/Activity.java b/src/main/java/ru/ulstu/user/model/Activity.java new file mode 100644 index 0000000..74849dc --- /dev/null +++ b/src/main/java/ru/ulstu/user/model/Activity.java @@ -0,0 +1,80 @@ +package ru.ulstu.user.model; + +import org.springframework.format.annotation.DateTimeFormat; +import ru.ulstu.core.model.BaseEntity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import java.util.Date; + +@Entity +@Table(name = "activity") +public class Activity extends BaseEntity { + + public enum ActivityState { + TOOK_IN_WORK("Взято в работу"), + COMPLETED("Выполнено"); + + private String statusName; + + ActivityState(String statusName) { + this.statusName = statusName; + } + + public String getStatusName() { + return this.statusName; + } + } + + @ManyToOne() + @JoinColumn(name = "user_id") + private User user; + + @Temporal(value = TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date date; + + @Enumerated(value = EnumType.STRING) + @Column(name = "state") + private ActivityState activityState; + + public Activity(User user, ActivityState activityState) { + this.user = user; + this.date = new Date(); + this.activityState = activityState; + } + + public Activity() { + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public ActivityState getActivityState() { + return activityState; + } + + public void setActivityState(ActivityState activityState) { + this.activityState = activityState; + } +} diff --git a/src/main/java/ru/ulstu/user/repository/ActivityRepository.java b/src/main/java/ru/ulstu/user/repository/ActivityRepository.java new file mode 100644 index 0000000..89b6093 --- /dev/null +++ b/src/main/java/ru/ulstu/user/repository/ActivityRepository.java @@ -0,0 +1,19 @@ +package ru.ulstu.user.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +//import ru.ulstu.user.model.Activity; +// +//import java.util.Date; +//import java.util.List; +// +//public interface ActivityRepository extends JpaRepository { +// +// @Query("SELECT a FROM Activity a WHERE (:userId IS NULL OR a.user.id = :userId) " + +// "AND (CAST(:dateFrom AS date) IS NULL OR :dateFrom < a.date) " + +// "AND (CAST(:dateTo AS date) IS NULL OR :dateTo > a.date)") +// List getByUserAndDateInterval(@Param("userId") Integer userId, +// @Param("dateFrom") Date dateFrom, +// @Param("dateTo") Date dateTo); +//} diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 217215b..3056c18 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -30,6 +30,7 @@ import ru.ulstu.user.error.UserNotFoundException; import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException; import ru.ulstu.user.error.UserResetKeyError; import ru.ulstu.user.error.UserSendingMailException; +import ru.ulstu.user.model.Activity; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserInfoNow; @@ -38,6 +39,7 @@ import ru.ulstu.user.model.UserResetPasswordDto; import ru.ulstu.user.model.UserRole; import ru.ulstu.user.model.UserRoleConstants; import ru.ulstu.user.model.UserRoleDto; +import ru.ulstu.user.repository.ActivityRepository; import ru.ulstu.user.repository.UserRepository; import ru.ulstu.user.repository.UserRoleRepository; import ru.ulstu.user.util.UserUtils; @@ -48,9 +50,11 @@ import ru.ulstu.utils.timetable.model.Lesson; import javax.mail.MessagingException; import java.text.ParseException; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -73,6 +77,7 @@ public class UserService implements UserDetailsService { private final TimetableService timetableService; private final ConferenceService conferenceService; private final UserSessionService userSessionService; + private final ActivityRepository activityRepository; public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, @@ -81,7 +86,8 @@ public class UserService implements UserDetailsService { MailService mailService, ApplicationProperties applicationProperties, @Lazy ConferenceService conferenceRepository, - @Lazy UserSessionService userSessionService) throws ParseException { + @Lazy UserSessionService userSessionService, + ActivityRepository activityRepository) throws ParseException { this.userRepository = userRepository; this.passwordEncoder = passwordEncoder; this.userRoleRepository = userRoleRepository; @@ -91,6 +97,7 @@ public class UserService implements UserDetailsService { this.conferenceService = conferenceRepository; this.timetableService = new TimetableService(); this.userSessionService = userSessionService; + this.activityRepository = activityRepository; } private User getUserByEmail(String email) { @@ -252,7 +259,7 @@ public class UserService implements UserDetailsService { mailService.sendChangePasswordMail(user); } - public boolean requestUserPasswordReset(String email) { + public boolean requestUserPasswordReset(String email) { User user = userRepository.findOneByEmailIgnoreCase(email); if (user == null) { throw new UserNotFoundException(email); @@ -389,4 +396,34 @@ public class UserService implements UserDetailsService { } return ImmutableMap.of("users", usersInfoNow, "error", err); } + + public void createActivityTookToWork(List users) { + List activities = new ArrayList<>(); + for (User user : users) { + activities.add(new Activity(user, Activity.ActivityState.TOOK_IN_WORK)); + } + activityRepository.save(activities); + } + + public void createActivityCompleted(List users) { + List activities = new ArrayList<>(); + for (User user : users) { + activities.add(new Activity(user, Activity.ActivityState.COMPLETED)); + } + activityRepository.save(activities); + } + + public Map getUsersActivity(Integer userId, Date dateFrom, Date dateTo) { + List activities = activityRepository.getByUserAndDateInterval(userId, dateFrom, dateTo); + Map dateActivities = new HashMap<>(); + + for (Activity activity : activities) { + if (!dateActivities.containsKey(activity.getDate())) { + dateActivities.put(activity.getDate(), 1); + } else { + dateActivities.put(activity.getDate(), dateActivities.get(activity.getDate()) + 1); + } + } + return dateActivities; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f4be778..7bda533 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,7 +16,7 @@ logging.level.ru.ulstu=DEBUG # Mail Settings spring.mail.host=smtp.yandex.ru spring.mail.port=465 -spring.mail.username=nio17.ulstu@yandex.ru +spring.mail.username=nio171.ulstu@yandex.ru spring.mail.password=nio17.ulstu.ru spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.ssl.enable=true @@ -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,7 +34,7 @@ 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.debug_email= ng-tracker.use-https=false ng-tracker.check-run=false \ No newline at end of file diff --git a/src/main/resources/db/changelog-20190603_000002-schema.xml b/src/main/resources/db/changelog-20190603_000002-schema.xml new file mode 100644 index 0000000..8ff0184 --- /dev/null +++ b/src/main/resources/db/changelog-20190603_000002-schema.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index c8a6e63..ae5c400 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -49,4 +49,5 @@ + \ No newline at end of file diff --git a/src/main/resources/templates/users/activities.html b/src/main/resources/templates/users/activities.html new file mode 100644 index 0000000..9565a7e --- /dev/null +++ b/src/main/resources/templates/users/activities.html @@ -0,0 +1,47 @@ + + + + + + + + + + + +
+
+
+
+
+

Активность пользователей

+
+
+
+
+
Фильтр:
+ +
+ + +
+
+
+
+ +
+
+ +