Permalink
Browse files

enable Morphia converters

  • Loading branch information...
1 parent 8f96f41 commit 9b11ae98423bd5ef88371836fca854b1837ad7c9 @cstrempfer cstrempfer committed Oct 10, 2016
@@ -23,6 +23,7 @@
import org.mongodb.morphia.mapping.Mapper;
import com.mongodb.DBRef;
+import com.querydsl.core.types.Constant;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.PathMetadata;
import com.querydsl.mongodb.MongodbSerializer;
@@ -42,6 +43,12 @@ public MorphiaSerializer(Morphia morphia) {
}
@Override
+ public Object visit(Constant<?> expr, Void context) {
+ Object value = super.visit(expr, context);
+ return morphia.getMapper().toMongoObject(null, null, value);
+ }
+
+ @Override
protected String getKeyForPath(Path<?> expr, PathMetadata metadata) {
AnnotatedElement annotations = expr.getAnnotatedElement();
if (annotations.isAnnotationPresent(Id.class)) {
@@ -56,6 +56,7 @@
private final QAddress address = QAddress.address;
private final QMapEntity mapEntity = QMapEntity.mapEntity;
private final QDates dates = QDates.dates;
+ private final QCountry country = QCountry.country;
List<User> users = Lists.newArrayList();
User u1, u2, u3, u4;
@@ -71,6 +72,7 @@ public MongodbQueryTest() throws UnknownHostException, MongoException {
public void before() throws UnknownHostException, MongoException {
ds.delete(ds.createQuery(Item.class));
ds.delete(ds.createQuery(User.class));
+ ds.delete(ds.createQuery(Country.class));
ds.delete(ds.createQuery(MapEntity.class));
tampere = new City("Tampere", 61.30, 23.50);
@@ -591,6 +593,15 @@ public void asDBObject() {
query.asDBObject());
}
+ @Test
+ public void converter() {
+ Country germany = new Country("Germany", Locale.GERMANY);
+ ds.save(germany);
+
+ Country fetchedCountry = query(Country.class).where(country.defaultLocale.eq(Locale.GERMANY)).fetchOne();
+ assertEquals(germany, fetchedCountry);
+ }
+
//TODO
// - test dates
// - test with empty values and nulls
@@ -23,6 +23,7 @@
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
+import org.mongodb.morphia.Morphia;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
@@ -57,7 +58,7 @@
@Before
public void before() {
- serializer = new MorphiaSerializer(null);
+ serializer = new MorphiaSerializer(new Morphia());
entityPath = new PathBuilder<Object>(Object.class, "obj");
title = entityPath.getString("title");
year = entityPath.getNumber("year", Integer.class);
@@ -0,0 +1,29 @@
+package com.querydsl.mongodb.domain;
+
+import java.util.Locale;
+
+import org.mongodb.morphia.annotations.Converters;
+import org.mongodb.morphia.annotations.Entity;
+
+@Entity
+@Converters(LocaleConverter.class)
+public class Country extends AbstractEntity {
+ private String name;
+ private Locale defaultLocale;
+
+ Country() { }
+
+ public Country(String name, Locale defaultLocale) {
+ this.name = name;
+ this.defaultLocale = defaultLocale;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Locale getDefaultLocale() {
+ return defaultLocale;
+ }
+
+}
@@ -0,0 +1,38 @@
+package com.querydsl.mongodb.domain;
+
+import java.util.Locale;
+
+import org.mongodb.morphia.converters.SimpleValueConverter;
+import org.mongodb.morphia.converters.TypeConverter;
+import org.mongodb.morphia.mapping.MappedField;
+import org.mongodb.morphia.mapping.MappingException;
+
+public class LocaleConverter extends TypeConverter implements SimpleValueConverter {
+
+ public LocaleConverter() {
+ super(Locale.class);
+ }
+
+ @Override
+ public final Object encode(Object value, MappedField optionalExtraInfo) throws MappingException {
+ if (value == null) {
+ return null;
+ }
+ if (!(value instanceof Locale)) {
+ throw new MappingException("Unable to convert " + value.getClass().getName());
+ }
+ return ((Locale) value).toLanguageTag();
+ }
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ public Locale decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) throws MappingException {
+ if (fromDBObject == null) {
+ return null;
+ }
+ if (fromDBObject instanceof String) {
+ return Locale.forLanguageTag((String) fromDBObject);
+ }
+ throw new MappingException("Unable to convert " + fromDBObject.getClass().getName());
+ }
+}

0 comments on commit 9b11ae9

Please sign in to comment.