package edu.northwestern.bioinformatics.studycalendar.restlets;

import edu.northwestern.bioinformatics.studycalendar.dao.SiteDao;
import edu.northwestern.bioinformatics.studycalendar.domain.Site;
import edu.northwestern.bioinformatics.studycalendar.service.dataproviders.SiteConsumer;
import edu.northwestern.bioinformatics.studycalendar.xml.StudyCalendarXmlCollectionSerializer;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
import org.springframework.beans.factory.annotation.Required;

import java.util.ArrayList;
import java.util.List;
/**
 * @author Jalpa Patel
 */
public class ProvidedSitesResource extends Resource {
    private StudyCalendarXmlCollectionSerializer<Site> xmlSerializer;
    private SiteConsumer siteConsumer;
    private SiteDao siteDao;

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

    public List<Site> getAllObjects() {
        String q = QueryParameters.Q.extractFrom(getRequest());
        List<Site> availableSites = siteDao.searchSitesBySearchText(q);
        List<Site> providedSites = siteConsumer.search(q);
        List<Site> duplicateSites = new ArrayList<Site>();
        for (Site providedSite : providedSites) {
            for (Site availableSite : availableSites ) {
               if( availableSite.getName().equals(providedSite.getName()) || availableSite.getAssignedIdentifier().equals(providedSite.getAssignedIdentifier())) {
                   duplicateSites.add(availableSite);
               }
            }
        }
        List<Site> all = new ArrayList<Site>(availableSites.size() + providedSites.size());
        all.addAll(availableSites);
        all.removeAll(duplicateSites);
        all.addAll(providedSites);
        return providedSites;
    }

    @Override
    public Representation represent(Variant variant) throws ResourceException {
        if (variant.getMediaType().includes(MediaType.TEXT_XML)) {
            return createXmlRepresentation(getAllObjects());
        } else {
            return null;
        }
    }
    
    protected Representation createXmlRepresentation(List<Site> sites) {
        return new StringRepresentation(
                getXmlSerializer().createDocumentString(sites), MediaType.TEXT_XML);
    }
    public StudyCalendarXmlCollectionSerializer<Site> getXmlSerializer() {
        return xmlSerializer;
    }

    public void setXmlSerializer(StudyCalendarXmlCollectionSerializer<Site> xmlSerializer) {
        this.xmlSerializer = xmlSerializer;
    }
    
    @Required
    public void setSiteConsumer(SiteConsumer siteProvider) {
        this.siteConsumer = siteProvider;
    }

    @Required
    public void setSiteDao(SiteDao siteDao) {
        this.siteDao = siteDao;
    }
}

@@ -190,16 +190,82 @@
 n.Site;%0A
+import edu.northwestern.bioinformatics.studycalendar.domain.Role;%0A
 import e
@@ -341,24 +341,24 @@
 teConsumer;%0A
-
 import edu.n
@@ -472,43 +472,8 @@
 xt;%0A
-import org.restlet.data.MediaType;%0A
 impo
@@ -558,203 +558,19 @@
 let.
-resource.Representation;%0Aimport org.restlet.resource.Resource;%0Aimport org.restlet.resource.ResourceException;%0Aimport org.restlet.resource.StringRepresentation;%0Aimport org.restlet.resource.Variant
+data.Method
 ;%0Aim
@@ -755,24 +755,43 @@
 extends 
+ AbstractCollection
 Resource
  %7B%0A    p
@@ -782,16 +782,22 @@
 Resource
+%3CSite%3E
  %7B%0A    p
@@ -1083,81 +1083,82 @@
  set
-Readable(true);%0A        getVariants().add(new Variant(MediaType.TEXT_XML)
+AuthorizedFor(Method.GET, Role.SITE_COORDINATOR, Role.SYSTEM_ADMINISTRATOR
 );%0A 
@@ -2065,498 +2065,8 @@
  %7D%0A%0A
-    @Override%0A    public Representation represent(Variant variant) throws ResourceException %7B%0A        if (variant.getMediaType().includes(MediaType.TEXT_XML)) %7B%0A            return createXmlRepresentation(getAllObjects());%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D%0A    %0A    protected Representation createXmlRepresentation(List%3CSite%3E sites) %7B%0A        return new StringRepresentation(%0A                getXmlSerializer().createDocumentString(sites), MediaType.TEXT_XML);%0A    %7D%0A
     

Change base class for ProvidedSitesResouce as now authentication is required for site creation. Related to #768.