Ok, big merge. here's the new xml-config stuff in action. There's a few
[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
33     public AbstractEntity() {
34       theLog = Logfile.getInstance(this.getClass().getName());
35       this.changed = false;
36       instances++;
37       Integer i = new Integer(instances);
38       System.err.println("New abstract entity instance: "+i.toString());
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+theValue);
142
143   }
144
145   /**
146    * Gibt die Feldnamen der Entity als ArrayList zurueck
147    * @return ArrayList mit Feldnamen
148    * @exception StorageObjectException wird geworfen, wenn kein Zugriff auf die Datenbank
149    *    möglich.
150    */
151   public ArrayList getFields () throws StorageObjectException {
152     return  theStorageObject.getFields();
153     }
154
155   /**
156    * Liefert ein int[] mit den Typen der Felder zurueck
157    * @return int[] mit den Feldtypen
158    * @exception StorageObjectException
159    */
160   public int[] getTypes () throws StorageObjectException {
161     return  theStorageObject.getTypes();
162     }
163
164   /**
165    * Liefert ArrayListe mit Feldnamen zurueck.
166    * @return Liste mit Feldnamen
167    * @exception StorageObjectException
168    */
169   public ArrayList getLabels () throws StorageObjectException {
170     return  theStorageObject.getLabels();
171     }
172
173   /**
174    * Liefert eine Hashmap mit allen Werten der Entity zurueck
175    * @return HashMap mit Feldname/Wert
176    */
177     public HashMap getValues() {
178       return theValuesHash;
179     }
180
181     /**
182      *  Liefert einen ArrayList mit allen Datenbankfeldern, die
183      *  als streamedInput ausgelesen werden muessen.
184      *  Waere automatisierbar ueber die types (blob, etc.)
185      *  Bisher manuell anzulegen in der erbenden Klasse
186      */
187
188   public ArrayList streamedInput() {
189     return streamedInput;
190   }
191
192    /* Fragt ab, ob fieldName einem Feld entspricht
193    * @param fieldName
194    * @return true, wennn ja, sonst false
195    * @exception StorageObjectException
196    */
197   public boolean isField (String fieldName) throws StorageObjectException {
198     return  theStorageObject.getFields().contains(fieldName);
199   }
200
201    /** Liefert Anzahl der Instanzen zurück
202    * @return int
203    */
204   public int getInstances() {
205      return instances;
206   }
207   /**
208    * Gibt eine Instanz frei
209    */
210   public void finalize () {
211     instances--;
212     try {
213       super.finalize();
214     } catch (Throwable t) {
215       System.err.println(t.toString());
216     }
217   }
218 }
219