#13 change model, schema

This commit is contained in:
Семенова Мария 2019-03-18 11:07:30 +04:00
parent ae58a5b6e4
commit 7b601c744f
5 changed files with 49 additions and 49 deletions

View File

@ -9,26 +9,8 @@ import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileData;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import javax.persistence.CascadeType; import javax.persistence.*;
import javax.persistence.Column; import java.util.*;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@Entity @Entity
public class Paper extends BaseEntity implements UserContainer { public class Paper extends BaseEntity implements UserContainer {
@ -77,9 +59,11 @@ public class Paper extends BaseEntity implements UserContainer {
private Boolean locked = false; private Boolean locked = false;
@ManyToOne
@JoinColumn(name = "file_id") @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private FileData fileData; @JoinColumn(name = "paper_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
private List<FileData> files = new ArrayList<>();
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
private Set<User> authors = new HashSet<>(); private Set<User> authors = new HashSet<>();
@ -132,12 +116,12 @@ public class Paper extends BaseEntity implements UserContainer {
this.locked = locked; this.locked = locked;
} }
public FileData getFileData() { public List<FileData> getFiles() {
return fileData; return files;
} }
public void setFileData(FileData fileData) { public void setFiles(List<FileData> files) {
this.fileData = fileData; this.files = files;
} }
public String getTitle() { public String getTitle() {

View File

@ -79,9 +79,9 @@ public class PaperDto {
this.comment = paper.getComment(); this.comment = paper.getComment();
this.locked = paper.getLocked(); this.locked = paper.getLocked();
this.tmpFileName = null; this.tmpFileName = null;
this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId(); // this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId();
this.fileName = paper.getFileData() == null ? null : paper.getFileData().getName(); // this.fileName = paper.getFileData() == null ? null : paper.getFileData().getName();
this.fileCreateDate = paper.getFileData() == null ? null : paper.getFileData().getCreateDate(); // this.fileCreateDate = paper.getFileData() == null ? null : paper.getFileData().getCreateDate();
this.authorIds = convert(paper.getAuthors(), user -> user.getId()); this.authorIds = convert(paper.getAuthors(), user -> user.getId());
this.authors = convert(paper.getAuthors(), UserDto::new); this.authors = convert(paper.getAuthors(), UserDto::new);
} }

View File

@ -14,20 +14,12 @@ import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.*;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.paper.model.Paper.PaperStatus.ATTENTION; import static ru.ulstu.paper.model.Paper.PaperStatus.*;
import static ru.ulstu.paper.model.Paper.PaperStatus.COMPLETED;
import static ru.ulstu.paper.model.Paper.PaperStatus.DRAFT;
import static ru.ulstu.paper.model.Paper.PaperStatus.FAILED;
import static ru.ulstu.paper.model.Paper.PaperStatus.ON_PREPARATION;
@Service @Service
public class PaperService { public class PaperService {
@ -88,9 +80,9 @@ public class PaperService {
paper.setTitle(paperDto.getTitle()); paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(new Date()); paper.setUpdateDate(new Date());
paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines()));
if (paperDto.getTmpFileName() != null) { // if (paperDto.getTmpFileName() != null) {
paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); // paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
} // }
paper.getAuthors().clear(); paper.getAuthors().clear();
if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) {
paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds)));
@ -103,9 +95,9 @@ public class PaperService {
Paper paper = paperRepository.findOne(paperDto.getId()); Paper paper = paperRepository.findOne(paperDto.getId());
Paper.PaperStatus oldStatus = paper.getStatus(); Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors()); Set<User> oldAuthors = new HashSet<>(paper.getAuthors());
if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { // if (paperDto.getTmpFileName() != null && paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData()); // fileService.deleteFile(paper.getFileData());
} // }
paperRepository.save(copyFromDto(paper, paperDto)); paperRepository.save(copyFromDto(paper, paperDto));
paper.getAuthors().forEach(author -> { paper.getAuthors().forEach(author -> {
@ -124,9 +116,9 @@ public class PaperService {
@Transactional @Transactional
public void delete(Integer paperId) throws IOException { public void delete(Integer paperId) throws IOException {
Paper paper = paperRepository.findOne(paperId); Paper paper = paperRepository.findOne(paperId);
if (paper.getFileData() != null) { // if (paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData()); // fileService.deleteFile(paper.getFileData());
} // }
paperRepository.delete(paper); paperRepository.delete(paper);
} }

View File

@ -0,0 +1,23 @@
<?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="masha" id="20190318_000000-1">
<dropColumn columnName="file_id" tableName="paper"/>
<addColumn tableName="file">
<column name="paper_id" type="integer"/>
</addColumn>
<createTable tableName="paper_files">
<column name="paper_id" type="integer"/>
<column name="file_id" type="integer"/>
</createTable>
<addForeignKeyConstraint baseTableName="paper_files" baseColumnNames="paper_id"
constraintName="fk_paper_paper_files" referencedTableName="paper"
referencedColumnNames="id"/>
<addForeignKeyConstraint baseTableName="paper_files" baseColumnNames="file_id"
constraintName="fk_file_paper_files" referencedTableName="file"
referencedColumnNames="id"/>
</changeSet>
</databaseChangeLog>

View File

@ -18,5 +18,6 @@
<include file="db/changelog-20181111_000000-schema.xml"/> <include file="db/changelog-20181111_000000-schema.xml"/>
<include file="db/changelog-20181208_000000-schema.xml"/> <include file="db/changelog-20181208_000000-schema.xml"/>
<include file="db/changelog-20181224_000000-schema.xml"/> <include file="db/changelog-20181224_000000-schema.xml"/>
<include file="db/changelog-20190318_000000-schema.xml"/>
<include file="db/common/changelog-20190312_130000-schema.xml"/> <include file="db/common/changelog-20190312_130000-schema.xml"/>
</databaseChangeLog> </databaseChangeLog>