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 any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
37 import mir.config.MirPropertiesConfiguration;
38 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
39 import mir.log.LoggerWrapper;
40 import mir.storage.StorageObject;
41 import mir.storage.StorageObjectExc;
42 import mir.storage.StorageObjectFailure;
45 * Base class the entities are derived from. Provides base functionality of
46 * an entity. Entities are used to represent rows of a database table.<p>
48 * @version $Id: Entity.java,v 1.21.2.5 2003/10/23 14:55:28 rk Exp $
55 protected static MirPropertiesConfiguration configuration;
57 // protected Map theValuesHash; // tablekey / value
59 protected StorageObject theStorageObject;
60 protected List streamedInput = null;
61 protected LoggerWrapper logger;
65 configuration = MirPropertiesConfiguration.instance();
67 catch (PropertiesConfigExc e) {
68 throw new RuntimeException(e.getMessage());
73 logger = new LoggerWrapper("Entity");
75 values = new HashMap();
80 * @param StorageObject The StorageObject of the Entity.
83 public Entity(StorageObject StorageObject) {
86 setStorage(StorageObject);
94 public void setStorage(StorageObject storage) {
95 this.theStorageObject = storage;
99 * Sets the values of the Entity. (Only to be called by the Storage Object)
101 * @param aMap Map containing the new values of the Entity
104 public void setValues(Map aMap) {
106 Iterator i = aMap.entrySet().iterator();
108 while (i.hasNext()) {
109 Map.Entry entry = (Map.Entry) i.next();
111 setValueForProperty( (String) entry.getKey(), (String) entry.getValue());
118 * Returns the primary key of the Entity.
121 public String getId() {
122 return (String) getValue(theStorageObject.getIdName());
126 * Defines the primary key of the Entity (only to be set by the StorageObject)
129 public void setId(String id) {
130 setValueForProperty(theStorageObject.getIdName(), id);
134 * Returns the value of a field by field name.
135 * @param field The name of the field
136 * @return value of the field
138 public String getValue(String field) {
139 String returnValue = null;
142 returnValue = (String) values.get(field);
147 public boolean hasValueForField(String field) {
148 return values.containsKey(field);
152 * Insers Entity into the database via StorageObject
153 * @return Primary Key of the Entity
154 * @exception StorageObjectException
156 public String insert() throws StorageObjectExc {
157 logger.debug("Entity: trying to insert ...");
159 if (theStorageObject != null) {
160 return theStorageObject.insert(this);
163 throw new StorageObjectExc("theStorageObject == null!");
167 * Saves changes of this Entity to the database
168 * @exception StorageObjectException
170 public void update() throws StorageObjectFailure {
171 theStorageObject.update(this);
175 * Sets the value for a field. Issues a log message if the field name
176 * supplied was not found in the Entity.
177 * @param theProp The field name whose value has to be set
178 * @param theValue The new value of the field
179 * @exception StorageObjectException
181 public void setValueForProperty(String theProp, String theValue) throws StorageObjectFailure {
183 if (isField(theProp))
184 values.put(theProp, theValue);
186 logger.warn("Entity.setValueForProperty: Property not found: " + theProp + " (" + theValue + ")");
189 catch (Throwable t) {
190 logger.error("Entity.setValueForProperty: " + t.toString());
191 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
193 throw new StorageObjectFailure(t);
198 * Returns the field names of the Entity as ArrayListe.
199 * @return ArrayList with field names
200 * @exception StorageObjectException is throuwn if database access was impossible
202 public List getFields() throws StorageObjectFailure {
203 return theStorageObject.getFields();
207 * Returns an int[] with the types of the fields
208 * @return int[] that contains the types of the fields
209 * @exception StorageObjectException
211 public int[] getTypes() throws StorageObjectFailure {
212 return theStorageObject.getTypes();
216 * Returns an ArrayList with field names
217 * @return List with field names
218 * @exception StorageObjectException
220 public List getLabels() throws StorageObjectFailure {
221 return theStorageObject.getLabels();
226 * Returns an ArrayList with all database fields that can
227 * be evaluated as streamedInput.
228 * Could be automated by the types (blob, etc.)
229 * Until now to be created manually in the inheriting class
231 * Liefert einen ArrayList mit allen Datenbankfeldern, die
232 * als streamedInput ausgelesen werden muessen.
233 * Waere automatisierbar ueber die types (blob, etc.)
234 * Bisher manuell anzulegen in der erbenden Klasse
237 public List streamedInput() {
238 return streamedInput;
241 /** Returns whether fieldName is a valid field name of this Entity.
243 * @return true in case fieldName is a field name, else false.
244 * @exception StorageObjectException
246 public boolean isField(String fieldName) throws StorageObjectFailure {
247 return theStorageObject.getFields().contains(fieldName);
250 protected void throwStorageObjectFailure(Throwable e, String wo) throws StorageObjectFailure {
251 logger.error(e.toString() + " function: " + wo);
252 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
254 throw new StorageObjectFailure("Storage Object Exception in entity", e);