package controller.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import common.controller.BaseController;
import dao.ProvenienzaDAO;

@Controller
@RequestMapping("provenienza")
public class ProvenienzaRestController extends BaseController {
	
	@Autowired
	ProvenienzaDAO provenienzaDAO;
	
	@RequestMapping("/{type}")
	@ResponseBody
	private ResponseEntity<Object> listByType(@PathVariable("type")String type, HttpServletRequest request){
		try {
			setRequest(request);
			
			if(getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Object>(provenienzaDAO.getProvenienze(type), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}
