package es.uv.saic.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import es.uv.saic.domain.SysStatus;
import es.uv.saic.domain.SysStatusRepository;

@Service
public class SysStatusService {

	@Autowired
	private SysStatusRepository r;
	
	public List<SysStatus> findAll(){
		return this.r.findAll();
	}
	
	public List<SysStatus> findByType(String t){
		return this.r.findByType(t);
	}
	
	public void log(String msg) {
		SysStatus s = new SysStatus();
		s.setTimestamp(new Timestamp(System.currentTimeMillis()));
		s.setType("I");
		s.setMsg(msg);
		this.save(s);
	}
	
	public void err(String msg, String err) {
		SysStatus s = new SysStatus();
		s.setTimestamp(new Timestamp(System.currentTimeMillis()));
		s.setType("E");
		s.setMsg(msg);
		s.setErr(err);
		this.save(s);
	}
	
	public void warn(String msg, String warn) {
		SysStatus s = new SysStatus();
		s.setTimestamp(new Timestamp(System.currentTimeMillis()));
		s.setType("W");
		s.setMsg(msg);
		s.setErr(warn);
		this.save(s);
	}
	
	public void save(SysStatus s) {
		this.r.save(s);
		this.r.flush();
	}
	
}
