Browse Source

DATADOC-110 removed 'substituteMappedIdIfNecessary' since this is now handled by QueryMapper

pull/1/head
Thomas Risberg 15 years ago
parent
commit
6ed1d2b226
  1. 82
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java

82
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java

@ -1074,88 +1074,6 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher @@ -1074,88 +1074,6 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher
}
}
/**
* Substitutes the id key if it is found in he query. Any 'id' keys will be replaced with '_id' and the value
* converted to an ObjectId if possible. This conversion should match the way that the id fields are converted during
* read operations.
*
* @param query
* @param targetClass
* @param reader
*/
protected void substituteMappedIdIfNecessary(DBObject query, Class<?> targetClass, MongoReader<?> reader) {
MongoConverter converter = null;
if (reader instanceof SimpleMongoConverter) {
converter = (MongoConverter) reader;
} else if (reader instanceof MappingMongoConverter) {
converter = (MappingMongoConverter) reader;
} else {
return;
}
String idKey = null;
if (query.containsField("id")) {
idKey = "id";
}
if (query.containsField("_id")) {
idKey = "_id";
}
if (idKey == null) {
// no ids in this query
return;
}
MongoPropertyDescriptor descriptor;
try {
MongoPropertyDescriptor mpd = new MongoPropertyDescriptor(new PropertyDescriptor(idKey, targetClass), targetClass);
descriptor = mpd;
} catch (IntrospectionException e) {
// no property descriptor for this key - try the other
try {
String theOtherIdKey = "id".equals(idKey) ? "_id" : "id";
MongoPropertyDescriptor mpd2 = new MongoPropertyDescriptor(new PropertyDescriptor(theOtherIdKey, targetClass),
targetClass);
descriptor = mpd2;
} catch (IntrospectionException e2) {
// no property descriptor for this key either - bail
return;
}
}
if (descriptor.isIdProperty() && descriptor.isOfIdType()) {
Object value = query.get(idKey);
if (value instanceof DBObject) {
DBObject dbo = (DBObject) value;
if (dbo.containsField("$in")) {
List<Object> ids = new ArrayList<Object>();
int count = 0;
for (Object o : (Object[]) dbo.get("$in")) {
count++;
ObjectId newValue = convertIdValue(converter, o);
if (newValue != null) {
ids.add(newValue);
}
}
if (ids.size() > 0 && ids.size() != count) {
throw new InvalidDataAccessApiUsageException("Inconsistent set of id values provided "
+ Arrays.asList((Object[]) dbo.get("$in")));
}
if (ids.size() > 0) {
dbo.removeField("$in");
dbo.put("$in", ids.toArray());
}
}
query.removeField(idKey);
query.put(MongoPropertyDescriptor.ID_KEY, value);
} else {
ObjectId newValue = convertIdValue(converter, value);
query.removeField(idKey);
if (newValue != null) {
query.put(MongoPropertyDescriptor.ID_KEY, newValue);
} else {
query.put(MongoPropertyDescriptor.ID_KEY, value);
}
}
}
}
private MongoPersistentEntity<?> getPersistentEntity(Class<?> type) {
return type == null ? null : mappingContext.getPersistentEntity(type);
}

Loading…
Cancel
Save