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