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