From e680aa30c4798685c9c6c6f2c6ebf3c5fc846b66 Mon Sep 17 00:00:00 2001
From: "Artem.Arefev"
Date: Fri, 31 May 2019 00:33:21 +0400
Subject: [PATCH] #112 ping activity
---
.../ru/ulstu/conference/model/Conference.java | 11 ++-
.../{UserContainer.java => UserActivity.java} | 5 +-
src/main/java/ru/ulstu/grant/model/Grant.java | 6 +-
src/main/java/ru/ulstu/paper/model/Paper.java | 6 +-
.../ru/ulstu/paper/service/PaperService.java | 1 -
src/main/java/ru/ulstu/ping/model/Ping.java | 13 +++
.../java/ru/ulstu/ping/model/PingInfo.java | 35 ++++++++
.../ulstu/ping/repository/PingRepository.java | 13 +--
.../ru/ulstu/ping/service/PingScheduler.java | 59 +++++++++++++
.../ru/ulstu/ping/service/PingService.java | 12 ++-
.../java/ru/ulstu/project/model/Project.java | 11 ++-
.../strategy/api/EntityCreateStrategy.java | 8 +-
.../ulstu/user/controller/UserController.java | 4 +-
.../user/controller/UserMvcController.java | 19 ++--
.../ru/ulstu/user/model/DiagramElement.java | 86 -------------------
.../ru/ulstu/user/service/UserService.java | 39 +++++----
.../mail_templates/pingsInfoWeekEmail.html | 23 +++++
src/main/resources/public/js/core.js | 16 ++++
src/main/resources/public/js/users.js | 22 ++---
.../resources/templates/grants/grant.html | 18 +---
.../resources/templates/papers/paper.html | 17 +---
.../resources/templates/projects/project.html | 4 +-
.../resources/templates/users/activities.html | 51 -----------
src/main/resources/templates/users/pings.html | 53 ++++++++++++
.../resources/templates/users/profile.html | 7 --
25 files changed, 296 insertions(+), 243 deletions(-)
rename src/main/java/ru/ulstu/core/model/{UserContainer.java => UserActivity.java} (50%)
create mode 100644 src/main/java/ru/ulstu/ping/model/PingInfo.java
create mode 100644 src/main/java/ru/ulstu/ping/service/PingScheduler.java
delete mode 100644 src/main/java/ru/ulstu/user/model/DiagramElement.java
create mode 100644 src/main/resources/mail_templates/pingsInfoWeekEmail.html
delete mode 100644 src/main/resources/templates/users/activities.html
create mode 100644 src/main/resources/templates/users/pings.html
diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java
index 4df1a1f..0c3972d 100644
--- a/src/main/java/ru/ulstu/conference/model/Conference.java
+++ b/src/main/java/ru/ulstu/conference/model/Conference.java
@@ -5,8 +5,10 @@ import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;
import ru.ulstu.core.model.BaseEntity;
+import ru.ulstu.core.model.UserActivity;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.paper.model.Paper;
+import ru.ulstu.user.model.User;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@@ -25,10 +27,12 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
@Entity
@Table(name = "conference")
-public class Conference extends BaseEntity {
+public class Conference extends BaseEntity implements UserActivity {
@NotBlank
private String title;
@@ -147,4 +151,9 @@ public class Conference extends BaseEntity {
.filter(d -> d.getDate().after(new Date()))
.findFirst();
}
+
+ @Override
+ public Set getActivityUsers() {
+ return getUsers().stream().map(ConferenceUser::getUser).collect(Collectors.toSet());
+ }
}
diff --git a/src/main/java/ru/ulstu/core/model/UserContainer.java b/src/main/java/ru/ulstu/core/model/UserActivity.java
similarity index 50%
rename from src/main/java/ru/ulstu/core/model/UserContainer.java
rename to src/main/java/ru/ulstu/core/model/UserActivity.java
index 7f83cd3..8e4e270 100644
--- a/src/main/java/ru/ulstu/core/model/UserContainer.java
+++ b/src/main/java/ru/ulstu/core/model/UserActivity.java
@@ -4,6 +4,7 @@ import ru.ulstu.user.model.User;
import java.util.Set;
-public interface UserContainer {
- Set getUsers();
+public interface UserActivity {
+ String getTitle();
+ Set getActivityUsers();
}
diff --git a/src/main/java/ru/ulstu/grant/model/Grant.java b/src/main/java/ru/ulstu/grant/model/Grant.java
index 369568f..a653772 100644
--- a/src/main/java/ru/ulstu/grant/model/Grant.java
+++ b/src/main/java/ru/ulstu/grant/model/Grant.java
@@ -4,7 +4,7 @@ import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity;
-import ru.ulstu.core.model.UserContainer;
+import ru.ulstu.core.model.UserActivity;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData;
import ru.ulstu.paper.model.Paper;
@@ -35,7 +35,7 @@ import java.util.Set;
@Entity
@Table(name = "grants")
-public class Grant extends BaseEntity implements UserContainer {
+public class Grant extends BaseEntity implements UserActivity {
public enum GrantStatus {
APPLICATION("Заявка"),
ON_COMPETITION("Отправлен на конкурс"),
@@ -155,7 +155,7 @@ public class Grant extends BaseEntity implements UserContainer {
}
@Override
- public Set getUsers() {
+ public Set getActivityUsers() {
return getAuthors();
}
diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java
index 413c5db..5d951a5 100644
--- a/src/main/java/ru/ulstu/paper/model/Paper.java
+++ b/src/main/java/ru/ulstu/paper/model/Paper.java
@@ -5,7 +5,7 @@ import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.conference.model.Conference;
import ru.ulstu.core.model.BaseEntity;
-import ru.ulstu.core.model.UserContainer;
+import ru.ulstu.core.model.UserActivity;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData;
import ru.ulstu.grant.model.Grant;
@@ -34,7 +34,7 @@ import java.util.Optional;
import java.util.Set;
@Entity
-public class Paper extends BaseEntity implements UserContainer {
+public class Paper extends BaseEntity implements UserActivity {
public enum PaperStatus {
ATTENTION("Обратить внимание"),
ON_PREPARATION("На подготовке"),
@@ -249,7 +249,7 @@ public class Paper extends BaseEntity implements UserContainer {
}
@Override
- public Set getUsers() {
+ public Set getActivityUsers() {
return getAuthors();
}
diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java
index 912b85e..3be9aee 100644
--- a/src/main/java/ru/ulstu/paper/service/PaperService.java
+++ b/src/main/java/ru/ulstu/paper/service/PaperService.java
@@ -1,6 +1,5 @@
package ru.ulstu.paper.service;
-import com.sun.deploy.pings.Pings;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
diff --git a/src/main/java/ru/ulstu/ping/model/Ping.java b/src/main/java/ru/ulstu/ping/model/Ping.java
index d52bb07..82c9fe2 100644
--- a/src/main/java/ru/ulstu/ping/model/Ping.java
+++ b/src/main/java/ru/ulstu/ping/model/Ping.java
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.format.annotation.DateTimeFormat;
import ru.ulstu.conference.model.Conference;
import ru.ulstu.core.model.BaseEntity;
+import ru.ulstu.core.model.UserActivity;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.project.model.Project;
@@ -121,4 +122,16 @@ public class Ping extends BaseEntity {
this.paper = (Paper) object;
}
}
+
+ public UserActivity getActivity() {
+ if (conference != null) {
+ return conference;
+ } else if (project != null) {
+ return project;
+ } else if (paper != null) {
+ return paper;
+ } else {
+ return grant;
+ }
+ }
}
diff --git a/src/main/java/ru/ulstu/ping/model/PingInfo.java b/src/main/java/ru/ulstu/ping/model/PingInfo.java
new file mode 100644
index 0000000..e8b943b
--- /dev/null
+++ b/src/main/java/ru/ulstu/ping/model/PingInfo.java
@@ -0,0 +1,35 @@
+package ru.ulstu.ping.model;
+
+import ru.ulstu.user.model.User;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PingInfo {
+ private User user;
+ private List pings = new ArrayList<>();
+
+ public PingInfo(User user) {
+ this.user = user;
+ }
+
+ public User getUser() {
+ return user;
+ }
+
+ public void setUser(User user) {
+ this.user = user;
+ }
+
+ public List getPings() {
+ return pings;
+ }
+
+ public void setPings(List pings) {
+ this.pings = pings;
+ }
+
+ public void addPing(Ping ping) {
+ this.pings.add(ping);
+ }
+}
diff --git a/src/main/java/ru/ulstu/ping/repository/PingRepository.java b/src/main/java/ru/ulstu/ping/repository/PingRepository.java
index 0a97c31..116c4fc 100644
--- a/src/main/java/ru/ulstu/ping/repository/PingRepository.java
+++ b/src/main/java/ru/ulstu/ping/repository/PingRepository.java
@@ -6,6 +6,7 @@ import org.springframework.data.repository.query.Param;
import ru.ulstu.conference.model.Conference;
import ru.ulstu.ping.model.Ping;
+import java.util.Date;
import java.util.List;
public interface PingRepository extends JpaRepository {
@@ -13,9 +14,11 @@ public interface PingRepository extends JpaRepository {
@Query("SELECT count(*) FROM Ping p WHERE (DAY(p.date) = :day) AND (MONTH(p.date) = :month) AND (YEAR(p.date) = :year) AND (p.conference = :conference)")
long countByConferenceAndDate(@Param("conference") Conference conference, @Param("day") Integer day, @Param("month") Integer month, @Param("year") Integer year);
- @Query("SELECT p FROM Ping p WHERE (:user IS NULL OR p.user.id = :user) " +
- "AND (:activity != 'conferences' OR p.conference IS NOT NULL) AND (:activity != 'papers' OR p.paper IS NOT NULL) " +
- "AND (:activity != 'projects' OR p.project IS NOT NULL) AND (:activity != 'grants' OR p.grant IS NOT NULL)")
- List getPings(@Param("user") Integer user,
- @Param("activity") String activity);
+ @Query("SELECT p FROM Ping p WHERE (:activity != 'conferences' OR p.conference IS NOT NULL) " +
+ "AND (:activity != 'papers' OR p.paper IS NOT NULL) AND (:activity != 'projects' OR p.project IS NOT NULL) " +
+ "AND (:activity != 'grants' OR p.grant IS NOT NULL)")
+ List getPings(@Param("activity") String activity);
+
+ @Query("SELECT p FROM Ping p WHERE (:dateFrom < date)")
+ List findByDate(@Param("dateFrom") Date dateFrom);
}
diff --git a/src/main/java/ru/ulstu/ping/service/PingScheduler.java b/src/main/java/ru/ulstu/ping/service/PingScheduler.java
new file mode 100644
index 0000000..a60e061
--- /dev/null
+++ b/src/main/java/ru/ulstu/ping/service/PingScheduler.java
@@ -0,0 +1,59 @@
+package ru.ulstu.ping.service;
+
+import com.google.common.collect.ImmutableMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+import ru.ulstu.core.model.UserActivity;
+import ru.ulstu.ping.model.Ping;
+import ru.ulstu.ping.model.PingInfo;
+import ru.ulstu.ping.repository.PingRepository;
+import ru.ulstu.user.model.User;
+import ru.ulstu.user.service.MailService;
+
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+@Service
+public class PingScheduler {
+ private final Logger log = LoggerFactory.getLogger(PingScheduler.class);
+ private final PingRepository pingRepository;
+ private final MailService mailService;
+ private final static String PING_MAIL_SUBJECT = "Ping статистика";
+
+ public PingScheduler(PingRepository pingRepository, MailService mailService) {
+ this.pingRepository = pingRepository;
+ this.mailService = mailService;
+ }
+
+
+ @Scheduled(cron = "0 0 * * 1 ?")
+ public void sendPingsInfo() {
+ log.debug("Scheduler.sendPingsInfo started");
+
+ List pingInfos = new ArrayList<>();
+
+ for (Ping ping : pingRepository.findByDate(java.sql.Date.valueOf(LocalDate.now().minusWeeks(1)))) {
+ UserActivity pingActivity = ping.getActivity();
+ Set users = pingActivity.getActivityUsers();
+
+ for (User user : users) {
+ PingInfo userPing = pingInfos.stream().filter(u -> u.getUser() == user).findFirst().orElse(null);
+ if (userPing == null) {
+ userPing = new PingInfo(user);
+ pingInfos.add(userPing);
+ }
+ userPing.addPing(ping);
+ }
+ }
+
+ for (PingInfo pingInfo : pingInfos) {
+ mailService.sendEmailFromTemplate(ImmutableMap.of("pings", pingInfo.getPings()),
+ pingInfo.getUser(), "pingsInfoWeekEmail", PING_MAIL_SUBJECT);
+ }
+
+ }
+}
diff --git a/src/main/java/ru/ulstu/ping/service/PingService.java b/src/main/java/ru/ulstu/ping/service/PingService.java
index de61422..97cc353 100644
--- a/src/main/java/ru/ulstu/ping/service/PingService.java
+++ b/src/main/java/ru/ulstu/ping/service/PingService.java
@@ -16,15 +16,19 @@ import java.util.List;
public class PingService {
private final PingRepository pingRepository;
private final UserService userService;
+ private final PingScheduler pingScheduler;
public PingService(PingRepository pingRepository,
- UserService userService) {
+ UserService userService,
+ PingScheduler pingScheduler) {
this.pingRepository = pingRepository;
this.userService = userService;
+ this.pingScheduler = pingScheduler;
}
@Transactional
- public Ping addPing(T activity) throws IOException {
+ public Ping addPing(T activity) throws IOException {
+ pingScheduler.sendPingsInfo();
Ping newPing = new Ping(new Date(), userService.getCurrentUser());
newPing.setActivity(activity);
return pingRepository.save(newPing);
@@ -35,7 +39,7 @@ public class PingService {
calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR)));
}
- public List getPings(Integer userId, String activity) {
- return pingRepository.getPings(userId, activity);
+ public List getPings(String activity) {
+ return pingRepository.getPings(activity);
}
}
diff --git a/src/main/java/ru/ulstu/project/model/Project.java b/src/main/java/ru/ulstu/project/model/Project.java
index c971c4b..08dfec2 100644
--- a/src/main/java/ru/ulstu/project/model/Project.java
+++ b/src/main/java/ru/ulstu/project/model/Project.java
@@ -2,9 +2,11 @@ package ru.ulstu.project.model;
import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity;
+import ru.ulstu.core.model.UserActivity;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData;
import ru.ulstu.grant.model.Grant;
+import ru.ulstu.user.model.User;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
@@ -15,10 +17,17 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
@Entity
-public class Project extends BaseEntity {
+public class Project extends BaseEntity implements UserActivity {
+ @Override
+ public Set getActivityUsers() {
+ return new HashSet<>();
+ }
+
public enum ProjectStatus {
APPLICATION("Заявка"),
ON_COMPETITION("Отправлен на конкурс"),
diff --git a/src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java b/src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java
index 8449e10..8a14399 100644
--- a/src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java
+++ b/src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java
@@ -1,21 +1,21 @@
package ru.ulstu.strategy.api;
-import ru.ulstu.core.model.UserContainer;
+import ru.ulstu.core.model.UserActivity;
import ru.ulstu.user.model.User;
import java.util.List;
import java.util.stream.Collectors;
-public abstract class EntityCreateStrategy {
+public abstract class EntityCreateStrategy {
protected abstract List getActiveEntities();
protected abstract void createEntity(User user);
- protected void createDefaultEntityIfNeed(List allUsers, List extends UserContainer> entities) {
+ protected void createDefaultEntityIfNeed(List allUsers, List extends UserActivity> entities) {
allUsers.forEach(user -> {
if (entities
.stream()
- .filter(entity -> entity.getUsers().contains(user))
+ .filter(entity -> entity.getActivityUsers().contains(user))
.collect(Collectors.toSet()).isEmpty()) {
createEntity(user);
}
diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java
index 3487370..8e85d05 100644
--- a/src/main/java/ru/ulstu/user/controller/UserController.java
+++ b/src/main/java/ru/ulstu/user/controller/UserController.java
@@ -20,7 +20,6 @@ import ru.ulstu.odin.controller.OdinController;
import ru.ulstu.odin.model.OdinMetadata;
import ru.ulstu.odin.model.OdinVoid;
import ru.ulstu.odin.service.OdinService;
-import ru.ulstu.user.model.DiagramElement;
import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.model.UserListDto;
@@ -35,7 +34,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
-import java.util.List;
import java.util.Map;
import static ru.ulstu.user.controller.UserController.URL;
@@ -175,7 +173,7 @@ public class UserController extends OdinController {
}
@GetMapping("/activities/pings")
- public Response> getActivitesStats(@RequestParam(value = "userId", required = false) Integer userId,
+ public Response
-
-
-
-
-
-
-
-
Фильтр:
-
-
-
-
-
-
-
-
-
diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html
index ea748ef..5c26d0f 100644
--- a/src/main/resources/templates/papers/paper.html
+++ b/src/main/resources/templates/papers/paper.html
@@ -326,7 +326,7 @@
@@ -562,21 +562,6 @@
$(this).autocomplete("search");
});
- function sendPing() {
- id = document.getElementById("paperId").value
-
- $.ajax({
- url:"/api/1.0/papers/ping?paperId=" + id,
- contentType: "application/json; charset=utf-8",
- method: "POST",
- success: function() {
- showFeedbackMessage("Пароль был обновлен", MessageTypesEnum.SUCCESS)
- },
- error: function(errorData) {
- showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING)
- }
- })
- }
/*]]>*/
diff --git a/src/main/resources/templates/projects/project.html b/src/main/resources/templates/projects/project.html
index c291ff8..92c6410 100644
--- a/src/main/resources/templates/projects/project.html
+++ b/src/main/resources/templates/projects/project.html
@@ -106,7 +106,7 @@
@@ -157,7 +157,7 @@
contentType: "application/json; charset=utf-8",
method: "POST",
success: function() {
- showFeedbackMessage("Пароль был обновлен", MessageTypesEnum.SUCCESS)
+ showFeedbackMessage("Ping был отправлен", MessageTypesEnum.SUCCESS)
},
error: function(errorData) {
showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING)
diff --git a/src/main/resources/templates/users/activities.html b/src/main/resources/templates/users/activities.html
deleted file mode 100644
index 1b93828..0000000
--- a/src/main/resources/templates/users/activities.html
+++ /dev/null
@@ -1,51 +0,0 @@
-
-
-