From 5d214a4da515670d14a14a34b4345a5699d04411 Mon Sep 17 00:00:00 2001 From: Tobias Metzke Date: Mon, 9 Sep 2019 12:59:27 +0200 Subject: [PATCH] fix(engine): prepare to handle all dates in db as UTC dates --- .../impl/db/sql/UTCDateOnlyTypeHandler.java | 61 +++++++++++++++++++ .../impl/db/sql/UTCDateTypeHandler.java | 61 +++++++++++++++++++ .../impl/db/sql/UTCTimeOnlyTypeHandler.java | 61 +++++++++++++++++++ .../bpm/engine/impl/mapping/mappings.xml | 5 ++ .../test/bpmn/async/FoxJobRetryCmdTest.java | 2 +- 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateOnlyTypeHandler.java create mode 100644 engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateTypeHandler.java create mode 100644 engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCTimeOnlyTypeHandler.java diff --git a/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateOnlyTypeHandler.java b/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateOnlyTypeHandler.java new file mode 100644 index 0000000000..d047f1c68b --- /dev/null +++ b/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateOnlyTypeHandler.java @@ -0,0 +1,61 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. Camunda licenses this file to you under the Apache License, + * Version 2.0; you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.camunda.bpm.engine.impl.db.sql; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import org.apache.ibatis.type.DateOnlyTypeHandler; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +@MappedJdbcTypes(value = {JdbcType.DATE}) +@MappedTypes(java.util.Date.class) +public class UTCDateOnlyTypeHandler extends DateOnlyTypeHandler { + + private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC"); + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException { + ps.setDate(i, new java.sql.Date(parameter.getTime()), Calendar.getInstance(TIME_ZONE)); + } + + @Override + public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { + java.sql.Date sqlDate = rs.getDate(columnName, Calendar.getInstance(TIME_ZONE)); + return sqlDate != null ? new Date(sqlDate.getTime()) : null; + } + + @Override + public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + java.sql.Date sqlDate = rs.getDate(columnIndex, Calendar.getInstance(TIME_ZONE)); + return sqlDate != null ? new Date(sqlDate.getTime()) : null; + } + + @Override + public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + java.sql.Date sqlDate = cs.getDate(columnIndex, Calendar.getInstance(TIME_ZONE)); + return sqlDate != null ? new Date(sqlDate.getTime()) : null; + } + +} diff --git a/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateTypeHandler.java b/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateTypeHandler.java new file mode 100644 index 0000000000..ed57453dde --- /dev/null +++ b/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCDateTypeHandler.java @@ -0,0 +1,61 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. Camunda licenses this file to you under the Apache License, + * Version 2.0; you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.camunda.bpm.engine.impl.db.sql; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import org.apache.ibatis.type.DateTypeHandler; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +@MappedJdbcTypes(value = {JdbcType.TIMESTAMP}, includeNullJdbcType = true) +@MappedTypes(java.util.Date.class) +public class UTCDateTypeHandler extends DateTypeHandler { + + private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC"); + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException { + ps.setTimestamp(i, new Timestamp(parameter.getTime()), Calendar.getInstance(TIME_ZONE)); + } + + @Override + public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { + Timestamp sqlTimestamp = rs.getTimestamp(columnName, Calendar.getInstance(TIME_ZONE)); + return sqlTimestamp != null ? new Date(sqlTimestamp.getTime()) : null; + } + + @Override + public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Timestamp sqlTimestamp = rs.getTimestamp(columnIndex, Calendar.getInstance(TIME_ZONE)); + return sqlTimestamp != null ? new Date(sqlTimestamp.getTime()) : null; + } + + @Override + public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Timestamp sqlTimestamp = cs.getTimestamp(columnIndex, Calendar.getInstance(TIME_ZONE)); + return sqlTimestamp != null ? new Date(sqlTimestamp.getTime()) : null; + } +} diff --git a/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCTimeOnlyTypeHandler.java b/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCTimeOnlyTypeHandler.java new file mode 100644 index 0000000000..e3e8941d57 --- /dev/null +++ b/engine/src/main/java/org/camunda/bpm/engine/impl/db/sql/UTCTimeOnlyTypeHandler.java @@ -0,0 +1,61 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. Camunda licenses this file to you under the Apache License, + * Version 2.0; you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.camunda.bpm.engine.impl.db.sql; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Time; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; +import org.apache.ibatis.type.TimeOnlyTypeHandler; + +@MappedJdbcTypes(value = {JdbcType.TIME}) +@MappedTypes(java.util.Date.class) +public class UTCTimeOnlyTypeHandler extends TimeOnlyTypeHandler { + + private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC"); + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException { + ps.setTime(i, new Time(parameter.getTime()), Calendar.getInstance(TIME_ZONE)); + } + + @Override + public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { + Time sqlTime = rs.getTime(columnName, Calendar.getInstance(TIME_ZONE)); + return sqlTime != null ? new Date(sqlTime.getTime()) : null; + } + + @Override + public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Time sqlTime = rs.getTime(columnIndex, Calendar.getInstance(TIME_ZONE)); + return sqlTime != null ? new Date(sqlTime.getTime()) : null; + } + + @Override + public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Time sqlTime = cs.getTime(columnIndex, Calendar.getInstance(TIME_ZONE)); + return sqlTime != null ? new Date(sqlTime.getTime()) : null; + } +} diff --git a/engine/src/main/resources/org/camunda/bpm/engine/impl/mapping/mappings.xml b/engine/src/main/resources/org/camunda/bpm/engine/impl/mapping/mappings.xml index e70d471187..78d7e231d6 100644 --- a/engine/src/main/resources/org/camunda/bpm/engine/impl/mapping/mappings.xml +++ b/engine/src/main/resources/org/camunda/bpm/engine/impl/mapping/mappings.xml @@ -23,6 +23,11 @@ + + + + + diff --git a/engine/src/test/java/org/camunda/bpm/engine/test/bpmn/async/FoxJobRetryCmdTest.java b/engine/src/test/java/org/camunda/bpm/engine/test/bpmn/async/FoxJobRetryCmdTest.java index 9a2222a2b6..d0fa6bb420 100644 --- a/engine/src/test/java/org/camunda/bpm/engine/test/bpmn/async/FoxJobRetryCmdTest.java +++ b/engine/src/test/java/org/camunda/bpm/engine/test/bpmn/async/FoxJobRetryCmdTest.java @@ -264,7 +264,7 @@ public void testRetryOnServiceTaskLikeMessageThrowEvent() { } @Deployment(resources = { "org/camunda/bpm/engine/test/bpmn/async/FoxJobRetryCmdTest.testFailedServiceTask.bpmn20.xml" }) - public void FAILING_testFailedRetryWithTimeShift() throws ParseException { + public void testFailedRetryWithTimeShift() throws ParseException { // set date to hour before time shift (2015-10-25T03:00:00 CEST => // 2015-10-25T02:00:00 CET) Date tenMinutesBeforeTimeShift = createDateFromLocalString("2015-10-25T02:50:00 CEST");