package net.etalia.crepuscolo.domain;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Entities {

	private static Map<String, Class<? extends BaseEntity>> classes = new HashMap<String, Class<? extends BaseEntity>>();
	private static int prefixLength = 1;

	private Entities() {
	}

	public static void setPrefixLength(int length) {
		//TODO: chech on empty map
		prefixLength = length;
	}

	public static void add(String prefix, Class<? extends BaseEntity> clazz) {
		if (prefix.trim().length() != prefixLength) {
			throw new IllegalArgumentException("Prefix length is not valid.");
		}
		classes.put(prefix, clazz);
	}

	@SuppressWarnings("unchecked")
	public static void add(String prefix, String clazz) throws ClassNotFoundException {
		add(prefix, (Class<? extends BaseEntity>) Class.forName(clazz));
	}

	@SuppressWarnings("unchecked")
	public static <T extends BaseEntity> Class<T> getDomainClassByPrefix(String prefix) {
		if (prefix.trim().length() != prefixLength) {
			throw new IllegalArgumentException("Prefix length is not valid.");
		}
		return (Class<T>) classes.get(prefix);
	}

	public static <T extends BaseEntity> Class<T> getDomainClassByID(String id) {
		return getDomainClassByPrefix(id.substring(0, prefixLength));
	}

	public static String getPrefix(Class<? extends BaseEntity> clazz) {
		String prefix = null;
		for (Entry<String, Class<? extends BaseEntity>> entry : classes.entrySet()) {
			if (entry.getValue() == clazz) {
				prefix = entry.getKey();
				break;
			}
		}
		if (prefix == null) {
			throw new IllegalArgumentException("Cannot find any prefix associated with this Class");
		}
		if (prefix.trim().length() != prefixLength) {
			throw new IllegalArgumentException("Prefix length is not valid.");
		}
		return prefix;
	}

	public static String getPrefix(String id) {
		return id.substring(0, prefixLength);
	}

}

@@ -253,16 +253,147 @@
 ty%3E%3E();%0A
+%09private static Map%3CString, Class%3C? extends BaseEntity%3E%5B%5D%3E extendedClasses = new HashMap%3CString, Class%3C? extends BaseEntity%3E%5B%5D%3E();%0A
 %09private
@@ -785,24 +785,270 @@
 clazz);%0A%09%7D%0A%0A
+%09public static void add(String prefix, Class%3C? extends BaseEntity%3E%5B%5D classes) %7B%0A%09%09if (prefix.trim().length() != prefixLength) %7B%0A%09%09%09throw new IllegalArgumentException(%22Prefix length is not valid.%22);%0A%09%09%7D%0A%09%09extendedClasses.put(prefix, classes);%0A%09%7D%0A%0A
 %09@SuppressWa
@@ -1857,24 +1857,25 @@
 e() == clazz
+ 
 ) %7B%0A%09%09%09%09pref
@@ -1935,24 +1935,313 @@
  == null) %7B%0A
+%09%09%09for (Entry%3CString, Class%3C? extends BaseEntity%3E%5B%5D%3E entry : extendedClasses.entrySet()) %7B%0A%09%09%09%09for (Class%3C? extends BaseEntity%3E extendedClass : entry.getValue()) %7B%0A%09%09%09%09%09if (extendedClass == clazz) %7B%0A%09%09%09%09%09%09prefix = entry.getKey();%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (prefix == null) %7B%0A
 %09%09%09throw new

Handle inheritance in domain classes