scripts/mir-setup/README: update with link to new doc on wiki
[mir.git] / source / mir / entity / AbstractEntity.java
1 /*
2  * Copyright (C) 2005 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
26  */
27 package  mir.entity;
28
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33
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;
39
40 /**
41  * Base class the entities are derived from. Provides base functionality of
42  * an entity.
43  *
44  * @version $Id: AbstractEntity.java,v 1.8.2.9 2005/12/24 11:43:34 zapata Exp $
45  */
46
47 public class AbstractEntity implements Entity {
48   protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
49
50   protected Map values;
51   protected Database database;
52
53   public AbstractEntity() {
54     values = new HashMap();
55   }
56
57   public void setStorage(Database aDatabase) {
58     database = aDatabase;
59   }
60
61   /** {@inheritDoc} */
62   public void setFieldValues(Map aMap) {
63     if (aMap!=null) {
64       Iterator i = aMap.entrySet().iterator();
65       synchronized(this) {
66         while (i.hasNext()) {
67           Map.Entry entry = (Map.Entry) i.next();
68
69           setFieldValue( (String) entry.getKey(), (String) entry.getValue());
70         }
71       }
72     }
73   }
74
75   /** {@inheritDoc} */
76   public String getId() {
77     return getFieldValue(database.getIdFieldName());
78   }
79
80   /** {@inheritDoc} */
81   public void setId(String id) {
82     setFieldValue(database.getIdFieldName(), id);
83   }
84
85   /** {@inheritDoc} */
86   public String insert() throws DatabaseExc {
87     getLogger().debug("Entity: trying to insert ...");
88
89     if (database != null) {
90       return database.insert(this);
91     }
92
93     throw new DatabaseExc("database == null!");
94   }
95
96   /** {@inheritDoc} */
97   public void update() throws DatabaseFailure {
98     database.update(this);
99   }
100
101   /** {@inheritDoc} */
102   public String getFieldValue(String aFieldName) {
103     String returnValue = null;
104
105     if (aFieldName != null) {
106       returnValue = (String) values.get(aFieldName);
107     }
108     return returnValue;
109   }
110
111   /** {@inheritDoc} */
112   public boolean hasFieldValue(String aFieldName) {
113     return values.containsKey(aFieldName);
114   }
115
116   /**
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
122    */
123   public void setFieldValue(String theProp, String theValue) throws DatabaseFailure {
124     if (hasField(theProp))
125       values.put(theProp, theValue);
126     else {
127       getLogger().warn("Entity.setFieldValue: Property not found: " + theProp + " (" + theValue + ")");
128     }
129   }
130
131   /**
132    * Returns the field names of the Entity
133    */
134   public List getFieldNames() throws DatabaseFailure {
135     return database.getFieldNames();
136   }
137
138   /** Returns whether fieldName is a valid field name of this Entity.
139    * @param fieldName
140    * @return true in case fieldName is a field name, else false.
141    * @exception DatabaseFailure
142    */
143   public boolean hasField(String fieldName) throws DatabaseFailure {
144     return getFieldNames().contains(fieldName);
145   }
146
147   protected LoggerWrapper getLogger() {
148     return new LoggerWrapper("Entity");
149   }
150 }
151