d675ad61549e3ea5c7de3c8e5e5ecaf3e296f8a6
[mir.git] / source / mir / entity / Entity.java
1 /*
2  * Copyright (C) 2001, 2002 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  * 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.
29  */
30 package  mir.entity;
31
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36
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;
43
44 /**
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>
47  *
48  * @version $Id: Entity.java,v 1.21.2.5 2003/10/23 14:55:28 rk Exp $
49  * @author rk
50  *
51  */
52
53 public class Entity
54 {
55   protected static MirPropertiesConfiguration configuration;
56
57 //  protected Map theValuesHash; // tablekey / value
58   protected Map values;
59   protected StorageObject theStorageObject;
60   protected List streamedInput = null;
61   protected LoggerWrapper logger;
62
63   static {
64     try {
65       configuration = MirPropertiesConfiguration.instance();
66     }
67     catch (PropertiesConfigExc e) {
68       throw new RuntimeException(e.getMessage());
69     }
70   }
71
72   public Entity() {
73     logger = new LoggerWrapper("Entity");
74
75     values = new HashMap();
76   }
77
78   /**
79    * Constructor
80    * @param StorageObject The StorageObject of the Entity.
81    */
82
83   public Entity(StorageObject StorageObject) {
84     this();
85
86     setStorage(StorageObject);
87   }
88
89   /**
90    *
91    * @param storage
92    */
93
94   public void setStorage(StorageObject storage) {
95     this.theStorageObject = storage;
96   }
97
98   /**
99    * Sets the values of the Entity. (Only to be called by the Storage Object)
100    *
101    * @param aMap Map containing the new values of the Entity
102    */
103
104   public void setValues(Map aMap) {
105     if (aMap!=null) {
106       Iterator i = aMap.entrySet().iterator();
107       synchronized(this) {
108         while (i.hasNext()) {
109           Map.Entry entry = (Map.Entry) i.next();
110
111           setValueForProperty( (String) entry.getKey(), (String) entry.getValue());
112         }
113       }
114     }
115   }
116
117   /**
118    * Returns the primary key of the Entity.
119    * @return String Id
120    */
121   public String getId() {
122     return (String) getValue(theStorageObject.getIdName());
123   }
124
125   /**
126    * Defines the primary key of the Entity (only to be set by the StorageObject)
127    * @param id
128    */
129   public void setId(String id) {
130     setValueForProperty(theStorageObject.getIdName(), id);
131   }
132
133   /**
134    * Returns the value of a field by field name.
135    * @param field The name of the field
136    * @return value of the field
137    */
138   public String getValue(String field) {
139     String returnValue = null;
140
141     if (field != null) {
142       returnValue = (String) values.get(field);
143     }
144     return returnValue;
145   }
146
147   public boolean hasValueForField(String field) {
148     return values.containsKey(field);
149   }
150
151   /**
152    * Insers Entity into the database via StorageObject
153    * @return Primary Key of the Entity
154    * @exception StorageObjectException
155    */
156   public String insert() throws StorageObjectExc {
157     logger.debug("Entity: trying to insert ...");
158
159     if (theStorageObject != null) {
160       return theStorageObject.insert(this);
161     }
162     else
163       throw new StorageObjectExc("theStorageObject == null!");
164   }
165
166   /**
167    * Saves changes of this Entity to the database
168    * @exception StorageObjectException
169    */
170   public void update() throws StorageObjectFailure {
171     theStorageObject.update(this);
172   }
173
174   /**
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
180    */
181   public void setValueForProperty(String theProp, String theValue) throws StorageObjectFailure {
182     try {
183       if (isField(theProp))
184         values.put(theProp, theValue);
185       else {
186         logger.warn("Entity.setValueForProperty: Property not found: " + theProp + " (" + theValue + ")");
187       }
188     }
189     catch (Throwable t) {
190       logger.error("Entity.setValueForProperty: " + t.toString());
191       t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
192
193       throw new StorageObjectFailure(t);
194     }
195   }
196
197   /**
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
201    */
202   public List getFields() throws StorageObjectFailure {
203     return theStorageObject.getFields();
204   }
205
206   /**
207    * Returns an int[] with the types of the fields
208    * @return int[] that contains the types of the fields
209    * @exception StorageObjectException
210    */
211   public int[] getTypes() throws StorageObjectFailure {
212     return theStorageObject.getTypes();
213   }
214
215   /**
216    * Returns an ArrayList with field names
217    * @return List with field names
218    * @exception StorageObjectException
219    */
220   public List getLabels() throws StorageObjectFailure {
221     return theStorageObject.getLabels();
222   }
223
224
225   /**
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
230    *
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
235    */
236
237   public List streamedInput() {
238     return streamedInput;
239   }
240
241   /** Returns whether fieldName is a valid field name of this Entity.
242    * @param fieldName
243    * @return true in case fieldName is a field name, else false.
244    * @exception StorageObjectException
245    */
246   public boolean isField(String fieldName) throws StorageObjectFailure {
247     return theStorageObject.getFields().contains(fieldName);
248   }
249
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));
253
254     throw new StorageObjectFailure("Storage Object Exception in entity", e);
255   }
256 }
257