diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index aa871ba..528975c 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -2,10 +2,28 @@ package ru.ulstu.conference.controller; import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.Errors; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import ru.ulstu.conference.model.ConferenceDto; +import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.service.ConferenceService; import springfox.documentation.annotations.ApiIgnore; +import javax.validation.Valid; +import java.io.IOException; +import java.util.stream.Collectors; + +import static org.springframework.util.StringUtils.isEmpty; +import static ru.ulstu.core.controller.Navigation.CONFERENCES_PAGE; +import static ru.ulstu.core.controller.Navigation.CONFERENCE_PAGE; +import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; +import static ru.ulstu.core.controller.Navigation.hasErrors; + + @Controller() @RequestMapping(value = "/conferences") @ApiIgnore @@ -13,7 +31,39 @@ public class ConferenceController { private final ConferenceService conferenceService; - public ConferenceController(ConferenceService paperService) { - this.conferenceService = paperService; + public ConferenceController(ConferenceService conferenceService) { + this.conferenceService = conferenceService; + } + + @GetMapping("/conferences") + public void getConferences(ModelMap modelMap) { + modelMap.put("filteredConferences", new ConferenceFilterDto(conferenceService.findAllDto())); + } + + @GetMapping("/conference") + public void getConference(ModelMap modelMap, @RequestParam(value = "id") Integer id) { + if (id != null && id > 0) { + modelMap.put("conferenceDto", conferenceService.findOneDto(id)); + } else { + modelMap.put("conferenceDto", new ConferenceDto()); + } + } + + @PostMapping(value = "/conference", params = "save") + public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { + filterEmptyDeadlines(conferenceDto); + if (conferenceDto.getDeadlines().isEmpty()) { + errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); + } + hasErrors(errors, CONFERENCE_PAGE); + conferenceService.save(conferenceDto); + return String.format(REDIRECT_TO, CONFERENCES_PAGE); + + } + + private void filterEmptyDeadlines(ConferenceDto conferenceDto) { + conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() + .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) + .collect(Collectors.toList())); } } diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java index b2851ed..1c824b3 100644 --- a/src/main/java/ru/ulstu/conference/model/Conference.java +++ b/src/main/java/ru/ulstu/conference/model/Conference.java @@ -1,11 +1,143 @@ package ru.ulstu.conference.model; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; +import org.hibernate.validator.constraints.NotBlank; +import org.springframework.format.annotation.DateTimeFormat; import ru.ulstu.core.model.BaseEntity; +import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.paper.model.Paper; +import ru.ulstu.user.model.User; +import javax.persistence.CascadeType; +import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; +import javax.persistence.OrderBy; import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; @Entity @Table(name = "conference") public class Conference extends BaseEntity { + + @NotBlank + private String title; + + private String description; + + private String url; + + private int ping; + + @Column(name = "begin_date") + @Temporal(TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date beginDate; + + @Column(name = "end_date") + @Temporal(TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date endDate; + + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @JoinColumn(name = "conference_id", unique = true) + @Fetch(FetchMode.SUBSELECT) + @OrderBy("date") + private List deadlines = new ArrayList<>(); + + + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name = "paper_conference", + joinColumns = {@JoinColumn(name = "conference_id")}, + inverseJoinColumns = {@JoinColumn(name = "paper_id")}) + private Set papers = new HashSet<>(); + + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name = "users_conference", + joinColumns = {@JoinColumn(name = "conference_id")}, + inverseJoinColumns = {@JoinColumn(name = "users_id")}) + private Set users = new HashSet<>(); + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public int getPing() { + return ping; + } + + public void setPing(int ping) { + this.ping = ping; + } + + public Date getBeginDate() { + return beginDate; + } + + public void setBeginDate(Date beginDate) { + this.beginDate = beginDate; + } + + public Date getEndDate() { + return endDate; + } + + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + + public List getDeadlines() { + return deadlines; + } + + public void setDeadlines(List deadlines) { + this.deadlines = deadlines; + } + + public Set getPapers() { + return papers; + } + + public void setPapers(Set papers) { + this.papers = papers; + } + + public Set getUsers() { + return users; + } + + public void setUsers(Set users) { + this.users = users; + } } diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java index e3f757e..277e783 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java @@ -1,4 +1,197 @@ package ru.ulstu.conference.model; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.hibernate.validator.constraints.NotEmpty; +import org.springframework.format.annotation.DateTimeFormat; +import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.paper.model.PaperDto; +import ru.ulstu.user.model.UserDto; + +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.validation.constraints.Size; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static ru.ulstu.core.util.StreamApiUtils.convert; + public class ConferenceDto { + + private Integer id; + @NotEmpty + @Size(min = 2, max = 400) + private String title; + @Size(max = 500) + private String description = ""; + @Size(max = 255) + private String url = ""; + private int ping = 0; + @Temporal(TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date beginDate = new Date(); + @Temporal(TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date endDate = new Date(); + @NotEmpty + private List deadlines = new ArrayList<>(); + private Set userIds = new HashSet<>(); + private Set paperIds = new HashSet<>(); + private Set papers = new HashSet<>(); + private Set users = new HashSet<>(); + private Integer filterUserId; + + public ConferenceDto() { + deadlines.add(new Deadline()); + } + + @JsonCreator + public ConferenceDto(@JsonProperty("id") Integer id, + @JsonProperty("title") String title, + @JsonProperty("description") String description, + @JsonProperty("url") String url, + @JsonProperty("ping") Integer ping, + @JsonProperty("beginDate") Date beginDate, + @JsonProperty("endDate") Date endDate, + @JsonProperty("deadlines") List deadlines, + @JsonProperty("userIds") Set userIds, + @JsonProperty("paperIds") Set paperIds, + @JsonProperty("users") Set users, + @JsonProperty("papers") Set papers) { + this.id = id; + this.title = title; + this.description = description; + this.url = url; + this.ping = ping; + this.beginDate = beginDate; + this.endDate = endDate; + this.deadlines = deadlines; + this.userIds = userIds; + this.paperIds = paperIds; + this.users = users; + this.papers = papers; + } + + public ConferenceDto(Conference conference) { + this.id = conference.getId(); + this.title = conference.getTitle(); + this.description = conference.getDescription(); + this.url = conference.getUrl(); + this.ping = conference.getPing(); + this.beginDate = conference.getBeginDate(); + this.endDate = conference.getEndDate(); + this.deadlines = conference.getDeadlines(); + this.userIds = convert(conference.getUsers(), user -> user.getId()); + this.paperIds = convert(conference.getPapers(), paper -> paper.getId()); + this.users = convert(conference.getUsers(), UserDto::new); + this.papers = convert(conference.getPapers(), PaperDto::new); + + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public int getPing() { + return ping; + } + + public void setPing(int ping) { + this.ping = ping; + } + + public Date getBeginDate() { + return beginDate; + } + + public void setBeginDate(Date beginDate) { + this.beginDate = beginDate; + } + + public Date getEndDate() { + return endDate; + } + + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + + public List getDeadlines() { + return deadlines; + } + + public void setDeadlines(List deadlines) { + this.deadlines = deadlines; + } + + public Set getUserIds() { + return userIds; + } + + public void setUserIds(Set userIds) { + this.userIds = userIds; + } + + public Set getPaperIds() { + return paperIds; + } + + public void setPaperIds(Set paperIds) { + this.paperIds = paperIds; + } + + public Set getPapers() { + return papers; + } + + public void setPapers(Set papers) { + this.papers = papers; + } + + public Set getUsers() { + return users; + } + + public void setUsers(Set users) { + this.users = users; + } + + public Integer getFilterUserId() { + return filterUserId; + } + + public void setFilterUserId(Integer filterUserId) { + this.filterUserId = filterUserId; + } } diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceFilterDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceFilterDto.java index 379c8b6..37878b2 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceFilterDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceFilterDto.java @@ -1,4 +1,47 @@ package ru.ulstu.conference.model; +import java.util.List; + public class ConferenceFilterDto { + + private List conferences; + private Integer filterAuthorId; + private Integer year; + + public ConferenceFilterDto() { + } + + public ConferenceFilterDto(List conferenceDtos, Integer filterAuthorId, Integer year) { + this.conferences = conferenceDtos; + this.filterAuthorId = filterAuthorId; + this.year = year; + } + + public ConferenceFilterDto(List conferenceDtos) { + this(conferenceDtos, null, null); + } + + public List getConferences() { + return conferences; + } + + public void setConferences(List conferences) { + this.conferences = conferences; + } + + public Integer getFilterAuthorId() { + return filterAuthorId; + } + + public void setFilterAuthorId(Integer filterAuthorId) { + this.filterAuthorId = filterAuthorId; + } + + public Integer getYear() { + return year; + } + + public void setYear(Integer year) { + this.year = year; + } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index a8147c9..c28c637 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -1,11 +1,23 @@ package ru.ulstu.conference.service; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.conference.model.Conference; +import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.conference.repository.ConferenceRepository; import ru.ulstu.deadline.service.DeadlineService; +import java.io.IOException; +import java.util.List; + +import static org.springframework.util.ObjectUtils.isEmpty; +import static ru.ulstu.core.util.StreamApiUtils.convert; + @Service public class ConferenceService { + private final static int MAX_DISPLAY_SIZE = 40; + private final ConferenceRepository conferenceRepository; private final DeadlineService deadlineService; @@ -14,4 +26,59 @@ public class ConferenceService { this.conferenceRepository = conferenceRepository; this.deadlineService = deadlineService; } + + public List findAll() { + return conferenceRepository.findAll(); + } + + public List findAllDto() { + List conferences = convert(findAll(), ConferenceDto::new); + conferences.forEach(conferenceDto -> conferenceDto.setTitle(StringUtils.abbreviate(conferenceDto.getTitle(), MAX_DISPLAY_SIZE))); + return conferences; + } + + public ConferenceDto findOneDto(Integer id) { + return new ConferenceDto(conferenceRepository.findOne(id)); + } + + public void save(ConferenceDto conferenceDto) throws IOException { + if (isEmpty(conferenceDto.getId())) { + create(conferenceDto); + } else { + update(conferenceDto); + } + } + + @Transactional + public Integer create(ConferenceDto conferenceDto) throws IOException { + Conference newConference = copyFromDto(new Conference(), conferenceDto); + newConference = conferenceRepository.save(newConference); + + + return newConference.getId(); + } + + @Transactional + public Integer update(ConferenceDto conferenceDto) throws IOException { + Conference conference = conferenceRepository.findOne(conferenceDto.getId()); + + conferenceRepository.save(copyFromDto(conference, conferenceDto)); + + return conference.getId(); + } + + private Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException { + conference.setTitle(conferenceDto.getTitle()); + conference.setDescription(conferenceDto.getDescription()); + conference.setUrl(conferenceDto.getUrl()); + conference.setPing(0); + conference.setBeginDate(conferenceDto.getBeginDate()); + conference.setEndDate(conferenceDto.getEndDate()); + + conference.setDeadlines(deadlineService.saveOrCreate(conferenceDto.getDeadlines())); + + return conference; + } + + } diff --git a/src/main/java/ru/ulstu/grant/controller/Navigation.java b/src/main/java/ru/ulstu/core/controller/Navigation.java similarity index 69% rename from src/main/java/ru/ulstu/grant/controller/Navigation.java rename to src/main/java/ru/ulstu/core/controller/Navigation.java index bf1626f..caea429 100644 --- a/src/main/java/ru/ulstu/grant/controller/Navigation.java +++ b/src/main/java/ru/ulstu/core/controller/Navigation.java @@ -1,4 +1,4 @@ -package ru.ulstu.grant.controller; +package ru.ulstu.core.controller; import org.springframework.validation.Errors; @@ -7,6 +7,9 @@ public class Navigation { public static final String GRANTS_PAGE = "/grants/grants"; public static final String GRANT_PAGE = "/grants/grant"; + public static final String CONFERENCES_PAGE = "/conferences/conferences"; + public static final String CONFERENCE_PAGE = "/conferences/conference"; + public static String hasErrors(Errors errors, String page) { if (errors.hasErrors()) { return page; diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 65b11dc..a7dc948 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -21,10 +21,10 @@ import java.util.List; import java.util.stream.Collectors; import static org.springframework.util.StringUtils.isEmpty; -import static ru.ulstu.grant.controller.Navigation.GRANTS_PAGE; -import static ru.ulstu.grant.controller.Navigation.GRANT_PAGE; -import static ru.ulstu.grant.controller.Navigation.REDIRECT_TO; -import static ru.ulstu.grant.controller.Navigation.hasErrors; +import static ru.ulstu.core.controller.Navigation.GRANTS_PAGE; +import static ru.ulstu.core.controller.Navigation.GRANT_PAGE; +import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; +import static ru.ulstu.core.controller.Navigation.hasErrors; @Controller() diff --git a/src/main/resources/templates/conferences/conference.html b/src/main/resources/templates/conferences/conference.html index 59545b5..3330bed 100644 --- a/src/main/resources/templates/conferences/conference.html +++ b/src/main/resources/templates/conferences/conference.html @@ -18,44 +18,54 @@
-
+
- +
-
-
-
- -
+
+ - + placeholder="Описание" + th:field="*{deadlines[__${rowStat.index}__].description}"/> + Удалить + alt="Удалить" + th:onclick="|$('#deadlines${rowStat.index}\\.description').val(''); + $('#deadlines${rowStat.index}\\.date').val(''); + $('#addDeadline').click();|"/>
+

Incorrect title

- +
- +
diff --git a/src/main/resources/templates/conferences/conferences.html b/src/main/resources/templates/conferences/conferences.html index c725f2b..fa88aaf 100644 --- a/src/main/resources/templates/conferences/conferences.html +++ b/src/main/resources/templates/conferences/conferences.html @@ -7,7 +7,7 @@
- +
@@ -19,7 +19,9 @@
- + +
+
diff --git a/src/main/resources/templates/conferences/fragments/confLineFragment.html b/src/main/resources/templates/conferences/fragments/confLineFragment.html index f54a777..317da03 100644 --- a/src/main/resources/templates/conferences/fragments/confLineFragment.html +++ b/src/main/resources/templates/conferences/fragments/confLineFragment.html @@ -6,9 +6,8 @@