/* TemplatesResource.java created by Gervais on 1 nov. 2009, 11:09:22 for EduWebServices */
package me.myapp.web.services.rest.resources.epublish;

import java.util.logging.Level;

import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

import me.myapp.domain.epublish.Template;
import me.myapp.domain.services.TemplateService;

/**
 *
 * @version 0.$VERS$
 */
public class TemplateResource extends ServerResource {
	
	private final TemplateService service;

	
	public TemplateResource(final TemplateService service) {
		this.service = service;
		getLogger().log(Level.INFO, "New template resource");
	}
	
	@Override
	protected void doInit() throws ResourceException {
		getLogger().log(Level.INFO, "Initializing resource");
		// HOWTO register variants ??
		//setNegotiated(false);
		/*getVariants().put(Method.DELETE, Arrays.asList(new Variant[]{
				new Variant(MediaType.TEXT_XML),
				new Variant(MediaType.APPLICATION_XML),
				new Variant(MediaType.APPLICATION_ALL),
				new Variant(MediaType.TEXT_ALL)
			}));*/
	}
	
	@Override
	protected Representation get() throws ResourceException {
		getLogger().log(Level.INFO, "GET ()"+getRequestAttributes().get("identifier"));
		System.out.println("GET () "+getRequestAttributes().get("identifier"));
		return super.get();
	}
	
	@Override
	protected Representation get(Variant variant) throws ResourceException {
		System.out.println("GET ("+(variant==null?variant:variant.getMediaType())+") "+getRequestAttributes().get("identifier"));
		Template template = service.open(getRequestAttributes().get("identifier"), null, null);
		return super.get(variant);
	}

	@Override
	protected Representation delete() throws ResourceException {
		System.out.println("DELETE () "+getRequestAttributes().get("identifier"));		
		return Representation.createEmpty();
	}
	
	@Override
	protected Representation delete(Variant variant) throws ResourceException {
		System.out.println("DELETE ("+(variant==null?variant:variant.getMediaType())+") "+getRequestAttributes().get("identifier"));
		service.delete(null, getRequestAttributes().get("identifier"));
		return Representation.createEmpty();
	}
	
	@Override
	protected Representation put(Representation representation)
			throws ResourceException {
		System.out.println("PUT () "+representation);
		return super.put(representation);
	}

	@Override
	protected Representation put(Representation representation, Variant variant)
			throws ResourceException {
		System.out.println("PUT ("+(variant==null?variant:variant.getMediaType())+") "+representation);
		return super.put(representation, variant);
	}
	
	@Override
	protected Representation post(Representation entity)
			throws ResourceException {
		System.out.println("POST () "+entity);
		return super.post(entity);
	}
	
	@Override
	protected Representation post(Representation entity, Variant variant)
			throws ResourceException {
		System.out.println("POST ("+(variant==null?variant:variant.getMediaType())+") "+entity);
		return super.post(entity, variant);
	}
}

