package net.anotheria.moskito.webui.accumulators.resource;

import net.anotheria.anoplass.api.APIException;
import net.anotheria.anoplass.api.APIFinder;
import net.anotheria.moskito.webui.accumulators.api.AccumulatorAPI;
import net.anotheria.moskito.webui.accumulators.api.AccumulatorDefinitionAO;
import net.anotheria.moskito.webui.accumulators.api.AccumulatorPO;
import net.anotheria.moskito.webui.shared.resource.ReplyObject;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;

/**
 * JAX-RS Resource that serves requests to accumulators.
 *
 * @author lrosenberg
 * @since 13.02.13 22:44
 */
@Path("accumulators")
public class AccumulatorResource {
	/**
	 * API Instance.
	 */
	private AccumulatorAPI accumulatorAPI = APIFinder.findAPI(AccumulatorAPI.class);

	/**
	 * Returns all accumulators.
	 * @return
	 */
	@GET
	@Path("list")
	public ReplyObject getAccumulators(){
		try{
			return ReplyObject.success("accumulators", accumulatorAPI.getAccumulatorDefinitions() );
		}catch(APIException e){
			throw new WebApplicationException(e);
		}

	}

	/**
	 * Removes one accumulator by id.
	 * @param id
	 * @return
	 */
	@GET
	@Path("remove/{id}")
	public ReplyObject deleteAccumulator(@PathParam("id") String id){
		try{
			accumulatorAPI.removeAccumulator(id);
			return ReplyObject.success();
		}catch(APIException e){
			throw new WebApplicationException(e);
		}
	}

	/**
	 * Returns an accumulator by id.
	 * @param id of the accumulator to return.
	 * @return
	 */
	@GET
	@Path("get/{id}")
	public ReplyObject getAccumulator(@PathParam("id") String id){
		try{
			ReplyObject ro = ReplyObject.success();
			ro.addResult("accumulator", accumulatorAPI.getAccumulatorDefinition(id));
			ro.addResult("graphData", accumulatorAPI.getAccumulatorGraphData(id));
			return ro;
		}catch(APIException e){
			throw new WebApplicationException(e);
		}
	}

	/**
	 * Creates new accumulator.
	 * @param po the accumulator parameter object.
	 * @return
	 */
	@POST @Path("create")
	public ReplyObject createAccumulator(AccumulatorPO po){
		try{
			AccumulatorDefinitionAO ret = accumulatorAPI.createAccumulator(po);
			return ReplyObject.success("created", ret);
		}catch(APIException e){
			throw new WebApplicationException(e);
		}
	}


}

@@ -105,121 +105,8 @@
 on;%0A
-import net.anotheria.anoplass.api.APIFinder;%0Aimport net.anotheria.moskito.webui.accumulators.api.AccumulatorAPI;%0A
 impo
@@ -245,16 +245,85 @@
 atorPO;%0A
+import net.anotheria.moskito.webui.shared.resource.AbstractResource;%0A
 import n
@@ -702,119 +702,33 @@
 rce 
-%7B%0A%09/**%0A%09 * API Instance.%0A%09 */%0A%09private AccumulatorAPI accumulatorAPI = APIFinder.findAPI(AccumulatorAPI.class);
+extends AbstractResource%7B
 %0A%0A%09/
@@ -890,17 +890,20 @@
 ators%22, 
-a
+getA
 ccumulat
@@ -899,32 +899,34 @@
 etAccumulatorAPI
+()
 .getAccumulatorD
@@ -1193,17 +1193,20 @@
 try%7B%0A%09%09%09
-a
+getA
 ccumulat
@@ -1210,16 +1210,18 @@
 latorAPI
+()
 .removeA
@@ -1614,17 +1614,20 @@
 lator%22, 
-a
+getA
 ccumulat
@@ -1623,32 +1623,34 @@
 etAccumulatorAPI
+()
 .getAccumulatorD
@@ -1694,17 +1694,20 @@
 hData%22, 
-a
+getA
 ccumulat
@@ -1711,16 +1711,18 @@
 latorAPI
+()
 .getAccu
@@ -2052,17 +2052,20 @@
 O ret = 
-a
+getA
 ccumulat
@@ -2069,16 +2069,18 @@
 latorAPI
+()
 .createA

use api through abstractresource