#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.user.model.User;
import javax.persistence.CascadeType;
import javax.persistence.Column;
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;
import javax.persistence.*;
import java.util.*;
@Entity
public class Paper extends BaseEntity implements UserContainer {
@ -77,9 +59,11 @@ public class Paper extends BaseEntity implements UserContainer {
private Boolean locked = false;
@ManyToOne
@JoinColumn(name = "file_id")
private FileData fileData;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "paper_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
private List<FileData> files = new ArrayList<>();
@ManyToMany(fetch = FetchType.EAGER)
private Set<User> authors = new HashSet<>();
@ -132,12 +116,12 @@ public class Paper extends BaseEntity implements UserContainer {
this.locked = locked;
}
public FileData getFileData() {
return fileData;
public List<FileData> getFiles() {
return files;
}
public void setFileData(FileData fileData) {
this.fileData = fileData;
public void setFiles(List<FileData> files) {
this.files = files;
}
public String getTitle() {

View File

@ -79,9 +79,9 @@ public class PaperDto {
this.comment = paper.getComment();
this.locked = paper.getLocked();
this.tmpFileName = null;
this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId();
this.fileName = paper.getFileData() == null ? null : paper.getFileData().getName();
this.fileCreateDate = paper.getFileData() == null ? null : paper.getFileData().getCreateDate();
// this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId();
// this.fileName = paper.getFileData() == null ? null : paper.getFileData().getName();
// this.fileCreateDate = paper.getFileData() == null ? null : paper.getFileData().getCreateDate();
this.authorIds = convert(paper.getAuthors(), user -> user.getId());
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 java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import static org.springframework.util.ObjectUtils.isEmpty;
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.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;
import static ru.ulstu.paper.model.Paper.PaperStatus.*;
@Service
public class PaperService {
@ -88,9 +80,9 @@ public class PaperService {
paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(new Date());
paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines()));
if (paperDto.getTmpFileName() != null) {
paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
}
// if (paperDto.getTmpFileName() != null) {
// paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
// }
paper.getAuthors().clear();
if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) {
paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds)));
@ -103,9 +95,9 @@ public class PaperService {
Paper paper = paperRepository.findOne(paperDto.getId());
Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors());
if (paperDto.getTmpFileName() != null && paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData());
}
// if (paperDto.getTmpFileName() != null && paper.getFileData() != null) {
// fileService.deleteFile(paper.getFileData());
// }
paperRepository.save(copyFromDto(paper, paperDto));
paper.getAuthors().forEach(author -> {
@ -124,9 +116,9 @@ public class PaperService {
@Transactional
public void delete(Integer paperId) throws IOException {
Paper paper = paperRepository.findOne(paperId);
if (paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData());
}
// if (paper.getFileData() != null) {
// fileService.deleteFile(paper.getFileData());
// }
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-20181208_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"/>
</databaseChangeLog>