package edu.northwestern.bioinformatics.studycalendar.restlets;

import org.restlet.resource.Resource;
import org.restlet.resource.Variant;
import org.restlet.resource.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.MediaType;
import org.restlet.Context;
import org.springframework.beans.factory.annotation.Required;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gov.nih.nci.cabig.ctms.domain.DomainObject;
import edu.northwestern.bioinformatics.studycalendar.xml.StudyCalendarXmlFactory;

/**
 * Implements GET for a resource backed by a single domain object.
 *
 * @author Rhett Sutphin
 */
public abstract class AbstractDomainObjectResource<D extends DomainObject> extends Resource {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    private D requestedObject;
    protected StudyCalendarXmlFactory studyCalendarXmlFactory;

    protected abstract D loadRequestedObject(Request request);

    public void init(Context context, Request request, Response response) {
        super.init(context, request, response);
        setReadable(true);
        setModifiable(false);
        getVariants().add(new Variant(MediaType.TEXT_XML));

        requestedObject = loadRequestedObject(request);
        setAvailable(requestedObject != null);
        if (isAvailable()) {
            log.debug("Request {} does not map to an existing domain object", request.getResourceRef());
        } else {
            log.debug("Request {} maps to {}", request.getResourceRef(), requestedObject);
        }
    }

    public Representation represent(Variant variant) throws ResourceException {
        if (variant.getMediaType() == MediaType.TEXT_XML) {
            return createXmlRepresentation(requestedObject);
        } else {
            return null;
        }
    }

    protected Representation createXmlRepresentation(D object) {
        return new StringRepresentation(
            studyCalendarXmlFactory.createDocumentString(object), MediaType.TEXT_XML);
    }

    ////// CONFIGURATION

    @Required
    public void setStudyCalendarXmlFactory(StudyCalendarXmlFactory studyCalendarXmlFactory) {
        this.studyCalendarXmlFactory = studyCalendarXmlFactory;
    }
}

@@ -1495,50 +1495,19 @@
 t %7B%7D
- does not
  map
+s
  to 
-an existing domain object
+%7B%7D
 %22, r
@@ -1529,16 +1529,33 @@
 rceRef()
+, requestedObject
 );%0A     
@@ -1595,35 +1595,66 @@
 (%22Request %7B%7D
+ does not
  map
-s
  to 
-%7B%7D
+an existing domain object
 %22, request.g
@@ -1668,33 +1668,16 @@
 rceRef()
-, requestedObject
 );%0A     

Correct inverted debugging messages