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;
46 * Base class the entities are derived from. Provides base functionality of
47 * an entity. Entities are used to represent rows of a database table.<p>
49 * @version $Id: Entity.java,v 1.21.2.6 2003/12/21 13:32:03 zapata Exp $
56 protected static MirPropertiesConfiguration configuration;
58 // protected Map theValuesHash; // tablekey / value
60 protected StorageObject theStorageObject;
61 protected List streamedInput = null;
62 protected LoggerWrapper logger;
66 configuration = MirPropertiesConfiguration.instance();
68 catch (PropertiesConfigExc e) {
69 throw new RuntimeException(e.getMessage());
74 logger = new LoggerWrapper("Entity");
76 values = new HashMap();
81 * @param StorageObject The StorageObject of the Entity.
84 public Entity(StorageObject StorageObject) {
87 setStorage(StorageObject);
95 public void setStorage(StorageObject storage) {
96 this.theStorageObject = storage;
100 * Sets the values of the Entity. (Only to be called by the Storage Object)
102 * @param aMap Map containing the new values of the Entity
105 public void setValues(Map aMap) {
107 Iterator i = aMap.entrySet().iterator();
109 while (i.hasNext()) {
110 Map.Entry entry = (Map.Entry) i.next();
112 setValueForProperty( (String) entry.getKey(), (String) entry.getValue());
119 * Returns the primary key of the Entity.
122 public String getId() {
123 return (String) getValue(theStorageObject.getIdName());
127 * Defines the primary key of the Entity (only to be set by the StorageObject)
130 public void setId(String id) {
131 setValueForProperty(theStorageObject.getIdName(), id);
135 * Returns the value of a field by field name.
136 * @param field The name of the field
137 * @return value of the field
139 public String getValue(String field) {
140 String returnValue = null;
143 returnValue = (String) values.get(field);
148 public boolean hasValueForField(String field) {
149 return values.containsKey(field);
153 * Insers Entity into the database via StorageObject
154 * @return Primary Key of the Entity
155 * @exception StorageObjectException
157 public String insert() throws StorageObjectExc {
158 logger.debug("Entity: trying to insert ...");
160 if (theStorageObject != null) {
161 return theStorageObject.insert(this);
164 throw new StorageObjectExc("theStorageObject == null!");
168 * Saves changes of this Entity to the database
169 * @exception StorageObjectException
171 public void update() throws StorageObjectFailure {
172 theStorageObject.update(this);
176 * Sets the value for a field. Issues a log message if the field name
177 * supplied was not found in the Entity.
178 * @param theProp The field name whose value has to be set
179 * @param theValue The new value of the field
180 * @exception StorageObjectException
182 public void setValueForProperty(String theProp, String theValue) throws StorageObjectFailure {
184 if (isField(theProp))
185 values.put(theProp, theValue);
187 logger.warn("Entity.setValueForProperty: Property not found: " + theProp + " (" + theValue + ")");
190 catch (Throwable t) {
191 logger.error("Entity.setValueForProperty: " + t.toString());
192 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
194 throw new StorageObjectFailure(t);
199 * Returns the field names of the Entity as ArrayListe.
200 * @return ArrayList with field names
201 * @exception StorageObjectException is throuwn if database access was impossible
203 public List getFields() throws StorageObjectFailure {
204 return theStorageObject.getFields();
208 * Returns an int[] with the types of the fields
209 * @return int[] that contains the types of the fields
210 * @exception StorageObjectException
212 public int[] getTypes() throws StorageObjectFailure {
213 return theStorageObject.getTypes();
217 * Returns an ArrayList with field names
218 * @return List with field names
219 * @exception StorageObjectException
221 public List getLabels() throws StorageObjectFailure {
222 return theStorageObject.getLabels();
227 * Returns an ArrayList with all database fields that can
228 * be evaluated as streamedInput.
229 * Could be automated by the types (blob, etc.)
230 * Until now to be created manually in the inheriting class
232 * Liefert einen ArrayList mit allen Datenbankfeldern, die
233 * als streamedInput ausgelesen werden muessen.
234 * Waere automatisierbar ueber die types (blob, etc.)
235 * Bisher manuell anzulegen in der erbenden Klasse
238 public List streamedInput() {
239 return streamedInput;
242 /** Returns whether fieldName is a valid field name of this Entity.
244 * @return true in case fieldName is a field name, else false.
245 * @exception StorageObjectException
247 public boolean isField(String fieldName) throws StorageObjectFailure {
248 return theStorageObject.getFields().contains(fieldName);
251 protected void throwStorageObjectFailure(Throwable e, String wo) throws StorageObjectFailure {
252 logger.error(e.toString() + " function: " + wo);
253 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
255 throw new StorageObjectFailure("Storage Object Exception in entity", e);
259 * Helper method to append a line to a field
261 public void appendLineToField(String aFieldName, String aLine) {
262 StringBuffer fieldContent = new StringBuffer();
264 fieldContent.append(StringRoutines.interpretAsString(getValue(aFieldName)));
266 catch (Throwable t) {
268 if (fieldContent.length() > 0 && fieldContent.charAt(fieldContent.length() - 1) != '\n') {
269 fieldContent.append('\n');
272 fieldContent.append(aLine);
273 setValueForProperty(aFieldName, fieldContent.toString());