AuthorizationService.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.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.AuthorizationRepository;
import org.uoc.tfg.sel.repository.model.Authorization;
import org.uoc.tfg.sel.repository.model.User;
import org.uoc.tfg.sel.validation.NotAllowedException;
import org.uoc.tfg.sel.validation.NotFoundException;
import org.uoc.tfg.sel.web.model.ErrorCodes;

/**
 * The Class AuthorizationService.
 * @author Eduardo Rodriguez Carro
 */
@Service
public class AuthorizationService {

	/** The tutor type. */
	@Value("${user.type.tutor:2}")
	private Integer tutorType;
	
	
	/** The tutor type. */
	@Value("${user.type.legalTutor:4}")
	private Integer legalTutor;
	
	/** The authorization repository. */
	@Autowired
	private AuthorizationRepository authorizationRepository;
	
	/**
	 * Save.
	 *
	 * @param authorizations the authorizations
	 */
	@Transactional(readOnly = false)
	public void save(List<Authorization> authorizations) {
		for(Authorization auth: authorizations) {
			authorizationRepository.save(auth);
		}
	}
	
	/**
	 * Gets the by user.
	 *
	 * @param user the user
	 * @return the by user
	 */
	@Transactional(readOnly = false)
	public List<Authorization> getByUser(User user){
		List<Authorization> auths = null;
		if (user.getType() != null) {
			if  (tutorType  == user.getType().getId()) {
				auths = authorizationRepository.findByTeacherTutor(user);
			} else if( legalTutor == user.getType().getId() ){
				auths = authorizationRepository.findByStudent_LegalTutor(user);
			}
		}
		return auths;
	}
	
	/**
	 * Delete.
	 *
	 * @param id the id
	 * @param user the user
	 */
	@Transactional(readOnly = false)
	public void delete(Integer id, User user) {
		Optional<Authorization> optionalAuth = authorizationRepository.findById(id);
		if(optionalAuth.isPresent()) {
			Authorization auth = optionalAuth.get();
			if (auth.getTeacherTutor().getId() == user.getId()) {
				authorizationRepository.deleteById(auth.getId());
			}else {
				throw new NotAllowedException(ErrorCodes.VALIDATION,"Legal tutor not found or not valid");
			}
		}else {
			throw new NotFoundException(ErrorCodes.VALIDATION,"No auth found");
		}
	}			

	@Transactional(readOnly = false)
	public void confirm(Integer id,Integer confirm,String notes) {
		authorizationRepository.confirm(id, confirm, notes);
	}
}