Author: hansbak
Date: Fri Aug 14 07:42:30 2009 New Revision: 804104 URL: http://svn.apache.org/viewvc?rev=804104&view=rev Log: adjustment of storing/retrieving timestamp in the proper timezone. In the database the time zone is always GMT Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SQLProcessor.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=804104&r1=804103&r2=804104&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java Fri Aug 14 07:42:30 2009 @@ -78,7 +78,7 @@ public static Configuration makeConfiguration(BeansWrapper wrapper) { Configuration newConfig = new Configuration(); - + newConfig.setObjectWrapper(wrapper); newConfig.setSharedVariable("Static", wrapper.getStaticModels()); newConfig.setLocalizedLookup(false); @@ -218,11 +218,11 @@ } env.setLocale(locale); - TimeZone timeZone = (TimeZone) context.get("timeZone"); - if (timeZone == null) { - timeZone = TimeZone.getDefault(); - } - env.setTimeZone(timeZone); +// TimeZone timeZone = (TimeZone) context.get("timeZone"); +// if (timeZone == null) { +// timeZone = TimeZone.getDefault(); +// } +// env.setTimeZone(timeZone); } public static Configuration getDefaultOfbizConfig() { Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SQLProcessor.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SQLProcessor.java?rev=804104&r1=804103&r2=804104&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SQLProcessor.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SQLProcessor.java Fri Aug 14 07:42:30 2009 @@ -27,10 +27,15 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.TimeZone; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.GenericDataSourceException; +import org.ofbiz.entity.GenericDelegator; import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.config.DatasourceInfo; import org.ofbiz.entity.config.EntityConfigUtil; import org.ofbiz.entity.transaction.GenericTransactionException; @@ -578,6 +583,27 @@ */ public void setValue(java.sql.Timestamp field) throws SQLException { if (field != null) { + GenericDelegator delegator=GenericDelegator.getGenericDelegator("default"); + String userId=delegator.getCurrentUserIdentifier(); + try { + if(userId!=null){ + String timeZoneId=TimeZone.getDefault().getID(); + GenericValue userLogin=delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userId)); + if(userLogin!=null){ + if(UtilValidate.isNotEmpty(userLogin.get("lastTimeZone"))){ + timeZoneId=userLogin.getString("lastTimeZone"); + SqlJdbcUtil.setLastTimeZoneId(timeZoneId); + } + } + if (field instanceof java.sql.Timestamp) { + String dateField=SqlJdbcUtil.convertTimeZone(field.toString(),timeZoneId, "GMT"); + field= java.sql.Timestamp.valueOf(dateField); + } + } + } catch (GenericEntityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } _ps.setTimestamp(_ind, field); } else { _ps.setNull(_ind, Types.TIMESTAMP); Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java?rev=804104&r1=804103&r2=804104&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java Fri Aug 14 07:42:30 2009 @@ -30,11 +30,15 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Clob; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Collection; +import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TimeZone; import java.util.TreeSet; import javax.sql.rowset.serial.SerialBlob; @@ -44,6 +48,7 @@ import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.ObjectType; +import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.GenericDataSourceException; @@ -486,7 +491,35 @@ } } } - + + public static String lastTimeZoneId=TimeZone.getDefault().getID(); + public static void setLastTimeZoneId(String timeZoneId){ + lastTimeZoneId=timeZoneId; + } + public static String getLastTimeZoneId(){ + return lastTimeZoneId; + } + public static String convertTimeZone(String time, String sourceTZ, String destTZ){ + SimpleDateFormat sdf = new SimpleDateFormat(UtilDateTime.DATE_TIME_FORMAT); + Date specifiedTime=null; + try { + if (sourceTZ != null) + sdf.setTimeZone(TimeZone.getTimeZone(sourceTZ)); + else + sdf.setTimeZone(TimeZone.getDefault()); + specifiedTime = sdf.parse(time); + + if (destTZ != null) + sdf.setTimeZone(TimeZone.getTimeZone(destTZ)); + else + sdf.setTimeZone(TimeZone.getDefault()); + } catch (ParseException e) { + // TODO Auto-generated catch block + Debug.logError("Error Convert time zone: "+e.getMessage(), module); + } + return sdf.format(specifiedTime); + } + public static void getValue(ResultSet rs, int ind, ModelField curField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { ModelFieldType mft = modelFieldTypeReader.getModelFieldType(curField.getType()); @@ -539,7 +572,12 @@ break; case 2: - entity.dangerousSetNoCheckButFast(curField, rs.getTimestamp(ind)); + Object value=rs.getTimestamp(ind); + if (value!=null&&value instanceof java.sql.Timestamp) { + String dateField=convertTimeZone(value.toString(),"GMT",lastTimeZoneId); + value= java.sql.Timestamp.valueOf(dateField); + } + entity.dangerousSetNoCheckButFast(curField,value); break; case 3: Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java?rev=804104&r1=804103&r2=804104&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java (original) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java Fri Aug 14 07:42:30 2009 @@ -22,6 +22,7 @@ import java.math.BigDecimal; import java.text.DateFormat; import java.text.NumberFormat; +import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -675,7 +676,8 @@ if (locale == null) locale = Locale.getDefault(); TimeZone timeZone = (TimeZone) context.get("timeZone"); if (timeZone == null) timeZone = TimeZone.getDefault(); - + + SimpleDateFormat sdf=new SimpleDateFormat(UtilDateTime.DATE_TIME_FORMAT); String returnValue; // if useRequestParameters is TRUE then parameters will always be used, if FALSE then parameters will never be used @@ -736,27 +738,32 @@ } } - if (retVal != null) { - // format string based on the user's locale and time zone - if (retVal instanceof Double || retVal instanceof Float || retVal instanceof BigDecimal) { - NumberFormat nf = NumberFormat.getInstance(locale); - nf.setMaximumFractionDigits(10); - returnValue = nf.format(retVal); - } else if (retVal instanceof java.sql.Date) { - DateFormat df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, null); - returnValue = df.format((java.util.Date) retVal); - } else if (retVal instanceof java.sql.Time) { - DateFormat df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, null); - returnValue = df.format((java.util.Date) retVal); - } else if (retVal instanceof java.sql.Timestamp) { - DateFormat df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, null); - returnValue = df.format((java.util.Date) retVal); - } else if (retVal instanceof java.util.Date) { - DateFormat df = UtilDateTime.toDateTimeFormat("EEE MMM dd hh:mm:ss z yyyy", timeZone, null); - returnValue = df.format((java.util.Date) retVal); - } else { - returnValue = retVal.toString(); - } + if (retVal != null) { + // format string based on the user's locale and time zone + if (retVal instanceof Double || retVal instanceof Float || retVal instanceof BigDecimal) { + NumberFormat nf = NumberFormat.getInstance(locale); + nf.setMaximumFractionDigits(10); + returnValue = nf.format(retVal); + } else if (retVal instanceof java.sql.Date) { +// DateFormat df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, null); +// returnValue = df.format((java.util.Date) retVal); + returnValue=sdf.format((java.util.Date)retVal); + } else if (retVal instanceof java.sql.Time) { +// DateFormat df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, null); +// returnValue = df.format((java.util.Date) retVal); + returnValue=sdf.format((java.util.Date)retVal); + } else if (retVal instanceof java.sql.Timestamp) { +// DateFormat df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, null); +// returnValue = df.format((java.util.Date) retVal); + returnValue=sdf.format((java.util.Date)retVal); + } else if (retVal instanceof java.util.Date) { +// DateFormat df = UtilDateTime.toDateTimeFormat("EEE MMM dd hh:mm:ss z yyyy", timeZone, null); +// returnValue = df.format((java.util.Date) retVal); + SimpleDateFormat sdfz=new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy"); + returnValue=sdfz.format((java.util.Date)retVal); + } else { + returnValue = retVal.toString(); + } return returnValue; // do not encode date and number type fields } else { returnValue = defaultValue; |
Free forum by Nabble | Edit this page |