2019-04-02 10:53:09 +04:00
|
|
|
package ru.ulstu.conference.service;
|
|
|
|
|
2019-04-04 17:58:22 +04:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2019-04-02 10:53:09 +04:00
|
|
|
import org.springframework.stereotype.Service;
|
2019-04-05 16:30:45 +04:00
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2019-04-04 17:58:22 +04:00
|
|
|
import ru.ulstu.conference.model.Conference;
|
|
|
|
import ru.ulstu.conference.model.ConferenceDto;
|
2019-04-02 10:53:09 +04:00
|
|
|
import ru.ulstu.conference.repository.ConferenceRepository;
|
|
|
|
import ru.ulstu.deadline.service.DeadlineService;
|
|
|
|
|
2019-04-05 16:30:45 +04:00
|
|
|
import java.io.IOException;
|
2019-04-04 17:58:22 +04:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-04-05 16:30:45 +04:00
|
|
|
import static org.springframework.util.ObjectUtils.isEmpty;
|
2019-04-04 17:58:22 +04:00
|
|
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
|
|
|
|
2019-04-02 10:53:09 +04:00
|
|
|
@Service
|
|
|
|
public class ConferenceService {
|
2019-04-04 17:58:22 +04:00
|
|
|
private final static int MAX_DISPLAY_SIZE = 40;
|
|
|
|
|
2019-04-02 10:53:09 +04:00
|
|
|
private final ConferenceRepository conferenceRepository;
|
|
|
|
private final DeadlineService deadlineService;
|
|
|
|
|
|
|
|
public ConferenceService(ConferenceRepository conferenceRepository,
|
|
|
|
DeadlineService deadlineService) {
|
|
|
|
this.conferenceRepository = conferenceRepository;
|
|
|
|
this.deadlineService = deadlineService;
|
|
|
|
}
|
2019-04-04 17:58:22 +04:00
|
|
|
|
|
|
|
public List<Conference> findAll() {
|
|
|
|
return conferenceRepository.findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<ConferenceDto> findAllDto() {
|
|
|
|
List<ConferenceDto> conferences = convert(findAll(), ConferenceDto::new);
|
|
|
|
conferences.forEach(conferenceDto -> conferenceDto.setTitle(StringUtils.abbreviate(conferenceDto.getTitle(), MAX_DISPLAY_SIZE)));
|
|
|
|
return conferences;
|
|
|
|
}
|
|
|
|
|
2019-04-05 13:21:15 +04:00
|
|
|
public ConferenceDto findOneDto(Integer id) {
|
|
|
|
return new ConferenceDto(conferenceRepository.findOne(id));
|
|
|
|
}
|
2019-04-04 17:58:22 +04:00
|
|
|
|
2019-04-05 16:30:45 +04:00
|
|
|
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);
|
2019-04-06 11:05:30 +04:00
|
|
|
conference.setBeginDate(conferenceDto.getBeginDate());
|
|
|
|
conference.setEndDate(conferenceDto.getEndDate());
|
2019-04-05 16:30:45 +04:00
|
|
|
|
|
|
|
conference.setDeadlines(deadlineService.saveOrCreate(conferenceDto.getDeadlines()));
|
|
|
|
|
|
|
|
return conference;
|
|
|
|
}
|
|
|
|
|
2019-04-04 17:58:22 +04:00
|
|
|
|
2019-04-02 10:53:09 +04:00
|
|
|
}
|