introduced AdapterEntityModel
[mir.git] / source / mir / entity / adapter / EntityAdapter.java
1 package mir.entity.adapter;
2
3 import java.util.*;
4 import mir.entity.*;
5 import mir.util.*;
6
7 public class EntityAdapter implements Map {
8   private Entity entity;
9   private EntityAdapterDefinition definition;
10   private Map calculatedFieldsCache;
11   private EntityAdapterModel model;
12
13   public EntityAdapter(Entity anEntity, EntityAdapterDefinition aDefinition, EntityAdapterModel aModel) {
14     entity = anEntity;
15     definition = aDefinition;
16     calculatedFieldsCache = new HashMap();
17     model = aModel;
18   }
19
20   public boolean containsKey(Object aKey) {
21     try {
22       if (aKey instanceof String)
23         return     entity.hasValueForField((String) aKey)
24                 || definition.hasCalculatedField((String) aKey)
25                 || entity.getFields().contains(aKey);
26     }
27     catch (Throwable t) {
28     }
29
30     return false;
31   }
32
33   public boolean equals(Object anObject) {
34     return        anObject instanceof EntityAdapter
35            && ((EntityAdapter) anObject).entity.equals(entity);
36   }
37
38   public int hashCode() {
39     return entity.hashCode();
40   }
41
42   public Entity getEntity() {
43     return entity;
44   }
45
46   public EntityAdapterModel getModel() {
47     return model;
48   }
49
50   public Object get(Object aKey) {
51     Object result;
52
53     if (calculatedFieldsCache.containsKey(aKey)) {
54       return calculatedFieldsCache.get(aKey);
55     }
56     else if (aKey instanceof String && definition.hasCalculatedField((String) aKey)) {
57       result = definition.getCalculatedField((String) aKey).getValue(this);
58       calculatedFieldsCache.put(aKey, result);
59
60       return result;
61     }
62     else if (aKey instanceof String) {
63       return entity.getValue((String) aKey);
64     }
65     else {
66       return null;
67     }
68   }
69
70   public boolean isEmpty() {
71     throw new UnsupportedOperationException("EntityAdapter.isEmpty()");
72   }
73
74   public Set keySet() {
75     throw new UnsupportedOperationException("EntityAdapter.keySet()");
76   }
77
78   public Object put(Object aKey, Object value) {
79     throw new UnsupportedOperationException("EntityAdapter.put()");
80   }
81
82   public void putAll(Map t) {
83     throw new UnsupportedOperationException("EntityAdapter.putAll()");
84   }
85
86   public Object remove(Object aKey) {
87     throw new UnsupportedOperationException("EntityAdapter.remove()");
88   }
89
90   public int size() {
91     throw new UnsupportedOperationException("EntityAdapter.size()");
92   }
93
94   public Collection values() {
95     throw new UnsupportedOperationException("EntityAdapter.values()");
96   }
97
98   public void clear() {
99     throw new UnsupportedOperationException("EntityAdapter.clear()");
100   }
101
102   public boolean containsValue(Object value) {
103     throw new UnsupportedOperationException("EntityAdapter.containsValue()");
104   }
105
106   public Set entrySet() {
107     throw new UnsupportedOperationException("EntityAdapter.entrySet()");
108   }
109
110   public Object getRelation(String aWhereClause, String anOrderByClause, String aDefinition) {
111     try {
112       return
113           new CachingRewindableIterator(
114             new EntityIteratorAdapter(
115                 aWhereClause, anOrderByClause, -1, getModel(), aDefinition));
116     }
117     catch (Throwable t) {
118       throw new RuntimeException(t.getMessage());
119     }
120   }
121
122 }