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.log.LoggerWrapper;
39 import mir.storage.DatabaseExc;
40 import mir.storage.DatabaseFailure;
41 import mir.storage.Database;
44 * Base class the entities are derived from. Provides base functionality of
47 * @version $Id: AbstractEntity.java,v 1.8.2.8 2005/10/30 00:46:57 zapata Exp $
50 public class AbstractEntity implements Entity {
51 protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
54 protected Database database;
56 public AbstractEntity() {
57 values = new HashMap();
60 public void setStorage(Database aDatabase) {
65 public void setFieldValues(Map aMap) {
67 Iterator i = aMap.entrySet().iterator();
70 Map.Entry entry = (Map.Entry) i.next();
72 setFieldValue( (String) entry.getKey(), (String) entry.getValue());
79 public String getId() {
80 return getFieldValue(database.getIdFieldName());
84 public void setId(String id) {
85 setFieldValue(database.getIdFieldName(), id);
89 public String insert() throws DatabaseExc {
90 getLogger().debug("Entity: trying to insert ...");
92 if (database != null) {
93 return database.insert(this);
96 throw new DatabaseExc("database == null!");
100 public void update() throws DatabaseFailure {
101 database.update(this);
105 public String getFieldValue(String aFieldName) {
106 String returnValue = null;
108 if (aFieldName != null) {
109 returnValue = (String) values.get(aFieldName);
115 public boolean hasFieldValue(String aFieldName) {
116 return values.containsKey(aFieldName);
120 * Sets the value for a field. Issues a log message if the field name
121 * supplied was not found in the Entity.
122 * @param theProp The field name whose value has to be set
123 * @param theValue The new value of the field
124 * @exception DatabaseFailure
126 public void setFieldValue(String theProp, String theValue) throws DatabaseFailure {
127 if (hasField(theProp))
128 values.put(theProp, theValue);
130 getLogger().warn("Entity.setFieldValue: Property not found: " + theProp + " (" + theValue + ")");
135 * Returns the field names of the Entity
137 public List getFieldNames() throws DatabaseFailure {
138 return database.getFieldNames();
141 /** Returns whether fieldName is a valid field name of this Entity.
143 * @return true in case fieldName is a field name, else false.
144 * @exception DatabaseFailure
146 public boolean hasField(String fieldName) throws DatabaseFailure {
147 return getFieldNames().contains(fieldName);
150 protected LoggerWrapper getLogger() {
151 return new LoggerWrapper("Entity");