package com.nflabs.zeppelin.rest;

import java.io.OutputStream;
import java.util.Collection;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.nflabs.zeppelin.scheduler.Job;
import com.nflabs.zeppelin.server.JsonResponse;
import com.nflabs.zeppelin.server.ZANJob;
import com.nflabs.zeppelin.server.ZANJobManager;
import com.nflabs.zeppelin.server.ZQLJob;
import com.nflabs.zeppelin.zan.Info;
import com.nflabs.zeppelin.zan.ZANException;

@Path("/zan")
public class ZANRestApi {
	Logger logger = Logger.getLogger(ZANRestApi.class);
	
	private com.nflabs.zeppelin.zan.ZAN zan;

	private ZANJobManager jobManager;

	public ZANRestApi(com.nflabs.zeppelin.zan.ZAN zan, ZANJobManager zanJobManager){
		this.zan = zan;
		this.jobManager = zanJobManager;
	}
	
	public static class SearchRequest{
		public String query;
	}
	
    @POST
    @Path("search")
    @Produces("application/json")
    public Response search(String json) {
    	Gson gson = new Gson();
    	SearchRequest sr = gson.fromJson(json, SearchRequest.class);
    	// TODO implement search instead of list
    	try {
			return new JsonResponse<List<Info>>(Status.OK, "", zan.list()).build();
		} catch (ZANException e) {
			logger.error("Search failed", e);
			return new JsonResponse<Object>(Status.INTERNAL_SERVER_ERROR, e.getMessage()).build();
		}    	
    }
    
    @GET
    @Path("update")
    @Produces("application/json")
    public Response update() {
    	jobManager.update();
    	return new JsonResponse<Object>(Status.OK, "Job submitted").build();
    }
    
    @GET
    @Path("running")
    @Produces("application/json")
    public Response getJobsRunning(){
    	List<ZANJob> job = jobManager.getJobsRunning();
    	return new JsonResponse<List<ZANJob>>(Status.OK, "", job).build();
    }
    
    @GET
    @Path("install/{libName}")
    @Produces("application/json")
    public Response install(@PathParam("libName") String libName){
    	jobManager.install(libName);
    	return new JsonResponse<List<ZANJob>>(Status.OK, "").build();
    }

    @GET
    @Path("uninstall/{libName}")
    @Produces("application/json")
    public Response uninstall(@PathParam("libName") String libName){
    	jobManager.uninstall(libName);
    	return new JsonResponse<List<ZANJob>>(Status.OK, "").build();
    }

    @GET
    @Path("upgrade/{libName}")
    @Produces("application/json")
    public Response upgrade(@PathParam("libName") String libName){
    	jobManager.upgrade(libName);
    	return new JsonResponse<List<ZANJob>>(Status.OK, "").build();
    }

}

@@ -690,21 +690,221 @@
 on;%0A
-%0A@Path(%22/zan%22
+import com.wordnik.swagger.annotations.Api;%0A%0A@Path(%22/zan%22)%0A@Api( value = %22/zan%22, description = %22Zeppelin Archive Network is library sharing network. You can download some library from ZAN or publish you library.%22 
 )%0Apu
@@ -980,18 +980,16 @@
 class);%0A
-%09%0A
 %09private
@@ -1022,17 +1022,16 @@
 AN zan;%0A
-%0A
 %09private
@@ -1058,16 +1058,106 @@
 nager;%0A%0A
+%09/**%0A%09 * This is required by Swagger%0A%09 */%0A    public ZANRestApi() %7B%0A      super();%0A    %7D%0A%0A
 %09public 
@@ -1286,17 +1286,16 @@
 ger;%0A%09%7D%0A
-%09
 %0A%09public
@@ -1349,17 +1349,16 @@
 ery;%0A%09%7D%0A
-%09
 %0A    @PO
@@ -1371,16 +1371,17 @@
  @Path(%22
+/
 search%22)
@@ -1847,24 +1847,15 @@
 %0A%09%09%7D
-    %09
 %0A    %7D%0A
-    
 %0A   
@@ -1871,16 +1871,17 @@
  @Path(%22
+/
 update%22)
@@ -2044,36 +2044,32 @@
 .build();%0A    %7D%0A
-    
 %0A    @GET%0A    @P
@@ -2073,16 +2073,17 @@
  @Path(%22
+/
 running%22
@@ -2287,20 +2287,16 @@
 ;%0A    %7D%0A
-    
 %0A    @GE
@@ -2308,16 +2308,17 @@
  @Path(%22
+/
 install/
@@ -2558,16 +2558,17 @@
  @Path(%22
+/
 uninstal
@@ -2814,16 +2814,17 @@
  @Path(%22
+/
 upgrade/

Change path - add '/'