Initial revision
[mir.git] / source / mir / entity / AbstractEntity.java
diff --git a/source/mir/entity/AbstractEntity.java b/source/mir/entity/AbstractEntity.java
new file mode 100755 (executable)
index 0000000..3f2c0db
--- /dev/null
@@ -0,0 +1,218 @@
+/**
+ * <b>abstrakte Basisklasse der Entity-Klassen</b><p>
+ */
+
+
+package  mir.entity;
+
+import java.lang.*;
+import java.io.*;
+import java.util.*;
+import java.sql.*;
+
+import mir.storage.*;
+import mir.misc.*;
+
+/**
+ * abstrakte Basisklasse der Entity-Klassen
+ *
+ * @author RK
+ * @version    29.6.1999
+ *
+ */
+
+public class AbstractEntity implements Entity
+{
+       private boolean             changed;
+       protected HashMap           theValuesHash;   // tablekey / value
+       protected StorageObject     theStorageObject;
+       protected static Logfile    theLog;
+       protected ArrayList         streamedInput=null;
+       private static int instances = 0;
+               static {
+                       theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("Entity.Logfile"));
+               }
+
+               public AbstractEntity() {
+                       this.changed = false;
+                       instances++;
+               }
+
+       /**
+        * Konstruktor
+        */
+       public AbstractEntity (StorageObject StorageObject) {
+               this();
+               setStorage(StorageObject);
+       }
+
+       /*
+        * Setzt das StorageObject der Entity.
+        */
+       public void setStorage (StorageObject storage) {
+               this.theStorageObject = storage;
+       }
+
+       /**
+        * Setzt die Werte der Entity
+        * @param theStringValues
+        */
+
+       public void setValues(HashMap theStringValues)
+       {
+               /** @todo should be synchronized */
+               theValuesHash = new HashMap();
+               String aKey;
+               Set set = theStringValues.keySet();
+               Iterator it = set.iterator();
+               int size = set.size();
+               for (int i = 0; i < size; i++) {
+                       aKey = (String)it.next();
+                       theValuesHash.put(aKey, (String)theStringValues.get(aKey));
+               }
+ }
+
+       /**
+        * Liefert boolschen Wert, ob sich der Inhalt der Entity geändert hat.
+        * @return true wenn ja, sonst false
+        */
+       public boolean changed () {
+               return  changed;
+       }
+
+       /**
+        * Liefert den Primärschluessel der Entity zurueck
+        * @return String Id
+        */
+       public String getId () {
+               return  (String)getValue(theStorageObject.getIdName());
+       }
+
+       /**
+        * Setzt den Primaerschluessel der Entity
+        * @param id
+        */
+       public void setId (String id) {
+               theValuesHash.put(theStorageObject.getIdName(), id);
+                       }
+
+       /**
+        * Liefert den Wert für einen Feldnamen zurueck
+        * @param theFieldString
+        * @return Wert für Feld
+        */
+       public String getValue (String theFieldString) {
+               return  (String)theValuesHash.get(theFieldString);
+               }
+
+       /**
+        * Fügt Entity via StorageObject in Datenbank ein.
+        * @return Primary Key der Entity
+        * @exception StorageObjectException
+        */
+       public String insert () throws StorageObjectException {
+               theLog.printDebugInfo("Entity: trying to insert ...");
+               if (theStorageObject != null) {
+                       return  theStorageObject.insert((Entity)this);
+               }
+               else
+                       throw  new StorageObjectException("Kein StorageObject gesetzt!");
+       }
+
+       /**
+        * Aktualisiert Aenderungen an der Entity in der Datenbank
+        * @exception StorageObjectException
+        */
+       public void update () throws StorageObjectException {
+               theStorageObject.update((Entity)this);
+       }
+
+       /**
+        * Setzt den Wert fuer ein Feld
+        * @param theProp
+        * @param theValue
+        * @exception StorageObjectException
+        */
+       public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
+               this.changed = true;
+               if (isField(theProp))
+                       theValuesHash.put(theProp, theValue);
+               else
+                       theLog.printWarning("Property not found: " + theProp);
+       }
+
+       /**
+        * Gibt die Feldnamen der Entity als ArrayList zurueck
+        * @return ArrayList mit Feldnamen
+        * @exception StorageObjectException wird geworfen, wenn kein Zugriff auf die Datenbank
+        *    möglich.
+        */
+       public ArrayList getFields () throws StorageObjectException {
+               return  theStorageObject.getFields();
+               }
+
+       /**
+        * Liefert ein int[] mit den Typen der Felder zurueck
+        * @return int[] mit den Feldtypen
+        * @exception StorageObjectException
+        */
+       public int[] getTypes () throws StorageObjectException {
+               return  theStorageObject.getTypes();
+               }
+
+       /**
+        * Liefert ArrayListe mit Feldnamen zurueck.
+        * @return Liste mit Feldnamen
+        * @exception StorageObjectException
+        */
+       public ArrayList getLabels () throws StorageObjectException {
+               return  theStorageObject.getLabels();
+               }
+
+       /**
+        * Liefert eine Hashmap mit allen Werten der Entity zurueck
+        * @return HashMap mit Feldname/Wert
+        */
+               public HashMap getValues() {
+                       return theValuesHash;
+               }
+
+               /**
+                *  Liefert einen ArrayList mit allen Datenbankfeldern, die
+                *  als streamedInput ausgelesen werden muessen.
+                *  Waere automatisierbar ueber die types (blob, etc.)
+                *  Bisher manuell anzulegen in der erbenden Klasse
+                */
+
+       public ArrayList streamedInput() {
+               return streamedInput;
+       }
+
+        /* Fragt ab, ob fieldName einem Feld entspricht
+        * @param fieldName
+        * @return true, wennn ja, sonst false
+        * @exception StorageObjectException
+        */
+       public boolean isField (String fieldName) throws StorageObjectException {
+               return  theStorageObject.getFields().contains(fieldName);
+       }
+
+        /** Liefert Anzahl der Instanzen zurück
+        * @return int
+        */
+       public int getInstances() {
+     return instances;
+  }
+       /**
+        * Gibt eine Instanz frei
+        */
+       public void finalize () {
+    instances--;
+    try {
+      super.finalize();
+    } catch (Throwable t) {
+      System.err.println(t.toString());
+    }
+  }
+}
+