package de.hsadmin.remote;

import java.util.Map;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.mods.db.MySqlDatabase;

public class MysqlDbRemote extends AbstractRemote {

	@Override
	protected void entity2map(AbstractEntity entity, Map<String, String> map) {
		MySqlDatabase db = (MySqlDatabase) entity;
		String id = Long.toString(db.getId());
		String name = db.getName();
		String instance = db.getInstance();
		String owner = db.getOwner();
		String encoding = db.getEncoding();
		String pac = db.getPac().getName();
		String hive = db.getHiveName();
		map.put("id", id);
		map.put("name", name);
		map.put("instance", instance);
		map.put("pac", pac);
		map.put("hive", hive);
		map.put("owner", owner);
		map.put("encoding", encoding);
	}

	@Override
	protected Class<? extends AbstractEntity> getEntityClass() {
		return MySqlDatabase.class;
	}

	@Override
	protected void map2entity(Map<String, String> map, AbstractEntity entity) {
		MySqlDatabase db = (MySqlDatabase) entity;
		db.setInstance("mysql");
		String name = map.get("name");
		String owner = map.get("owner");
		String encoding = map.get("encoding");
		if (assertNotNull(name)) {
			db.setName(name);
		}
		if (assertNotNull(owner)) {
			db.setOwner(owner);
		}
		if (assertNotNull(encoding)) {
			db.setEncoding(encoding);
		}
	}

	@Override
	protected void regularizeKeys(Map<String, String> whereParams) {
		replaceKey(whereParams, "pac", "pac.name");
	}

}
