fixed a tiny bug with the producer logger
[mir.git] / source / mir / storage / Database.java
1 /*
2  * put your module comment here
3  */
4 package mir.storage;
5
6 import  java.sql.*;
7 import  java.lang.*;
8 import  java.io.*;
9 import  java.util.*;
10 import  freemarker.template.*;
11 import  com.codestudio.sql.*;
12 import  com.codestudio.util.*;
13
14 import  mir.storage.StorageObject;
15 import  mir.storage.store.*;
16 import  mir.entity.*;
17 import  mir.misc.*;
18
19
20 /**
21  * Diese Klasse implementiert die Zugriffsschicht auf die Datenbank.
22  * Alle Projektspezifischen Datenbankklassen erben von dieser Klasse.
23  * In den Unterklassen wird im Minimalfall nur die Tabelle angegeben.
24  * Im Konfigurationsfile findet sich eine Verweis auf den verwendeten
25  * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
26  * Datenbank erfolgt.
27  *
28  * @author RK
29  * @version 16.7.1999
30  */
31 public class Database implements StorageObject {
32
33         protected String                    theTable;
34         protected String                    theCoreTable=null;
35         protected String                    thePKeyName="id";
36         protected int                       thePKeyType, thePKeyIndex;
37         protected boolean                   evaluatedMetaData=false;
38         protected ArrayList                 metadataFields,metadataLabels,
39                                                                                                                                                         metadataNotNullFields;
40         protected int[]                     metadataTypes;
41         protected Class                     theEntityClass;
42         protected StorageObject             myselfDatabase;
43         protected SimpleList                popupCache=null;
44         protected boolean                   hasPopupCache = false;
45         protected SimpleHash                hashCache=null;
46         protected boolean                   hasTimestamp=true;
47         private String                      database_driver, database_url;
48         private int                         defaultLimit;
49         protected DatabaseAdaptor           theAdaptor;
50         protected Logfile                   theLog;
51         private static Class                GENERIC_ENTITY_CLASS=null,
52                                       STORABLE_OBJECT_ENTITY_CLASS=null;
53   private static SimpleHash           POPUP_EMTYLINE=new SimpleHash();
54   protected static final ObjectStore  o_store=ObjectStore.getInstance();
55
56         static {
57                 // always same object saves a little space
58                 POPUP_EMTYLINE.put("key", ""); POPUP_EMTYLINE.put("value", "--");
59     try {
60       GENERIC_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
61       STORABLE_OBJECT_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
62     }
63     catch (Exception e) {
64       System.err.println("FATAL: Database.java could not initialize" + e.toString());
65     }
66   }
67
68
69         /**
70          * Kontruktor bekommt den Filenamen des Konfigurationsfiles übergeben.
71          * Aus diesem file werden <code>Database.Logfile</code>,
72          * <code>Database.Username</code>,<code>Database.Password</code>,
73          * <code>Database.Host</code> und <code>Database.Adaptor</code>
74          * ausgelesen und ein Broker für die Verbindugen zur Datenbank
75          * erzeugt.
76          *
77          * @param   String confFilename Dateiname der Konfigurationsdatei
78          */
79         public Database() throws StorageObjectException {
80                 theLog = Logfile.getInstance(MirConfig.getProp("Home")+
81                                                                                                                                 MirConfig.getProp("Database.Logfile"));
82                 String theAdaptorName=MirConfig.getProp("Database.Adaptor");
83                 defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit"));
84                 try {
85                         theEntityClass = GENERIC_ENTITY_CLASS;
86                         theAdaptor = (DatabaseAdaptor)Class.forName(theAdaptorName).newInstance();
87                 } catch (Exception e){
88                         theLog.printError("Error in Database() constructor with "+
89                                                                                                 theAdaptorName + " -- " +e.toString());
90                         throw new StorageObjectException("Error in Database() constructor with "
91                                                                                                                                                          +e.toString());
92                 }
93                 /*String database_username=MirConfig.getProp("Database.Username");
94                 String database_password=MirConfig.getProp("Database.Password");
95                 String database_host=MirConfig.getProp("Database.Host");
96                 try {
97                         database_driver=theAdaptor.getDriver();
98                         database_url=theAdaptor.getURL(database_username,database_password,
99                                                                                                                                                 database_host);
100                         theLog.printDebugInfo("adding Broker with: " +database_driver+":"+
101                                                                                                                 database_url  );
102                         MirConfig.addBroker(database_driver,database_url);
103                         //myBroker=MirConfig.getBroker();
104                 }*/
105         }
106
107         /**
108          * Liefert die Entity-Klasse zurück, in der eine Datenbankzeile gewrappt
109          * wird. Wird die Entity-Klasse durch die erbende Klasse nicht überschrieben,
110          * wird eine mir.entity.GenericEntity erzeugt.
111          *
112          * @return Class-Objekt der Entity
113          */
114         public java.lang.Class getEntityClass () {
115                 return  theEntityClass;
116         }
117
118         /**
119          * Liefert die Standardbeschränkung von select-Statements zurück, also
120          * wieviel Datensätze per Default selektiert werden.
121          *
122          * @return Standard-Anzahl der Datensätze
123          */
124         public int getLimit () {
125                 return  defaultLimit;
126         }
127
128         /**
129          * Liefert den Namen des Primary-Keys zurück. Wird die Variable nicht von
130          * der erbenden Klasse überschrieben, so ist der Wert <code>PKEY</code>
131          * @return Name des Primary-Keys
132          */
133         public String getIdName () {
134                 return  thePKeyName;
135         }
136
137         /**
138          * Liefert den Namen der Tabelle, auf das sich das Datenbankobjekt bezieht.
139          *
140          * @return Name der Tabelle
141          */
142         public String getTableName () {
143                 return  theTable;
144         }
145
146         /*
147          *   Dient dazu vererbte Tabellen bei objectrelationalen DBMS
148          *   zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet
149          *   wird.
150          *   @return liefert theCoreTabel als String zurueck, wenn gesetzt, sonst
151          *    the Table
152          */
153
154         public String getCoreTable(){
155                 if (theCoreTable!=null) return theCoreTable;
156                 else return theTable;
157         }
158
159         /**
160          * Liefert Feldtypen der Felder der Tabelle zurueck (s.a. java.sql.Types)
161          * @return int-Array mit den Typen der Felder
162          * @exception StorageObjectException
163          */
164         public int[] getTypes () throws StorageObjectException {
165                 if (metadataTypes == null)
166                         get_meta_data();
167                 return  metadataTypes;
168         }
169
170         /**
171          * Liefert eine Liste der Labels der Tabellenfelder
172          * @return ArrayListe mit Labeln
173          * @exception StorageObjectException
174          */
175         public ArrayList getLabels () throws StorageObjectException {
176                 if (metadataLabels == null)
177                         get_meta_data();
178                 return  metadataLabels;
179         }
180
181         /**
182          * Liefert eine Liste der Felder der Tabelle
183          * @return ArrayList mit Feldern
184          * @exception StorageObjectException
185          */
186         public ArrayList getFields () throws StorageObjectException {
187                 if (metadataFields == null)
188                         get_meta_data();
189                 return  metadataFields;
190         }
191
192
193         /*
194          *   Gets value out of ResultSet according to type and converts to String
195          *   @param inValue  Wert aus ResultSet.
196          *   @param aType  Datenbanktyp.
197          *   @return liefert den Wert als String zurueck. Wenn keine Umwandlung moeglich
198          *           dann /unsupported value/
199          */
200         private String getValueAsString (ResultSet rs, int valueIndex, int aType) throws StorageObjectException {
201                 String outValue = null;
202                 if (rs != null) {
203                         try {
204                                 switch (aType) {
205                                         case java.sql.Types.BIT:
206                                                 outValue = (rs.getBoolean(valueIndex) == true) ? "1" : "0";
207                                                 break;
208                                         case java.sql.Types.INTEGER:case java.sql.Types.SMALLINT:case java.sql.Types.TINYINT:case java.sql.Types.BIGINT:
209                                                 int out = rs.getInt(valueIndex);
210                                                 if (!rs.wasNull())
211                                                         outValue = new Integer(out).toString();
212                                                 break;
213                                         case java.sql.Types.NUMERIC:
214             /** @todo Numeric can be float or double depending upon
215              *  metadata.getScale() / especially with oracle */
216                                                 long outl = rs.getLong(valueIndex);
217                                                 if (!rs.wasNull())
218                                                         outValue = new Long(outl).toString();
219                                                 break;
220                                         case java.sql.Types.REAL:
221                                                 float tempf = rs.getFloat(valueIndex);
222                                                 if (!rs.wasNull()) {
223                                                         tempf *= 10;
224                                                         tempf += 0.5;
225                                                         int tempf_int = (int)tempf;
226                                                         tempf = (float)tempf_int;
227                                                         tempf /= 10;
228                                                         outValue = "" + tempf;
229                                                         outValue = outValue.replace('.', ',');
230                                                 }
231                                                 break;
232                                         case java.sql.Types.DOUBLE:
233                                                 double tempd = rs.getDouble(valueIndex);
234                                                 if (!rs.wasNull()) {
235                                                         tempd *= 10;
236                                                         tempd += 0.5;
237                                                         int tempd_int = (int)tempd;
238                                                         tempd = (double)tempd_int;
239                                                         tempd /= 10;
240                                                         outValue = "" + tempd;
241                                                         outValue = outValue.replace('.', ',');
242                                                 }
243                                                 break;
244                                         case java.sql.Types.CHAR:case java.sql.Types.VARCHAR:case java.sql.Types.LONGVARCHAR:
245                                                 outValue = rs.getString(valueIndex);
246                                                 //if (outValue != null)
247                                                         //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
248                                                 break;
249                                         case java.sql.Types.LONGVARBINARY:
250                                                 outValue = rs.getString(valueIndex);
251                                                 //if (outValue != null)
252                                                         //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
253                                                 break;
254                                         case java.sql.Types.TIMESTAMP:
255                                                 //Timestamp timestamp = (rs.getTimestamp(valueIndex));
256                                                 //jbdc drops time zone info,
257                                                 //so just get the string from
258                                                 //postgres
259                                                 String timestamp = (rs.getString(valueIndex));
260                                                 if (!rs.wasNull()) {
261                                                         outValue = timestamp;
262                                                 }
263                                                 break;
264                                         default:
265                                                 outValue = "<unsupported value>";
266                                                 theLog.printWarning("Unsupported Datatype: at " + valueIndex +
267                                                                 " (" + aType + ")");
268                                 }
269                         } catch (SQLException e) {
270                                 throw  new StorageObjectException("Could not get Value out of Resultset -- "
271                                                 + e.toString());
272                         }
273                 }
274                 return  outValue;
275         }
276
277         /*
278          *   select-Operator um einen Datensatz zu bekommen.
279          *   @param id Primaerschluessel des Datensatzes.
280          *   @return liefert EntityObject des gefundenen Datensatzes oder null.
281          */
282         public Entity selectById(String id)     throws StorageObjectException
283   {
284                 if (id==null||id.equals(""))
285                         throw new StorageObjectException("id war null");
286
287     // ask object store for object
288     if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
289       String uniqueId = id;
290       if ( theEntityClass.equals(StorableObjectEntity.class) )
291         uniqueId+="@"+theTable;
292       StoreIdentifier search_sid = new StoreIdentifier(theEntityClass, uniqueId);
293       theLog.printDebugInfo("CACHE: (dbg) looking for sid " + search_sid.toString());
294       Entity hit = (Entity)o_store.use(search_sid);
295       if ( hit!=null ) return hit;
296     }
297
298                 Statement stmt=null;Connection con=getPooledCon();
299                 Entity returnEntity=null;
300                 try {
301                         ResultSet rs;
302                         /** @todo better prepared statement */
303                         String selectSql = "select * from " + theTable + " where " + thePKeyName + "=" + id;
304                         stmt = con.createStatement();
305                         rs = executeSql(stmt, selectSql);
306                         if (rs != null) {
307                                 if (evaluatedMetaData==false) evalMetaData(rs.getMetaData());
308                                 if (rs.next())
309                                         returnEntity = makeEntityFromResultSet(rs);
310                                 else theLog.printDebugInfo("Keine daten fuer id: " + id + "in Tabelle" + theTable);
311                                 rs.close();
312                         }
313                         else {
314                                 theLog.printDebugInfo("No Data for Id " + id + " in Table " + theTable);
315                         }
316                 }
317                 catch (SQLException sqe){
318                         throwSQLException(sqe,"selectById"); return null;
319                 }
320                 catch (NumberFormatException e) {
321                         theLog.printError("ID ist keine Zahl: " + id);
322                 }
323                 finally { freeConnection(con,stmt); }
324
325                 /** @todo OS: Entity should be saved in ostore */
326                 return returnEntity;
327         }
328
329
330         /**
331          *   select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
332          *   @param key  Datenbankfeld der Bedingung.
333          *   @param value  Wert die der key anehmen muss.
334          *   @return EntityList mit den gematchten Entities
335          */
336         public EntityList selectByFieldValue(String aField, String aValue)
337                 throws StorageObjectException
338         {
339                 return selectByFieldValue(aField, aValue, 0);
340         }
341
342         /**
343          *   select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
344          *   @param key  Datenbankfeld der Bedingung.
345          *   @param value  Wert die der key anehmen muss.
346          *   @param offset  Gibt an ab welchem Datensatz angezeigt werden soll.
347          *   @return EntityList mit den gematchten Entities
348          */
349         public EntityList selectByFieldValue(String aField, String aValue, int offset)
350                 throws StorageObjectException
351         {
352                 return selectByWhereClause(aField + "=" + aValue, offset);
353         }
354
355
356         /**
357          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
358          * Also offset wird der erste Datensatz genommen.
359          *
360          * @param wc where-Clause
361          * @return EntityList mit den gematchten Entities
362          * @exception StorageObjectException
363          */
364         public EntityList selectByWhereClause(String where)
365                 throws StorageObjectException
366         {
367                 return selectByWhereClause(where, 0);
368         }
369
370
371         /**
372          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
373          * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
374          *
375          * @param wc where-Clause
376          * @param offset ab welchem Datensatz.
377          * @return EntityList mit den gematchten Entities
378          * @exception StorageObjectException
379          */
380         public EntityList selectByWhereClause(String whereClause, int offset)
381                 throws StorageObjectException
382         {
383                 return selectByWhereClause(whereClause, null, offset);
384         }
385
386
387         /**
388          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
389          * Also offset wird der erste Datensatz genommen.
390          * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
391          *
392          * @param wc where-Clause
393          * @param ob orderBy-Clause
394          * @return EntityList mit den gematchten Entities
395          * @exception StorageObjectException
396          */
397
398         public EntityList selectByWhereClause(String where, String order)
399                 throws StorageObjectException {
400                 return selectByWhereClause(where, order, 0);
401         }
402
403
404         /**
405          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
406          * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
407          *
408          * @param wc where-Clause
409          * @param ob orderBy-Clause
410          * @param offset ab welchem Datensatz
411          * @return EntityList mit den gematchten Entities
412          * @exception StorageObjectException
413          */
414
415         public EntityList selectByWhereClause(String whereClause, String orderBy, int offset)
416                 throws StorageObjectException {
417                 return selectByWhereClause(whereClause, orderBy, offset, defaultLimit);
418         }
419
420
421         /**
422          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
423          * @param wc where-Clause
424          * @param ob orderBy-Clause
425          * @param offset ab welchem Datensatz
426          * @param limit wieviele Datensätze
427          * @return EntityList mit den gematchten Entities
428          * @exception StorageObjectException
429          */
430
431         public EntityList selectByWhereClause(String wc, String ob, int offset, int limit)
432                 throws StorageObjectException
433   {
434
435     // check o_store for entitylist
436     if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
437       StoreIdentifier search_sid =
438         new StoreIdentifier( theEntityClass,
439                              StoreContainerType.STOC_TYPE_ENTITYLIST,
440                              StoreUtil.getEntityListUniqueIdentifierFor(theTable,wc,ob,offset,limit) );
441       EntityList hit = (EntityList)o_store.use(search_sid);
442       if ( hit!=null ) {
443         theLog.printDebugInfo("CACHE (hit): " + search_sid.toString());
444         return hit;
445       }
446     }
447
448                 // local
449                 EntityList    theReturnList=null;
450                 Connection    con=null; Statement stmt=null;
451                 ResultSet     rs;
452                 int           offsetCount = 0, count=0;
453
454                 // build sql-statement
455
456                 /** @todo count sql string should only be assembled if we really count
457                  *  see below at the end of method //rk */
458
459                 if (wc != null && wc.length() == 0) {
460                         wc = null;
461                 }
462                 StringBuffer countSql = new StringBuffer("select count(*) from ").append(theTable);
463                 StringBuffer selectSql = new StringBuffer("select * from ").append(theTable);
464                 if (wc != null) {
465                         selectSql.append(" where ").append(wc);
466                         countSql.append(" where ").append(wc);
467                 }
468                 if (ob != null && !(ob.length() == 0)) {
469                         selectSql.append(" order by ").append(ob);
470                 }
471                 if (theAdaptor.hasLimit()) {
472                         if (limit > -1 && offset > -1) {
473                                 selectSql.append(" limit ");
474                                 if (theAdaptor.reverseLimit()) {
475                                         selectSql.append(limit).append(",").append(offset);
476                                 }
477                                 else {
478                                         selectSql.append(offset).append(",").append(limit);
479                                 }
480                         }
481                 }
482
483                 // execute sql
484                 try {
485                         con = getPooledCon();
486                         stmt = con.createStatement();
487
488                         // selecting...
489                         rs = executeSql(stmt, selectSql.toString());
490                         if (rs != null) {
491                                 if (!evaluatedMetaData) evalMetaData(rs.getMetaData());
492
493                                 theReturnList = new EntityList();
494                                 Entity theResultEntity;
495                                 while (rs.next()) {
496                                         theResultEntity = makeEntityFromResultSet(rs);
497                                         theReturnList.add(theResultEntity);
498                                         offsetCount++;
499                                 }
500                                 rs.close();
501                         }
502
503                         // making entitylist infos
504                         if (!(theAdaptor.hasLimit())) count = offsetCount;
505
506                         if (theReturnList != null) {
507                                 // now we decide if we have to know an overall count...
508                                 count=offsetCount;
509                                 if (limit > -1 && offset > -1) {
510                                         if (offsetCount==limit) {
511                                                 /** @todo counting should be deffered to entitylist
512                                                  *  getSize() should be used */
513                                                 rs = executeSql(stmt, countSql.toString());
514                                                 if (rs != null) {
515                                                         if ( rs.next() ) count = rs.getInt(1);
516                                                         rs.close();
517                                                 }
518                                                 else theLog.printError("Could not count: " + countSql);
519                                         }
520                                 }
521                                 theReturnList.setCount(count);
522                                 theReturnList.setOffset(offset);
523                                 theReturnList.setWhere(wc);
524                                 theReturnList.setOrder(ob);
525         theReturnList.setStorage(this);
526         theReturnList.setLimit(limit);
527                                 if ( offset >= limit )
528                                         theReturnList.setPrevBatch(offset - limit);
529                                 if ( offset+offsetCount < count )
530                                         theReturnList.setNextBatch(offset + limit);
531         if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
532           StoreIdentifier sid=theReturnList.getStoreIdentifier();
533           theLog.printDebugInfo("CACHE (add): " + sid.toString());
534           o_store.add(sid);
535         }
536                         }
537                 }
538                 catch (SQLException sqe) { throwSQLException(sqe, "selectByWhereClause"); }
539                 finally { freeConnection(con, stmt); }
540
541                 return  theReturnList;
542         }
543
544
545         /**
546          *  Bastelt aus einer Zeile der Datenbank ein EntityObjekt.
547          *
548          *  @param rs Das ResultSetObjekt.
549          *  @return Entity Die Entity.
550          */
551         private Entity makeEntityFromResultSet (ResultSet rs)
552                 throws StorageObjectException
553         {
554                 /** @todo OS: get Pkey from ResultSet and consult ObjectStore */
555                 HashMap theResultHash = new HashMap();
556                 String theResult = null;
557                 int theType;
558                 Entity returnEntity = null;
559                 try {
560                         int size = metadataFields.size();
561                         for (int i = 0; i < size; i++) {
562                                 // alle durchlaufen bis nix mehr da
563
564                                 theType = metadataTypes[i];
565                                 if (theType == java.sql.Types.LONGVARBINARY) {
566 /*                                      InputStream us = rs.getAsciiStream(i + 1);
567                                         if (us != null) {
568                                                 InputStreamReader is = new InputStreamReader(us);
569                                                 char[] data = new char[32768];
570                                                 StringBuffer theResultString = new StringBuffer();
571                                                 int len;
572                                                 while ((len = is.read(data)) > 0) {
573                                                         theResultString.append(data, 0, len);
574                                                 }
575                                                 is.close();
576                                                 theResult = theResultString.toString();
577
578             theResult = null;
579                                         }
580                                         else {
581                                                 theResult = null;
582                                         }
583 */        theResult = null;
584                                 }
585                                 else {
586                                         theResult = getValueAsString(rs, (i + 1), theType);
587                                 }
588                                 if (theResult != null) {
589                                         theResultHash.put(metadataFields.get(i), theResult);
590                                 }
591                         }
592       if (theEntityClass != null) {
593         returnEntity = (Entity)theEntityClass.newInstance();
594         returnEntity.setValues(theResultHash);
595         returnEntity.setStorage(myselfDatabase);
596         if ( returnEntity instanceof StorableObject ) {
597           theLog.printDebugInfo("CACHE: ( in) " + returnEntity.getId() + " :"+theTable);
598           o_store.add(((StorableObject)returnEntity).getStoreIdentifier());
599         }
600       } else {
601         throwStorageObjectException("Internal Error: theEntityClass not set!");
602       }
603                 } catch (IllegalAccessException e) {
604                         throwStorageObjectException("Kein Zugriff! -- " + e.toString());
605 /*              } catch (IOException e) {
606                         throwStorageObjectException("IOException! -- " + e.toString());
607 */              } catch (InstantiationException e) {
608                         throwStorageObjectException("Keine Instantiiierung! -- " + e.toString());
609 /*              } catch (SQLException sqe) {
610                         throwSQLException(sqe, "makeEntityFromResultSet");
611                         return  null;
612 */              }
613                 return  returnEntity;
614         }
615
616         /**
617          * insert-Operator: fügt eine Entity in die Tabelle ein. Eine Spalte WEBDB_CREATE
618          * wird automatisch mit dem aktuellen Datum gefuellt.
619          *
620          * @param theEntity
621          * @return der Wert des Primary-keys der eingefügten Entity
622          */
623         public String insert (Entity theEntity) throws StorageObjectException {
624                 //cache
625                 invalidatePopupCache();
626
627     // invalidating all EntityLists corresponding with theEntityClass
628     if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
629       StoreContainerType stoc_type =
630         StoreContainerType.valueOf( theEntityClass,
631                                     StoreContainerType.STOC_TYPE_ENTITYLIST);
632       o_store.invalidate(stoc_type);
633     }
634
635                 String returnId = null;
636                 Connection con = null; PreparedStatement pstmt = null;
637
638     try {
639                         ArrayList streamedInput = theEntity.streamedInput();
640                         StringBuffer f = new StringBuffer();
641                         StringBuffer v = new StringBuffer();
642                         String aField, aValue;
643                         boolean firstField = true;
644                         // make sql-string
645                         for (int i = 0; i < getFields().size(); i++) {
646                                 aField = (String)getFields().get(i);
647                                 if (!aField.equals(thePKeyName)) {
648                                         aValue = null;
649                                         // sonderfaelle
650                                         if (aField.equals("webdb_create")) {
651                                                 aValue = "NOW()";
652                                         }
653                                         else {
654                                                 if (streamedInput != null && streamedInput.contains(aField)) {
655                                                         aValue = "?";
656                                                 }
657                                                 else {
658                                                         if (theEntity.hasValueForField(aField)) {
659                                                                 aValue = "'" + StringUtil.quote((String)theEntity.getValue(aField))
660                                                                                 + "'";
661                                                         }
662                                                 }
663                                         }
664                                         // wenn Wert gegeben, dann einbauen
665                                         if (aValue != null) {
666                                                 if (firstField == false) {
667                                                         f.append(",");
668                                                         v.append(",");
669                                                 }
670                                                 else {
671                                                         firstField = false;
672                                                 }
673                                                 f.append(aField);
674                                                 v.append(aValue);
675                                         }
676                                 }
677                         }         // end for
678                         // insert into db
679                         StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
680                         String sql = sqlBuf.toString();
681                         theLog.printInfo("INSERT: " + sql);
682                         con = getPooledCon();
683                         con.setAutoCommit(false);
684                         pstmt = con.prepareStatement(sql);
685                         if (streamedInput != null) {
686                                 for (int i = 0; i < streamedInput.size(); i++) {
687                                         String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
688                                         pstmt.setBytes(i + 1, inputString.getBytes());
689                                 }
690                         }
691                         int ret = pstmt.executeUpdate();
692                         if(ret == 0){
693                                 //insert failed
694                                 return null;
695                         }
696                         pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
697                         ResultSet rs = pstmt.executeQuery();
698                         rs.next();
699                         returnId = rs.getString(1);
700                         theEntity.setId(returnId);
701                 } catch (SQLException sqe) {
702                         throwSQLException(sqe, "insert");
703                 } finally {
704                         try {
705                                 con.setAutoCommit(true);
706                         } catch (Exception e) {
707                                 ;
708                         }
709                         freeConnection(con, pstmt);
710                 }
711     /** @todo store entity in o_store */
712                 return  returnId;
713         }
714
715         /**
716          * update-Operator: aktualisiert eine Entity. Eine Spalte WEBDB_LASTCHANGE
717          * wird automatisch mit dem aktuellen Datum gefuellt.
718          *
719          * @param theEntity
720          */
721         public void update (Entity theEntity) throws StorageObjectException
722   {
723                 Connection con = null; PreparedStatement pstmt = null;
724                 /** @todo this is stupid: why do we prepare statement, when we
725                  *  throw it away afterwards. should be regular statement
726                  *  update/insert could better be one routine called save()
727                  *  that chooses to either insert or update depending if we
728                  *  have a primary key in the entity. i don't know if we
729                  *  still need the streamed input fields. // rk  */
730
731                 /** @todo extension: check if Entity did change, otherwise we don't need
732      *  the roundtrip to the database */
733
734                 /** invalidating corresponding entitylists in o_store*/
735     if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
736       StoreContainerType stoc_type =
737         StoreContainerType.valueOf( theEntityClass,
738                                     StoreContainerType.STOC_TYPE_ENTITYLIST);
739       o_store.invalidate(stoc_type);
740     }
741
742                 ArrayList streamedInput = theEntity.streamedInput();
743                 String id = theEntity.getId();
744                 String aField;
745                 StringBuffer fv = new StringBuffer();
746                 boolean firstField = true;
747                 //cache
748                 invalidatePopupCache();
749                 // build sql statement
750                 for (int i = 0; i < getFields().size(); i++) {
751                         aField = (String)metadataFields.get(i);
752                         // only normal cases
753                         if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
754                                         aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
755                                 if (theEntity.hasValueForField(aField)) {
756                                         if (firstField == false) {
757                                                 fv.append(", ");
758                                         }
759                                         else {
760                                                 firstField = false;
761                                         }
762                                         fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
763                                 }
764                         }
765                 }
766                 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
767                 // exceptions
768                 if (metadataFields.contains("webdb_lastchange")) {
769                         sql.append(",webdb_lastchange=NOW()");
770                 }
771                 if (streamedInput != null) {
772                         for (int i = 0; i < streamedInput.size(); i++) {
773                                 sql.append(",").append(streamedInput.get(i)).append("=?");
774                         }
775                 }
776                 sql.append(" where id=").append(id);
777                 theLog.printInfo("UPDATE: " + sql);
778                 // execute sql
779                 try {
780                         con = getPooledCon();
781                         con.setAutoCommit(false);
782                         pstmt = con.prepareStatement(sql.toString());
783                         if (streamedInput != null) {
784                                 for (int i = 0; i < streamedInput.size(); i++) {
785                                         String inputString = theEntity.getValue((String)streamedInput.get(i));
786                                         pstmt.setBytes(i + 1, inputString.getBytes());
787                                 }
788                         }
789                         pstmt.executeUpdate();
790                 } catch (SQLException sqe) {
791                         throwSQLException(sqe, "update");
792                 } finally {
793                         try {
794                                 con.setAutoCommit(true);
795                         } catch (Exception e) {
796                                 ;
797                         }
798                         freeConnection(con, pstmt);
799                 }
800         }
801
802         /*
803          *   delete-Operator
804          *   @param id des zu loeschenden Datensatzes
805          *   @return boolean liefert true zurueck, wenn loeschen erfolgreich war.
806          */
807         public boolean delete (String id) throws StorageObjectException {
808
809                 invalidatePopupCache();
810     // ostore send notification
811     if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
812       String uniqueId = id;
813       if ( theEntityClass.equals(StorableObjectEntity.class) )
814         uniqueId+="@"+theTable;
815                         theLog.printInfo("CACHE: (del) " + id);
816                         StoreIdentifier search_sid =
817         new StoreIdentifier(theEntityClass, StoreContainerType.STOC_TYPE_ENTITY, uniqueId);
818       o_store.invalidate(search_sid);
819                 }
820
821                 /** @todo could be prepared Statement */
822                 Statement stmt = null; Connection con = null;
823                 int res = 0;
824                 String sql="delete from "+theTable+" where "+thePKeyName+"='"+id+"'";
825                 theLog.printInfo("DELETE " + sql);
826                 try {
827                         con = getPooledCon(); stmt = con.createStatement();
828                         res = stmt.executeUpdate(sql);
829                 }
830     catch (SQLException sqe) { throwSQLException(sqe, "delete"); }
831     finally { freeConnection(con, stmt); }
832
833                 return  (res > 0) ? true : false;
834         }
835
836         /* noch nicht implementiert.
837          * @return immer false
838          */
839         public boolean delete (EntityList theEntityList) {
840                 invalidatePopupCache();
841                 return  false;
842         }
843
844         /**
845          * Diese Methode sollte ueberschrieben werden, wenn fuer die abgeleitete Database-Klasse
846          * eine SimpleList mit Standard-Popupdaten erzeugt werden koennen soll.
847          * @return null
848          */
849         public SimpleList getPopupData () throws StorageObjectException {
850                 return  null;
851         }
852
853         /**
854          *  Holt Daten fuer Popups.
855          *  @param name  Name des Feldes.
856          *  @param hasNullValue  Wenn true wird eine leerer  Eintrag fuer die Popups erzeugt.
857          *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
858          */
859         public SimpleList getPopupData (String name, boolean hasNullValue)
860                 throws StorageObjectException {
861                 return  getPopupData(name, hasNullValue, null);
862         }
863
864         /**
865          *  Holt Daten fuer Popups.
866          *  @param name  Name des Feldes.
867          *  @param hasNullValue  Wenn true wird eine leerer  Eintrag fuer die Popups erzeugt.
868          *  @param where  Schraenkt die Selektion der Datensaetze ein.
869          *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
870          */
871         public SimpleList getPopupData (String name, boolean hasNullValue, String where) throws StorageObjectException {
872          return  getPopupData(name, hasNullValue, where, null);
873         }
874
875         /**
876          *  Holt Daten fuer Popups.
877          *  @param name  Name des Feldes.
878          *  @param hasNullValue  Wenn true wird eine leerer  Eintrag fuer die Popups erzeugt.
879          *  @param where  Schraenkt die Selektion der Datensaetze ein.
880          *  @param order  Gibt ein Feld als Sortierkriterium an.
881          *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
882          */
883         public SimpleList getPopupData (String name, boolean hasNullValue, String where, String order) throws StorageObjectException {
884                 // caching
885                 if (hasPopupCache && popupCache != null)
886                         return  popupCache;
887                 SimpleList simpleList = null;
888                 Connection con = null;
889                 Statement stmt = null;
890                 // build sql
891                 StringBuffer sql = new StringBuffer("select ").append(thePKeyName)
892                                                                                                                                                                 .append(",").append(name).append(" from ")
893                                                                                                                                                                 .append(theTable);
894                 if (where != null && !(where.length() == 0))
895                         sql.append(" where ").append(where);
896                 sql.append(" order by ");
897                 if (order != null && !(order.length() == 0))
898                         sql.append(order);
899                 else
900                         sql.append(name);
901                 // execute sql
902                 try {
903                         con = getPooledCon();
904                 } catch (Exception e) {
905                         throw new StorageObjectException(e.toString());
906                 }
907                 try {
908                         stmt = con.createStatement();
909                         ResultSet rs = executeSql(stmt, sql.toString());
910
911                         if (rs != null) {
912                                 if (!evaluatedMetaData) get_meta_data();
913                                 simpleList = new SimpleList();
914                                 // if popup has null-selector
915                                 if (hasNullValue) simpleList.add(POPUP_EMTYLINE);
916
917                                 SimpleHash popupDict;
918                                 while (rs.next()) {
919                                         popupDict = new SimpleHash();
920                                         popupDict.put("key", getValueAsString(rs, 1, thePKeyType));
921                                         popupDict.put("value", rs.getString(2));
922                                         simpleList.add(popupDict);
923                                 }
924                                 rs.close();
925                         }
926                 } catch (Exception e) {
927                         theLog.printError("getPopupData: "+e.toString());
928                         throw new StorageObjectException(e.toString());
929                 } finally {
930                         freeConnection(con, stmt);
931                 }
932
933                 if (hasPopupCache) popupCache = simpleList;
934                 return  simpleList;
935         }
936
937         /**
938          * Liefert alle Daten der Tabelle als SimpleHash zurueck. Dies wird verwandt,
939          * wenn in den Templates ein Lookup-Table benoetigt wird. Sollte nur bei kleinen
940          * Tabellen Verwendung finden.
941          * @return SimpleHash mit den Tabellezeilen.
942          */
943         public SimpleHash getHashData () {
944                 /** @todo dangerous! this should have a flag to be enabled, otherwise
945                  *  very big Hashes could be returned */
946                 if (hashCache == null) {
947                         try {
948                                 hashCache = HTMLTemplateProcessor.makeSimpleHash(selectByWhereClause("",
949                                                 -1));
950                         } catch (StorageObjectException e) {
951                                 theLog.printDebugInfo(e.toString());
952                         }
953                 }
954                 return  hashCache;
955         }
956
957         /* invalidates the popupCache
958          */
959         protected void invalidatePopupCache () {
960                 /** @todo  invalidates toooo much */
961                 popupCache = null;
962                 hashCache = null;
963         }
964
965         /**
966          * Diese Methode fuehrt den Sqlstring <i>sql</i> aus und timed im Logfile.
967          * @param stmt Statemnt
968          * @param sql Sql-String
969          * @return ResultSet
970          * @exception StorageObjectException
971          */
972         public ResultSet executeSql (Statement stmt, String sql)
973                 throws StorageObjectException, SQLException
974         {
975                 long startTime = System.currentTimeMillis();
976                 ResultSet rs;
977                 try {
978                         rs = stmt.executeQuery(sql);
979                         theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
980                                 + sql);
981                 }
982                 catch (SQLException e)
983                 {
984                         theLog.printDebugInfo("Failed: " + (System.currentTimeMillis()
985                                                                                                                 - startTime) + "ms. for: "+ sql);
986                         throw e;
987                 }
988
989                 return  rs;
990         }
991
992         /**
993          * Fuehrt Statement stmt aus und liefert Resultset zurueck. Das SQL-Statment wird
994          * getimed und geloggt.
995          * @param stmt PreparedStatement mit der SQL-Anweisung
996          * @return Liefert ResultSet des Statements zurueck.
997          * @exception StorageObjectException, SQLException
998          */
999         public ResultSet executeSql (PreparedStatement stmt)
1000                 throws StorageObjectException, SQLException {
1001
1002                 long startTime = (new java.util.Date()).getTime();
1003                 ResultSet rs = stmt.executeQuery();
1004                 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms.");
1005                 return  rs;
1006         }
1007
1008                 /**
1009          * returns the number of rows in the table
1010          */
1011         public int getSize(String where)
1012                 throws SQLException,StorageObjectException
1013         {
1014                 long  startTime = System.currentTimeMillis();
1015                 String sql = "SELECT count(*) FROM "+ theTable + " where " + where;
1016                 Connection con = null;
1017                 Statement stmt = null;
1018                 int result = 0;
1019
1020                 try {
1021                         con = getPooledCon();
1022                         stmt = con.createStatement();
1023                         ResultSet rs = executeSql(stmt,sql);
1024                         while(rs.next()){
1025                                 result = rs.getInt(1);
1026                         }
1027                 } catch (SQLException e) {
1028                         theLog.printError(e.toString());
1029                 } finally {
1030                         freeConnection(con,stmt);
1031                 }
1032                 //theLog.printInfo(theTable + " has "+ result +" rows where " + where);
1033                 theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
1034                                                                                 + sql);
1035                 return result;
1036         }
1037
1038         public int executeUpdate(Statement stmt, String sql)
1039                 throws StorageObjectException, SQLException
1040         {
1041                 int rs;
1042                 long  startTime = (new java.util.Date()).getTime();
1043                 try
1044                 {
1045                         rs = stmt.executeUpdate(sql);
1046                         theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
1047                                                                                                 + sql);
1048                 }
1049                 catch (SQLException e)
1050                 {
1051                         theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime()
1052                                                                                                                 - startTime) + "ms. for: "+ sql);
1053                         throw e;
1054                 }
1055                 return rs;
1056         }
1057
1058         public int executeUpdate(String sql)
1059                 throws StorageObjectException, SQLException
1060         {
1061                 int result=-1;
1062                 long  startTime = (new java.util.Date()).getTime();
1063                 Connection con=null;PreparedStatement pstmt=null;
1064                 try {
1065                         con=getPooledCon();
1066                         pstmt = con.prepareStatement(sql);
1067                         result = pstmt.executeUpdate();
1068                 }
1069                 catch (Exception e) {
1070                         theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());
1071                         throw new StorageObjectException("executeUpdate failed: "+e.toString());
1072                 }
1073                 finally { freeConnection(con,pstmt); }
1074                 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
1075                                                                                 + sql);
1076                 return result;
1077         }
1078
1079         /**
1080          * Wertet ResultSetMetaData aus und setzt interne Daten entsprechend
1081          * @param md ResultSetMetaData
1082          * @exception StorageObjectException
1083          */
1084         private void evalMetaData (ResultSetMetaData md)
1085                 throws StorageObjectException {
1086
1087                 this.evaluatedMetaData = true;
1088                 this.metadataFields = new ArrayList();
1089                 this.metadataLabels = new ArrayList();
1090                 this.metadataNotNullFields = new ArrayList();
1091                 try {
1092                         int numFields = md.getColumnCount();
1093                         this.metadataTypes = new int[numFields];
1094                         String aField;
1095                         int aType;
1096                         for (int i = 1; i <= numFields; i++) {
1097                                 aField = md.getColumnName(i);
1098                                 metadataFields.add(aField);
1099                                 metadataLabels.add(md.getColumnLabel(i));
1100                                 aType = md.getColumnType(i);
1101                                 metadataTypes[i - 1] = aType;
1102                                 if (aField.equals(thePKeyName)) {
1103                                         thePKeyType = aType; thePKeyIndex = i;
1104                                 }
1105                                 if (md.isNullable(i) == md.columnNullable) {
1106                                         metadataNotNullFields.add(aField);
1107                                 }
1108                         }
1109                 } catch (SQLException e) {
1110                         throwSQLException(e, "evalMetaData");
1111                 }
1112         }
1113
1114         /**
1115          *  Wertet die Metadaten eines Resultsets fuer eine Tabelle aus,
1116          *  um die alle Columns und Typen einer Tabelle zu ermitteln.
1117          */
1118         private void get_meta_data () throws StorageObjectException {
1119                 Connection con = null;
1120                 PreparedStatement pstmt = null;
1121                 String sql = "select * from " + theTable + " where 0=1";
1122                 try {
1123                         con = getPooledCon();
1124                         pstmt = con.prepareStatement(sql);
1125                         theLog.printInfo("METADATA: " + sql);
1126                         ResultSet rs = pstmt.executeQuery();
1127                         evalMetaData(rs.getMetaData());
1128                         rs.close();
1129                 } catch (SQLException e) {
1130                         throwSQLException(e, "get_meta_data");
1131                 } finally {
1132                         freeConnection(con, pstmt);
1133                 }
1134         }
1135
1136
1137         public Connection getPooledCon() throws StorageObjectException {
1138                 /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh
1139                         try{
1140                         Class.forName("com.codestudio.sql.PoolMan").newInstance();
1141                 } catch (Exception e){
1142                         throw new StorageObjectException("Could not find the PoolMan Driver"
1143                                                                                                                                                                 +e.toString());
1144                 }*/
1145                 Connection con = null;
1146                 try{
1147                         con = SQLManager.getInstance().requestConnection();
1148                 } catch(SQLException e){
1149                         theLog.printError("could not connect to the database "+e.toString());
1150                         System.err.println("could not connect to the database "+e.toString());
1151                         throw new StorageObjectException("Could not connect to the database"+
1152                                                                                                                                                                 e.toString());
1153                 }
1154                 return con;
1155         }
1156
1157         public void freeConnection (Connection con, Statement stmt)
1158                 throws StorageObjectException {
1159                 SQLManager.getInstance().closeStatement(stmt);
1160                 SQLManager.getInstance().returnConnection(con);
1161         }
1162
1163         /**
1164          * Wertet SQLException aus und wirft dannach eine StorageObjectException
1165          * @param sqe SQLException
1166          * @param wo Funktonsname, in der die SQLException geworfen wurde
1167          * @exception StorageObjectException
1168          */
1169         protected void throwSQLException (SQLException sqe, String wo)
1170                 throws StorageObjectException {
1171                 String state = "";
1172                 String message = "";
1173                 int vendor = 0;
1174                 if (sqe != null) {
1175                         state = sqe.getSQLState();
1176                         message = sqe.getMessage();
1177                         vendor = sqe.getErrorCode();
1178                 }
1179                 theLog.printError(state + ": " + vendor + " : " + message + " Funktion: "
1180                                 + wo);
1181                 throw  new StorageObjectException((sqe == null) ? "undefined sql exception" :
1182                                 sqe.toString());
1183         }
1184
1185         protected void _throwStorageObjectException (Exception e, String wo)
1186                 throws StorageObjectException {
1187                 if (e != null) {
1188                                 theLog.printError(e.toString()+ wo);
1189                                 throw  new StorageObjectException(wo + e.toString());
1190                 } else {
1191                                 theLog.printError(wo);
1192                                 throw  new StorageObjectException(wo);
1193                 }
1194
1195         }
1196
1197         /**
1198          * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
1199          * eine StorageObjectException
1200          * @param message Nachricht mit dem Fehler
1201          * @exception StorageObjectException
1202          */
1203         void throwStorageObjectException (String message)
1204                 throws StorageObjectException {
1205                 _throwStorageObjectException(null, message);
1206         }
1207
1208 }
1209
1210
1211