package de.afbb.bibo.servletclient.connection;

import java.net.ConnectException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

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

import de.afbb.bibo.share.IMediumService;
import de.afbb.bibo.share.beans.BeanExclusionStrategy;
import de.afbb.bibo.share.callback.EventListener;
import de.afbb.bibo.share.model.Borrower;
import de.afbb.bibo.share.model.Medium;

public class MediumServiceImpl implements IMediumService {

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

	private final Set<EventListener> listeners = new HashSet<EventListener>();

	@Override
	public void update(final Medium medium) throws ConnectException {
		// TODO Auto-generated method stub
		// currently never called
		throw new UnsupportedOperationException("not implemented");
	}

	@Override
	public void create(final Medium medium) throws ConnectException {
		// TODO Auto-generated method stub
		// currently never called
		throw new UnsupportedOperationException("not implemented");
	}

	@Override
	public Medium getMedium(final int id) throws ConnectException {
		// TODO Auto-generated method stub
		// currently never called
		throw new UnsupportedOperationException("not implemented");
	}

	@Override
	public Medium getMedium(final String isbn) throws ConnectException {
		// TODO Auto-generated method stub
		// currently never called
		throw new UnsupportedOperationException("not implemented");
	}

	@Override
	public Collection<Borrower> listLent(final String isbn) throws ConnectException {
		// TODO Auto-generated method stub
		// currently never called
		throw new UnsupportedOperationException("not implemented");
	}

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

	@Override
	public void delete(final Medium medium) throws ConnectException {
		// currently never called
		throw new UnsupportedOperationException("not implemented");
	}

	@Override
	public void register(final EventListener listener) {
		listeners.add(listener);
	}

}

@@ -125,19 +125,67 @@
 til.Hash
-Set
+Map;%0Aimport java.util.HashSet;%0Aimport java.util.Map
 ;%0Aimport
@@ -315,16 +315,66 @@
 ilder;%0A%0A
+import de.afbb.bibo.servletclient.ServiceLocator;%0A
 import d
@@ -1574,132 +1574,749 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A%09%09// currently never called%0A%09%09throw new UnsupportedOperationException(%22not implemented%22);
+final Map%3CString, String%3E param = new HashMap%3CString, String%3E();%0A%09%09param.put(%22ISBN%22, isbn);%0A%09%09final HttpResponse resp = ServerConnection.getInstance().request(%22/stock/getMedium%22, %22GET%22, param, null);%0A%09%09if (resp.getStatus() == HttpServletResponse.SC_OK) %7B%0A%09%09%09final Medium medium = gson.fromJson(resp.getData(), Medium.class);%0A%09%09%09if (medium != null) %7B%0A%09%09%09%09// we only get the id for type filled, so we need to fetch type%0A%09%09%09%09// separately%0A%09%09%09%09medium.setType(ServiceLocator.getInstance().getTypService().get(medium.getType().getId()));%0A%09%09%09%7D%0A%09%09%09return medium;%0A%09%09%7D else if (resp.getStatus() == HttpServletResponse.SC_NOT_FOUND) %7B%0A%09%09%09return null;%0A%09%09%7D else %7B%0A%09%09%09throw new ConnectException(%22Wrong status code. Recieved was: %22 + resp.getStatus());%0A%09%09%7D
 %0A%09%7D%0A

implement service call to fetch medium by ISBN