package de.hsadmin.remote;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.QueueTask;
import de.hsadmin.core.qserv.QueueTask.QueueTaskStatus;
import de.hsadmin.mods.user.UnixUser;

public class QueueTaskRemote extends AbstractRemote {

	private static final DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.SHORT);
	
	@Override
	protected Class<? extends AbstractEntity> getEntityClass() {
		return QueueTask.class;
	}

	@Override
	protected void entity2map(AbstractEntity entity, Map<String, String> resultMap) {
		QueueTask task = (QueueTask) entity;
		resultMap.put("id", Long.toString(task.getId()));
		QueueTaskStatus status = task.getStatus();
		if (status != null) resultMap.put("status", status.toString());
		if (assertNotNull(task.getTitle())) resultMap.put("title", task.getTitle());
		if (assertNotNull(task.getDetails())) resultMap.put("details", task.getDetails());
		if (assertNotNull(task.getException())) resultMap.put("exception", task.getException());
		UnixUser user = task.getUser();
		if (user != null) resultMap.put("user", user.getName());
		Date started = task.getStarted();
		if (assertNotNull(started)) {
			resultMap.put("started", df.format(started));
		}
		Date finished = task.getFinished();
		if (assertNotNull(finished)) {
			resultMap.put("finished", df.format(finished));
		}
	}

	@Override
	protected void map2entity(Map<String, String> setParams, AbstractEntity entity) {
		// never used
	}

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

}
