Initial revision
[mir.git] / source / mir / entity / AbstractEntity.java
1 /**
2  * <b>abstrakte Basisklasse der Entity-Klassen</b><p>
3  */
4
5
6 package  mir.entity;
7
8 import java.lang.*;
9 import java.io.*;
10 import java.util.*;
11 import java.sql.*;
12
13 import mir.storage.*;
14 import mir.misc.*;
15
16 /**
17  * abstrakte Basisklasse der Entity-Klassen
18  *
19  * @author RK
20  * @version     29.6.1999
21  *
22  */
23
24 public class AbstractEntity implements Entity
25 {
26         private boolean             changed;
27         protected HashMap           theValuesHash;   // tablekey / value
28         protected StorageObject     theStorageObject;
29         protected static Logfile    theLog;
30         protected ArrayList         streamedInput=null;
31         private static int instances = 0;
32                 static {
33                         theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("Entity.Logfile"));
34                 }
35
36                 public AbstractEntity() {
37                         this.changed = false;
38                         instances++;
39                 }
40
41         /**
42          * Konstruktor
43          */
44         public AbstractEntity (StorageObject StorageObject) {
45                 this();
46                 setStorage(StorageObject);
47         }
48
49         /*
50          * Setzt das StorageObject der Entity.
51          */
52         public void setStorage (StorageObject storage) {
53                 this.theStorageObject = storage;
54         }
55
56         /**
57          * Setzt die Werte der Entity
58          * @param theStringValues
59          */
60
61         public void setValues(HashMap theStringValues)
62         {
63                 /** @todo should be synchronized */
64                 theValuesHash = new HashMap();
65                 String aKey;
66                 Set set = theStringValues.keySet();
67                 Iterator it = set.iterator();
68                 int size = set.size();
69                 for (int i = 0; i < size; i++) {
70                         aKey = (String)it.next();
71                         theValuesHash.put(aKey, (String)theStringValues.get(aKey));
72                 }
73  }
74
75         /**
76          * Liefert boolschen Wert, ob sich der Inhalt der Entity geändert hat.
77          * @return true wenn ja, sonst false
78          */
79         public boolean changed () {
80                 return  changed;
81         }
82
83         /**
84          * Liefert den Primärschluessel der Entity zurueck
85          * @return String Id
86          */
87         public String getId () {
88                 return  (String)getValue(theStorageObject.getIdName());
89         }
90
91         /**
92          * Setzt den Primaerschluessel der Entity
93          * @param id
94          */
95         public void setId (String id) {
96                 theValuesHash.put(theStorageObject.getIdName(), id);
97                         }
98
99         /**
100          * Liefert den Wert für einen Feldnamen zurueck
101          * @param theFieldString
102          * @return Wert für Feld
103          */
104         public String getValue (String theFieldString) {
105                 return  (String)theValuesHash.get(theFieldString);
106                 }
107
108         /**
109          * Fügt Entity via StorageObject in Datenbank ein.
110          * @return Primary Key der Entity
111          * @exception StorageObjectException
112          */
113         public String insert () throws StorageObjectException {
114                 theLog.printDebugInfo("Entity: trying to insert ...");
115                 if (theStorageObject != null) {
116                         return  theStorageObject.insert((Entity)this);
117                 }
118                 else
119                         throw  new StorageObjectException("Kein StorageObject gesetzt!");
120         }
121
122         /**
123          * Aktualisiert Aenderungen an der Entity in der Datenbank
124          * @exception StorageObjectException
125          */
126         public void update () throws StorageObjectException {
127                 theStorageObject.update((Entity)this);
128         }
129
130         /**
131          * Setzt den Wert fuer ein Feld
132          * @param theProp
133          * @param theValue
134          * @exception StorageObjectException
135          */
136         public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
137                 this.changed = true;
138                 if (isField(theProp))
139                         theValuesHash.put(theProp, theValue);
140                 else
141                         theLog.printWarning("Property not found: " + theProp);
142         }
143
144         /**
145          * Gibt die Feldnamen der Entity als ArrayList zurueck
146          * @return ArrayList mit Feldnamen
147          * @exception StorageObjectException wird geworfen, wenn kein Zugriff auf die Datenbank
148          *    möglich.
149          */
150         public ArrayList getFields () throws StorageObjectException {
151                 return  theStorageObject.getFields();
152                 }
153
154         /**
155          * Liefert ein int[] mit den Typen der Felder zurueck
156          * @return int[] mit den Feldtypen
157          * @exception StorageObjectException
158          */
159         public int[] getTypes () throws StorageObjectException {
160                 return  theStorageObject.getTypes();
161                 }
162
163         /**
164          * Liefert ArrayListe mit Feldnamen zurueck.
165          * @return Liste mit Feldnamen
166          * @exception StorageObjectException
167          */
168         public ArrayList getLabels () throws StorageObjectException {
169                 return  theStorageObject.getLabels();
170                 }
171
172         /**
173          * Liefert eine Hashmap mit allen Werten der Entity zurueck
174          * @return HashMap mit Feldname/Wert
175          */
176                 public HashMap getValues() {
177                         return theValuesHash;
178                 }
179
180                 /**
181                  *  Liefert einen ArrayList mit allen Datenbankfeldern, die
182                  *  als streamedInput ausgelesen werden muessen.
183                  *  Waere automatisierbar ueber die types (blob, etc.)
184                  *  Bisher manuell anzulegen in der erbenden Klasse
185                  */
186
187         public ArrayList streamedInput() {
188                 return streamedInput;
189         }
190
191          /* Fragt ab, ob fieldName einem Feld entspricht
192          * @param fieldName
193          * @return true, wennn ja, sonst false
194          * @exception StorageObjectException
195          */
196         public boolean isField (String fieldName) throws StorageObjectException {
197                 return  theStorageObject.getFields().contains(fieldName);
198         }
199
200          /** Liefert Anzahl der Instanzen zurück
201          * @return int
202          */
203         public int getInstances() {
204      return instances;
205   }
206         /**
207          * Gibt eine Instanz frei
208          */
209         public void finalize () {
210     instances--;
211     try {
212       super.finalize();
213     } catch (Throwable t) {
214       System.err.println(t.toString());
215     }
216   }
217 }
218