Commit 4ec715bf authored by Asier Lostalé's avatar Asier Lostalé
Browse files

related to bug 38671: use format in Openbravo.properties in other places

  There were other places that created a SimpleDateFormat directly from format
  stored in session. They have been replaced to use the same mechanism defined
  in DateUIDefinition.

--HG--
extra : rebase_source : 2705f5dbad059bfa4b23bbc3b77f8b48c87a6877
parent 4c4ef4f4
......@@ -11,7 +11,7 @@
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2010-2018 Openbravo SLU
* All portions are Copyright (C) 2010-2019 Openbravo SLU
* All Rights Reserved.
* Contributor(s): ______________________________________.
************************************************************************
......@@ -33,6 +33,7 @@ import org.openbravo.base.util.Check;
import org.openbravo.base.util.OBClassLoader;
import org.openbravo.base.weld.WeldUtils;
import org.openbravo.client.application.window.ApplicationDictionaryCachedStructures;
import org.openbravo.client.kernel.reference.UIDefinitionController;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.model.ad.ui.Window;
......@@ -50,8 +51,6 @@ public class OBBindings {
private OBContext context;
private Map<String, String> requestMap;
private HttpSession httpSession;
private SimpleDateFormat dateFormat = null;
private SimpleDateFormat dateTimeFormat = null;
private SimpleDateFormat jsDateTimeFormat = null;
public OBBindings(OBContext obContext) {
......@@ -74,11 +73,6 @@ public class OBBindings {
requestMap = parameters;
dateFormat = new SimpleDateFormat((String) httpSession.getAttribute("#AD_JAVADATEFORMAT"));
dateTimeFormat = new SimpleDateFormat(
(String) httpSession.getAttribute("#AD_JAVADATETIMEFORMAT"));
jsDateTimeFormat = JsonUtils.createJSTimeFormat();
}
......@@ -188,7 +182,7 @@ public class OBBindings {
}
public String formatDate(Date d) {
return dateFormat.format(d);
return UIDefinitionController.DATE_UI_DEFINITION.convertToClassicString(d);
}
public String formatDate(Object d) {
......@@ -216,7 +210,7 @@ public class OBBindings {
}
public String formatDateTime(Date d) {
return dateTimeFormat.format(d);
return UIDefinitionController.DATETIME_UI_DEFINITION.convertToClassicString(d);
}
public String formatDateTime(Object d) {
......@@ -225,26 +219,22 @@ public class OBBindings {
public Date parseDate(String date) {
try {
return dateFormat.parse(date);
return UIDefinitionController.DATE_UI_DEFINITION.parse(date);
} catch (Exception e) {
log.error("Error parsing string date " + date + " with format: " + dateFormat, e);
return null;
}
return null;
}
public Date parseDateTime(String dateTime) {
try {
Date result = convertToLocalTime(jsDateTimeFormat.parse(dateTime));
return result;
return convertToLocalTime(jsDateTimeFormat.parse(dateTime));
} catch (Exception e) {
try {
Date result = convertToLocalTime(dateTimeFormat.parse(dateTime));
return result;
return convertToLocalTime(UIDefinitionController.DATETIME_UI_DEFINITION.parse(dateTime));
} catch (Exception ex) {
log.error("Error parsing string date " + dateTime + " with format: " + dateTimeFormat, e);
return null;
}
}
return null;
}
private static Date convertToLocalTime(Date UTCTime) {
......
......@@ -20,6 +20,7 @@ package org.openbravo.client.kernel.reference;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
......@@ -142,13 +143,23 @@ public class DateUIDefinition extends UIDefinition {
if (value.contains("T")) {
return value;
}
final Date date = getClassicFormat().parse(value);
final Date date = parse(value);
return getUIFormat().format(date);
} catch (Exception e) {
throw new OBException(e);
}
}
/** Parses an {@link String} to a {@link Date} using reference's classic format. */
public synchronized Date parse(String value) {
try {
return getClassicFormat().parse(value);
} catch (ParseException e) {
log.error("Could not parse date {}", value, e);
throw new OBException(e);
}
}
@Override
public String getTypeProperties() {
final StringBuilder sb = new StringBuilder();
......
......@@ -58,6 +58,11 @@ public class UIDefinitionController extends BaseTemplateComponent {
public static final String INPUTFORMAT_QUALIFIER = "Edition";
public static final String NORMALFORMAT_QUALIFIER = "Inform";
public static final DateUIDefinition DATE_UI_DEFINITION = (DateUIDefinition) instance
.getUIDefinitionByReferenceId("15");
public static final DateUIDefinition DATETIME_UI_DEFINITION = (DateTimeUIDefinition) instance
.getUIDefinitionByReferenceId("16");
private static final String EncryptedStringReferenceID = "16EC6DF4A59747749FDF256B7FBBB058";
private static final String HashedStringReferenecID = "C5C21C28B39E4683A91779F16C112E40";
......@@ -85,12 +90,16 @@ public class UIDefinitionController extends BaseTemplateComponent {
}
public UIDefinition getUIDefinition(Reference reference) {
return getUIDefinitionByReferenceId(reference.getId());
}
private UIDefinition getUIDefinitionByReferenceId(String referenceId) {
if (cachedDefinitions == null) {
setInitCachedDefinitions();
}
final UIDefinition uiDefinition = cachedDefinitions.get(reference.getId());
final UIDefinition uiDefinition = cachedDefinitions.get(referenceId);
if (uiDefinition == null) {
log.warn("NO UIDefinition found for reference " + reference.getId());
log.warn("NO UIDefinition found for reference {}", referenceId);
}
return uiDefinition;
}
......
......@@ -68,7 +68,7 @@ import org.openbravo.client.application.window.OBViewUtil;
import org.openbravo.client.kernel.BaseKernelServlet;
import org.openbravo.client.kernel.KernelUtils;
import org.openbravo.client.kernel.OBUserException;
import org.openbravo.client.kernel.RequestContext;
import org.openbravo.client.kernel.reference.UIDefinitionController;
import org.openbravo.dal.core.DalUtil;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.core.SessionHandler;
......@@ -648,24 +648,15 @@ public class DataSourceServlet extends BaseKernelServlet {
} else if (dateCols.contains(key) && keyValue != null
&& !keyValue.toString().equals("null")) {
Date date = JsonUtils.createDateFormat().parse(keyValue.toString());
String pattern = RequestContext.get()
.getSessionAttribute("#AD_JAVADATEFORMAT")
.toString();
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
dateFormat.setLenient(true);
keyValue = dateFormat.format(date);
keyValue = UIDefinitionController.DATE_UI_DEFINITION.convertToClassicString(date);
} else if (dateTimeCols.contains(key) && keyValue != null
&& !keyValue.toString().equals("null")) {
final String repairedString = JsonUtils.convertFromXSDToJavaFormat(keyValue.toString());
Date localDate = JsonUtils.createDateTimeFormat().parse(repairedString);
Date clientTimezoneDate = null;
clientTimezoneDate = convertFromLocalToClientTimezone(localDate);
String pattern = RequestContext.get()
.getSessionAttribute("#AD_JAVADATETIMEFORMAT")
.toString();
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
dateFormat.setLenient(true);
keyValue = dateFormat.format(clientTimezoneDate);
keyValue = UIDefinitionController.DATETIME_UI_DEFINITION
.convertToClassicString(clientTimezoneDate);
} else if (timeCols.contains(key) && keyValue != null
&& !keyValue.toString().equals("null")) {
Date UTCdate = JsonUtils.createTimeFormatWithoutGMTOffset().parse(keyValue.toString());
......
......@@ -11,7 +11,7 @@
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2010-2016 Openbravo SLU
* All portions are Copyright (C) 2010-2019 Openbravo SLU
* All Rights Reserved.
* Contributor(s): ______________________________________.
************************************************************************
......@@ -19,8 +19,6 @@
package org.openbravo.userinterface.selector;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
......@@ -66,8 +64,6 @@ import org.openbravo.service.json.JsonUtils;
public class SelectorDataSourceFilter implements DataSourceFilter {
private static Logger log = LogManager.getLogger();
private String dateFormat = null;
private DateFormat systemDateFormat = null;
private TextMatching textMatching = TextMatching.exact;
@Inject
......@@ -385,15 +381,9 @@ public class SelectorDataSourceFilter implements DataSourceFilter {
} else if (Boolean.class == property.getPrimitiveObjectType() || property.isNumericType()) {
sb.append("e." + sf.getProperty() + " = " + result.toString());
} else if (Date.class.isAssignableFrom(property.getPrimitiveObjectType())) {
if (dateFormat == null || systemDateFormat == null) {
dateFormat = (String) request.getSession(false).getAttribute("#AD_JAVADATEFORMAT");
systemDateFormat = new SimpleDateFormat(dateFormat);
}
try {
final Calendar cal = Calendar.getInstance();
cal.setTime(systemDateFormat.parse(result.toString()));
cal.setTime(UIDefinitionController.DATE_UI_DEFINITION.parse(result.toString()));
sb.append("(day(" + "e." + sf.getProperty() + ") = " + cal.get(Calendar.DATE)
+ " and month(" + "e." + sf.getProperty() + ") = " + (cal.get(Calendar.MONTH) + 1)
+ " and year(" + "e." + sf.getProperty() + ") = " + cal.get(Calendar.YEAR) + ")");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment