Merge pull request #2035 from cstrempfer/enable-morphia-converters

enable Morphia converters
This commit is contained in:
Ruben Dijkstra 2016-12-15 13:47:46 +01:00 committed by GitHub
commit a301fe712b
5 changed files with 87 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import org.mongodb.morphia.annotations.Reference;
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;
@ -41,6 +42,12 @@ public class MorphiaSerializer extends MongodbSerializer {
this.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();

View File

@ -56,6 +56,7 @@ public class MongodbQueryTest {
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 class MongodbQueryTest {
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 class MongodbQueryTest {
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

View File

@ -23,6 +23,7 @@ import java.util.List;
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 @@ public class MongodbSerializerTest {
@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);

View File

@ -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;
}
}

View File

@ -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());
}
}