full support for comment status in admin
[mir.git] / source / mir / entity / adapter / EntityAdapter.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mir.entity.adapter;
33
34 import java.util.*;
35 import mir.entity.*;
36 import mir.util.*;
37
38 public class EntityAdapter implements Map {
39   private Entity entity;
40   private EntityAdapterDefinition definition;
41   private Map calculatedFieldsCache;
42   private EntityAdapterModel model;
43
44   public EntityAdapter(Entity anEntity, EntityAdapterDefinition aDefinition, EntityAdapterModel aModel) {
45     entity = anEntity;
46     definition = aDefinition;
47     calculatedFieldsCache = new HashMap();
48     model = aModel;
49   }
50
51   public boolean containsKey(Object aKey) {
52     try {
53       if (aKey instanceof String)
54         return     entity.hasValueForField((String) aKey)
55                 || definition.hasCalculatedField((String) aKey)
56                 || entity.getFields().contains(aKey);
57     }
58     catch (Throwable t) {
59     }
60
61     return false;
62   }
63
64   public boolean equals(Object anObject) {
65     return        anObject instanceof EntityAdapter
66            && ((EntityAdapter) anObject).entity.equals(entity);
67   }
68
69   public int hashCode() {
70     return entity.hashCode();
71   }
72
73   public Entity getEntity() {
74     return entity;
75   }
76
77   public EntityAdapterModel getModel() {
78     return model;
79   }
80
81   public Object get(Object aKey) {
82     Object result;
83
84     if (calculatedFieldsCache.containsKey(aKey)) {
85       return calculatedFieldsCache.get(aKey);
86     }
87     else if (aKey instanceof String && definition.hasCalculatedField((String) aKey)) {
88       result = definition.getCalculatedField((String) aKey).getValue(this);
89       calculatedFieldsCache.put(aKey, result);
90
91       return result;
92     }
93     else if (aKey instanceof String) {
94       return entity.getValue((String) aKey);
95     }
96     else {
97       return null;
98     }
99   }
100
101   public boolean isEmpty() {
102     return false;
103   }
104
105   public Set keySet() {
106     throw new UnsupportedOperationException("EntityAdapter.keySet()");
107   }
108
109   public Object put(Object aKey, Object value) {
110     throw new UnsupportedOperationException("EntityAdapter.put()");
111   }
112
113   public void putAll(Map t) {
114     throw new UnsupportedOperationException("EntityAdapter.putAll()");
115   }
116
117   public Object remove(Object aKey) {
118     throw new UnsupportedOperationException("EntityAdapter.remove()");
119   }
120
121   public int size() {
122     throw new UnsupportedOperationException("EntityAdapter.size()");
123   }
124
125   public Collection values() {
126     throw new UnsupportedOperationException("EntityAdapter.values()");
127   }
128
129   public void clear() {
130     throw new UnsupportedOperationException("EntityAdapter.clear()");
131   }
132
133   public boolean containsValue(Object value) {
134     throw new UnsupportedOperationException("EntityAdapter.containsValue()");
135   }
136
137   public Set entrySet() {
138     throw new UnsupportedOperationException("EntityAdapter.entrySet()");
139   }
140
141   public Object getRelation(String aWhereClause, String anOrderByClause, String aDefinition) {
142     try {
143       return
144           new CachingRewindableIterator(
145             new EntityIteratorAdapter(
146                 aWhereClause, anOrderByClause, -1, getModel(), aDefinition));
147     }
148     catch (Throwable t) {
149       throw new RuntimeException(t.getMessage());
150     }
151   }
152
153   public Object getToOneRelation(String aWhereClause, String anOrderByClause, String aDefinition) {
154     try {
155       Iterator i = new EntityIteratorAdapter(aWhereClause, anOrderByClause, -1, getModel(), aDefinition);
156
157       if (i.hasNext())
158         return i.next();
159       else
160         return null;
161     }
162     catch (Throwable t) {
163       throw new RuntimeException(t.getMessage());
164     }
165   }
166 }