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