From d5d64c648446135efa9b84bb2d8af2d96aee15fc Mon Sep 17 00:00:00 2001 From: ruslanTash Date: Sat, 17 Jun 2023 19:04:30 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B2=D1=8B=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/weblibrary/controller/ReportController.java | 5 ----- src/main/java/com/example/weblibrary/model/Position.java | 5 ----- src/main/java/com/example/weblibrary/model/Report.java | 2 +- .../com/example/weblibrary/repository/ReportRepository.java | 3 +-- 4 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/weblibrary/controller/ReportController.java b/src/main/java/com/example/weblibrary/controller/ReportController.java index 655218e..b8801f9 100644 --- a/src/main/java/com/example/weblibrary/controller/ReportController.java +++ b/src/main/java/com/example/weblibrary/controller/ReportController.java @@ -12,17 +12,12 @@ public class ReportController { private final ReportService reportService; - - // Он должен формировать JSON-файл со статистикой по отделам: - // А также сохранять содержимое файла в базу данных. - // Метод возвращает целочисленный идентификатор сохраненного в базе данных файла. @PostMapping public Integer createReport() { return reportService.createReports(); } - // Он должен находить и возвращать созданный ранее файл в формате JSON по переданному уникальному идентификатору. @GetMapping("/{id}") public Report getReportById(@PathVariable("id") Integer id) { return reportService.getReportById(id); diff --git a/src/main/java/com/example/weblibrary/model/Position.java b/src/main/java/com/example/weblibrary/model/Position.java index 7220e68..e2a6d99 100644 --- a/src/main/java/com/example/weblibrary/model/Position.java +++ b/src/main/java/com/example/weblibrary/model/Position.java @@ -21,9 +21,4 @@ public class Position implements Serializable { private Integer positionId; private String positionName; -// @OneToMany(mappedBy = "employee") -// @JoinColumn(name = "employee_id") -// private List employees; - - } diff --git a/src/main/java/com/example/weblibrary/model/Report.java b/src/main/java/com/example/weblibrary/model/Report.java index c89af8d..194b8a2 100644 --- a/src/main/java/com/example/weblibrary/model/Report.java +++ b/src/main/java/com/example/weblibrary/model/Report.java @@ -17,7 +17,7 @@ public class Report implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "report_id") private Integer id; - // Название отдела. + @Lob @Column(name = "data", columnDefinition = "text") private String data; diff --git a/src/main/java/com/example/weblibrary/repository/ReportRepository.java b/src/main/java/com/example/weblibrary/repository/ReportRepository.java index 8cea53a..ed1547b 100644 --- a/src/main/java/com/example/weblibrary/repository/ReportRepository.java +++ b/src/main/java/com/example/weblibrary/repository/ReportRepository.java @@ -7,8 +7,7 @@ import java.util.List; public interface ReportRepository extends CrudRepository { -// @Query(value = "SELECT new com.example.weblibrary.DTO.ReportDTO(p.positionName, max(e.salary), min(e.salary), avg(e.salary)) FROM Employee e join fetch Position p WHERE e.position = p GROUP BY p.positionId") -// List createReport(); + @Query("SELECT new com.example.weblibrary.model.Report(" + "p.positionName, COUNT (e.id), MAX (e.salary), MIN (e.salary), AVG (e.salary)) " + "FROM Employee e join fetch Position p " + From 994c071c51fc135e16e4071b4e43a9e656d5fd5f Mon Sep 17 00:00:00 2001 From: ruslanTash Date: Sat, 17 Jun 2023 22:43:05 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=202=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ReportController.java | 4 +- .../com/example/weblibrary/model/Report.java | 7 +++ .../repository/ReportRepository.java | 14 +++--- .../weblibrary/service/ReportService.java | 4 +- .../weblibrary/service/ReportServiceImpl.java | 48 +++++++++++++++---- 5 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/example/weblibrary/controller/ReportController.java b/src/main/java/com/example/weblibrary/controller/ReportController.java index b8801f9..af1ab28 100644 --- a/src/main/java/com/example/weblibrary/controller/ReportController.java +++ b/src/main/java/com/example/weblibrary/controller/ReportController.java @@ -19,7 +19,7 @@ public Integer createReport() { @GetMapping("/{id}") - public Report getReportById(@PathVariable("id") Integer id) { + public String getReportById(@PathVariable("id") Integer id) { return reportService.getReportById(id); } -} +} \ No newline at end of file diff --git a/src/main/java/com/example/weblibrary/model/Report.java b/src/main/java/com/example/weblibrary/model/Report.java index 194b8a2..c372258 100644 --- a/src/main/java/com/example/weblibrary/model/Report.java +++ b/src/main/java/com/example/weblibrary/model/Report.java @@ -4,7 +4,10 @@ import lombok.*; import lombok.experimental.Accessors; +import java.io.FileOutputStream; +import java.io.ObjectOutputStream; import java.io.Serializable; +import java.nio.file.Path; @AllArgsConstructor @NoArgsConstructor @@ -20,8 +23,11 @@ public class Report implements Serializable { @Lob @Column(name = "data", columnDefinition = "text") + @Transient private String data; + private String pathFile; + public Report(String data) { this.data = data; } @@ -29,4 +35,5 @@ public Report(String data) { public Report(String positionName, Long count, Integer max, Integer min, Double avg) { this.data = "Position: " + positionName + ", countOfEmployee: " + count + ", maxSalary: " + max + ", minSalary: " + min + ", avgSalary: " + avg; } + } diff --git a/src/main/java/com/example/weblibrary/repository/ReportRepository.java b/src/main/java/com/example/weblibrary/repository/ReportRepository.java index ed1547b..5b7abbf 100644 --- a/src/main/java/com/example/weblibrary/repository/ReportRepository.java +++ b/src/main/java/com/example/weblibrary/repository/ReportRepository.java @@ -8,11 +8,11 @@ public interface ReportRepository extends CrudRepository { -@Query("SELECT new com.example.weblibrary.model.Report(" + - "p.positionName, COUNT (e.id), MAX (e.salary), MIN (e.salary), AVG (e.salary)) " + - "FROM Employee e join fetch Position p " + - "where e.position = p " + - "Group by p.positionId " ) -List createReport(); + @Query("SELECT new com.example.weblibrary.model.Report(" + + "p.positionName, COUNT (e.id), MAX (e.salary), MIN (e.salary), AVG (e.salary)) " + + "FROM Employee e join fetch Position p " + + "where e.position = p " + + "Group by p.positionId " ) + List createReport(); -} +} \ No newline at end of file diff --git a/src/main/java/com/example/weblibrary/service/ReportService.java b/src/main/java/com/example/weblibrary/service/ReportService.java index 2df1a13..6accbd7 100644 --- a/src/main/java/com/example/weblibrary/service/ReportService.java +++ b/src/main/java/com/example/weblibrary/service/ReportService.java @@ -1,9 +1,7 @@ package com.example.weblibrary.service; -import com.example.weblibrary.model.Report; - public interface ReportService { - Report getReportById(Integer id); + String getReportById(Integer id); Integer createReports(); } diff --git a/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java b/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java index d1307c4..ed6e997 100644 --- a/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java +++ b/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java @@ -2,14 +2,19 @@ import com.example.weblibrary.exceptions.ReportNotFoundException; import com.example.weblibrary.model.Report; -import com.example.weblibrary.repository.EmployeeRepository; import com.example.weblibrary.repository.ReportRepository; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; -import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Service; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectOutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; @RequiredArgsConstructor @@ -20,18 +25,36 @@ public class ReportServiceImpl implements ReportService { ObjectMapper objectMapper = new ObjectMapper(); @Override - public Report getReportById(Integer id) { - return reportRepository.findById(id) + public String getReportById(Integer id) { + Report report = reportRepository.findById(id) .orElseThrow(()-> new ReportNotFoundException(id)); + return report.getPathFile(); } @Override public Integer createReports() { - List reportDTOS = reportRepository.createReport(); + List reports = reportRepository.createReport(); + + + + + + try { - String json = objectMapper.writeValueAsString(reportDTOS); - Report report = new Report(json); + String text = objectMapper.writeValueAsString(reports); + Report report = new Report(text); reportRepository.save(report); + + String fileName = + "c:\\Users\\Ruslan\\" + + "report" + report.getId(); + File file = new File(fileName); + file.createNewFile(); + report.setPathFile(fileName); + writeTextToFile(text, fileName); + reportRepository.save(report); + + return report.getId(); } catch (IOException e) { e.printStackTrace(); @@ -39,4 +62,13 @@ public Integer createReports() { } -} + private static void writeTextToFile(String text, String fileName) { + Path path = Paths.get(fileName); + try { + Files.write(path, text.getBytes(StandardCharsets.UTF_8)); + } catch (IOException ioException) { + ioException.printStackTrace(); + } + } + +} \ No newline at end of file From 4cb8693460fbf1492cd52b967051c74f0fbdf204 Mon Sep 17 00:00:00 2001 From: ruslanTash Date: Sat, 17 Jun 2023 22:49:35 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=202=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/weblibrary/service/ReportServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java b/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java index ed6e997..6b9f4d1 100644 --- a/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java +++ b/src/main/java/com/example/weblibrary/service/ReportServiceImpl.java @@ -47,7 +47,7 @@ public Integer createReports() { String fileName = "c:\\Users\\Ruslan\\" + - "report" + report.getId(); + "report" + report.getId() + ".json"; File file = new File(fileName); file.createNewFile(); report.setPathFile(fileName);