package it.cnr.isti.driver.web;

import javax.annotation.Resource;

import it.cnr.isti.driver.utils.PublisherServiceLocator;
import it.cnr.isti.driver.utils.ResultFormatter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class DocumentController {
	protected Log log = LogFactory.getLog(DocumentController.class);
		
	@Autowired
	protected PublisherServiceLocator publisherLocator;
	
	/**
	 * This object formats the whole metadata record 
	 * in HTML. This implementation is tailored on "DMF"
	 */
	@Resource(name="documentFormatter")
	private ResultFormatter documentFormatter;
	
	@RequestMapping("document.do")
	public void document(ModelMap map,
			@RequestParam("id") String id, 
			@RequestParam("format") String format) {
		
		String body = publisherLocator.getService().getResourceById(id, format);

		map.addAttribute("id", id);
		map.addAttribute("format", format);
		map.addAttribute("body", documentFormatter.viewDocument(body));
	}
	
}
