01: package org.osbl.domain.logic;
02:
03: import org.conform.AbstractDynamicDomainProvider;
04: import org.osbl.domain.model.Domain;
05: import org.osbl.domain.model.DomainValue;
06: import org.osbl.domain.DomainRegistry;
07:
08: import java.util.*;
09:
10: public class ManagableDomainProvider<T> extends
11: AbstractDynamicDomainProvider<T> {
12: protected String key;
13:
14: public ManagableDomainProvider(String key) {
15: this .key = key;
16: }
17:
18: public String getKey() {
19: return key;
20: }
21:
22: protected List<T> createValues() {
23: Domain domain = DomainRegistry.getDomain(key);
24: Class<T> type = loadClass(domain);
25: List<T> values = new ArrayList<T>(domain.getValues().size() + 1);
26: Map<T, String> value2String = new HashMap<T, String>(domain
27: .getValues().size() + 1);
28: values.add(null);
29: value2String.put(null, "");
30: for (DomainValue domainValue : domain.getValues()) {
31: T value = instantiate(type, domainValue.getValue());
32: values.add(value);
33: value2String.put(value, domainValue.getName());
34: }
35: setValue2String(value2String);
36: return values;
37: }
38:
39: private T instantiate(Class<T> type, String string) {
40: try {
41: if (type == String.class && string == null)
42: string = "";
43: return type.getConstructor(String.class)
44: .newInstance(string);
45: } catch (Exception e) {
46: throw new RuntimeException(e);
47: }
48: }
49:
50: private Class<T> loadClass(Domain domain) {
51: try {
52: return (Class<T>) Class.forName(domain.getType());
53: } catch (ClassNotFoundException e) {
54: throw new RuntimeException(e);
55: }
56: }
57: }
|