diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/Conversions.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/Conversions.java index 3ece35a3c..c1ade681d 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/conversion/Conversions.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/Conversions.java @@ -22,6 +22,8 @@ import static org.mapstruct.ap.internal.conversion.ReverseConversion.reverse; import java.math.BigDecimal; import java.math.BigInteger; +import java.sql.Time; +import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; import java.util.HashMap; @@ -189,6 +191,9 @@ public class Conversions { register( Enum.class, String.class, new EnumStringConversion() ); register( Date.class, String.class, new DateToStringConversion() ); register( BigDecimal.class, BigInteger.class, new BigDecimalToBigIntegerConversion() ); + register( Date.class, Time.class, new DateToSqlTimeConversion() ); + register( Date.class, java.sql.Date.class, new DateToSqlDateConversion() ); + register( Date.class, Timestamp.class, new DateToSqlTimestampConversion() ); } private void registerJodaConversions() { diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlDateConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlDateConversion.java new file mode 100644 index 000000000..5c340c884 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlDateConversion.java @@ -0,0 +1,40 @@ +/** + * Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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.mapstruct.ap.internal.conversion; + + +import org.mapstruct.ap.internal.model.common.ConversionContext; + +/** + * Conversion between {@link java.util.Date} and {@link java.sql.Date}. + * + * @author Filip Hrisafov + */ +public class DateToSqlDateConversion extends SimpleConversion { + + @Override + protected String getToExpression(ConversionContext conversionContext) { + return "new java.sql.Date( .getTime() )"; + } + + @Override + protected String getFromExpression(ConversionContext conversionContext) { + return ""; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlTimeConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlTimeConversion.java new file mode 100644 index 000000000..767012741 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlTimeConversion.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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.mapstruct.ap.internal.conversion; + +import org.mapstruct.ap.internal.model.common.ConversionContext; + +/** + * Conversion between {@link java.util.Date} and {@link java.sql.Time}. + * + * @author Filip Hrisafov + */ +public class DateToSqlTimeConversion extends SimpleConversion { + + @Override + protected String getToExpression(ConversionContext conversionContext) { + return "new java.sql.Time( .getTime() )"; + } + + @Override + protected String getFromExpression(ConversionContext conversionContext) { + return ""; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlTimestampConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlTimestampConversion.java new file mode 100644 index 000000000..291bb279d --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToSqlTimestampConversion.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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.mapstruct.ap.internal.conversion; + +import org.mapstruct.ap.internal.model.common.ConversionContext; + +/** + * Conversion between {@link java.util.Date} and {@link java.sql.Timestamp}. + * + * @author Filip Hrisafov + */ +public class DateToSqlTimestampConversion extends SimpleConversion { + + @Override + protected String getToExpression(ConversionContext conversionContext) { + return "new java.sql.Timestamp( .getTime() )"; + } + + @Override + protected String getFromExpression(ConversionContext conversionContext) { + return ""; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java index d167851c0..f5d888d63 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java @@ -41,11 +41,7 @@ public class BuiltInMappingMethods { new StringToXmlGregorianCalendar( typeFactory ), new XmlGregorianCalendarToString( typeFactory ), new CalendarToXmlGregorianCalendar( typeFactory ), - new XmlGregorianCalendarToCalendar( typeFactory ), - new DateToSqlDate( typeFactory ), - new DateToSqlTime( typeFactory ), - new DateToSqlTimestamp( typeFactory ) - + new XmlGregorianCalendarToCalendar( typeFactory ) ); if ( isJaxbAvailable( typeFactory ) ) { diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java index 7d4b42e83..0971f9dad 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java @@ -20,7 +20,10 @@ package org.mapstruct.ap.test.conversion.date; import static org.assertj.core.api.Assertions.assertThat; +import java.sql.Time; +import java.sql.Timestamp; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; @@ -161,4 +164,46 @@ public class DateConversionTest { new GregorianCalendar( 2013, 3, 11 ).getTime() } ); } + + @IssueKey("858") + @Test + public void shouldApplyDateToSqlConversion() throws Exception { + GregorianCalendar time = new GregorianCalendar( 2016, Calendar.AUGUST, 24, 20, 30, 30 ); + GregorianCalendar sqlDate = new GregorianCalendar( 2016, Calendar.AUGUST, 23, 21, 35, 35 ); + GregorianCalendar timestamp = new GregorianCalendar( 2016, Calendar.AUGUST, 22, 21, 35, 35 ); + Source source = new Source(); + source.setTime( time.getTime() ); + source.setSqlDate( sqlDate.getTime() ); + source.setTimestamp( timestamp.getTime() ); + + + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + Time expectedTime = new Time( time.getTime().getTime() ); + java.sql.Date expectedSqlDate = new java.sql.Date( sqlDate.getTime().getTime() ); + Timestamp expectedTimestamp = new Timestamp( timestamp.getTime().getTime() ); + + assertThat( target.getTime() ).isEqualTo( expectedTime ); + assertThat( target.getSqlDate() ).isEqualTo( expectedSqlDate ); + assertThat( target.getTimestamp() ).isEqualTo( expectedTimestamp ); + } + + @IssueKey("858") + @Test + public void shouldApplySqlToDateConversion() throws Exception { + Target target = new Target(); + GregorianCalendar time = new GregorianCalendar( 2016, Calendar.AUGUST, 24, 20, 30, 30 ); + GregorianCalendar sqlDate = new GregorianCalendar( 2016, Calendar.AUGUST, 23, 21, 35, 35 ); + GregorianCalendar timestamp = new GregorianCalendar( 2016, Calendar.AUGUST, 22, 21, 35, 35 ); + target.setTime( new Time( time.getTime().getTime() ) ); + target.setSqlDate( new java.sql.Date( sqlDate.getTime().getTime() ) ); + target.setTimestamp( new Timestamp( timestamp.getTime().getTime() ) ); + + + Source source = SourceTargetMapper.INSTANCE.targetToSource( target ); + + assertThat( source ).isNotNull(); + assertThat( source.getTime() ).isEqualTo( target.getTime() ); + assertThat( source.getSqlDate() ).isEqualTo( target.getSqlDate() ); + assertThat( source.getTimestamp() ).isEqualTo( target.getTimestamp() ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java index 9453cb633..ed2c7c4c6 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java @@ -24,6 +24,9 @@ public class Source { private Date date; private Date anotherDate; + private Date time; + private Date sqlDate; + private Date timestamp; public Date getDate() { return date; @@ -40,4 +43,29 @@ public class Source { public void setAnotherDate(Date anotherDate) { this.anotherDate = anotherDate; } + + public Date getTime() { + return time; + } + + public void setTime(Date time) { + this.time = time; + } + + public Date getSqlDate() { + return sqlDate; + } + + public void setSqlDate(Date sqlDate) { + this.sqlDate = sqlDate; + } + + public Date getTimestamp() { + return timestamp; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + } diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java index b12620eef..a2416221a 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java @@ -18,10 +18,17 @@ */ package org.mapstruct.ap.test.conversion.date; +import java.sql.Date; +import java.sql.Time; +import java.sql.Timestamp; + public class Target { private String date; private String anotherDate; + private Time time; + private Date sqlDate; + private Timestamp timestamp; public String getDate() { return date; @@ -38,4 +45,28 @@ public class Target { public void setAnotherDate(String anotherDate) { this.anotherDate = anotherDate; } + + public Time getTime() { + return time; + } + + public void setTime(Time time) { + this.time = time; + } + + public Date getSqlDate() { + return sqlDate; + } + + public void setSqlDate(Date sqlDate) { + this.sqlDate = sqlDate; + } + + public Timestamp getTimestamp() { + return timestamp; + } + + public void setTimestamp(Timestamp timestamp) { + this.timestamp = timestamp; + } }