2 * Copyright (C) 2005 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 * You must obey the GNU General Public License in all respects for all of the code used
23 * other than the above mentioned libraries. If you modify this file, you may extend this
24 * exception to your version of the file, but you are not obligated to do so.
25 * If you do not wish to do so, delete this exception statement from your version.
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
34 import mir.config.MirPropertiesConfiguration;
35 import mir.log.LoggerWrapper;
36 import mir.storage.DatabaseExc;
37 import mir.storage.DatabaseFailure;
38 import mir.storage.Database;
41 * Base class the entities are derived from. Provides base functionality of
44 * @version $Id: AbstractEntity.java,v 1.8.2.9 2005/12/24 11:43:34 zapata Exp $
47 public class AbstractEntity implements Entity {
48 protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
51 protected Database database;
53 public AbstractEntity() {
54 values = new HashMap();
57 public void setStorage(Database aDatabase) {
62 public void setFieldValues(Map aMap) {
64 Iterator i = aMap.entrySet().iterator();
67 Map.Entry entry = (Map.Entry) i.next();
69 setFieldValue( (String) entry.getKey(), (String) entry.getValue());
76 public String getId() {
77 return getFieldValue(database.getIdFieldName());
81 public void setId(String id) {
82 setFieldValue(database.getIdFieldName(), id);
86 public String insert() throws DatabaseExc {
87 getLogger().debug("Entity: trying to insert ...");
89 if (database != null) {
90 return database.insert(this);
93 throw new DatabaseExc("database == null!");
97 public void update() throws DatabaseFailure {
98 database.update(this);
102 public String getFieldValue(String aFieldName) {
103 String returnValue = null;
105 if (aFieldName != null) {
106 returnValue = (String) values.get(aFieldName);
112 public boolean hasFieldValue(String aFieldName) {
113 return values.containsKey(aFieldName);
117 * Sets the value for a field. Issues a log message if the field name
118 * supplied was not found in the Entity.
119 * @param theProp The field name whose value has to be set
120 * @param theValue The new value of the field
121 * @exception DatabaseFailure
123 public void setFieldValue(String theProp, String theValue) throws DatabaseFailure {
124 if (hasField(theProp))
125 values.put(theProp, theValue);
127 getLogger().warn("Entity.setFieldValue: Property not found: " + theProp + " (" + theValue + ")");
132 * Returns the field names of the Entity
134 public List getFieldNames() throws DatabaseFailure {
135 return database.getFieldNames();
138 /** Returns whether fieldName is a valid field name of this Entity.
140 * @return true in case fieldName is a field name, else false.
141 * @exception DatabaseFailure
143 public boolean hasField(String fieldName) throws DatabaseFailure {
144 return getFieldNames().contains(fieldName);
147 protected LoggerWrapper getLogger() {
148 return new LoggerWrapper("Entity");