EventService.java
/**
* TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
* Copyright (C) 2020 Eduardo Rodriguez Carro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.uoc.tfg.sel.service;
/**
* TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
* Copyright (C) 2020 Eduardo Rodriguez Carro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.uoc.tfg.sel.repository.EventJobRepository;
import org.uoc.tfg.sel.repository.EventRepository;
import org.uoc.tfg.sel.repository.EventTypeRepository;
import org.uoc.tfg.sel.repository.SubjectClassRepository;
import org.uoc.tfg.sel.repository.model.Event;
import org.uoc.tfg.sel.repository.model.EventJob;
import org.uoc.tfg.sel.repository.model.EventType;
import org.uoc.tfg.sel.repository.model.SubjectClass;
import org.uoc.tfg.sel.repository.model.User;
import org.uoc.tfg.sel.validation.NotFoundException;
import org.uoc.tfg.sel.validation.ValidatorUtils;
import org.uoc.tfg.sel.web.model.ErrorCodes;
/**
* The Class EventService.
* @author Eduardo Rodriguez Carro
*/
@Service
public class EventService {
/** The event repository. */
@Autowired
private EventRepository eventRepository;
/** The event repository. */
@Autowired
private EventTypeRepository eventTypeRepository;
/** The event job repository. */
@Autowired
private EventJobRepository eventJobRepository;
/** The subject class repository. */
@Autowired
private SubjectClassRepository subjectClassRepository;
/**
* Gets the event by subject class.
*
* @param subjectClassId the subject class id
* @param page the page
* @param itemsPerPage the items per page
* @return the event by subject class
*/
@Transactional(readOnly = true)
public List<Event> getEventBySubjectClass(Integer subjectClassId,Integer page,Integer itemsPerPage){
Pageable pageRequest = PageRequest.of(page, itemsPerPage, Sort.by("date").descending());
return ServiceUtils.iterableToList(eventRepository.findBySubjectclass_Id(subjectClassId,pageRequest));
}
/**
* Gets the event by id.
*
* @param id the id
* @return the event by id
*/
@Transactional(readOnly = true)
public Event getEventById(Integer id) {
Optional<Event> item = eventRepository.findById(id);
if (item.isPresent()) {
return item.get();
}
return null;
}
/**
* Gets the event jobs by event id.
*
* @param id the id
* @return the event jobs by event id
*/
@Transactional(readOnly = true)
public List<EventJob> getEventJobsByEventId(Integer id) {
return eventJobRepository.findByEventId(id);
}
/**
* Save event.
*
* @param item the item
* @return the event
*/
@Transactional(readOnly = false)
public Event saveEvent(Event item) {
ValidatorUtils.assertIsNonNull("Event Title",item.getTitle());
ValidatorUtils.assertIsNonNull("Event content",item.getContent());
ValidatorUtils.assertIsNonNull("Event Subject",item.getSubjectclass());
ValidatorUtils.assertIsNonNull("Event Subject id",item.getSubjectclass().getId());
ValidatorUtils.assertIsNonNull("Event Type",item.getType());
ValidatorUtils.assertIsNonNull("Event Type id",item.getType().getId());
Optional<EventType> optionalEventType = eventTypeRepository.findById(item.getType().getId());
if(optionalEventType.isPresent()) {
Optional<SubjectClass> optionalSubjectClass = subjectClassRepository.findById(item.getSubjectclass().getId());
if(optionalSubjectClass.isPresent()) {
item.setType(optionalEventType.get());
item.setSubjectclass(optionalSubjectClass.get());
item.setDate(new Date());
eventRepository.save(item);
}else {
throw new NotFoundException(ErrorCodes.VALIDATION, "No class");
}
}else {
throw new NotFoundException(ErrorCodes.VALIDATION, "No event type");
}
return item;
}
/**
* Delete event.
*
* @param id the id
*/
@Transactional(readOnly = false)
public void deleteEvent(Integer id) {
eventRepository.deleteById(id);
}
/**
* Gets the event types.
*
* @return the event types
*/
@Transactional(readOnly = true)
public List<EventType> getEventTypes() {
return ServiceUtils.iterableToList(eventTypeRepository.findAll());
}
/**
* Save event job grade.
*
* @param eventId the event id
* @param jobId the job id
* @param grade the grade
*/
@Transactional(readOnly = false)
public void saveEventJobGrade(Integer eventId, Integer jobId,String grade) {
Optional<EventJob> optionalJob = eventJobRepository.findById(jobId);
if(optionalJob.isPresent()) {
EventJob job = optionalJob.get();
job.setGrade(grade);
eventJobRepository.save(job);
} else {
throw new NotFoundException(ErrorCodes.VALIDATION,"Event Job not found");
}
}
/**
* Save event job grade.
*
* @param user the user
* @param eventId the event id
* @param name the name
* @param mime the mime
* @param content the content
*/
@Transactional(readOnly = false)
public void saveEventJob(User user, Integer eventId, String name, String mime, byte[] content) {
Optional<Event> event = eventRepository.findById(eventId);
if(event.isPresent()) {
//TODO event is for that student
EventJob eventJob = new EventJob();
eventJob.setEvent(event.get());
eventJob.setContent(content);
eventJob.setContentName(name);
eventJob.setContentSize(content.length);
eventJob.setContentType(mime);
eventJob.setDate(new Date());
eventJob.setStudent(user);
eventJobRepository.save(eventJob);
} else {
throw new NotFoundException("Event not found");
}
}
/**
* Gets the event job by id.
*
* @param jobId the job id
* @return the event job by id
*/
@Transactional(readOnly = true)
public EventJob getEventJobById(Integer jobId) {
Optional<EventJob> optionalJob = eventJobRepository.findByJobId(jobId);
if(optionalJob.isPresent()) {
return optionalJob.get();
}
return null;
}
/**
* Gets the event job content by id.
*
* @param jobId the job id
* @return the event job content by id
*/
@Transactional(readOnly = true)
public byte[] getEventJobContentById(Integer jobId) {
byte[] content = eventJobRepository.findByContentById(jobId);
return content;
}
/**
* Gets the event jobs by event id and student.
*
* @param id the id
* @param user the user
* @return the event jobs by event id and student
*/
@Transactional(readOnly = true)
public List<EventJob> getEventJobsByEventIdAndStudent(Integer eventId, User user) {
return eventJobRepository.findByEventIdAndStudent(eventId, user);
}
}