/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slc.sli.api.search.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import org.slc.sli.api.resources.generic.GenericResource;
import org.slc.sli.api.resources.generic.representation.Resource;
import org.slc.sli.api.resources.generic.representation.ServiceResponse;
import org.slc.sli.api.resources.generic.util.ResourceMethod;
import org.slc.sli.api.resources.generic.util.ResourceTemplate;
import org.slc.sli.api.search.service.SearchResourceService;

/**
 * Resource for handling search requests
 *
 */

@Component
@Scope("request")
public class SearchResource extends GenericResource {

    @Autowired
    SearchResourceService resourceService;

    @GET
    public Response get(@Context final UriInfo uriInfo) {

        return getAllResponseBuilder.build(uriInfo, ResourceTemplate.ONE_PART, ResourceMethod.GET, new GetResourceLogic() {
            @Override
            public ServiceResponse run(Resource resource) {

                return resourceService.list(resource, null, uriInfo.getRequestUri(), false);
            }
        });

    }

    @GET
    @Path("{entity}")
    public Response get(@PathParam("entity") final String entity, @Context final UriInfo uriInfo) {

        return getAllResponseBuilder.build(uriInfo, ResourceTemplate.SEARCH, ResourceMethod.GET, new GetResourceLogic() {
            @Override
            public ServiceResponse run(Resource resource) {

                return resourceService.list(resource, entity, uriInfo.getRequestUri(), false);
            }
        });

    }

    @GET
    @Path("{entity}")
    @Consumes("application/vnd.slc.search.full+json")
    public Response getAndRoute(@PathParam("entity") final String entity, @Context final UriInfo uriInfo) {

        return getAllResponseBuilder.build(uriInfo, ResourceTemplate.SEARCH, ResourceMethod.GET, new GetResourceLogic() {
            @Override
            public ServiceResponse run(Resource resource) {

                return resourceService.list(resource, entity, uriInfo.getRequestUri(), true);
            }
        });
    }

}

@@ -1461,17 +1461,16 @@
 %0A *%0A */%0A
-%0A
 @Compone
@@ -1697,313 +1697,58 @@
 tAll
-ResponseBuilder.build(uriInfo, ResourceTemplate.ONE_PART, ResourceMethod.GET, new GetResourceLogic() %7B%0A            @Override%0A            public ServiceResponse run(Resource resource) %7B%0A%0A                return resourceService.list(resource, null, uriInfo.getRequestUri(), false);%0A            %7D%0A        %7D);%0A
+(ResourceTemplate.ONE_PART, null, uriInfo, false);
 %0A   
@@ -1874,33 +1874,32 @@
 Info uriInfo) %7B%0A
-%0A
         return g
@@ -1907,310 +1907,56 @@
 tAll
-ResponseBuilder.build(uriInfo, ResourceTemplate.SEARCH, ResourceMethod.GET, new GetResourceLogic() %7B%0A            @Override%0A            public ServiceResponse run(Resource resource) %7B%0A%0A                return resourceService.list(resource, entity, uriInfo.getRequestUri(), false);%0A            %7D%0A        %7D
+(ResourceTemplate.SEARCH, entity, uriInfo, false
 );%0A%0A
@@ -2148,32 +2148,251 @@
 nfo uriInfo) %7B%0A%0A
+        return getAll(ResourceTemplate.SEARCH, entity, uriInfo, true);%0A    %7D%0A%0A    public Response getAll(ResourceTemplate resourceTemplate, final String entity, final UriInfo uriInfo, final boolean routeToEntityApp) %7B%0A%0A
         return g
@@ -2427,17 +2427,17 @@
 riInfo, 
-R
+r
 esourceT
@@ -2443,23 +2443,16 @@
 Template
-.SEARCH
 , Resour
@@ -2660,20 +2660,32 @@
 tUri(), 
-true
+routeToEntityApp
 );%0A     

refactor of SearchResource