WIP: Resolve "Страница редактирования загруженности" #231

Draft
arefiev1997 wants to merge 1 commits from 90-edit-workload into dev
10 changed files with 383 additions and 47 deletions

View File

@ -2,8 +2,8 @@ package ru.ulstu.user.controller;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.annotation.Secured; 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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; 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.UserRoleConstants;
import ru.ulstu.user.model.UserRoleDto; import ru.ulstu.user.model.UserRoleDto;
import ru.ulstu.user.model.UserSessionListDto; import ru.ulstu.user.model.UserSessionListDto;
import ru.ulstu.user.model.UserWorkloadDto;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
import ru.ulstu.user.service.UserSessionService; import ru.ulstu.user.service.UserSessionService;
import ru.ulstu.user.service.UserWorkloadService;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map; import java.util.Map;
import static ru.ulstu.user.controller.UserController.URL; import static ru.ulstu.user.controller.UserController.URL;
@ -57,8 +61,10 @@ public class UserController extends OdinController<UserListDto, UserDto> {
private final UserSessionService userSessionService; private final UserSessionService userSessionService;
private final OdinService<UserRoleDto, UserRoleDto> odinRolesService; private final OdinService<UserRoleDto, UserRoleDto> odinRolesService;
private final OdinService<UserSessionListDto, OdinVoid> odinSessionsService; private final OdinService<UserSessionListDto, OdinVoid> odinSessionsService;
private final UserWorkloadService userWorkloadService;
public UserController(UserService userService, public UserController(UserService userService,
UserWorkloadService userWorkloadService,
UserSessionService userSessionService, UserSessionService userSessionService,
OdinService<UserRoleDto, UserRoleDto> odinRolesService, OdinService<UserRoleDto, UserRoleDto> odinRolesService,
OdinService<UserSessionListDto, OdinVoid> odinSessionsService) { OdinService<UserSessionListDto, OdinVoid> odinSessionsService) {
@ -67,6 +73,7 @@ public class UserController extends OdinController<UserListDto, UserDto> {
this.userSessionService = userSessionService; this.userSessionService = userSessionService;
this.odinRolesService = odinRolesService; this.odinRolesService = odinRolesService;
this.odinSessionsService = odinSessionsService; this.odinSessionsService = odinSessionsService;
this.userWorkloadService = userWorkloadService;
} }
@GetMapping(ROLES_URL) @GetMapping(ROLES_URL)
@ -172,4 +179,22 @@ public class UserController extends OdinController<UserListDto, UserDto> {
public void inviteUser(@RequestParam("email") String email) { public void inviteUser(@RequestParam("email") String email) {
userService.inviteUser(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<List<UserWorkloadDto>> 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));
}
} }

View File

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

View File

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

View File

@ -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<UserWorkload, Integer> {
List<UserWorkload> findAllByUser(User user);
@Query("SELECT uw FROM UserWorkload uw WHERE (user = :user) AND (dateFrom >= :dateFrom) AND (dateTo <= :dateTo) ORDER BY uw.dateFrom")
List<UserWorkload> findByUserAndInterval(@Param("user") User user, @Param("dateFrom") Date dateFrom, @Param("dateTo") Date dateTo);
}

View File

@ -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<UserWorkloadDto> getUserWorkloads(User user, Date dateFrom, Date dateTo) {
List<UserWorkloadDto> workloadDtos = new ArrayList<>();
for (UserWorkload workload : userWorkloadRepository.findByUserAndInterval(user, dateFrom, dateTo)) {
workloadDtos.add(new UserWorkloadDto(workload));
}
return workloadDtos;
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet author="arefyev" id="20190521_000000-1">
<createTable tableName="users_workloads">
<column name="id" type="integer">
<constraints nullable="false"/>
</column>
<column name="version" type="INTEGER"/>
<column name="hours" type="integer"/>
<column name="date_from" type="timestamp"/>
<column name="date_to" type="timestamp"/>
<column name="user_id" type="integer"/>
</createTable>
<addPrimaryKey tableName="users_workloads" columnNames="id" constraintName="users_workloads_pkey"/>
<addForeignKeyConstraint baseTableName="users_workloads" baseColumnNames="user_id"
constraintName="fk_user__workload" referencedTableName="users"
referencedColumnNames="id"/>
</changeSet>
</databaseChangeLog>

View File

@ -42,4 +42,5 @@
<include file="db/changelog-20190507_000000-schema.xml"/> <include file="db/changelog-20190507_000000-schema.xml"/>
<include file="db/changelog-20190507_000001-schema.xml"/> <include file="db/changelog-20190507_000001-schema.xml"/>
<include file="db/changelog-20190511_000000-schema.xml"/> <include file="db/changelog-20190511_000000-schema.xml"/>
<include file="db/changelog-20190521_000000-schema.xml"/>
</databaseChangeLog> </databaseChangeLog>

View File

@ -55,4 +55,56 @@ function inviteUser() {
showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING) showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING)
} }
}) })
} }
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)
}
})
}

View File

@ -109,6 +109,7 @@
<script src="/js/core.js"></script> <script src="/js/core.js"></script>
<script src="/js/config.js"></script> <script src="/js/config.js"></script>
<script src="/js/odin.js"></script> <script src="/js/odin.js"></script>
<script src="/js/users.js"></script>
<script th:inline="javascript"> <script th:inline="javascript">
/*<![CDATA[*/ /*<![CDATA[*/

View File

@ -1,9 +1,19 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
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" xmlns="http://www.w3.org/1999/html"> layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html">
<head> <head>
<link rel="stylesheet" href="../css/grant.css"/> <link rel="stylesheet" href="../css/grant.css"/>
<script src="js/users.js"></script>
<script src="js/core.js"></script>
<script src="https://www.google.com/jsapi"></script>
<script>
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
</script>
</head> </head>
<body> <body>
@ -19,53 +29,97 @@
<hr/> <hr/>
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<form id="profile-form" method="post" th:action="@{'/users/profile'}" <nav>
th:object="${userDto}"> <div class="nav nav-tabs" id="nav-tab" role="tablist">
<input type="hidden" name="id" th:field="*{id}"/> <a class="nav-item nav-link active" id="nav-main-tab" data-toggle="tab"
<div class="form-group"> href="#nav-main" role="tab" aria-controls="nav-main" aria-selected="true">Статья</a>
<label for="firstName">Имя:</label> <a class="nav-item nav-link" id="nav-latex-tab" data-toggle="tab"
<input class="form-control" id="firstName" type="text" href="#nav-workload" role="tab" aria-controls="nav-workload" aria-selected="false">Загруженность</a>
placeholder="Имя"
th:field="*{firstName}"/>
<p th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"
class="alert alert-danger">Incorrect firstName</p>
<p class="help-block text-danger"></p>
</div> </div>
<div class="form-group"> </nav>
<label for="lastName">Фамилия:</label> <div class="tab-content" id="nav-tabContent">
<input class="form-control" id="lastName" type="text" <div class="tab-pane fade show active" id="nav-main" role="tabpanel"
placeholder="lastName" aria-labelledby="nav-main-tab">
th:field="*{lastName}"/> <form id="profile-form" method="post" th:action="@{'/users/profile'}"
<p th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}" th:object="${userDto}">
class="alert alert-danger">Incorrect lastName</p> <input type="hidden" name="id" th:field="*{id}"/>
<p class="help-block text-danger"></p> <div class="form-group">
<label for="firstName">Имя:</label>
<input class="form-control" id="firstName" type="text"
placeholder="Имя"
th:field="*{firstName}"/>
<p th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"
class="alert alert-danger">Incorrect firstName</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<label for="lastName">Фамилия:</label>
<input class="form-control" id="lastName" type="text"
placeholder="lastName"
th:field="*{lastName}"/>
<p th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}"
class="alert alert-danger">Incorrect lastName</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input class="form-control" id="email" type="text"
placeholder="Email"
th:field="*{email}"/>
<p th:if="${#fields.hasErrors('email')}" th:errors="*{email}"
class="alert alert-danger">Incorrect email</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<label for="login">Логин:</label>
<input class="form-control" id="login" type="text"
placeholder="Login"
th:field="*{login}"/>
<p th:if="${#fields.hasErrors('login')}" th:errors="*{login}"
class="alert alert-danger">Incorrect email</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<button id="sendMessageButton" name="save"
class="btn btn-success text-uppercase"
type="submit">
Сохранить
</button>
</div>
</form>
</div> </div>
<div class="form-group"> <div class="tab-pane fade" id="nav-workload" role="tabpanel"
<label for="email">Email:</label> aria-labelledby="nav-profile-tab">
<input class="form-control" id="email" type="text" <div class="form-group">
placeholder="Email" <label for="hours">Часы:</label>
th:field="*{email}"/> <input class="form-control" id="hours" type="text"
<p th:if="${#fields.hasErrors('email')}" th:errors="*{email}" placeholder="Количество часов"/>
class="alert alert-danger">Incorrect email</p> </div>
<p class="help-block text-danger"></p> <div class="form-group">
<label for="dateFrom">Дата начала недели:</label>
<input class="form-control" id="dateFrom" type="date"/>
</div>
<div class="form-group">
<label for="dateTo">Дата окончания недели:</label>
<input class="form-control" id="dateTo" type="date"/>
</div>
<div class="form-group">
<button id="sendWorkload" name="save"
class="btn btn-success text-uppercase"
type="button" onclick="setUserWorkload()">
Сохранить часы
</button>
</div>
<div class="form-group">
<button id="drawChartBtn" name="save"
class="btn btn-success text-uppercase"
type="button" onclick="drawChart()">
Показать график
</button>
</div>
<div id="oil" style="width: 500px; height: 400px;"></div>
</div> </div>
<div class="form-group"> </div>
<label for="login">Логин:</label>
<input class="form-control" id="login" type="text"
placeholder="Login"
th:field="*{login}"/>
<p th:if="${#fields.hasErrors('login')}" th:errors="*{login}"
class="alert alert-danger">Incorrect email</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<button id="sendMessageButton" name="save"
class="btn btn-success text-uppercase"
type="submit">
Сохранить
</button>
</div>
</form>
</div> </div>
</div> </div>
</div> </div>