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 45646c2cd..9178318f1 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 @@ -61,6 +61,8 @@ public class BuiltInMappingMethods { builtInMethods.add( new XmlGregorianCalendarToJodaDateTime( typeFactory ) ); builtInMethods.add( new JodaLocalDateTimeToXmlGregorianCalendar( typeFactory ) ); builtInMethods.add( new XmlGregorianCalendarToJodaLocalDateTime( typeFactory ) ); + builtInMethods.add( new JodaLocalDateToXmlGregorianCalendar( typeFactory ) ); + builtInMethods.add( new XmlGregorianCalendarToJodaLocalDate( typeFactory ) ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.java new file mode 100644 index 000000000..5b847fd86 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.java @@ -0,0 +1,74 @@ +/** + * 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.model.source.builtin; + +import static org.mapstruct.ap.internal.util.Collections.asSet; + +import java.util.Set; + +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.mapstruct.ap.internal.model.common.Parameter; +import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.model.common.TypeFactory; +import org.mapstruct.ap.internal.util.JodaTimeConstants; + +/** + * @author Sjaak Derksen + */ +public class JodaLocalDateToXmlGregorianCalendar extends BuiltInMethod { + + private final Parameter parameter; + private final Type returnType; + private final Set importTypes; + + public JodaLocalDateToXmlGregorianCalendar(TypeFactory typeFactory) { + this.parameter = new Parameter( + "dt", + typeFactory.getType( JodaTimeConstants.LOCAL_DATE_FQN ) + ); + this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); + + this.importTypes = asSet( + returnType, + parameter.getType(), + typeFactory.getType( DatatypeConstants.class ), + typeFactory.getType( DatatypeFactory.class ), + typeFactory.getType( DatatypeConfigurationException.class ) + ); + } + + @Override + public Set getImportTypes() { + return importTypes; + } + + @Override + public Parameter getParameter() { + return parameter; + } + + @Override + public Type getReturnType() { + return returnType; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.java new file mode 100644 index 000000000..067f57b01 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.java @@ -0,0 +1,64 @@ +/** + * 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.model.source.builtin; + +import static org.mapstruct.ap.internal.util.Collections.asSet; + +import java.util.Set; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.mapstruct.ap.internal.model.common.Parameter; +import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.model.common.TypeFactory; +import org.mapstruct.ap.internal.util.JodaTimeConstants; + +/** + * @author Sjaak Derksen + */ +public class XmlGregorianCalendarToJodaLocalDate extends BuiltInMethod { + + private final Parameter parameter; + private final Type returnType; + private final Set importTypes; + + public XmlGregorianCalendarToJodaLocalDate(TypeFactory typeFactory) { + this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) ); + this.returnType = typeFactory.getType( JodaTimeConstants.LOCAL_DATE_FQN ); + this.importTypes = asSet( + typeFactory.getType( DatatypeConstants.class ), + returnType, + parameter.getType() ); + } + + @Override + public Parameter getParameter() { + return parameter; + } + + @Override + public Type getReturnType() { + return returnType; + } + + @Override + public Set getImportTypes() { + return importTypes; + } +} diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl new file mode 100644 index 000000000..6888838c6 --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl @@ -0,0 +1,36 @@ +<#-- + + 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. + +--> +private XMLGregorianCalendar ${name}( LocalDate dt ) { + if ( dt == null ) { + return null; + } + + try { + return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( + dt.getYear(), + dt.getMonthOfYear(), + dt.getDayOfMonth(), + DatatypeConstants.FIELD_UNDEFINED ); + } + catch ( DatatypeConfigurationException ex ) { + throw new RuntimeException( ex ); + } +} \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl new file mode 100644 index 000000000..129a19a62 --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl @@ -0,0 +1,33 @@ +<#-- + + 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. + +--> +private static LocalDate ${name}( XMLGregorianCalendar xcal ) { + if ( xcal == null ) { + return null; + } + + if ( xcal.getYear() != DatatypeConstants.FIELD_UNDEFINED + && xcal.getMonth() != DatatypeConstants.FIELD_UNDEFINED + && xcal.getDay() != DatatypeConstants.FIELD_UNDEFINED ) { + return new LocalDate( xcal.getYear(), xcal.getMonth(), xcal.getDay() ); + } + + return null; +} \ No newline at end of file diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java index 41fab881f..aec9ee13e 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java @@ -25,15 +25,19 @@ import javax.xml.datatype.XMLGregorianCalendar; import static org.assertj.core.api.Assertions.assertThat; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; +import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.test.builtin.bean.jodatime.bean.DateTimeBean; +import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateBean; import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateTimeBean; import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean; import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.DateTimeToXmlGregorianCalendar; import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.LocalDateTimeToXmlGregorianCalendar; +import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.LocalDateToXmlGregorianCalendar; import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToDateTime; +import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToLocalDate; import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToLocalDateTime; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; @@ -45,6 +49,7 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; */ @WithClasses({ DateTimeBean.class, + LocalDateBean.class, LocalDateTimeBean.class, XmlGregorianCalendarBean.class }) @@ -385,6 +390,61 @@ public class JodaTimeTest { LocalDateTimeBean res = XmlGregorianCalendarToLocalDateTime.INSTANCE.toDateTimeBean( in ); assertThat( res.getLocalDateTime() ).isNull(); + } + @Test + @WithClasses(LocalDateToXmlGregorianCalendar.class) + public void shouldMapLocalDateToXmlGregorianCalendar() { + + LocalDateBean in = new LocalDateBean(); + LocalDate dt = new LocalDate(2010, 1, 15 ); + in.setLocalDate( dt ); + XmlGregorianCalendarBean res = LocalDateToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( in ); + + assertThat( res.getxMLGregorianCalendar().getYear() ).isEqualTo( 2010 ); + assertThat( res.getxMLGregorianCalendar().getMonth() ).isEqualTo( 1 ); + assertThat( res.getxMLGregorianCalendar().getDay() ).isEqualTo( 15 ); + assertThat( res.getxMLGregorianCalendar().getHour() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED ); + assertThat( res.getxMLGregorianCalendar().getMinute() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED ); + assertThat( res.getxMLGregorianCalendar().getSecond() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED ); + assertThat( res.getxMLGregorianCalendar().getMillisecond() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED ); + assertThat( res.getxMLGregorianCalendar().getTimezone() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED ); + } + + @Test + @WithClasses(XmlGregorianCalendarToLocalDate.class) + public void shouldMapXmlGregorianCalendarToLocalDate() throws Exception { + + XmlGregorianCalendarBean in = new XmlGregorianCalendarBean(); + XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendarDate( + 1999, + 5, + 25, + DatatypeConstants.FIELD_UNDEFINED ); + in.setxMLGregorianCalendar( xcal ); + + LocalDateBean res = XmlGregorianCalendarToLocalDate.INSTANCE.toLocalDateBean( in ); + assertThat( res.getLocalDate().getYear() ).isEqualTo( 1999 ); + assertThat( res.getLocalDate().getMonthOfYear() ).isEqualTo( 5 ); + assertThat( res.getLocalDate().getDayOfMonth() ).isEqualTo( 25 ); + } + + + @Test + @WithClasses(XmlGregorianCalendarToLocalDate.class) + public void shouldNotMapXmlGregorianCalendarWithoutDaysToLocalDate() throws Exception { + + XmlGregorianCalendarBean in = new XmlGregorianCalendarBean(); + XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendarDate( + 1999, + 5, + DatatypeConstants.FIELD_UNDEFINED, + DatatypeConstants.FIELD_UNDEFINED ); + in.setxMLGregorianCalendar( xcal ); + + LocalDateBean res = XmlGregorianCalendarToLocalDate.INSTANCE.toLocalDateBean( in ); + assertThat( res.getLocalDate() ).isNull(); + + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/LocalDateBean.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/LocalDateBean.java new file mode 100644 index 000000000..ddf4181cf --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/LocalDateBean.java @@ -0,0 +1,38 @@ +/** + * 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.test.builtin.bean.jodatime.bean; + +import org.joda.time.LocalDate; + +/** + * + * @author Sjaak Derksen + */ +public class LocalDateBean { + + private LocalDate localDate; + + public LocalDate getLocalDate() { + return localDate; + } + + public void setLocalDate(LocalDate localDate) { + this.localDate = localDate; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/LocalDateToXmlGregorianCalendar.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/LocalDateToXmlGregorianCalendar.java new file mode 100644 index 000000000..396fd06fb --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/LocalDateToXmlGregorianCalendar.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.test.builtin.bean.jodatime.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateBean; +import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper +public interface LocalDateToXmlGregorianCalendar { + + LocalDateToXmlGregorianCalendar INSTANCE = Mappers.getMapper( LocalDateToXmlGregorianCalendar.class ); + + @Mapping( target = "xMLGregorianCalendar", source = "localDate") + XmlGregorianCalendarBean toXmlGregorianCalendarBean( LocalDateBean in ); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/XmlGregorianCalendarToLocalDate.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/XmlGregorianCalendarToLocalDate.java new file mode 100644 index 000000000..7cf8e1baf --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/XmlGregorianCalendarToLocalDate.java @@ -0,0 +1,38 @@ +/** + * 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.test.builtin.bean.jodatime.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateBean; +import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper +public interface XmlGregorianCalendarToLocalDate { + + XmlGregorianCalendarToLocalDate INSTANCE = Mappers.getMapper( XmlGregorianCalendarToLocalDate.class ); + + @Mapping( target = "localDate", source = "xMLGregorianCalendar" ) + LocalDateBean toLocalDateBean( XmlGregorianCalendarBean in ); +}