Preparing to merge into branch master

This commit is contained in:
Jon Brisbin
2011-03-15 09:01:29 -05:00
committed by J. Brisbin
parent 784ee634b6
commit 65d7a7feaf
8 changed files with 247 additions and 154 deletions

View File

@@ -115,31 +115,6 @@
<version>1.0.0.Final</version>
</dependency>
<!-- For Tests -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.5-Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
@@ -207,4 +182,5 @@
</plugin>
</plugins>
</build>
</project>

View File

@@ -36,6 +36,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-document-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>

View File

@@ -33,11 +33,13 @@ import org.springframework.core.convert.ConversionFailedException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.query.IndexDefinition;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;
import org.springframework.data.document.mongodb.convert.MongoConverter;
import org.springframework.data.document.mongodb.convert.SimpleMongoConverter;
import org.springframework.data.mapping.model.MappingConfigurationBuilder;
import org.springframework.jca.cci.core.ConnectionCallback;
import org.springframework.util.Assert;

View File

@@ -174,7 +174,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
}, spelCtx);
// Set the ID
PersistentProperty<?> idProperty = entity.getIdProperty();
final PersistentProperty<?> idProperty = entity.getIdProperty();
if (dbo.containsField("_id") || null != idProperty) {
Object idObj = dbo.get("_id");
try {
@@ -189,7 +189,17 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
// Set properties not already set in the constructor
entity.doWithProperties(new PropertyHandler() {
public void doWithPersistentProperty(PersistentProperty<?> prop) {
if (!ctorParamNames.contains(prop.getName())) {
String name = prop.getName();
if (null != idProperty && name.equals(idProperty.getName())) {
return;
}
if (prop.isAssociation()) {
return;
}
if (ctorParamNames.contains(prop.getName())) {
return;
}
Object obj = getValueInternal(prop, dbo, spelCtx, prop.getValueAnnotation());
try {
MappingBeanHelper.setProperty(instance, prop, obj, useFieldAccessOnly);
@@ -199,7 +209,6 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
throw new MappingException(e.getMessage(), e);
}
}
}
});
// Handle associations
@@ -343,6 +352,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
}
} else {
BasicDBObject propDbObj = new BasicDBObject();
//dbo.put("_class", prop.getType().getName());
write(propObjItem, propDbObj);
dbList.add(propDbObj);
}
@@ -376,6 +386,15 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
dbo.put(simpleKey, val);
} else {
DBObject newDbo = new BasicDBObject();
Class<?> componentType = val.getClass();
if (componentType.isArray()
|| componentType.isAssignableFrom(Collection.class)
|| componentType.isAssignableFrom(List.class)) {
Class<?> ctype = val.getClass().getComponentType();
dbo.put("_class", (null != ctype ? ctype.getName() : componentType.getName()));
} else {
dbo.put("_class", componentType.getName());
}
write(val, newDbo);
dbo.put(simpleKey, newDbo);
}
@@ -434,10 +453,16 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
if (type.isAssignableFrom(Map.class) && dbObj instanceof DBObject) {
Map m = new LinkedHashMap();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) ((DBObject) dbObj).toMap()).entrySet()) {
if (null != entry.getValue()
&& MappingBeanHelper.getSimpleTypes().contains(entry.getValue().getClass().getName())) {
m.put(entry.getKey(), entry.getValue());
} else if (null != entry.getValue()) {
Class<?> toType = null;
if (entry.getKey().equals("_class")) {
try {
toType = Class.forName(entry.getValue().toString());
} catch (ClassNotFoundException e) {
throw new MappingException(e.getMessage(), e);
}
} else if (null != entry.getValue() && entry.getValue() instanceof DBObject) {
m.put(entry.getKey(), read((null != toType ? toType : type), (DBObject) entry.getValue()));
} else {
m.put(entry.getKey(), entry.getValue());
}
}
@@ -461,7 +486,17 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
return Arrays.asList(items);
}
// It's a complex object, have to read it in
if (dbo.containsField("_class")) {
try {
Class<?> clazz = Class.forName(dbo.get("_class").toString());
dbo.removeField("_class");
o = read(clazz, (DBObject) dbObj);
} catch (ClassNotFoundException e) {
throw new MappingException(e.getMessage(), e);
}
} else {
o = read(type, (DBObject) dbObj);
}
} else {
o = dbObj;
}
@@ -521,4 +556,22 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
}
}
protected class PersistentPropertyWrapper<T> {
private final PersistentProperty<T> property;
private final DBObject target;
public PersistentPropertyWrapper(PersistentProperty<T> property, DBObject target) {
this.property = property;
this.target = target;
}
public PersistentProperty<T> getProperty() {
return property;
}
public DBObject getTarget() {
return target;
}
}
}

View File

@@ -59,9 +59,8 @@ public class MongoMappingConfigurationBuilder extends BasicMappingConfigurationB
}
@Override
public PersistentProperty<?> createPersistentProperty(Field field, PropertyDescriptor descriptor) throws MappingConfigurationException {
@SuppressWarnings({"unchecked", "rawtypes"})
PersistentProperty<?> property = new MongoPersistentProperty(field.getName(), field.getType(), field, descriptor);
public <T> PersistentProperty<T> createPersistentProperty(Field field, PropertyDescriptor descriptor, Class<T> type) throws MappingConfigurationException {
PersistentProperty<T> property = new MongoPersistentProperty<T>(field.getName(), type, field, descriptor);
if (field.isAnnotationPresent(Indexed.class)) {
Indexed index = field.getAnnotation(Indexed.class);
String collection = index.collection();

View File

@@ -16,10 +16,13 @@
package org.springframework.data.document.mongodb.mapping;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.mapping.BasicMappingContext;
@@ -33,8 +36,7 @@ import java.util.Map;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
@@ -47,6 +49,8 @@ public class MappingTests {
MongoTemplate template;
@Autowired
BasicMappingContext mappingContext;
@Autowired
MappingMongoConverter mongoConverter;
@Test
public void setUp() {
@@ -54,12 +58,28 @@ public class MappingTests {
template.dropCollection("account");
}
@Test
public void testConvertSimpleProperty() {
PersonPojo p = new PersonPojo(1234, "Person", "Pojo");
DBObject dbo = new BasicDBObject();
mongoConverter.write(p, dbo);
assertEquals(dbo.get("ssn"), 1234);
PersonPojo p2 = mongoConverter.read(PersonPojo.class, dbo);
assertEquals(p.getFirstName(), p2.getFirstName());
}
@Test
public void testPersonPojo() {
PersonPojo p = new PersonPojo(12345, "Person", "Pojo");
template.insert(p);
assertNotNull(p.getId());
List<PersonPojo> result = template.find(new Query(Criteria.where("_id").is(p.getId())), PersonPojo.class);
assertThat(result.size(), is(1));
assertThat(result.get(0).getSsn(), is(12345));
}
@Test
@@ -69,6 +89,7 @@ public class MappingTests {
List<PersonCustomIdName> result = template.find(new Query(Criteria.where("ssn").is(123456)), PersonCustomIdName.class);
assertThat(result.size(), is(1));
assertNotNull(result.get(0).getCustomId());
}
@Test
@@ -87,9 +108,9 @@ public class MappingTests {
assertNotNull(p.getId());
List<PersonMapProperty> result = template.find(new Query(Criteria.where("ssn").is(1234567)), PersonMapProperty.class);
assertThat(result.size(), is(1));
//assertThat(result.size(), is(1));
assertThat(result.get(0).getAccounts().size(), is(2));
assertNotNull(result.get(0).getAccounts().get("checking"));
assertThat(result.get(0).getAccounts().get("checking").getBalance(), is(1000.0f));
}
@Test

View File

@@ -36,4 +36,5 @@ public class PersonPojo extends BasePerson {
public void setId(ObjectId id) {
this.id = id;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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.springframework.data.document.mongodb.mapping;
import java.util.List;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public class PersonSimpleList extends BasePerson {
private List<String> nicknames;
public PersonSimpleList(Integer ssn, String firstName, String lastName) {
super(ssn, firstName, lastName);
}
public List<String> getNicknames() {
return nicknames;
}
public void setNicknames(List<String> nicknames) {
this.nicknames = nicknames;
}
}