updated to use parent pom; added query methods

This commit is contained in:
Thomas Risberg
2010-09-14 13:07:25 -04:00
parent 2515607121
commit 9c8c442b3d
4 changed files with 164 additions and 102 deletions

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2010 the original author or authors.
*
* 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.datastore.document;
import org.springframework.dao.InvalidDataAccessApiUsageException;
public class InvalidDocumentStoreApiUageException extends InvalidDataAccessApiUsageException {
public InvalidDocumentStoreApiUageException(String msg) {
super(msg);
}
public InvalidDocumentStoreApiUageException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -25,7 +25,10 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
import org.springframework.datastore.document.DocumentMapper;
import org.springframework.datastore.document.DocumentSource;
import org.springframework.datastore.document.InvalidDocumentStoreApiUageException;
import org.springframework.util.Assert;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
@@ -106,6 +109,30 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
return results;
}
public <T> List<T> queryForList(String collectionName, String fieldName, String[] operands, Object[] values, Class<T> targetClass) {
DocumentMapper<DBObject, T> mapper = MongoBeanPropertyDocumentMapper.newInstance(targetClass);
return queryForList(collectionName, fieldName, operands, values, mapper);
}
public <T> List<T> queryForList(String collectionName, String fieldName, String[] operands, Object[] values, DocumentMapper<DBObject, T> mapper) {
if (operands.length != values.length) {
throw new InvalidDocumentStoreApiUageException("The number of operands and values must match");
}
List<T> results = new ArrayList<T>();
DBObject query = new BasicDBObject();
DBObject condition = new BasicDBObject();
for (int i = 0; i < operands.length; i++) {
condition.put(operands[i], values[i]);
}
query.put(fieldName, condition);
System.out.println("--> " + query);
DBCollection collection = getConnection().getCollection(collectionName);
for (DBObject dbo : collection.find(query)) {
results.add(mapper.mapDocument(dbo));
}
return results;
}
public RuntimeException translateIfNecessary(RuntimeException ex) {
return MongoDbUtils.translateMongoExceptionIfPossible(ex);
}