package de.hsadmin.remote;

import java.util.Map;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.mods.user.UnixUser;

public class UnixUserRemote extends AbstractRemote {

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

	@Override
	protected void entity2map(AbstractEntity entity, Map<String, String> map) {
		UnixUser user = (UnixUser) entity;
		map.put("id", Long.toString(user.getId()));
		map.put("name", user.getName());
		map.put("comment", user.getComment());
		map.put("userid", Long.toString(user.getUserId()));
		map.put("pac", user.getPac().getName());
		map.put("shell", user.getShell());
		map.put("homedir", user.getHomedir());
		Integer quotaSoft = user.getQuotaSoftlimit();
		if (assertNotNull(quotaSoft)) {
			map.put("quota_softlimit", quotaSoft.toString());
		}
		Integer quotaHard = user.getQuotaHardlimit();
		if (assertNotNull(quotaHard)) {
			map.put("quota_hardlimit", quotaHard.toString());
		}
	}

	@Override
	protected void map2entity(Map<String, String> map, AbstractEntity entity) {
		UnixUser user = (UnixUser) entity;
		String id = map.get("id");
		if (assertNotNull(id)) {
			user.setId(Long.parseLong(id));
		}
		String name = map.get("name");
		if (assertNotNull(name)) {
			user.setName(name);
		}
		String password = map.get("password");
		if (assertNotNull(password)) {
			user.setPassword(password);
		}
		String comment = map.get("comment");
		if (assertNotNull(comment)) {
			user.setComment(comment);
		}
		String userid = map.get("userid");
		if (assertNotNull(userid)) {
			user.setUserId(Long.parseLong(userid));
		}
		String shell = map.get("shell");
		if (assertNotNull(shell)) {
			user.setShell(shell);
		}
		String homedir = map.get("homedir");
		if (assertNotNull(homedir)) {
			user.setHomedir(homedir);
		}
		String quota = map.get("quota_softlimit");
		if (assertNotNull(quota)) {
			user.setQuotaSoftlimit(Integer.parseInt(quota));
		}
		String quotaLimit = map.get("quota_hardlimit");
		if (assertNotNull(quotaLimit)) {
			user.setQuotaHardlimit(Integer.parseInt(quotaLimit));
		}
	}

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

}
