package org.osbl.domain.logic;
import org.conform.AbstractDynamicDomainProvider;
import org.osbl.domain.model.Domain;
import org.osbl.domain.model.DomainValue;
import org.osbl.domain.DomainRegistry;
import java.util.*;
public class ManagableDomainProvider<T>
extends AbstractDynamicDomainProvider<T>
{
protected String key;
public ManagableDomainProvider(String key) {
this.key = key;
}
public String getKey() {
return key;
}
protected List<T> createValues() {
Domain domain = DomainRegistry.getDomain(key);
Class<T> type = loadClass(domain);
List<T> values = new ArrayList<T>(domain.getValues().size() + 1);
Map<T, String> value2String = new HashMap<T, String>(domain.getValues().size() + 1);
values.add(null);
value2String.put(null, "");
for (DomainValue domainValue : domain.getValues()) {
T value = instantiate(type, domainValue.getValue());
values.add(value);
value2String.put(value, domainValue.getName());
}
setValue2String(value2String);
return values;
}
private T instantiate(Class<T> type, String string) {
try {
if (type == String.class && string == null)
string = "";
return type.getConstructor(String.class).newInstance(string);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private Class<T> loadClass(Domain domain) {
try {
return (Class<T>)Class.forName(domain.getType());
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
|