SubjectClassService.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;

import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.uoc.tfg.sel.repository.SubjectClassRepository;
import org.uoc.tfg.sel.repository.model.SubjectClass;
import org.uoc.tfg.sel.repository.model.User;

/**
 * The Class SubjectClassService.
 * 
 * @author Eduardo Rodriguez Carro
 */
@Service
public class SubjectClassService {
	
	/** The Constant logger. */
	private static final Logger logger = LoggerFactory.getLogger(SubjectClassService.class);

	/** The student type. */
	@Value("${user.type.student:5}")
	private Integer studentType;
	
	/** The subject class repository. */
	@Autowired
	private SubjectClassRepository subjectClassRepository;
	
	/**
	 * Gets the all.
	 *
	 * @return the all
	 */
	@Transactional(readOnly = true)
	public List<SubjectClass> getAll(){
		 return ServiceUtils.iterableToList(subjectClassRepository.findAll());
	}

	/**
	 * Gets the myclasses.
	 *
	 * @param user the user
	 * @return the myclasses
	 */
	@Transactional(readOnly = true)
	public List<SubjectClass> getMyclasses(User user) {
		Integer typeId = user.getType().getId();
		
		if (typeId == studentType) {
			return subjectClassRepository.getByStudents(user);
		}else {
			return subjectClassRepository.getByTeacher(user);	
		}
	}
	
	/**
	 * Gets the by id.
	 *
	 * @param id the id
	 * @param withStudents the with students
	 * @return the by id
	 */
	@Transactional(readOnly = true)
	public SubjectClass getById(Integer id,boolean withStudents, User user) {
		Optional<SubjectClass> item = subjectClassRepository.findById(id);
		if (item.isPresent()) {
			SubjectClass subjectClass = item.get();
			if (withStudents) {
				// JPA Lazy load
				int size = subjectClass.getStudents().size();
				logger.debug("Class {} has {} students",subjectClass,size);
			}else {
				logger.debug("Class {} no students requested",subjectClass);
			}
			return subjectClass;
		}
		return null;
	}
	
	/**
	 * Save.
	 *
	 * @param item the item
	 * @return the subject class
	 */
	@Transactional(readOnly = false)
	public SubjectClass save(SubjectClass item) {
		subjectClassRepository.save(item);
		return item;
	}
	
	/**
	 * Save student to class.
	 *
	 * @param classId the class id
	 * @param studentId the student id
	 */
	@Transactional(readOnly = false)
	public void saveStudentToClass(Integer classId, Integer studentId) {
		subjectClassRepository.addStudentToClass(classId, studentId);
	}	
	
	/**
	 * Delete.
	 *
	 * @param id the id
	 */
	@Transactional(readOnly = false)
	public void delete(Integer id) {
		subjectClassRepository.deleteById(id);
	}
	
	/**
	 * Delete student from class.
	 *
	 * @param classId the class id
	 * @param studentId the student id
	 */
	@Transactional(readOnly = false)
	public void deleteStudentFromClass(Integer classId, Integer studentId) {
		subjectClassRepository.deleteStudentToClass(classId, studentId);
	}
}