From 7c1b124c9470821b47c82642a7c6b945724efd04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Thu, 23 May 2019 15:48:46 +0400 Subject: [PATCH] #127 references tab, crud --- .../paper/controller/PaperController.java | 20 ++++ src/main/java/ru/ulstu/paper/model/Paper.java | 13 +++ .../java/ru/ulstu/paper/model/PaperDto.java | 25 ++++- .../java/ru/ulstu/paper/model/Reference.java | 2 - .../ru/ulstu/paper/model/ReferenceDto.java | 39 +++++++- .../paper/repository/ReferenceRepository.java | 8 ++ .../ru/ulstu/paper/service/PaperService.java | 57 +++++++++++ .../db/changelog-20190523_000000-schema.xml | 13 +-- src/main/resources/public/css/paper.css | 20 ++++ .../resources/templates/papers/paper.html | 97 ++++++++++++++++++- 10 files changed, 279 insertions(+), 15 deletions(-) create mode 100644 src/main/java/ru/ulstu/paper/repository/ReferenceRepository.java diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 41d5586..beae772 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -16,6 +16,7 @@ import ru.ulstu.deadline.model.Deadline; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperListDto; +import ru.ulstu.paper.model.ReferenceDto; import ru.ulstu.paper.service.LatexService; import ru.ulstu.paper.service.PaperService; import ru.ulstu.user.model.User; @@ -105,6 +106,15 @@ public class PaperController { return "/papers/paper"; } + @PostMapping(value = "/paper", params = "addReference") + public String addReference(@Valid PaperDto paperDto, Errors errors) { + if (errors.hasErrors()) { + return "/papers/paper"; + } + paperDto.getReferences().add(new ReferenceDto()); + return "/papers/paper"; + } + @ModelAttribute("allStatuses") public List getPaperStatuses() { return paperService.getPaperStatuses(); @@ -129,6 +139,16 @@ public class PaperController { return years; } + @ModelAttribute("allFormatStandards") + public List getFormatStandards() { + return paperService.getFormatStandards(); + } + + @ModelAttribute("allReferenceTypes") + public List getReferenceTypes() { + return paperService.getReferenceTypes(); + } + @PostMapping("/generatePdf") public ResponseEntity getPdfFile(PaperDto paper) throws IOException, InterruptedException { HttpHeaders headers = new HttpHeaders(); diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index 15ee4b4..413c5db 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -123,6 +123,11 @@ public class Paper extends BaseEntity implements UserContainer { @ManyToMany(mappedBy = "papers") private List grants; + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @JoinColumn(name = "paper_id", unique = true) + @Fetch(FetchMode.SUBSELECT) + private List references = new ArrayList<>(); + public PaperStatus getStatus() { return status; } @@ -248,6 +253,14 @@ public class Paper extends BaseEntity implements UserContainer { return getAuthors(); } + public List getReferences() { + return references; + } + + public void setReferences(List references) { + this.references = references; + } + public Optional getNextDeadline() { return deadlines .stream() diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index 11d6dfc..8474a34 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -38,6 +38,8 @@ public class PaperDto { private Set authors; private Integer filterAuthorId; private String latexText; + private List references = new ArrayList<>(); + private ReferenceDto.FormatStandard formatStandard = ReferenceDto.FormatStandard.GOST; public PaperDto() { deadlines.add(new Deadline()); @@ -57,7 +59,9 @@ public class PaperDto { @JsonProperty("locked") Boolean locked, @JsonProperty("files") List files, @JsonProperty("authorIds") Set authorIds, - @JsonProperty("authors") Set authors) { + @JsonProperty("authors") Set authors, + @JsonProperty("references") List references, + @JsonProperty("formatStandard") ReferenceDto.FormatStandard formatStandard) { this.id = id; this.title = title; this.status = status; @@ -71,6 +75,8 @@ public class PaperDto { this.locked = locked; this.files = files; this.authors = authors; + this.references = references; + this.formatStandard = formatStandard; } public PaperDto(Paper paper) { @@ -88,6 +94,7 @@ public class PaperDto { this.files = convert(paper.getFiles(), FileDataDto::new); this.authorIds = convert(paper.getAuthors(), user -> user.getId()); this.authors = convert(paper.getAuthors(), UserDto::new); + this.references = convert(paper.getReferences(), ReferenceDto::new); } public Integer getId() { @@ -216,4 +223,20 @@ public class PaperDto { public void setFilterAuthorId(Integer filterAuthorId) { this.filterAuthorId = filterAuthorId; } + + public List getReferences() { + return references; + } + + public void setReferences(List references) { + this.references = references; + } + + public ReferenceDto.FormatStandard getFormatStandard() { + return formatStandard; + } + + public void setFormatStandard(ReferenceDto.FormatStandard formatStandard) { + this.formatStandard = formatStandard; + } } diff --git a/src/main/java/ru/ulstu/paper/model/Reference.java b/src/main/java/ru/ulstu/paper/model/Reference.java index 1901f41..289cdc1 100644 --- a/src/main/java/ru/ulstu/paper/model/Reference.java +++ b/src/main/java/ru/ulstu/paper/model/Reference.java @@ -1,6 +1,5 @@ package ru.ulstu.paper.model; -import org.hibernate.validator.constraints.NotBlank; import ru.ulstu.core.model.BaseEntity; import javax.persistence.Column; @@ -11,7 +10,6 @@ import javax.persistence.Enumerated; @Entity public class Reference extends BaseEntity { - @NotBlank private String authors; @Column(name = "publication_title") diff --git a/src/main/java/ru/ulstu/paper/model/ReferenceDto.java b/src/main/java/ru/ulstu/paper/model/ReferenceDto.java index 8d71ae5..1705e08 100644 --- a/src/main/java/ru/ulstu/paper/model/ReferenceDto.java +++ b/src/main/java/ru/ulstu/paper/model/ReferenceDto.java @@ -34,6 +34,7 @@ public class ReferenceDto { } } + private Integer id; private String authors; private String publicationTitle; private Integer publicationYear; @@ -42,9 +43,11 @@ public class ReferenceDto { private String journalOrCollectionTitle; private ReferenceType referenceType; private FormatStandard formatStandard; + private boolean deleted; @JsonCreator public ReferenceDto( + @JsonProperty("id") Integer id, @JsonProperty("authors") String authors, @JsonProperty("publicationTitle") String publicationTitle, @JsonProperty("publicationYear") Integer publicationYear, @@ -52,7 +55,9 @@ public class ReferenceDto { @JsonProperty("pages") String pages, @JsonProperty("journalOrCollectionTitle") String journalOrCollectionTitle, @JsonProperty("referenceType") ReferenceType referenceType, - @JsonProperty("formatStandard") FormatStandard formatStandard) { + @JsonProperty("formatStandard") FormatStandard formatStandard, + @JsonProperty("isDeleted") boolean deleted) { + this.id = id; this.authors = authors; this.publicationTitle = publicationTitle; this.publicationYear = publicationYear; @@ -61,6 +66,22 @@ public class ReferenceDto { this.journalOrCollectionTitle = journalOrCollectionTitle; this.referenceType = referenceType; this.formatStandard = formatStandard; + this.deleted = deleted; + } + + public ReferenceDto(Reference reference) { + this.id = reference.getId(); + this.authors = reference.getAuthors(); + this.publicationTitle = reference.getPublicationTitle(); + this.publicationYear = reference.getPublicationYear(); + this.publisher = reference.getPublisher(); + this.pages = reference.getPages(); + this.journalOrCollectionTitle = reference.getJournalOrCollectionTitle(); + this.referenceType = reference.getReferenceType(); + } + + public ReferenceDto() { + referenceType = ReferenceType.ARTICLE; } public String getAuthors() { @@ -126,4 +147,20 @@ public class ReferenceDto { public void setFormatStandard(FormatStandard formatStandard) { this.formatStandard = formatStandard; } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public boolean getDeleted() { + return deleted; + } + + public void setDeleted(boolean deleted) { + this.deleted = deleted; + } } diff --git a/src/main/java/ru/ulstu/paper/repository/ReferenceRepository.java b/src/main/java/ru/ulstu/paper/repository/ReferenceRepository.java new file mode 100644 index 0000000..68e97d4 --- /dev/null +++ b/src/main/java/ru/ulstu/paper/repository/ReferenceRepository.java @@ -0,0 +1,8 @@ +package ru.ulstu.paper.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.paper.model.Reference; + +public interface ReferenceRepository extends JpaRepository { + void deleteById(Integer id); +} diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index db810f5..8658015 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -10,8 +10,10 @@ import ru.ulstu.file.service.FileService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperListDto; +import ru.ulstu.paper.model.Reference; import ru.ulstu.paper.model.ReferenceDto; import ru.ulstu.paper.repository.PaperRepository; +import ru.ulstu.paper.repository.ReferenceRepository; import ru.ulstu.timeline.service.EventService; import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; @@ -39,6 +41,7 @@ import static ru.ulstu.paper.model.ReferenceDto.ReferenceType.ARTICLE; import static ru.ulstu.paper.model.ReferenceDto.ReferenceType.BOOK; @Service +@Transactional public class PaperService { private final static int MAX_DISPLAY_SIZE = 40; private final static String PAPER_FORMATTED_TEMPLATE = "%s %s"; @@ -49,14 +52,17 @@ public class PaperService { private final DeadlineService deadlineService; private final FileService fileService; private final EventService eventService; + private final ReferenceRepository referenceRepository; public PaperService(PaperRepository paperRepository, + ReferenceRepository referenceRepository, FileService fileService, PaperNotificationService paperNotificationService, UserService userService, DeadlineService deadlineService, EventService eventService) { this.paperRepository = paperRepository; + this.referenceRepository = referenceRepository; this.fileService = fileService; this.paperNotificationService = paperNotificationService; this.userService = userService; @@ -117,6 +123,7 @@ public class PaperService { paper.setTitle(paperDto.getTitle()); paper.setUpdateDate(new Date()); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); + paper.setReferences(saveOrCreateReferences(paperDto.getReferences())); paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream() .filter(f -> !f.isDeleted()) .collect(toList()))); @@ -127,6 +134,43 @@ public class PaperService { return paper; } + public List saveOrCreateReferences(List references) { + return references + .stream() + .filter(reference -> !reference.getDeleted()) + .map(reference -> reference.getId() != null ? updateReference(reference) : createReference(reference)) + .collect(Collectors.toList()); + } + + @Transactional + public Reference updateReference(ReferenceDto referenceDto) { + Reference updateReference = referenceRepository.findOne(referenceDto.getId()); + updateReference.setAuthors(referenceDto.getAuthors()); + updateReference.setJournalOrCollectionTitle(referenceDto.getJournalOrCollectionTitle()); + updateReference.setPages(referenceDto.getPages()); + updateReference.setPublicationTitle(referenceDto.getPublicationTitle()); + updateReference.setPublicationYear(referenceDto.getPublicationYear()); + updateReference.setPublisher(referenceDto.getPublisher()); + updateReference.setReferenceType(referenceDto.getReferenceType()); + referenceRepository.save(updateReference); + return updateReference; + } + + @Transactional + public Reference createReference(ReferenceDto referenceDto) { + Reference newReference = new Reference(); + newReference.setAuthors(referenceDto.getAuthors()); + newReference.setJournalOrCollectionTitle(referenceDto.getJournalOrCollectionTitle()); + newReference.setPages(referenceDto.getPages()); + newReference.setPublicationTitle(referenceDto.getPublicationTitle()); + newReference.setPublicationYear(referenceDto.getPublicationYear()); + newReference.setPublisher(referenceDto.getPublisher()); + newReference.setReferenceType(referenceDto.getReferenceType()); + newReference = referenceRepository.save(newReference); + return newReference; + } + + @Transactional public Integer update(PaperDto paperDto) throws IOException { Paper paper = paperRepository.findOne(paperDto.getId()); @@ -139,6 +183,11 @@ public class PaperService { fileService.delete(file.getId()); } paperRepository.save(copyFromDto(paper, paperDto)); + for (ReferenceDto referenceDto : paperDto.getReferences().stream() + .filter(f -> f.getDeleted() && f.getId() != null) + .collect(toList())) { + referenceRepository.deleteById(referenceDto.getId()); + } eventService.updatePaperDeadlines(paper); paper.getAuthors().forEach(author -> { @@ -168,6 +217,14 @@ public class PaperService { return Arrays.asList(Paper.PaperType.values()); } + public List getFormatStandards() { + return Arrays.asList(ReferenceDto.FormatStandard.values()); + } + + public List getReferenceTypes() { + return Arrays.asList(ReferenceDto.ReferenceType.values()); + } + @Transactional public Paper create(String title, User user, Date deadlineDate) { Paper paper = new Paper(); diff --git a/src/main/resources/db/changelog-20190523_000000-schema.xml b/src/main/resources/db/changelog-20190523_000000-schema.xml index 2bdc788..aca880f 100644 --- a/src/main/resources/db/changelog-20190523_000000-schema.xml +++ b/src/main/resources/db/changelog-20190523_000000-schema.xml @@ -7,20 +7,13 @@ - - - - - - - - - + + + - diff --git a/src/main/resources/public/css/paper.css b/src/main/resources/public/css/paper.css index 824209f..1be8c1f 100644 --- a/src/main/resources/public/css/paper.css +++ b/src/main/resources/public/css/paper.css @@ -6,4 +6,24 @@ .nav-tabs { margin-bottom: 20px; +} + +#nav-references label, #nav-references select, #nav-references input { + display: inline-block; + vertical-align: middle; +} + +#nav-references .collapse-heading { + border: 1px solid #ddd; + border-radius: 4px; + padding: 5px 15px; + background-color: #efefef; +} + +#nav-references .collapse-heading a { + text-decoration: none; +} + +#nav-references a:hover { + text-decoration: none; } \ No newline at end of file diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index d408913..ef4217d 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -31,6 +31,9 @@ href="#nav-main" role="tab" aria-controls="nav-main" aria-selected="true">Статья Latex + References +
@@ -378,7 +474,6 @@ $('#pdfBtn').prop('disabled', false); } } -