From 49e407f455dedff90d2250761e6852506dfaf447 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Wed, 22 May 2019 03:34:11 +0400 Subject: [PATCH] #90 diagrams --- .../ulstu/user/controller/UserController.java | 27 +++- .../ru/ulstu/user/model/UserWorkload.java | 68 +++++++++ .../ru/ulstu/user/model/UserWorkloadDto.java | 48 ++++++ .../repository/UserWorkloadRepository.java | 17 +++ .../user/service/UserWorkloadService.java | 48 ++++++ .../db/changelog-20190521_000000-schema.xml | 22 +++ src/main/resources/db/changelog-master.xml | 1 + src/main/resources/public/js/users.js | 54 ++++++- src/main/resources/templates/default.html | 1 + .../resources/templates/users/profile.html | 144 ++++++++++++------ 10 files changed, 383 insertions(+), 47 deletions(-) create mode 100644 src/main/java/ru/ulstu/user/model/UserWorkload.java create mode 100644 src/main/java/ru/ulstu/user/model/UserWorkloadDto.java create mode 100644 src/main/java/ru/ulstu/user/repository/UserWorkloadRepository.java create mode 100644 src/main/java/ru/ulstu/user/service/UserWorkloadService.java create mode 100644 src/main/resources/db/changelog-20190521_000000-schema.xml diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index 59e023b..8bcbc45 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -2,8 +2,8 @@ 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; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -27,13 +27,17 @@ import ru.ulstu.user.model.UserResetPasswordDto; import ru.ulstu.user.model.UserRoleConstants; import ru.ulstu.user.model.UserRoleDto; import ru.ulstu.user.model.UserSessionListDto; +import ru.ulstu.user.model.UserWorkloadDto; import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserSessionService; +import ru.ulstu.user.service.UserWorkloadService; 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; @@ -57,8 +61,10 @@ public class UserController extends OdinController { private final UserSessionService userSessionService; private final OdinService odinRolesService; private final OdinService odinSessionsService; + private final UserWorkloadService userWorkloadService; public UserController(UserService userService, + UserWorkloadService userWorkloadService, UserSessionService userSessionService, OdinService odinRolesService, OdinService odinSessionsService) { @@ -67,6 +73,7 @@ public class UserController extends OdinController { this.userSessionService = userSessionService; this.odinRolesService = odinRolesService; this.odinSessionsService = odinSessionsService; + this.userWorkloadService = userWorkloadService; } @GetMapping(ROLES_URL) @@ -172,4 +179,22 @@ public class UserController extends OdinController { public void inviteUser(@RequestParam("email") String email) { userService.inviteUser(email); } + + @PostMapping("/setWorkload") + public void setWeekHours(@RequestBody UserWorkloadDto workload, HttpServletRequest request) { + HttpSession session = request.getSession(false); + final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); + User user = userSessionService.getUserBySessionId(sessionId); + userWorkloadService.setUserWorkload(user, workload); + } + + @GetMapping("/getWorkloads") + public Response> getWorklogs(@RequestParam(name = "dateFrom") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateFrom, + @RequestParam(name = "dateTo") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateTo, + HttpServletRequest request) { + HttpSession session = request.getSession(false); + final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); + User user = userSessionService.getUserBySessionId(sessionId); + return new Response<>(userWorkloadService.getUserWorkloads(user, dateFrom, dateTo)); + } } diff --git a/src/main/java/ru/ulstu/user/model/UserWorkload.java b/src/main/java/ru/ulstu/user/model/UserWorkload.java new file mode 100644 index 0000000..8c4a4ad --- /dev/null +++ b/src/main/java/ru/ulstu/user/model/UserWorkload.java @@ -0,0 +1,68 @@ +package ru.ulstu.user.model; + +import ru.ulstu.core.model.BaseEntity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.validation.constraints.NotNull; +import java.util.Date; + + +@Entity +@Table(name = "users_workloads") +public class UserWorkload extends BaseEntity { + + @Column(name = "hours") + private int hours; + + @NotNull + @Temporal(value = TemporalType.TIMESTAMP) + @Column(name = "date_from") + private Date dateFrom; + + @NotNull + @Column(name = "date_to") + @Temporal(value = TemporalType.TIMESTAMP) + private Date dateTo; + + @ManyToOne(optional = false) + @JoinColumn(name = "user_id") + private User user; + + public int getHours() { + return hours; + } + + public void setHours(int hours) { + this.hours = hours; + } + + public Date getDateFrom() { + return dateFrom; + } + + public void setDateFrom(Date dateFrom) { + this.dateFrom = dateFrom; + } + + public Date getDateTo() { + return dateTo; + } + + public void setDateTo(Date dateTo) { + this.dateTo = dateTo; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } +} diff --git a/src/main/java/ru/ulstu/user/model/UserWorkloadDto.java b/src/main/java/ru/ulstu/user/model/UserWorkloadDto.java new file mode 100644 index 0000000..71c45d3 --- /dev/null +++ b/src/main/java/ru/ulstu/user/model/UserWorkloadDto.java @@ -0,0 +1,48 @@ +package ru.ulstu.user.model; + +import java.util.Date; + +public class UserWorkloadDto { + private int hours; + private Date dateFrom; + private Date dateTo; + + public UserWorkloadDto() { + } + + public UserWorkloadDto(int hours, Date dateFrom, Date dateTo) { + this.hours = hours; + this.dateFrom = dateFrom; + this.dateTo = dateTo; + } + + public UserWorkloadDto(UserWorkload workload) { + this.hours = workload.getHours(); + this.dateFrom = workload.getDateFrom(); + this.dateTo = workload.getDateTo(); + } + + public int getHours() { + return hours; + } + + public void setHours(int hours) { + this.hours = hours; + } + + public Date getDateFrom() { + return dateFrom; + } + + public void setDateFrom(Date dateFrom) { + this.dateFrom = dateFrom; + } + + public Date getDateTo() { + return dateTo; + } + + public void setDateTo(Date dateTo) { + this.dateTo = dateTo; + } +} diff --git a/src/main/java/ru/ulstu/user/repository/UserWorkloadRepository.java b/src/main/java/ru/ulstu/user/repository/UserWorkloadRepository.java new file mode 100644 index 0000000..61f2f98 --- /dev/null +++ b/src/main/java/ru/ulstu/user/repository/UserWorkloadRepository.java @@ -0,0 +1,17 @@ +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.User; +import ru.ulstu.user.model.UserWorkload; + +import java.util.Date; +import java.util.List; + +public interface UserWorkloadRepository extends JpaRepository { + List findAllByUser(User user); + + @Query("SELECT uw FROM UserWorkload uw WHERE (user = :user) AND (dateFrom >= :dateFrom) AND (dateTo <= :dateTo) ORDER BY uw.dateFrom") + List findByUserAndInterval(@Param("user") User user, @Param("dateFrom") Date dateFrom, @Param("dateTo") Date dateTo); +} diff --git a/src/main/java/ru/ulstu/user/service/UserWorkloadService.java b/src/main/java/ru/ulstu/user/service/UserWorkloadService.java new file mode 100644 index 0000000..619e431 --- /dev/null +++ b/src/main/java/ru/ulstu/user/service/UserWorkloadService.java @@ -0,0 +1,48 @@ +package ru.ulstu.user.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.configuration.ApplicationProperties; +import ru.ulstu.user.model.User; +import ru.ulstu.user.model.UserWorkload; +import ru.ulstu.user.model.UserWorkloadDto; +import ru.ulstu.user.repository.UserWorkloadRepository; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Service +@Transactional +public class UserWorkloadService { + private final Logger log = LoggerFactory.getLogger(UserWorkloadService.class); + private final ApplicationProperties applicationProperties; + private final UserWorkloadRepository userWorkloadRepository; + + public UserWorkloadService(UserWorkloadRepository userWorkloadRepository, + ApplicationProperties applicationProperties) { + this.applicationProperties = applicationProperties; + this.userWorkloadRepository = userWorkloadRepository; + } + + public void setUserWorkload(User user, UserWorkloadDto userWorkloadDto) { + UserWorkload workload = new UserWorkload(); + + workload.setHours(userWorkloadDto.getHours()); + workload.setDateFrom(userWorkloadDto.getDateFrom()); + workload.setDateTo(userWorkloadDto.getDateTo()); + workload.setUser(user); + + userWorkloadRepository.save(workload); + } + + public List getUserWorkloads(User user, Date dateFrom, Date dateTo) { + List workloadDtos = new ArrayList<>(); + for (UserWorkload workload : userWorkloadRepository.findByUserAndInterval(user, dateFrom, dateTo)) { + workloadDtos.add(new UserWorkloadDto(workload)); + } + return workloadDtos; + } +} diff --git a/src/main/resources/db/changelog-20190521_000000-schema.xml b/src/main/resources/db/changelog-20190521_000000-schema.xml new file mode 100644 index 0000000..39d4bb8 --- /dev/null +++ b/src/main/resources/db/changelog-20190521_000000-schema.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 18bd645..f354dba 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -42,4 +42,5 @@ + \ No newline at end of file diff --git a/src/main/resources/public/js/users.js b/src/main/resources/public/js/users.js index 76bd41d..c14b5ca 100644 --- a/src/main/resources/public/js/users.js +++ b/src/main/resources/public/js/users.js @@ -55,4 +55,56 @@ function inviteUser() { showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING) } }) -} \ No newline at end of file +} + +function setUserWorkload() { + $.ajax({ + url:"/api/1.0/users/setWorkload", + contentType: "application/json; charset=utf-8", + method: "POST", + data: JSON.stringify({ + "hours": document.getElementById("hours").value, + "dateFrom": document.getElementById("dateFrom").value, + "dateTo": document.getElementById("dateTo").value, + }), + success: function() { + showFeedbackMessage("Время успешно зарегистрировано", MessageTypesEnum.SUCCESS) + }, + error: function(errorData) { + showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING) + } + }) +} + +function drawChart() { + $.ajax({ + url:"/api/1.0/users/getWorkloads?dateFrom=" + document.getElementById("dateFrom").value + "&dateTo=" + document.getElementById("dateTo").value, + contentType: "application/json; charset=utf-8", + method: "GET", + success: function(response) { + if (response.data.length == 0) { + return; + } + array = [['Недели', '']] + response.data.forEach(function(element) { + dateFrom = new Date(element.dateFrom) + dateTo = new Date(element.dateTo) + dateStr = dateFrom.getDate() + '.' + (dateFrom.getMonth() + 1) + '.' + dateFrom.getFullYear() + ' - ' + + dateTo.getDate() + '.' + (dateTo.getMonth() + 1) + '.' + dateTo.getFullYear(); + array.push([dateStr, element.hours]) + }) + + var data = google.visualization.arrayToDataTable(array); + var options = { + title: 'Рабочие часы по неделям', + hAxis: {title: 'Неделя'}, + vAxis: {title: 'Часы'} + }; + var chart = new google.visualization.ColumnChart(document.getElementById('oil')); + chart.draw(data, options); + }, + error: function(errorData) { + showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING) + } + }) +} diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 32939e7..d3c49dd 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -109,6 +109,7 @@ + + + + @@ -19,53 +29,97 @@
-
- -
- - -

Incorrect firstName

-

+ +
-- 2.34.1