added necessary bits in templates and Database.java to make webdb_create modifiable...
[mir.git] / source / mir / storage / Database.java
index c04e992..2e8f893 100755 (executable)
@@ -7,6 +7,8 @@ import  java.sql.*;
 import  java.lang.*;
 import  java.io.*;
 import  java.util.*;
+import  java.text.SimpleDateFormat;
+import  java.text.ParseException;
 import  freemarker.template.*;
 import  com.codestudio.sql.*;
 import  com.codestudio.util.*;
@@ -25,8 +27,14 @@ import  mir.misc.*;
  * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
  * Datenbank erfolgt.
  *
- * @author RK
- * @version 16.7.1999
+ * @version $Revision: 1.18 $ $Date: 2002/06/28 20:42:13 $
+ * @author $Author: mh $
+ *
+ * $Log: Database.java,v $
+ * Revision 1.18  2002/06/28 20:42:13  mh
+ * added necessary bits in templates and Database.java to make webdb_create modifiable. make the conversion from sql/Timestamp to String more robust
+ *
+ *
  */
 public class Database implements StorageObject {
 
@@ -52,6 +60,14 @@ public class Database implements StorageObject {
                                       STORABLE_OBJECT_ENTITY_CLASS=null;
   private static SimpleHash           POPUP_EMTYLINE=new SimpleHash();
   protected static final ObjectStore  o_store=ObjectStore.getInstance();
+  private SimpleDateFormat _dateFormatterOut = 
+                                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+  private SimpleDateFormat _dateFormatterIn = 
+                                    new SimpleDateFormat("yyyy-MM-dd");
+  private Calendar _cal = new GregorianCalendar();
+
+  private static final int _millisPerHour = 60 * 60 * 1000;
+  private static final int _millisPerMinute = 60 * 1000;
 
        static {
                // always same object saves a little space
@@ -249,17 +265,24 @@ public class Database implements StorageObject {
                                        case java.sql.Types.LONGVARBINARY:
                                                outValue = rs.getString(valueIndex);
                                                //if (outValue != null)
-                                                       //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
+                                               //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
                                                break;
                                        case java.sql.Types.TIMESTAMP:
-                                               //Timestamp timestamp = (rs.getTimestamp(valueIndex));
-                                               //jbdc drops time zone info, 
-                                               //so just get the string from 
-                                               //postgres
-                                               String timestamp = (rs.getString(valueIndex));
-                                               if (!rs.wasNull()) {
-                                                       outValue = timestamp;
-                                               }
+            // it's important to use Timestamp here as getting it
+            // as a string is undefined and is only there for debugging
+            // according to the API. we can make it a string through formatting.
+            // -mh
+                                         Timestamp timestamp = (rs.getTimestamp(valueIndex));
+            if(!rs.wasNull()) {
+              java.util.Date date = new java.util.Date(timestamp.getTime());
+              outValue = _dateFormatterOut.format(date);
+              _cal.setTime(date);
+              int offset = _cal.get(Calendar.ZONE_OFFSET)+
+                            _cal.get(Calendar.DST_OFFSET);
+              String tzOffset = StringUtil.zeroPaddingNumber(
+                                                     offset/_millisPerHour,2,2);
+              outValue = outValue+"+"+tzOffset;
+            }
                                                break;
                                        default:
                                                outValue = "<unsupported value>";
@@ -763,6 +786,18 @@ public class Database implements StorageObject {
                if (metadataFields.contains("webdb_lastchange")) {
                        sql.append(",webdb_lastchange=NOW()");
                }
+               if (metadataFields.contains("webdb_create") &&
+        theEntity.hasValueForField("webdb_create")) {
+      // TimeStamp stuff
+      try {
+        java.util.Date d = _dateFormatterIn.parse(
+                                            theEntity.getValue("webdb_create"));
+        Timestamp tStamp = new Timestamp(d.getTime());
+        sql.append(",webdb_create='"+tStamp.toString()+"'");
+      } catch (ParseException e) {
+        throw new StorageObjectException(e.toString());
+      }
+               }
                if (streamedInput != null) {
                        for (int i = 0; i < streamedInput.size(); i++) {
                                sql.append(",").append(streamedInput.get(i)).append("=?");