package de.afbb.bibo.servletclient.connection;

import java.net.ConnectException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import de.afbb.bibo.share.ITypService;
import de.afbb.bibo.share.beans.BeanExclusionStrategy;
import de.afbb.bibo.share.model.MediumType;

/**
 * Implementation of HTTP service for {@link MediumType}
 *
 * @author David Becker
 *
 */
public class TypServiceImpl implements ITypService {

	private static final Gson gson = new GsonBuilder().addSerializationExclusionStrategy(new BeanExclusionStrategy())
			.create();

	@Override
	public void create(final MediumType type) throws ConnectException {
		final HttpResponse resp = ServerConnection.getInstance().request("/stock/addMediaType", "POST", null,
				gson.toJson(type));
		if (resp.getStatus() != HttpServletResponse.SC_OK) {
			throw new ConnectException("Wrong status code. Recieved was: " + resp.getStatus());
		}
	}

	@Override
	public Collection<MediumType> list() throws ConnectException {
		final HttpResponse resp = ServerConnection.getInstance().request("/stock/listMediaTypes", "GET", null, null);
		if (resp.getStatus() == HttpServletResponse.SC_OK) {
			final Collection<MediumType> result = new HashSet<>();
			final String[] data = resp.getData().split("\n");
			for (int i = 0; i < data.length; i++) {
				result.add(gson.fromJson(data[i], MediumType.class));
			}
			return result;
		} else {
			throw new ConnectException("Wrong status code. Recieved was: " + resp.getStatus());
		}
	}

	@Override
	public MediumType get(final Integer id) throws ConnectException {
		final Map<String, String> param = new HashMap<String, String>();
		param.put("id", id.toString());
		final HttpResponse resp = ServerConnection.getInstance().request("/stock/getMediaType", "GET", param, null);
		if (resp.getStatus() == HttpServletResponse.SC_OK) {
			return gson.fromJson(resp.getData(), MediumType.class);
		} else {
			throw new ConnectException("Wrong status code. Recieved was: " + resp.getStatus());
		}
	}

}

@@ -711,16 +711,101 @@
 ate();%0A%0A
+%09private final Map%3CInteger, MediumType%3E cache = new HashMap%3CInteger, MediumType%3E();%0A%0A
 %09@Overri
@@ -799,32 +799,32 @@
 ();%0A%0A%09@Override%0A
-
 %09public void cre
@@ -1454,16 +1454,62 @@
 et%3C%3E();%0A
+%09%09%09synchronized (cache) %7B%0A%09%09%09%09cache.clear();%0A%09
 %09%09%09final
@@ -1556,16 +1556,17 @@
 n%22);%0A%09%09%09
+%09
 for (int
@@ -1605,19 +1605,33 @@
 %09%09%09%09
-result.add(
+%09final MediumType type = 
 gson
@@ -1666,18 +1666,116 @@
 e.class)
-)
 ;
+%0A%09%09%09%09%09if (type != null) %7B%0A%09%09%09%09%09%09result.add(type);%0A%09%09%09%09%09%09cache.put(type.getId(), type);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 %0A%09%09%09%7D%0A%09%09
@@ -1966,32 +1966,124 @@
 nectException %7B%0A
+%09%09synchronized (cache) %7B%0A%09%09%09if (cache.containsKey(id)) %7B%0A%09%09%09%09return cache.get(id);%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09final Map%3CStri
@@ -2336,22 +2336,39 @@
 K) %7B%0A%09%09%09
-return
+final MediumType type =
  gson.fr
@@ -2409,16 +2409,98 @@
 class);%0A
+%09%09%09synchronized (cache) %7B%0A%09%09%09%09cache.put(type.getId(), type);%0A%09%09%09%7D%0A%09%09%09return type;%0A
 %09%09%7D else

implement cache for medium type