package ch.uzh.csg.reimbursement.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import ch.uzh.csg.reimbursement.model.Token;
import ch.uzh.csg.reimbursement.model.User;
import ch.uzh.csg.reimbursement.model.exception.TokenNotFoundException;
import ch.uzh.csg.reimbursement.repository.TokenRepositoryProvider;

@Service
public class MobileService {

	@Autowired
	private UserService userService;

	@Autowired
	private TokenRepositoryProvider repository;

	@Value("${reimbursement.token.signatureMobile.expirationInDays}")
	private int expirationInDays;

	public void createSignature(String tokenString, MultipartFile file) {
		Token token = repository.findByUid(tokenString);
		checkValidity(token);
		// TODO remove previous tokens of that user and type

		User user = token.getUser();
		userService.addSignature(user, file);
	}

	private void checkValidity(Token token) {
		if(token == null) {
			throw new TokenNotFoundException();
		}
		// TODO check token expiration with expirationInDays
		/*if(token.getCreated().before(new Date())) {
			throw new TokenExpiredException();
		}*/
	}
}

@@ -205,16 +205,81 @@
 ervice;%0A
+import org.springframework.transaction.annotation.Transactional;%0A
 import o
@@ -566,16 +566,31 @@
 Service%0A
+@Transactional%0A
 public c

fix signature storing on mobile upload