[cvs] expresso commit by yves: Yves Amaizo new changes commit today

JCorporate Ltd jcorp at jcorporate.com
Mon Aug 22 22:47:00 UTC 2005


Log Message:
-----------
Yves Amaizo new changes commit today 2005/08/17

Added Files:
-----------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc:
        DateTimeForThread.java

Revision Data
-------------
--- /dev/null
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc/DateTimeForThread.java
@@ -0,0 +1,597 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by Jcorporate Ltd.
+ *        (http://www.jcorporate.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ *    not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written permission,
+ *    please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ *    or other Jcorporate product names; nor may "Expresso" or other
+ *    Jcorporate product names appear in their name, without prior
+ *    written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ *    market space, i.e. framework, without prior written permission
+ *    of Jcorporate Ltd. For written permission, please contact
+ *    partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.misc;
+
+import java.lang.ref.SoftReference;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+import com.jcorporate.expresso.core.dataobjects.DataException;
+import com.jcorporate.expresso.core.db.DBConnection;
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.i18n.Messages;
+import com.jcorporate.expresso.core.registry.RequestRegistry;
+import com.jcorporate.expresso.kernel.util.FastStringBuffer;
+
+/**
+ * prepare timestamps for database storage
+ *
+ * @author Michael Nash
+ * @author Michael Rimov
+ * @author Yves Henri Amaizo
+ * @author David Lloyd
+ */
+public class DateTimeForThread {
+
+    private static final Logger log = Logger.getLogger(DateTimeForThread.class);
+    public static final String DATE_TIME_FORMAT_FOR_DB = "yyyy-MM-dd HH:mm:ss ";
+    
+	private String dataContext = null;
+	
+	private ThreadLocal formattersDateTime = new ThreadLocal() {
+		protected synchronized Object initialValue() {
+			try {
+				String convertFormat = null;
+				ConfigJdbc myConfig = null;
+				try {
+					myConfig = ConfigManager.getJdbcRequired(getDataContext());
+				} catch (ConfigurationException ce) {
+					throw new DataException(ce);
+				}
+				if (!StringUtil.notNull(myConfig.getDateTimeSelectFormat()).equals("")) {
+					convertFormat = myConfig.getDateSelectFormat();
+				} 
+				return buildNewDateFormatter(convertFormat);
+			} catch (DBException e) {
+				throw new RuntimeException();
+			}
+		}
+	};
+	
+	private ThreadLocal formattersDate = new ThreadLocal() {
+		protected synchronized Object initialValue() {
+			try {
+				String convertFormat = null;
+				ConfigJdbc myConfig = null;
+				try {
+					myConfig = ConfigManager.getJdbcRequired(getDataContext());
+				} catch (ConfigurationException ce) {
+					throw new DataException(ce);
+				}
+				if (!StringUtil.notNull(myConfig.getDateSelectFormat()).equals("")) {
+					convertFormat = myConfig.getDateSelectFormat();
+				} 
+				return buildNewDateFormatter(convertFormat);
+			} catch (DBException e) {
+				throw new RuntimeException();
+			}
+		}
+	};
+	
+	private ThreadLocal formattersTime = new ThreadLocal() {
+		protected synchronized Object initialValue() {
+			try {
+				String convertFormat = null;
+				ConfigJdbc myConfig = null;
+				try {
+					myConfig = ConfigManager.getJdbcRequired(getDataContext());
+				} catch (ConfigurationException ce) {
+					throw new DataException(ce);
+				}
+				if (!StringUtil.notNull(myConfig.getTimeSelectFormat()).equals("")) {
+					convertFormat = myConfig.getTimeSelectFormat();
+				} 
+				return buildNewDateFormatter(convertFormat);
+			} catch (DBException e) {
+				throw new RuntimeException();
+			}
+		}
+	};
+	
+	private static Object lock = new Object();
+	//
+	private static HashMap instances = new HashMap();
+
+
+    /**
+     * Constructor
+     */
+    public DateTimeForThread() {
+    } /* DateTime() */
+
+	public DateTimeForThread(String newDataContext) {
+		dataContext = newDataContext;
+	} /* DateTime() */
+
+ 
+ 
+	public static DateTimeForThread getInstance(String newDataContext) {
+		DateTimeForThread instance = (DateTimeForThread) instances.get(newDataContext);
+		if (null == instance) {
+			// si pas initialisé : initialisation synchronisée
+			synchronized (lock) {
+				instance = (DateTimeForThread) instances.get(newDataContext);
+				if (null == instance) {
+					instance = new DateTimeForThread(newDataContext);
+					instances.put(newDataContext, instance);
+				}
+			}
+		}
+		return instance;
+	}
+	
+	
+	public SimpleDateFormat getDateTimeFormatter() throws DataException {
+		return (SimpleDateFormat) formattersDateTime.get();
+	}
+	
+	public SimpleDateFormat getDateFormatter() throws DataException {
+		return (SimpleDateFormat) formattersDate.get();
+	}
+	
+	public SimpleDateFormat getTimeFormatter() throws DataException {
+		return (SimpleDateFormat) formattersTime.get();
+	}
+	
+	private static SimpleDateFormat buildNewDateFormatter(String convertFormat) throws DBException {
+		SimpleDateFormat formatter = new SimpleDateFormat(convertFormat);
+		return formatter;
+	}
+	
+	
+	public Date getDateValue(String sDate) throws DBException {
+		try {
+			SimpleDateFormat formatterDate = getDateFormatter();
+			return formatterDate.parse(sDate);
+		} catch (ParseException e) {
+			throw new DBException(e.getMessage(), e);
+		}
+	}
+ 
+ 
+    /**
+     * Return the current date as a formatted string
+     *
+     * @return String The current date
+     */
+    public static String getDateString() {
+        Calendar rightNow = Calendar.getInstance();
+
+        return ("" + (rightNow.get(Calendar.MONTH) + 1) + "/" +
+                rightNow.get(Calendar.DAY_OF_MONTH) + "/" +
+                rightNow.get(Calendar.YEAR)).trim();
+    } /* getDateString() */
+
+
+    /**
+     * Get a date/time field formatted for insertion into a database
+     * Value of the date/time is the current date and time
+     * uses db context of RequestRegistry
+     *
+     * @return String The formatted date time field
+     */
+    public String getDateTimeForDB() throws com.jcorporate.expresso.core.db.DBException {
+        String dbname = getDataContextFromRegistry();
+        return getDateTimeForDB(new Date(), dbname);
+    } /* getDateTimeForDB() */
+
+    /**
+     * Get a date/time field formatted for insertion into a database
+     * Value of the date/time is the current date and time
+     *
+     * @param dbContext the database context to which to format this for.
+     * @return String The formatted date time field
+     */
+    public String getDateTimeForDB(String dbContext) throws DBException {
+        return getDateTimeForDB(new Date(), dbContext);
+    }
+
+    /**
+     * Get a date/time field formatted for insertion into a database
+     * Value of the date/time is the current date and time
+     *
+     * @param year    integer year
+     * @param month   integer month value
+     * @param day     integer day value
+     * @param hour    integer hour value
+     * @param min     integer minutes value
+     * @param sec     integer seconds value
+     * @param context the data context to format this date-time string for.
+     * @return String The formatted date time field
+     */
+    public String getDateTimeForDB(int year, int month,
+                                          int day, int hour,
+                                          int min, int sec, String context) throws com.jcorporate.expresso.core.db.DBException {
+        Calendar cal = new GregorianCalendar(year, month, day, hour, min, sec);
+
+        return getDateTimeForDB(cal.getTime(), context);
+    } /* getDateTimeForDB(int, int, int, int, int, int) */
+
+    /**
+     * Get a date/time field formatted for insertion into a database
+     *
+     * @param date    java.util.Date to format into a string for a database
+     * @param context the data context to format the string for.
+     * @return String The formatted date time field
+     */
+    public String getDateTimeForDB(Date date, String context) throws com.jcorporate.expresso.core.db.DBException {
+        if (date == null) {
+            return null;
+        }
+
+        /* If no format was specified, don't change the existing field */
+        SimpleDateFormat formatter = getDateTimeFormatter();
+
+        return formatter.format(date).trim();
+
+    } /* getDateTimeForDB(Date) */
+
+    /**
+     * Get a date/time field formatted for insertion into a database
+     * Value of the date/time is the current date and time
+     * uses db context of RequestRegistry
+     *
+     * @param year  integer year
+     * @param month integer month value
+     * @param day   integer day value
+     * @param hour  integer hour value
+     * @param min   integer minutes value
+     * @param sec   integer seconds value
+     * @return String The formatted date time field
+     * @throws DBException upon error
+     */
+    public String getDateTimeForDB(int year, int month,
+                                          int day, int hour,
+                                          int min, int sec) throws com.jcorporate.expresso.core.db.DBException {
+        Calendar cal = new GregorianCalendar(year, month, day, hour, min, sec);
+
+        return getDateTimeForDB(cal.getTime()).trim();
+    } /* getDateTimeForDB(int, int, int, int, int, int) */
+
+    /**
+     * Get a date/time field formatted for insertion into a database (default
+     * context)
+     *
+     * @param date the <code>java.util.Date</code> object to format for the
+     *             database.
+     * @return String The formatted date time field
+     */
+    public String getDateTimeForDB(Date date) throws com.jcorporate.expresso.core.db.DBException {
+        return getDateTimeForDB(date, "default");
+    } /* getDateTimeForDB(Date) */
+
+    /**
+     * Get a date/time string containing the current date and time,
+     * formatted for user readability
+     *
+     * @return String The formatted current date/time
+     */
+    public static String getDateTimeString() {
+
+        /* Calendar rightNow = Calendar.getInstance();
+return ("" + (rightNow.get(Calendar.MONTH) + 1)+ "/"
++ rightNow.get(Calendar.DAY_OF_MONTH)
++ "/" + rightNow.get(Calendar.YEAR) + " "
++ rightNow.get(Calendar.HOUR)
++ ":" + rightNow.get(Calendar.MINUTE)
++ ":" + rightNow.get(Calendar.SECOND)).trim(); */
+
+        // Format the current time. SimpleDateFormat formatter
+        SimpleDateFormat formatter = new SimpleDateFormat("E',' MMM d yyyy 'at' hh:mm:ss a");
+        Date currentTime_1 = new Date();
+        String dateString = formatter.format(currentTime_1);
+
+        return dateString.trim();
+    } /* getDateTimeString() */
+
+    /**
+     * Get a date/time string for a given date and time,
+     * formatted for user readability
+     *
+     * @param date <code>java.util.Date</code> to format for the default database
+     * @return String The formatted current date/time
+     */
+    public static String getDateTimeString(Date date) {
+        if (date == null) {
+            return null;
+        }
+        // Format the current time. SimpleDateFormat formatter
+        SimpleDateFormat formatter = new SimpleDateFormat("E',' MMM d yyyy 'at' hh:mm:ss a");
+        String dateString = formatter.format(date);
+
+        return dateString.trim();
+    } /* getDateTimeString() */
+
+    /**
+     * Get a time field formatted for insertion into a database
+     * Value of the time is the current time
+     * uses db context of RequestRegistry
+     *
+     * @return String The formatted time field
+     * @throws DBException upon error
+     */
+    public String getTimeForDB() throws com.jcorporate.expresso.core.db.DBException {
+        return getTimeForDB(new Date());
+    } /* getTimeForDB() */
+
+    /**
+     * Get atime field formatted for insertion into a database
+     *
+     * @param hour hours as an int
+     * @param min  minutes as an int
+     * @param sec  seconds as an int
+     * @return String The formatted time field
+     */
+    public String getTimeForDB(int hour, int min, int sec) throws com.jcorporate.expresso.core.db.DBException {
+        Calendar cal = new GregorianCalendar();
+        cal.set(Calendar.HOUR_OF_DAY, hour);
+        cal.set(Calendar.MINUTE, min);
+        cal.set(Calendar.SECOND, sec);
+
+        return getTimeForDB(cal.getTime(), "default");
+    } /* getTimeForDB(int, int, int) */
+
+    /**
+     * Get a time field formatted for insertion into a database
+     * uses db context of RequestRegistry
+     *
+     * @param date the <code>java.util.Date</code> object to format for
+     *             the default database
+     * @return String The formatted time field
+     */
+    public String getTimeForDB(Date date) throws com.jcorporate.expresso.core.db.DBException {
+        String dbname = getDataContextFromRegistry();
+        return getTimeForDB(date, dbname);
+    } /* getTimeForDB(Date) */
+
+    /**
+     * Takes into account for the database context.
+     *
+     * @param date    the <code>java.util.Date</code> object to format for
+     * @param context the data context to get the formatted string for.
+     * @return java.lang.String formatted for the specified context
+     */
+    public String getTimeForDB(Date date, String context) throws com.jcorporate.expresso.core.db.DBException {
+        if (date == null) {
+            return null;
+        }
+
+        /* If no format was specified, don't change the existing field */
+        SimpleDateFormat formatter = getTimeFormatter();
+        return formatter.format(date).trim();
+    }
+
+    /**
+     * Get a date field formatted for insertion into a database
+     * Value of the date is the current date
+     * author 		Yves Henri AMAIZO
+     * uses db context of RequestRegistry
+     *
+     * @return String The formatted date field
+     */
+    public String getDateForDB() throws com.jcorporate.expresso.core.db.DBException {
+        return getDateForDB(new Date());
+    } /* getTimeForDB() */
+
+    /**
+     * Get a date field formatted for insertion into a database
+     * author 		Yves Henri AMAIZO
+     * uses db context of RequestRegistry
+     *
+     * @param year The year as an integer. ex: 1999
+     * @param mon  The month as an integer [0-11]
+     * @param day  of month The day of the month [1-31 variable]
+     * @return String The formatted date field
+     */
+    public String getDateForDB(int year, int mon, int day) throws com.jcorporate.expresso.core.db.DBException {
+        Calendar cal = new GregorianCalendar();
+        cal.set(Calendar.YEAR, year);
+        cal.set(Calendar.MONTH, mon);
+        cal.set(Calendar.DAY_OF_MONTH, day);
+
+        String dbname = getDataContextFromRegistry();
+        return getDateForDB(cal.getTime(), dbname);
+    } /* getTimeForDB(int, int, int) */
+
+    /**
+     * Get a time field formatted for insertion into a database
+     * author 		Yves Henri AMAIZO
+     * uses db context of RequestRegistry
+     *
+     * @param date The java.util.Date to format
+     * @return String The formatted date field
+     */
+    public String getDateForDB(Date date) throws com.jcorporate.expresso.core.db.DBException {
+        String dbname = getDataContextFromRegistry();
+
+        return getDateForDB(date, dbname);
+    } /* getTimeForDB(Date) */
+
+    /**
+     * Takes into account for the database context.
+     * author 		Yves Henri AMAIZO
+     *
+     * @param date    The java.util.Date to format
+     * @param context The data context to format for
+     * @return String formatted for the specified database
+     */
+    public String getDateForDB(Date date, String context) throws DBException {
+
+        if (date == null) {
+            return null;
+        }
+
+        /* If no format was specified, don't change the existing field */
+        SimpleDateFormat formatter = getDateFormatter();
+        return formatter.format(date).trim();
+    }
+
+
+    /**
+     * map of locale to date format strings for getDateFormatStrings()
+     */
+    transient private static SoftReference sDateFormatStrings = new SoftReference(new HashMap());
+
+    /**
+     * Get the Date or DateTime format for a Locale.  Called when the item is not cached.
+     * author David Lloyd
+     *
+     * @param locale The locale to get the format for. This may not be null
+     * @param p      The format string to localize
+     * @return String formatted for the date
+     */
+    private static String convertDateFormatStringToLocalized(Locale locale, String p) {
+        try {
+            FastStringBuffer fsb = new FastStringBuffer(16);
+            int n = p.length();
+            for (int i = 0; i < n; i++) {
+                char c = p.charAt(i);
+                switch (c) {
+                    case 'M':
+                    case 'd':
+                    case 'y':
+                    case 'h':
+                    case 'H':
+                    case 'k':
+                    case 'K':
+                    case 'm':
+                    case 's':
+                        {
+                            String s;
+                            try {
+                                s = Messages.getString(locale, "datetime.format." + c + c);
+                                fsb.append(s);
+                            } catch (Exception e) {
+                                fsb.append(c).append(c);
+                            }
+                            while (i < n - 1 && c == p.charAt(i + 1)) {
+                                i++;
+                            }
+                        }
+                        break;
+                    default:
+                        fsb.append(c);
+                        break;
+                }
+            }
+            return fsb.toString();
+        } catch (Exception e) {
+            log.warn("Exception parsing date format string to localized string", e);
+        }
+        return "";
+    }
+
+    private static String getDataContextFromRegistry() {
+        String dbName = null;
+        try {
+            dbName = RequestRegistry.getDataContext();
+        } catch (Exception ex) {
+            dbName = DBConnection.DEFAULT_DB_CONTEXT_NAME;
+        }
+        return dbName;
+    }
+
+
+    /**
+     * DateFormat items in sDateFormatStrings hash table.
+     * author David Lloyd
+     */
+    static class DateFormatCacheItem {
+        public String key;
+        public String dateFormat;
+        public String dateTimeFormat;
+
+        DateFormatCacheItem(Locale locale, String dateFormat, String dateTimeFormat) {
+            key = deriveKey(locale);
+            this.dateFormat = dateFormat;
+            this.dateTimeFormat = dateTimeFormat;
+        }
+
+        public String getKey() {
+            return key;
+        }
+
+        static String deriveKey(Locale locale) {
+            return locale.getCountry() + "-" + locale.getLanguage();
+        }
+    }
+
+	/**
+	 * @return
+	 */
+	public String getDataContext() {
+		return dataContext;
+	}
+
+} /* DateTime */


More information about the cvs mailing list