2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mir.entity.adapter;
38 public class EntityAdapter implements Map {
39 private Entity entity;
40 private EntityAdapterDefinition definition;
41 private Map calculatedFieldsCache;
42 private EntityAdapterModel model;
44 public EntityAdapter(Entity anEntity, EntityAdapterDefinition aDefinition, EntityAdapterModel aModel) {
46 definition = aDefinition;
47 calculatedFieldsCache = new HashMap();
51 public boolean containsKey(Object aKey) {
53 if (aKey instanceof String)
54 return entity.hasValueForField((String) aKey)
55 || definition.hasCalculatedField((String) aKey)
56 || entity.getFields().contains(aKey);
64 public boolean equals(Object anObject) {
65 return anObject instanceof EntityAdapter
66 && ((EntityAdapter) anObject).entity.equals(entity);
69 public int hashCode() {
70 return entity.hashCode();
73 public Entity getEntity() {
77 public EntityAdapterModel getModel() {
81 public Object get(Object aKey) {
84 if (calculatedFieldsCache.containsKey(aKey)) {
85 return calculatedFieldsCache.get(aKey);
87 else if (aKey instanceof String && definition.hasCalculatedField((String) aKey)) {
88 result = definition.getCalculatedField((String) aKey).getValue(this);
89 calculatedFieldsCache.put(aKey, result);
93 else if (aKey instanceof String) {
94 return entity.getValue((String) aKey);
101 public boolean isEmpty() {
102 throw new UnsupportedOperationException("EntityAdapter.isEmpty()");
105 public Set keySet() {
106 throw new UnsupportedOperationException("EntityAdapter.keySet()");
109 public Object put(Object aKey, Object value) {
110 throw new UnsupportedOperationException("EntityAdapter.put()");
113 public void putAll(Map t) {
114 throw new UnsupportedOperationException("EntityAdapter.putAll()");
117 public Object remove(Object aKey) {
118 throw new UnsupportedOperationException("EntityAdapter.remove()");
122 throw new UnsupportedOperationException("EntityAdapter.size()");
125 public Collection values() {
126 throw new UnsupportedOperationException("EntityAdapter.values()");
129 public void clear() {
130 throw new UnsupportedOperationException("EntityAdapter.clear()");
133 public boolean containsValue(Object value) {
134 throw new UnsupportedOperationException("EntityAdapter.containsValue()");
137 public Set entrySet() {
138 throw new UnsupportedOperationException("EntityAdapter.entrySet()");
141 public Object getRelation(String aWhereClause, String anOrderByClause, String aDefinition) {
144 new CachingRewindableIterator(
145 new EntityIteratorAdapter(
146 aWhereClause, anOrderByClause, -1, getModel(), aDefinition));
148 catch (Throwable t) {
149 throw new RuntimeException(t.getMessage());
153 public Object getToOneRelation(String aWhereClause, String anOrderByClause, String aDefinition) {
155 Iterator i = new EntityIteratorAdapter(aWhereClause, anOrderByClause, -1, getModel(), aDefinition);
162 catch (Throwable t) {
163 throw new RuntimeException(t.getMessage());