package lib.morkim.mfw.repo.persistence;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import lib.morkim.mfw.app.AppContext;
import lib.morkim.mfw.domain.Entity;
import lib.morkim.mfw.repo.Filter;
import lib.morkim.mfw.repo.SpRepo;
import lib.morkim.mfw.repo.gateway.AbstractGateway;
import lib.morkim.mfw.repo.gateway.GatewayPersistException;
import lib.morkim.mfw.repo.gateway.GatewayRetrieveException;

import org.json.JSONException;

public abstract class SpDbGateway extends AbstractGateway {

	protected static final String SP_DEFAULT = "sp.default";
	
	private SpRepo spRepo;

	public SpDbGateway(AppContext appContext) {
		super(appContext);
		
		spRepo = new SpRepo(appContext.getContext());
	}

	@Override
	public void persist(Entity entity) throws GatewayPersistException {
		try {
			spRepo.write(source(), entity.getSysId(), serialize(entity));
		} catch (JSONException e) {
			throw new GatewayPersistException();
		}
	}

	@Override
	public Entity retrieve() {
		return null;
	}

	@Override
	public Entity retrieve(int source) {
		return null;
	}

	@Override
	public List<Entity> retrieveAll() throws GatewayRetrieveException {
		
		Map<String, ?> pairs = spRepo.read(source());
		
		List<Entity> list = new ArrayList<Entity>();
		for (String key : pairs.keySet())
			try {
				list.add(deserialize((String) pairs.get(key)));
			} catch (JSONException e) {
				throw new GatewayRetrieveException();
			}
		
		return list ;
	}

	@Override
	public List<Entity> retrieve(Filter filter) {
		return null;
	}
	
	protected abstract String serialize(Entity entity) throws JSONException;
	protected abstract Entity deserialize(String string) throws JSONException;

	protected long asLong(Object object) {
		return (object instanceof Integer) ? (Integer) object : (Long) object;
	}

	protected double asDouble(Object object) {
		return (object instanceof Integer) ? (Integer) object : (Double) object;
	}
}

@@ -107,16 +107,39 @@
 til.Map;
+%0Aimport java.util.UUID;
 %0A%0Aimport
@@ -1341,17 +1341,24 @@
 %09%09%09%09
-list.add(
+Entity entity = 
 dese
@@ -1389,16 +1389,80 @@
 et(key))
+;%0A%09%09%09%09entity.setSysId(UUID.fromString(key));%0A%09%09%09%09list.add(entity
 );%0A%09%09%09%7D 

Set SysId in SpDbRepo retrieve