fix saving paper

pull/244/head
Anton Romanov 5 years ago
parent c966a100da
commit 7c0a8be51b

@ -0,0 +1,69 @@
package ru.ulstu.boundary.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import ru.ulstu.user.model.User;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
public class ScienceGroupMemberDto {
private Integer id;
@NotBlank
@Size(min = 2, max = 50)
private String firstName;
@NotBlank
@Size(min = 2, max = 50)
private String lastName;
@JsonCreator
public ScienceGroupMemberDto(@JsonProperty("id") Integer id,
@JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public ScienceGroupMemberDto(User user) {
this.id = user.getId();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "ScienceGroupMember{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}

@ -1,6 +1,6 @@
package ru.ulstu.paper.model; package ru.ulstu.paper.model;
import ru.ulstu.user.model.UserDto; import ru.ulstu.boundary.model.ScienceGroupMemberDto;
import java.util.Set; import java.util.Set;
@ -11,14 +11,14 @@ public class PaperDashboardDto {
private Integer id; private Integer id;
private String title; private String title;
private Paper.PaperStatus status; private Paper.PaperStatus status;
private Set<UserDto> authors; private Set<ScienceGroupMemberDto> authors;
private String url; private String url;
public PaperDashboardDto(Paper paper) { public PaperDashboardDto(Paper paper) {
this.id = paper.getId(); this.id = paper.getId();
this.title = paper.getTitle(); this.title = paper.getTitle();
this.status = paper.getStatus(); this.status = paper.getStatus();
this.authors = convert(paper.getAuthors(), UserDto::new); this.authors = convert(paper.getAuthors(), ScienceGroupMemberDto::new);
this.url = paper.getUrl(); this.url = paper.getUrl();
} }
@ -46,11 +46,11 @@ public class PaperDashboardDto {
this.status = status; this.status = status;
} }
public Set<UserDto> getAuthors() { public Set<ScienceGroupMemberDto> getAuthors() {
return authors; return authors;
} }
public void setAuthors(Set<UserDto> authors) { public void setAuthors(Set<ScienceGroupMemberDto> authors) {
this.authors = authors; this.authors = authors;
} }

@ -3,9 +3,9 @@ package ru.ulstu.paper.model;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import ru.ulstu.boundary.model.ActivityDto; import ru.ulstu.boundary.model.ActivityDto;
import ru.ulstu.boundary.model.ScienceGroupMemberDto;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.user.model.UserDto;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@ -31,7 +31,7 @@ public class PaperDto implements ActivityDto {
private String url; private String url;
private Boolean locked; private Boolean locked;
private List<FileDataDto> files = new ArrayList<>(); private List<FileDataDto> files = new ArrayList<>();
private Set<UserDto> authors; private Set<ScienceGroupMemberDto> authors;
public String getLatexText() { public String getLatexText() {
return latexText; return latexText;
@ -51,7 +51,7 @@ public class PaperDto implements ActivityDto {
@JsonProperty("url") String url, @JsonProperty("url") String url,
@JsonProperty("locked") Boolean locked, @JsonProperty("locked") Boolean locked,
@JsonProperty("files") List<FileDataDto> files, @JsonProperty("files") List<FileDataDto> files,
@JsonProperty("authors") Set<UserDto> authors) { @JsonProperty("authors") Set<ScienceGroupMemberDto> authors) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
@ -78,7 +78,7 @@ public class PaperDto implements ActivityDto {
this.url = paper.getUrl(); this.url = paper.getUrl();
this.locked = paper.getLocked(); this.locked = paper.getLocked();
this.files = convert(paper.getFiles(), FileDataDto::new); this.files = convert(paper.getFiles(), FileDataDto::new);
this.authors = convert(paper.getAuthors(), UserDto::new); this.authors = convert(paper.getAuthors(), ScienceGroupMemberDto::new);
} }
public Integer getId() { public Integer getId() {
@ -161,11 +161,11 @@ public class PaperDto implements ActivityDto {
this.files = files; this.files = files;
} }
public Set<UserDto> getAuthors() { public Set<ScienceGroupMemberDto> getAuthors() {
return authors; return authors;
} }
public void setAuthors(Set<UserDto> authors) { public void setAuthors(Set<ScienceGroupMemberDto> authors) {
this.authors = authors; this.authors = authors;
} }

@ -1,7 +1,7 @@
package ru.ulstu.paper.model; package ru.ulstu.paper.model;
import ru.ulstu.boundary.model.ActivityListDto; import ru.ulstu.boundary.model.ActivityListDto;
import ru.ulstu.user.model.UserDto; import ru.ulstu.boundary.model.ScienceGroupMemberDto;
import java.util.Set; import java.util.Set;
@ -12,17 +12,13 @@ public class PaperListDto implements ActivityListDto {
private Integer id; private Integer id;
private String title; private String title;
private Paper.PaperStatus status; private Paper.PaperStatus status;
private Set<UserDto> authors; private Set<ScienceGroupMemberDto> authors;
public PaperListDto(Paper paper) { public PaperListDto(Paper paper) {
this.id = paper.getId(); this.id = paper.getId();
this.title = paper.getTitle(); this.title = paper.getTitle();
this.status = paper.getStatus(); this.status = paper.getStatus();
this.authors = convert(paper.getAuthors(), UserDto::new); this.authors = convert(paper.getAuthors(), ScienceGroupMemberDto::new);
}
public static PaperListDto createFromEntity(Paper entity) {
return new PaperListDto(entity);
} }
public Integer getId() { public Integer getId() {
@ -49,11 +45,11 @@ public class PaperListDto implements ActivityListDto {
this.status = status; this.status = status;
} }
public Set<UserDto> getAuthors() { public Set<ScienceGroupMemberDto> getAuthors() {
return authors; return authors;
} }
public void setAuthors(Set<UserDto> authors) { public void setAuthors(Set<ScienceGroupMemberDto> authors) {
this.authors = authors; this.authors = authors;
} }
} }

@ -2,15 +2,15 @@ package ru.ulstu.paper.repository;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import ru.ulstu.core.repository.JpaDetachableRepository;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import java.util.List; import java.util.List;
public interface PaperRepository extends JpaRepository<Paper, Integer> { public interface PaperRepository extends JpaDetachableRepository<Paper, Integer> {
@Query("SELECT p FROM Paper p WHERE (:author IS NULL OR :author MEMBER OF p.authors) AND (YEAR(p.createDate) = :year OR :year IS NULL)") @Query("SELECT p FROM Paper p WHERE (:author IS NULL OR :author MEMBER OF p.authors) AND (YEAR(p.createDate) = :year OR :year IS NULL)")
List<Paper> filter(@Param("author") User author, @Param("year") Integer year); List<Paper> filter(@Param("author") User author, @Param("year") Integer year);

@ -44,7 +44,6 @@ import static ru.ulstu.paper.model.Paper.PaperStatus.ON_PREPARATION;
import static ru.ulstu.paper.model.Paper.PaperType.OTHER; import static ru.ulstu.paper.model.Paper.PaperType.OTHER;
@Service @Service
@Transactional
public class PaperService extends AbstractActivityService<Paper, PaperDto, PaperListDto> { public class PaperService extends AbstractActivityService<Paper, PaperDto, PaperListDto> {
private final PaperNotificationService paperNotificationService; private final PaperNotificationService paperNotificationService;
private final PaperRepository paperRepository; private final PaperRepository paperRepository;
@ -100,7 +99,6 @@ public class PaperService extends AbstractActivityService<Paper, PaperDto, Paper
return newPaper; return newPaper;
} }
@Transactional
public Paper update(Paper paper) { public Paper update(Paper paper) {
paperRepository.save(paper); paperRepository.save(paper);
paperNotificationService.sendCreateNotification(paper, oldAuthors); paperNotificationService.sendCreateNotification(paper, oldAuthors);
@ -115,8 +113,7 @@ public class PaperService extends AbstractActivityService<Paper, PaperDto, Paper
@Transactional @Transactional
public PaperDto update(PaperDto paperDto) { public PaperDto update(PaperDto paperDto) {
Paper paper = paperRepository.findById(paperDto.getId()) Paper paper = findPaperById(paperDto.getId());
.orElseThrow(() -> new EntityNotFoundException("Paper with id=" + paperDto.getId() + " not found"));
oldStatus = paper.getStatus(); oldStatus = paper.getStatus();
oldAuthors = new HashSet<>(paper.getAuthors()); oldAuthors = new HashSet<>(paper.getAuthors());
@ -151,6 +148,7 @@ public class PaperService extends AbstractActivityService<Paper, PaperDto, Paper
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
paper.getAuthors().clear();
if (isNotEmpty(paperDto.getAuthors())) { if (isNotEmpty(paperDto.getAuthors())) {
paperDto.getAuthors().forEach(authors -> paper.getAuthors().add(userService.findById(authors.getId()))); paperDto.getAuthors().forEach(authors -> paper.getAuthors().add(userService.findById(authors.getId())));
} }
@ -224,7 +222,7 @@ public class PaperService extends AbstractActivityService<Paper, PaperDto, Paper
}); });
} }
public void save(PaperDto paperDto) throws IOException { public void save(PaperDto paperDto) {
if (isEmpty(paperDto.getId())) { if (isEmpty(paperDto.getId())) {
create(paperDto); create(paperDto);
} else { } else {
@ -233,11 +231,12 @@ public class PaperService extends AbstractActivityService<Paper, PaperDto, Paper
} }
public PaperDto findById(Integer paperId) { public PaperDto findById(Integer paperId) {
return new PaperDto(paperRepository.getOne(paperId)); return new PaperDto(findPaperById(paperId));
} }
public Paper findPaperById(Integer paperId) { public Paper findPaperById(Integer paperId) {
return paperRepository.getOne(paperId); return paperRepository.findById(paperId)
.orElseThrow(() -> new EntityNotFoundException("Paper with id=" + paperId + " not found"));
} }
public List<Paper> findAllNotSelect(List<Integer> paperIds) { public List<Paper> findAllNotSelect(List<Integer> paperIds) {

@ -15,7 +15,7 @@
<p v-else> <p v-else>
<i class="fa fa-fw fa-2x" aria-hidden="true"></i> <i class="fa fa-fw fa-2x" aria-hidden="true"></i>
</p> </p>
<router-link :to="{ path: 'papers/paper', query: { id: paper.id }}" class="portfolio-link"> <router-link :to="{ path: '/papers/paper', query: { id: paper.id }}" class="portfolio-link">
<span class="h6">{{ paper.title }}</span> <span class="h6">{{ paper.title }}</span>
</router-link> </router-link>
<p class="text-muted"> {{ getAuthors }}</p> <p class="text-muted"> {{ getAuthors }}</p>

@ -32,14 +32,14 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="type">Тип статьи:</label> <label for="type">Тип статьи:</label>
<select class="form-control" id="type" :value="paper.type"> <select class="form-control" id="type" v-model="paper.type">
<option v-for="type in allTypes" :value="type.id">{{ type.name }}</option> <option v-for="type in allTypes" :value="type.id">{{ type.name }}</option>
</select> </select>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="status">Статус:</label> <label for="status">Статус:</label>
<select class="form-control" id="status" :value="paper.status"> <select class="form-control" id="status" v-model="paper.status">
<option v-for="status in allStatuses" :value="status.id">{{status.name}} <option v-for="status in allStatuses" :value="status.id">{{status.name}}
</option> </option>
</select> </select>
@ -49,13 +49,24 @@
<div class="form-group"> <div class="form-group">
<label for="comment">Комментарий:</label> <label for="comment">Комментарий:</label>
<textarea class="form-control" rows="5" id="comment" <textarea class="form-control" rows="5" id="comment"
:value="paper.comment"></textarea> v-model="paper.comment"></textarea>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="title">Ссылка на сайт конференции:</label> <label for="title">Ссылка на сайт конференции:</label>
<input class="form-control" id="url" type="text" placeholder="Url" <div class="row">
:value="paper.url"/> <div class="col-10">
<input class="form-control" id="url" type="text" placeholder="Url"
v-model="paper.url"/>
</div>
<div class="col-2">
<p v-if="hasUrl" class="float-right">
<a target="_blank" v-bind:href="paper.url"><i
class="fa fa-external-link fa-1x"
aria-hidden="true"></i></a>
</p>
</div>
</div>
</div> </div>
<div v-for="deadline of paper.deadlines"> <div v-for="deadline of paper.deadlines">
@ -168,6 +179,9 @@
} }
this.$store.dispatch("addSuccessMessage", "Статья успешно сохранена!"); this.$store.dispatch("addSuccessMessage", "Статья успешно сохранена!");
event.preventDefault(); event.preventDefault();
},
hasUrl: function () {
return this.paper.url !== null && this.paper.url !== '';
} }
}, },
mounted: function () { mounted: function () {

Loading…
Cancel
Save