-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IGNITE-23756 Add custom converters support for Spring Data (#294)
- Loading branch information
Showing
7 changed files
with
260 additions
and
18 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
.../src/main/java/org/apache/ignite/springdata/repository/query/IgniteCustomConversions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springdata.repository.query; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.List; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.CustomConversions; | ||
import org.springframework.data.convert.WritingConverter; | ||
import org.springframework.data.mapping.model.SimpleTypeHolder; | ||
|
||
/** | ||
* Custom conversions implementation. | ||
* An application can define its own converter by defining the following bean: | ||
* <pre> | ||
* {@code | ||
* @Bean | ||
* public CustomConversions customConversions() { | ||
* return new IgniteCustomConversions(Arrays.asList(new LocalDateTimeWriteConverter())); | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
public class IgniteCustomConversions extends CustomConversions { | ||
|
||
private static final StoreConversions STORE_CONVERSIONS; | ||
|
||
static { | ||
List<Object> converters = new ArrayList<>(); | ||
converters.add(new TimestampToLocalDateTimeConverter()); | ||
converters.add(new TimestampToDateConverter()); | ||
|
||
List<Object> storeConverters = Collections.unmodifiableList(converters); | ||
STORE_CONVERSIONS = StoreConversions.of(SimpleTypeHolder.DEFAULT, storeConverters); | ||
} | ||
|
||
public IgniteCustomConversions() { | ||
this(Collections.emptyList()); | ||
} | ||
|
||
public IgniteCustomConversions(List<?> converters) { | ||
super(STORE_CONVERSIONS, converters); | ||
} | ||
|
||
@WritingConverter | ||
static class TimestampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> { | ||
@Override public LocalDateTime convert(Timestamp source) { | ||
return source.toInstant() | ||
.atZone(ZoneId.systemDefault()) | ||
.toLocalDateTime(); | ||
} | ||
} | ||
|
||
@WritingConverter | ||
static class TimestampToDateConverter implements Converter<Timestamp, Date> { | ||
@Override public Date convert(Timestamp source) { | ||
return new Date((source).getTime()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ring-data/src/test/java/org/apache/ignite/springdata/IgniteSpringDataConversionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springdata; | ||
|
||
import java.time.LocalDateTime; | ||
import org.apache.ignite.springdata.misc.ApplicationConfiguration; | ||
import org.apache.ignite.springdata.misc.CustomConvertersApplicationConfiguration; | ||
import org.apache.ignite.springdata.misc.Person; | ||
import org.apache.ignite.springdata.misc.PersonRepository; | ||
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; | ||
import org.junit.Test; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
public class IgniteSpringDataConversionsTest extends GridCommonAbstractTest { | ||
/** Repository. */ | ||
protected PersonRepository repo; | ||
|
||
/** Context. */ | ||
protected static AnnotationConfigApplicationContext ctx; | ||
|
||
@Override protected void beforeTest() { | ||
ctx = new AnnotationConfigApplicationContext(); | ||
} | ||
|
||
@Test | ||
public void testPutGetWithDefaultConverters() { | ||
init(ApplicationConfiguration.class); | ||
|
||
Person person = new Person("some_name", "some_surname", LocalDateTime.now()); | ||
|
||
assertEquals(person, savePerson(person)); | ||
} | ||
|
||
@Test | ||
public void testPutGetWithCustomConverters() { | ||
init(CustomConvertersApplicationConfiguration.class); | ||
|
||
Person person = new Person("some_name", "some_surname", LocalDateTime.now()); | ||
|
||
assertNull(savePerson(person).getCreatedAt()); | ||
} | ||
|
||
private void init(Class<? extends ApplicationConfiguration> applicationConfiguration) { | ||
ctx.register(applicationConfiguration); | ||
ctx.refresh(); | ||
|
||
repo = ctx.getBean(PersonRepository.class); | ||
} | ||
|
||
private Person savePerson(Person person) { | ||
int id = 1; | ||
|
||
assertEquals(person, repo.save(id, person)); | ||
assertTrue(repo.existsById(id)); | ||
|
||
return repo.selectByFirstNameWithCreatedAt(person.getFirstName()).get(0); | ||
} | ||
|
||
@Override protected void afterTest() { | ||
ctx.close(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...test/java/org/apache/ignite/springdata/misc/CustomConvertersApplicationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springdata.misc; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import org.apache.ignite.springdata.repository.query.IgniteCustomConversions; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.CustomConversions; | ||
|
||
/** | ||
* Test configuration that overrides default converter for LocalDateTime type. | ||
*/ | ||
public class CustomConvertersApplicationConfiguration extends ApplicationConfiguration { | ||
@Bean | ||
public CustomConversions customConversions() { | ||
return new IgniteCustomConversions(Arrays.asList(new LocalDateTimeWriteConverter())); | ||
} | ||
|
||
static class LocalDateTimeWriteConverter implements Converter<Timestamp, LocalDateTime> { | ||
@Override public LocalDateTime convert(Timestamp source) { | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.