This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-mongodb.git
commit 39d6fdb85b0aa4f17facdceaac0c0b4a9dd08e04 Author: Mike Müller <[email protected]> AuthorDate: Tue Sep 9 06:11:23 2014 +0000 SLING-3916 - Add simple query support to the MongoDBResourceProvider git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1623664 13f79535-47bb-0310-9956-ffa450edef68 --- .../mongodb/impl/MongoDBResourceProvider.java | 50 +++++++++++++++++++++- .../impl/MongoDBResourceProviderFactory.java | 4 +- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java index 55fdb5f..4e73d1f 100644 --- a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java +++ b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java @@ -37,10 +37,12 @@ import org.slf4j.LoggerFactory; import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; +import com.mongodb.CommandResult; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.QueryBuilder; +import com.mongodb.util.JSON; /** * The MongoDB resource provider creates resources based on MongoDB entries. @@ -435,8 +437,52 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou return PROP_PATH; } - public Iterator<Resource> findResources(ResourceResolver resolver, String query, String language) { - return null; + public Iterator<Resource> findResources(final ResourceResolver resolver, String query, String language) { + if ( !language.equals( "mongodb") || query == null || query.length() == 0 || query.indexOf( ".find(" ) <= 0 ) + { + return null; + } + Iterator<Resource> returnValue = null; + final String collectionName = query.substring( 0, query.indexOf( ".find(" ) ); + DBCollection col = this.getCollection( collectionName ); + if ( col != null ) + { + String criteria = query.trim().substring( query.indexOf( ".find(" ) + 6, query.length() - 1 ); + DBObject dbObject = (DBObject) JSON.parse( criteria ); + final DBCursor cur = col.find( dbObject ); + final String rootPath = context.getRootWithSlash(); + + return new Iterator<Resource>() { + + public boolean hasNext() { + return cur.hasNext(); + } + + public Resource next() { + final DBObject obj = cur.next(); + final String objPath = obj.get(getPROP_PATH()).toString(); + final int lastSlash = objPath.lastIndexOf('/'); + final String name; + if (lastSlash == -1) { + name = objPath; + } else { + name = objPath.substring(lastSlash + 1); + } + return new MongoDBResource(resolver, + rootPath + collectionName + "/" + name, + collectionName, + obj, + MongoDBResourceProvider.this); + } + + public void remove() { + throw new UnsupportedOperationException("remove"); + } + + }; + } + + return returnValue; } public Iterator<ValueMap> queryResources(ResourceResolver resolver, String query, String language) { diff --git a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java index 9bcf562..0f0f670 100644 --- a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java +++ b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java @@ -28,6 +28,7 @@ import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.ResourceProvider; +import org.apache.sling.api.resource.QueriableResourceProvider; import org.apache.sling.api.resource.ResourceProviderFactory; import org.apache.sling.commons.osgi.PropertiesUtil; import org.osgi.service.event.EventAdmin; @@ -50,7 +51,8 @@ policy=ConfigurationPolicy.REQUIRE, metatype=true) @Service(value=ResourceProviderFactory.class) @Properties({ - @Property(name=ResourceProvider.ROOTS, value="/mongo") + @Property(name=ResourceProvider.ROOTS, value="/mongo"), + @Property(name=QueriableResourceProvider.LANGUAGES, value="mongodb") }) public class MongoDBResourceProviderFactory implements ResourceProviderFactory { -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
