partially implements api

pull/244/head
Anton Romanov 5 years ago
parent 119f5a52ab
commit 6cf627bf02

@ -1,4 +1,19 @@
package ru.ulstu.activity.api.model;
public interface ActivityDashboardDto {
public abstract class ActivityDashboardDto {
private final Integer id;
private final String title;
protected ActivityDashboardDto(Integer id, String title) {
this.id = id;
this.title = title;
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
}

@ -1,4 +1,13 @@
package ru.ulstu.activity.api.model;
public interface ActivityDto {
public abstract class ActivityDto {
private final Integer id;
public ActivityDto(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}

@ -19,7 +19,7 @@ import java.util.Objects;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class ConferenceDto implements ActivityDto {
public class ConferenceDto extends ActivityDto {
private final static String BEGIN_DATE = "Начало: ";
private final static String END_DATE = "Конец: ";
@ -48,9 +48,6 @@ public class ConferenceDto implements ActivityDto {
private List<ConferenceUser> users = new ArrayList<>();
private boolean disabledTakePart = false;
public ConferenceDto() {
}
@JsonCreator
public ConferenceDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title,
@ -66,8 +63,7 @@ public class ConferenceDto implements ActivityDto {
@JsonProperty("papers") List<Paper> papers,
@JsonProperty("notSelectedPapers") List<Paper> notSelectedPapers,
@JsonProperty("notSelectedPapers") Boolean disabledTakePart) {
this.id = id;
this.title = title;
super(id);
this.description = description;
this.url = url;
this.ping = ping;
@ -83,7 +79,7 @@ public class ConferenceDto implements ActivityDto {
}
public ConferenceDto(Conference conference) {
this.id = conference.getId();
super(conference.getId());
this.title = conference.getTitle();
this.description = conference.getDescription();
this.url = conference.getUrl();
@ -97,6 +93,10 @@ public class ConferenceDto implements ActivityDto {
this.papers = conference.getPapers();
}
public ConferenceDto() {
super(null);
}
public Integer getId() {
return id;
}

@ -24,7 +24,6 @@ import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
@ -72,12 +71,6 @@ public class ConferenceService extends AbstractActivityService<ConferenceListDto
return conferenceDto;
}
public ConferenceDto getNewConference() {
ConferenceDto conferenceDto = new ConferenceDto();
conferenceDto.setNotSelectedPapers(getNotSelectPapers(new ArrayList<>()));
return conferenceDto;
}
public PageableItems<Conference> findAll(int offset, int count) {
final Page<Conference> page = conferenceRepository.findAll(new OffsetablePageRequest(offset, count));
return new PageableItems<>(page.getTotalElements(), page.getContent());

@ -0,0 +1,82 @@
package ru.ulstu.activity.grant.controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.activity.api.ActivityController;
import ru.ulstu.activity.grant.model.GrantDashboardDto;
import ru.ulstu.activity.grant.model.GrantDto;
import ru.ulstu.activity.grant.model.GrantListDto;
import ru.ulstu.activity.grant.service.GrantService;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.PageableItems;
import ru.ulstu.core.model.response.Response;
import javax.validation.Valid;
import java.io.IOException;
import java.text.ParseException;
@RestController
@RequestMapping(Constants.API_1_0 + "grants")
public class GrantController implements ActivityController<GrantListDto, GrantDashboardDto, GrantDto> {
private final GrantService grantService;
public GrantController(GrantService grantService) {
this.grantService = grantService;
}
@GetMapping("/grab")
public void grab() throws IOException, ParseException {
grantService.createFromKias();
}
@Override
@GetMapping("list")
public Response<PageableItems<GrantListDto>> getList(@RequestParam(value = "offset", defaultValue = "0") int offset,
@RequestParam(value = "count", defaultValue = "0") int count) {
return new Response<>(grantService.findAllDto(offset, count));
}
@Override
@GetMapping("dashboard")
public Response<PageableItems<GrantDashboardDto>> getDashboard(@RequestParam(value = "offset", defaultValue = "0") int offset,
@RequestParam(value = "count", defaultValue = "0") int count) {
return new Response<>(grantService.findAllActiveDto(offset, count));
}
@Override
@GetMapping
public Response<GrantDto> get(@PathVariable("grant-id") Integer entityId) {
return new Response<>(grantService.findById(entityId));
}
@Override
@PostMapping
public Response<GrantDto> create(@RequestBody @Valid GrantDto entity) {
return new Response<>(grantService.create(entity));
}
@Override
@PutMapping
public Response<GrantDto> update(@RequestBody @Valid GrantDto entity) {
return new Response<>(grantService.update(entity));
}
@Override
@DeleteMapping
public Response<Boolean> delete(@PathVariable("grant-id") Integer entityId) {
return new Response<>(grantService.delete(entityId));
}
@Override
@PostMapping("ping/{paper-id}")
public void ping(@PathVariable("paper-id") int entityId) {
grantService.ping(entityId);
}
}

@ -1,25 +0,0 @@
package ru.ulstu.activity.grant.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.activity.grant.service.GrantService;
import ru.ulstu.configuration.Constants;
import java.io.IOException;
import java.text.ParseException;
@RestController
@RequestMapping(Constants.API_1_0 + "grants")
public class GrantRestController {
private final GrantService grantService;
public GrantRestController(GrantService grantService) {
this.grantService = grantService;
}
@GetMapping("/grab")
public void grab() throws IOException, ParseException {
grantService.createFromKias();
}
}

@ -0,0 +1,27 @@
package ru.ulstu.activity.grant.model;
import ru.ulstu.activity.api.model.ActivityDashboardDto;
import ru.ulstu.activity.common.model.ScienceGroupMemberDto;
import java.util.Set;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class GrantDashboardDto extends ActivityDashboardDto {
private final Grant.GrantStatus status;
private final Set<ScienceGroupMemberDto> members;
public GrantDashboardDto(Grant grant) {
super(grant.getId(), grant.getTitle());
this.status = grant.getStatus();
this.members = convert(grant.getActivityMembers(), ScienceGroupMemberDto::new);
}
public Grant.GrantStatus getStatus() {
return status;
}
public Set<ScienceGroupMemberDto> getAuthors() {
return members;
}
}

@ -2,7 +2,6 @@ package ru.ulstu.activity.grant.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import ru.ulstu.activity.api.model.ActivityDto;
import ru.ulstu.activity.common.model.ScienceGroupMemberDto;
import ru.ulstu.activity.deadline.model.Deadline;
@ -15,14 +14,10 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class GrantDto implements ActivityDto {
private final static int MAX_AUTHORS_LENGTH = 60;
private Integer id;
public class GrantDto extends ActivityDto {
@NotEmpty
private String title;
private Grant.GrantStatus status;
@ -42,10 +37,6 @@ public class GrantDto implements ActivityDto {
private List<PaperDto> papers = new ArrayList<>();
private List<Integer> removedDeadlineIds = new ArrayList<>();
public GrantDto() {
deadlines.add(new Deadline());
}
@JsonCreator
public GrantDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title,
@ -62,7 +53,7 @@ public class GrantDto implements ActivityDto {
@JsonProperty("hasDegree") boolean hasDegree,
@JsonProperty("paperIds") List<Integer> paperIds,
@JsonProperty("papers") List<PaperDto> papers) {
this.id = id;
super(id);
this.title = title;
this.status = status;
this.deadlines = deadlines;
@ -80,7 +71,7 @@ public class GrantDto implements ActivityDto {
}
public GrantDto(Grant grant) {
this.id = grant.getId();
super(grant.getId());
this.title = grant.getTitle();
this.status = grant.getStatus();
this.deadlines = grant.getDeadlines();
@ -98,17 +89,14 @@ public class GrantDto implements ActivityDto {
}
public GrantDto(String grantTitle, Date deadLineDate) {
super(null);
this.title = grantTitle;
deadlines.add(new Deadline(deadLineDate, "Окончание приёма заявок"));
status = Grant.GrantStatus.LOADED_FROM_KIAS;
this.deadlines.add(new Deadline(deadLineDate, "Окончание приёма заявок"));
this.status = Grant.GrantStatus.LOADED_FROM_KIAS;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
public GrantDto() {
super(null);
}
public String getTitle() {
@ -175,13 +163,6 @@ public class GrantDto implements ActivityDto {
this.members = members;
}
public String getAuthorsString() {
return StringUtils.abbreviate(members
.stream()
.map(author -> author.getLastName())
.collect(Collectors.joining(", ")), MAX_AUTHORS_LENGTH);
}
public Integer getLeaderId() {
return leaderId;
}

@ -1,5 +1,7 @@
package ru.ulstu.activity.grant.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
@ -21,5 +23,5 @@ public interface GrantRepository extends JpaRepository<Grant, Integer>, Activity
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
@Query("SELECT g FROM Grant g WHERE (g.status <> 'SKIPPED') AND (g.status <> 'COMPLETED')")
List<Grant> findAllActive();
Page<Grant> findAllActive(Pageable pageable);
}

@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import java.io.IOException;
import java.text.ParseException;
import java.util.stream.Collectors;
@Service
public class GrantScheduler {
@ -27,7 +28,8 @@ public class GrantScheduler {
@Scheduled(cron = "0 0 8 * * MON", zone = "Europe/Samara")
public void checkDeadlineBeforeWeek() {
log.debug("GrantScheduler.checkDeadlineBeforeWeek started");
grantNotificationService.sendDeadlineNotifications(grantService.findAllActive(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
grantNotificationService.sendDeadlineNotifications(grantService.findAllActive(0, 100)
.getItems().stream().collect(Collectors.toList()), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
log.debug("GrantScheduler.checkDeadlineBeforeWeek finished");
}

@ -12,6 +12,7 @@ import ru.ulstu.activity.deadline.model.Deadline;
import ru.ulstu.activity.deadline.service.DeadlineService;
import ru.ulstu.activity.file.service.FileService;
import ru.ulstu.activity.grant.model.Grant;
import ru.ulstu.activity.grant.model.GrantDashboardDto;
import ru.ulstu.activity.grant.model.GrantDto;
import ru.ulstu.activity.grant.model.GrantListDto;
import ru.ulstu.activity.grant.repository.GrantRepository;
@ -41,7 +42,7 @@ import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.activity.grant.model.Grant.GrantStatus.APPLICATION;
import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
@Service
public class GrantService extends AbstractActivityService<GrantListDto, Grant, GrantDto> {
@ -82,7 +83,7 @@ public class GrantService extends AbstractActivityService<GrantListDto, Grant, G
}
public GrantDto getExistGrantById(Integer id) {
return new GrantDto(findById(id));
return new GrantDto(findGrantById(id));
}
@Override
@ -120,7 +121,7 @@ public class GrantService extends AbstractActivityService<GrantListDto, Grant, G
@Transactional
@Override
public GrantDto update(GrantDto grantDto) {
Grant grant = findById(grantDto.getId());
Grant grant = findGrantById(grantDto.getId());
return new GrantDto(update(grant));
}
@ -181,7 +182,7 @@ public class GrantService extends AbstractActivityService<GrantListDto, Grant, G
@Transactional
public boolean delete(Integer grantId) {
Grant grant = findById(grantId);
Grant grant = findGrantById(grantId);
if (grant != null) {
grantRepository.delete(grant);
return true;
@ -338,20 +339,26 @@ public class GrantService extends AbstractActivityService<GrantListDto, Grant, G
}
}
public List<GrantDto> findAllActiveDto() {
return convert(findAllActive(), GrantDto::new);
public PageableItems<GrantDashboardDto> findAllActiveDto(int offset, int count) {
return convertPageable(findAllActive(offset, count), GrantDashboardDto::new);
}
public List<Grant> findAllActive() {
return grantRepository.findAllActive();
public PageableItems<Grant> findAllActive(int offset, int count) {
Page<Grant> activeGrantsPage = grantRepository.findAllActive(new OffsetablePageRequest(offset, count));
return new PageableItems<>(activeGrantsPage.getTotalElements(), activeGrantsPage.getContent());
}
public GrantDto findById(Integer id) {
return new GrantDto(grantRepository.getOne(id));
}
public Grant findById(Integer id) {
public Grant findGrantById(Integer id) {
return grantRepository.getOne(id);
}
@Transactional
public void ping(int grantId) throws IOException {
pingService.addPing(findById(grantId));
public void ping(int grantId) {
pingService.addPing(findGrantById(grantId));
}
}

@ -7,38 +7,18 @@ import java.util.Set;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class PaperDashboardDto implements ActivityDashboardDto {
private Integer id;
private String title;
public class PaperDashboardDto extends ActivityDashboardDto {
private Paper.PaperStatus status;
private Set<ScienceGroupMemberDto> authors;
private String url;
public PaperDashboardDto(Paper paper) {
this.id = paper.getId();
this.title = paper.getTitle();
super(paper.getId(), paper.getTitle());
this.status = paper.getStatus();
this.authors = convert(paper.getAuthors(), ScienceGroupMemberDto::new);
this.url = paper.getUrl();
}
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 Paper.PaperStatus getStatus() {
return status;
}

@ -16,8 +16,7 @@ import java.util.Set;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class PaperDto implements ActivityDto {
private Integer id;
public class PaperDto extends ActivityDto {
@NotEmpty
@Size(min = 3, max = 254)
private String title;
@ -52,7 +51,7 @@ public class PaperDto implements ActivityDto {
@JsonProperty("locked") Boolean locked,
@JsonProperty("files") List<FileDataDto> files,
@JsonProperty("authors") Set<ScienceGroupMemberDto> authors) {
this.id = id;
super(id);
this.title = title;
this.status = status;
this.type = type;
@ -67,7 +66,7 @@ public class PaperDto implements ActivityDto {
}
public PaperDto(Paper paper) {
this.id = paper.getId();
super(paper.getId());
this.title = paper.getTitle();
this.status = paper.getStatus();
this.type = paper.getType();
@ -81,14 +80,6 @@ public class PaperDto implements ActivityDto {
this.authors = convert(paper.getAuthors(), ScienceGroupMemberDto::new);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}

@ -1,16 +0,0 @@
package ru.ulstu.activity.students.controller;
import org.springframework.validation.Errors;
class Navigation {
public static final String REDIRECT_TO = "redirect:%s";
public static final String TASKS_PAGE = "/students/tasks";
public static final String TASK_PAGE = "/students/task";
public static String hasErrors(Errors errors, String page) {
if (errors.hasErrors()) {
return page;
}
return null;
}
}

@ -1,36 +1,28 @@
package ru.ulstu.activity.students.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.activity.deadline.model.Deadline;
import ru.ulstu.activity.students.model.Task;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.activity.api.ActivityController;
import ru.ulstu.activity.students.model.TaskDashboardDto;
import ru.ulstu.activity.students.model.TaskDto;
import ru.ulstu.activity.students.model.TaskFilterDto;
import ru.ulstu.activity.students.model.TaskListDto;
import ru.ulstu.activity.students.service.TaskService;
import ru.ulstu.activity.tags.model.Tag;
import springfox.documentation.annotations.ApiIgnore;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.PageableItems;
import ru.ulstu.core.model.response.Response;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.util.StringUtils.isEmpty;
import static ru.ulstu.activity.students.controller.Navigation.REDIRECT_TO;
import static ru.ulstu.activity.students.controller.Navigation.TASKS_PAGE;
import static ru.ulstu.activity.students.controller.Navigation.TASK_PAGE;
@Controller()
@RequestMapping(value = "/students")
@ApiIgnore
public class TaskController {
@RestController
@RequestMapping(Constants.API_1_0 + "students")
public class TaskController implements ActivityController<TaskListDto, TaskDashboardDto, TaskDto> {
private final TaskService taskService;
@ -38,76 +30,40 @@ public class TaskController {
this.taskService = taskService;
}
@GetMapping("/dashboard")
public void getDashboard(ModelMap modelMap) {
modelMap.put("tasks", taskService.findAllDto());
}
@GetMapping("/tasks")
public void getTask(ModelMap modelMap) {
modelMap.put("filteredTasks", new TaskFilterDto(taskService.findAllDto(), null, null, null));
}
@GetMapping("/task")
public void getTask(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
if (id != null && id > 0) {
modelMap.put("taskDto", taskService.findOneDto(id));
} else {
modelMap.put("taskDto", new TaskDto());
}
}
@PostMapping("/tasks")
public void filterTasks(@Valid TaskFilterDto taskFilterDto, ModelMap modelMap) {
modelMap.put("filteredTasks", new TaskFilterDto(taskService.filter(taskFilterDto),
taskFilterDto.getStatus(),
taskFilterDto.getTag(),
taskFilterDto.getOrder()));
@GetMapping("list")
public Response<PageableItems<TaskListDto>> getList(@RequestParam(value = "offset", defaultValue = "0") int offset,
@RequestParam(value = "count", defaultValue = "10") int count) {
return new Response<>(taskService.findAllDto(offset, count));
}
@PostMapping(value = "/task", params = "save")
public String save(@Valid TaskDto taskDto, Errors errors) throws IOException {
filterEmptyDeadlines(taskDto);
if (taskDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
}
if (errors.hasErrors()) {
return TASK_PAGE;
}
taskService.save(taskDto);
return String.format(REDIRECT_TO, TASKS_PAGE);
@GetMapping("dashboard")
public Response<PageableItems<TaskDashboardDto>> getDashboard(@RequestParam(value = "offset", defaultValue = "0") int offset,
@RequestParam(value = "count", defaultValue = "10") int count) {
return new Response<>(taskService.findAllActiveDto(offset, count));
}
@PostMapping(value = "/task", params = "addDeadline")
public String addDeadline(@Valid TaskDto taskDto, Errors errors) {
filterEmptyDeadlines(taskDto);
if (errors.hasErrors()) {
return TASK_PAGE;
}
taskDto.getDeadlines().add(new Deadline());
return TASK_PAGE;
@GetMapping("{task-id}")
public Response<TaskDto> get(@PathVariable("task-id") Integer taskId) {
return new Response<>(taskService.findById(taskId));
}
@GetMapping("/delete/{task-id}")
public String delete(@PathVariable("task-id") Integer taskId) throws IOException {
taskService.delete(taskId);
return String.format(REDIRECT_TO, TASKS_PAGE);
@PostMapping
public Response<TaskDto> create(@RequestBody @Valid TaskDto taskDto) {
return new Response<>(taskService.create(taskDto));
}
@ModelAttribute("allStatuses")
public List<Task.TaskStatus> getTaskStatuses() {
return taskService.getTaskStatuses();
@PutMapping
public Response<TaskDto> update(@RequestBody @Valid TaskDto paperDto) {
return new Response<>(taskService.update(paperDto));
}
@ModelAttribute("allTags")
public List<Tag> getTags() {
return taskService.getTags();
@DeleteMapping("/{task-id}")
public Response<Boolean> delete(@PathVariable("task-id") Integer taskId) {
return new Response<>(taskService.delete(taskId));
}
private void filterEmptyDeadlines(TaskDto taskDto) {
taskDto.setDeadlines(taskDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
.collect(Collectors.toList()));
@PostMapping("ping/{task-id}")
public void ping(@PathVariable("task-id") int taskId) {
taskService.ping(taskId);
}
}

@ -2,11 +2,11 @@ package ru.ulstu.activity.students.model;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import ru.ulstu.activity.common.model.AbstractActivity;
import ru.ulstu.activity.common.model.EventSource;
import ru.ulstu.activity.deadline.model.Deadline;
import ru.ulstu.activity.tags.model.Tag;
import ru.ulstu.activity.timeline.model.Event;
import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.user.model.User;
import javax.persistence.CascadeType;
@ -27,9 +27,10 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
@Entity
public class Task extends BaseEntity implements EventSource {
public class Task extends AbstractActivity implements EventSource {
public enum TaskStatus {
IN_WORK("В работе"),
@ -99,6 +100,11 @@ public class Task extends BaseEntity implements EventSource {
this.title = title;
}
@Override
public Set<User> getActivityMembers() {
return Collections.emptySet();
}
public TaskStatus getStatus() {
return status;
}

@ -0,0 +1,16 @@
package ru.ulstu.activity.students.model;
import ru.ulstu.activity.api.model.ActivityDashboardDto;
public class TaskDashboardDto extends ActivityDashboardDto {
private final Task.TaskStatus status;
public TaskDashboardDto(Task task) {
super(task.getId(), task.getTitle());
this.status = task.getStatus();
}
public Task.TaskStatus getStatus() {
return status;
}
}

@ -3,6 +3,7 @@ package ru.ulstu.activity.students.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import ru.ulstu.activity.api.model.ActivityDto;
import ru.ulstu.activity.deadline.model.Deadline;
import ru.ulstu.activity.tags.model.Tag;
@ -10,15 +11,13 @@ import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class TaskDto {
public class TaskDto extends ActivityDto {
private final static int MAX_TAGS_LENGTH = 50;
private Integer id;
@NotEmpty
private String title;
private String description;
@ -29,10 +28,6 @@ public class TaskDto {
private Set<Integer> tagIds;
private List<Tag> tags = new ArrayList<>();
public TaskDto() {
deadlines.add(new Deadline());
}
@JsonCreator
public TaskDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title,
@ -43,7 +38,7 @@ public class TaskDto {
@JsonProperty("deadlines") List<Deadline> deadlines,
@JsonProperty("tagIds") Set<Integer> tagIds,
@JsonProperty("tags") List<Tag> tags) {
this.id = id;
super(id);
this.title = title;
this.status = status;
this.deadlines = deadlines;
@ -54,7 +49,7 @@ public class TaskDto {
}
public TaskDto(Task task) {
this.id = task.getId();
super(task.getId());
this.title = task.getTitle();
this.status = task.getStatus();
this.deadlines = task.getDeadlines();
@ -64,14 +59,6 @@ public class TaskDto {
this.tags = task.getTags();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
@ -136,29 +123,6 @@ public class TaskDto {
this.tags = tags;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TaskDto taskDto = (TaskDto) o;
return Objects.equals(id, taskDto.id) &&
Objects.equals(title, taskDto.title) &&
Objects.equals(description, taskDto.description) &&
status == taskDto.status &&
Objects.equals(deadlines, taskDto.deadlines) &&
Objects.equals(tagIds, taskDto.tagIds) &&
Objects.equals(tags, taskDto.tags);
}
@Override
public int hashCode() {
return Objects.hash(id, title, description, status, deadlines, createDate, updateDate, tagIds, tags);
}
public String getTagsString() {
return StringUtils.abbreviate(tags
.stream()

@ -0,0 +1,16 @@
package ru.ulstu.activity.students.model;
import ru.ulstu.activity.api.model.ActivityListDto;
public class TaskListDto extends ActivityListDto {
private final Task.TaskStatus status;
public TaskListDto(Task task) {
super(task.getId(), task.getTitle());
this.status = task.getStatus();
}
public Task.TaskStatus getStatus() {
return status;
}
}

@ -1,22 +1,29 @@
package ru.ulstu.activity.students.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.activity.common.service.AbstractActivityService;
import ru.ulstu.activity.deadline.model.Deadline;
import ru.ulstu.activity.deadline.service.DeadlineService;
import ru.ulstu.activity.ping.service.PingService;
import ru.ulstu.activity.students.model.Scheduler;
import ru.ulstu.activity.students.model.Task;
import ru.ulstu.activity.students.model.TaskDashboardDto;
import ru.ulstu.activity.students.model.TaskDto;
import ru.ulstu.activity.students.model.TaskFilterDto;
import ru.ulstu.activity.students.model.TaskListDto;
import ru.ulstu.activity.students.repository.SchedulerRepository;
import ru.ulstu.activity.students.repository.TaskRepository;
import ru.ulstu.activity.tags.model.Tag;
import ru.ulstu.activity.tags.service.TagService;
import ru.ulstu.activity.timeline.service.EventService;
import ru.ulstu.core.jpa.OffsetablePageRequest;
import ru.ulstu.core.model.response.PageableItems;
import ru.ulstu.core.util.DateUtils;
import javax.persistence.EntityNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@ -29,12 +36,12 @@ import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.activity.students.model.Task.TaskStatus.IN_WORK;
import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
@Service
public class TaskService {
public class TaskService extends AbstractActivityService<TaskListDto, Task, TaskDto> {
private final static int MAX_DISPLAY_SIZE = 40;
@ -43,29 +50,35 @@ public class TaskService {
private final DeadlineService deadlineService;
private final TagService tagService;
private final EventService eventService;
private final PingService pingService;
public TaskService(TaskRepository taskRepository,
DeadlineService deadlineService, TagService tagService, SchedulerRepository schedulerRepository, EventService eventService) {
DeadlineService deadlineService,
TagService tagService,
SchedulerRepository schedulerRepository,
EventService eventService,
PingService pingService) {
this.taskRepository = taskRepository;
this.deadlineService = deadlineService;
this.tagService = tagService;
this.eventService = eventService;
this.schedulerRepository = schedulerRepository;
this.pingService = pingService;
}
public List<Task> findAll() {
return taskRepository.findAll(new Sort(Sort.Direction.DESC, "createDate"));
}
public List<TaskDto> findAllDto() {
List<TaskDto> tasks = convert(findAll(), TaskDto::new);
tasks.forEach(taskDto -> taskDto.setTitle(StringUtils.abbreviate(taskDto.getTitle(), MAX_DISPLAY_SIZE)));
return tasks;
public PageableItems<Task> findAll(int offset, int count) {
final Page<Task> page = taskRepository.findAll(new OffsetablePageRequest(offset, count));
return new PageableItems<>(page.getTotalElements(), page.getContent());
}
public TaskDto findOneDto(Integer id) {
return new TaskDto(taskRepository.getOne(id));
@Override
protected TaskListDto getActivityListDto(Task entity) {
return new TaskListDto(entity);
}
public List<TaskDto> filter(TaskFilterDto filterDto) {
@ -81,11 +94,21 @@ public class TaskService {
}
@Transactional
public Integer create(TaskDto taskDto) throws IOException {
Task newTask = copyFromDto(new Task(), taskDto);
newTask = taskRepository.save(newTask);
eventService.createFromObject(newTask, Collections.emptyList(), true, "задачи");
return newTask.getId();
public TaskDto create(TaskDto taskDto) {
Task newTask = null;
try {
newTask = copyFromDto(new Task(), taskDto);
} catch (IOException e) {
e.printStackTrace();
}
return new TaskDto(create(newTask));
}
@Transactional
public Task create(Task task) {
task = taskRepository.save(task);
eventService.createFromObject(task, Collections.emptyList(), true, "задачи");
return task;
}
private Task copyFromDto(Task task, TaskDto taskDto) throws IOException {
@ -101,15 +124,20 @@ public class TaskService {
}
@Transactional
private Integer update(TaskDto taskDto) throws IOException {
public TaskDto update(TaskDto taskDto) {
Task task = taskRepository.getOne(taskDto.getId());
taskRepository.save(copyFromDto(task, taskDto));
return new TaskDto(update(task));
}
@Transactional
public Task update(Task task) {
task = taskRepository.save(task);
eventService.updateTaskDeadlines(task);
return task.getId();
return task;
}
@Transactional
public boolean delete(Integer taskId) throws IOException {
public boolean delete(Integer taskId) {
if (taskRepository.existsById(taskId)) {
Task scheduleTask = taskRepository.getOne(taskId);
Scheduler sch = schedulerRepository.findOneByTask(scheduleTask);
@ -120,15 +148,6 @@ public class TaskService {
return true;
}
return false;
}
public void save(TaskDto taskDto) throws IOException {
if (isEmpty(taskDto.getId())) {
create(taskDto);
} else {
update(taskDto);
}
}
private void copyMainPart(Task newTask, Task task) {
@ -255,4 +274,27 @@ public class TaskService {
taskRepository.save(newTask);
return newTask;
}
public PageableItems<TaskDashboardDto> findAllActiveDto(int offset, int count) {
return convertPageable(findAllActive(offset, count), TaskDashboardDto::new);
}
private PageableItems<Task> findAllActive(int offset, int count) {
return findAll(offset, count);
}
public TaskDto findById(Integer taskId) {
return new TaskDto(findTaskById(taskId));
}
public Task findTaskById(Integer taskId) {
return taskRepository.findById(taskId)
.orElseThrow(() -> new EntityNotFoundException("Paper with id=" + taskId + " not found"));
}
@Transactional
public void ping(int taskId) {
pingService.addPing(findTaskById(taskId));
}
}

@ -144,7 +144,7 @@ public class ConferenceServiceTest {
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.setNotSelectedPapers(papers);
ConferenceDto result = conferenceService.getNewConference();
ConferenceDto result = new ConferenceDto();
assertEquals(newConferenceDto.getId(), result.getId());
assertEquals(newConferenceDto.getNotSelectedPapers(), result.getNotSelectedPapers());
@ -155,7 +155,7 @@ public class ConferenceServiceTest {
public void findAll() {
when(conferenceRepository.findAll(SORT)).thenReturn(conferences);
assertEquals(Collections.singletonList(conferenceWithId), conferenceService.findAll());
assertEquals(Collections.singletonList(conferenceWithId), conferenceService.findAll(0, 100).getItems());
}
@Test

@ -127,11 +127,11 @@ public class GrantServiceTest {
public void findAll() {
when(grantRepository.findAll()).thenReturn(grants);
assertEquals(Collections.singletonList(grantWithId), grantService.findAll());
assertEquals(Collections.singletonList(grantWithId), grantService.findAll(0, 100).getItems());
}
@Test
public void create() throws IOException {
public void create() {
when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines);
when(userService.getUserByLogin("admin")).thenReturn(leader);
when(grantRepository.save(new Grant())).thenReturn(grantWithId);
@ -188,7 +188,7 @@ public class GrantServiceTest {
@Test
public void findById() {
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
Grant findGrant = grantService.findById(ID);
Grant findGrant = grantService.findGrantById(ID);
assertEquals(grantWithId.getId(), findGrant.getId());
}

Loading…
Cancel
Save