Permalink
Browse files

.

  • Loading branch information...
unknown
unknown committed Mar 2, 2010
0 parents commit 3e57518b5045f640f68c0e54d63b491cef91ba5d
@@ -0,0 +1,31 @@
+grails.project.class.dir = "target/classes"
+grails.project.test.class.dir = "target/test-classes"
+grails.project.test.reports.dir = "target/test-reports"
+//grails.project.war.file = "target/${appName}-${appVersion}.war"
+grails.project.dependency.resolution = {
+ // inherit Grails' default dependencies
+ inherits( "global" ) {
+ // uncomment to disable ehcache
+ // excludes 'ehcache'
+ }
+ log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
+ repositories {
+ grailsPlugins()
+ grailsHome()
+
+ // uncomment the below to enable remote dependency resolution
+ // from public Maven repositories
+ //mavenLocal()
+ mavenCentral()
+ //mavenRepo "http://snapshots.repository.codehaus.org"
+ //mavenRepo "http://repository.codehaus.org"
+ //mavenRepo "http://download.java.net/maven/2/"
+ //mavenRepo "http://repository.jboss.com/maven2/"
+ }
+ dependencies {
+ // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
+ runtime 'org.mongodb:mongo-java-driver:1.2.1'
+ // runtime 'mysql:mysql-connector-java:5.1.5'
+ }
+
+}
@@ -0,0 +1,32 @@
+dataSource {
+ pooled = true
+ driverClassName = "org.hsqldb.jdbcDriver"
+ username = "sa"
+ password = ""
+}
+hibernate {
+ cache.use_second_level_cache=true
+ cache.use_query_cache=true
+ cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider'
+}
+// environment specific settings
+environments {
+ development {
+ dataSource {
+ dbCreate = "create-drop" // one of 'create', 'create-drop','update'
+ url = "jdbc:hsqldb:mem:devDB"
+ }
+ }
+ test {
+ dataSource {
+ dbCreate = "update"
+ url = "jdbc:hsqldb:mem:testDb"
+ }
+ }
+ production {
+ dataSource {
+ dbCreate = "update"
+ url = "jdbc:hsqldb:file:prodDb;shutdown=true"
+ }
+ }
+}
@@ -0,0 +1,11 @@
+class UrlMappings {
+ static mappings = {
+ "/$controller/$action?/$id?"{
+ constraints {
+ // apply constraints here
+ }
+ }
+ "/"(view:"/index")
+ "500"(view:'/error')
+ }
+}
@@ -0,0 +1,54 @@
+<html>
+ <head>
+ <title>Grails Runtime Exception</title>
+ <style type="text/css">
+ .message {
+ border: 1px solid black;
+ padding: 5px;
+ background-color:#E9E9E9;
+ }
+ .stack {
+ border: 1px solid black;
+ padding: 5px;
+ overflow:auto;
+ height: 300px;
+ }
+ .snippet {
+ padding: 5px;
+ background-color:white;
+ border:1px solid black;
+ margin:3px;
+ font-family:courier;
+ }
+ </style>
+ </head>
+
+ <body>
+ <h1>Grails Runtime Exception</h1>
+ <h2>Error Details</h2>
+
+ <div class="message">
+ <strong>Error ${request.'javax.servlet.error.status_code'}:</strong> ${request.'javax.servlet.error.message'.encodeAsHTML()}<br/>
+ <strong>Servlet:</strong> ${request.'javax.servlet.error.servlet_name'}<br/>
+ <strong>URI:</strong> ${request.'javax.servlet.error.request_uri'}<br/>
+ <g:if test="${exception}">
+ <strong>Exception Message:</strong> ${exception.message?.encodeAsHTML()} <br />
+ <strong>Caused by:</strong> ${exception.cause?.message?.encodeAsHTML()} <br />
+ <strong>Class:</strong> ${exception.className} <br />
+ <strong>At Line:</strong> [${exception.lineNumber}] <br />
+ <strong>Code Snippet:</strong><br />
+ <div class="snippet">
+ <g:each var="cs" in="${exception.codeSnippet}">
+ ${cs?.encodeAsHTML()}<br />
+ </g:each>
+ </div>
+ </g:if>
+ </div>
+ <g:if test="${exception}">
+ <h2>Stack Trace</h2>
+ <div class="stack">
+ <pre><g:each in="${exception.stackTraceLines}">${it.encodeAsHTML()}<br/></g:each></pre>
+ </div>
+ </g:if>
+ </body>
+</html>
@@ -0,0 +1,10 @@
+//
+// This script is executed by Grails after plugin was installed to project.
+// This script is a Gant script so you can use all special variables provided
+// by Gant (such as 'baseDir' which points on project base dir). You can
+// use 'ant' to access a global instance of AntBuilder
+//
+// For example you can create directory under project tree:
+//
+// ant.mkdir(dir:"${basedir}/grails-app/jobs")
+//
@@ -0,0 +1,5 @@
+//
+// This script is executed by Grails when the plugin is uninstalled from project.
+// Use this script if you intend to do any additional clean-up on uninstall, but
+// beware of messing up SVN directories!
+//
@@ -0,0 +1,10 @@
+//
+// This script is executed by Grails during application upgrade ('grails upgrade'
+// command). This script is a Gant script so you can use all special variables
+// provided by Gant (such as 'baseDir' which points on project base dir). You can
+// use 'ant' to access a global instance of AntBuilder
+//
+// For example you can create directory under project tree:
+//
+// ant.mkdir(dir:"${basedir}/grails-app/jobs")
+//
@@ -0,0 +1,200 @@
+package com.iolog.mongodbtools;
+
+import com.mongodb.Mongo;
+
+
+import org.springframework.beans.factory.InitializingBean
+
+import com.mongodb.DBApiLayer
+
+import com.mongodb.BasicDBObject
+import com.mongodb.BasicDBList
+
+/**
+ * User: mark
+ * Date: Feb 27, 2010
+ * Time: 1:53:53 PM
+ */
+public class MongoDbWrapper implements InitializingBean
+{
+ def grailsApplication
+
+ Map<String, Mongo> mongos = new HashMap<String, Mongo>();
+ Map<String, Object> shortcuts = new HashMap<String, Object>();
+ Map<String, Class> typeMappings = new HashMap<String, Class>()
+ Map<String, MongoMapperModel> mappersByTypeName = new HashMap<String, MongoMapperModel>()
+ Map<Class, MongoMapperModel> mappersByClass = new HashMap<Class, MongoMapperModel>()
+ Map<Class,String> typeForClassMap = new HashMap<Class,String>()
+
+ /**
+ *
+ * @return
+ */
+ public Map<String, MongoMapperModel> getMappers() { return mappersByTypeName }
+
+ /**
+ *
+ */
+ public MongoMapperModel getMapper(String typeName) { return mappersByTypeName.get(typeName) }
+
+
+
+
+ /**
+ *
+ */
+ public MongoMapperModel getMapperForClass(Class c){ return mappersByClass.get(c) }
+
+ /**
+ *
+ */
+ public boolean typeIsMapped( Class c ){ return mappersByTypeName.containsKey(c) }
+
+
+ /**
+ * given a Class, this method returns the typeName used to associate a Mongo record/document
+ * with a class
+ *
+ */
+ public String getTypeNameForClass( Class ){ return typeForClassMap.get(c) }
+
+ /**
+ * add support so that in Groovy, databases can be referenced using
+ * mongo.<database>
+ * @param key
+ * @return
+ */
+ def Object propertyMissing(String name)
+ {
+ if (mongos.containsKey(name)) return mongos.get(name);
+ if (shortcuts.containsKey(name)) return shortcuts.get(name);
+
+ // first check shortcuts config to see if the propery references
+ // a DBCollection
+ if (grailsApplication.config.mongo.shortcuts)
+ {
+ grailsApplication.config.mongo.shortcuts.each {
+ println it
+ }
+ }
+
+ // if no MongoDB Is registered for the given key, lookup its
+ // connection details in grailsApplication.config
+ def dbConfig = grailsApplication.config.mongo.databases?."${name}"
+ if (!dbConfig)
+ {
+ throw new IllegalArgumentException("No MongoDB config found in grailsApplicaiton.config.mongo.databases.${name}")
+ }
+
+
+ def port = dbConfig.port ? dbConfig.port : 27017
+
+
+ Mongo mongo = new Mongo(dbConfig.host, port)
+ mongos.put(name, mongo)
+ return mongo
+ }
+
+ /**
+ * during the plugin initialization domain classes which have a static
+ * 'mongoFields' property will have a MongoMapperModel created for them
+ * which are then added to the wrapper.
+ */
+ public void addMapperModel(String typeName, MongoMapperModel mmm)
+ {
+ typeForClassMap.put( mmm.clazz , typeName )
+ mappersByTypeName.put(typeName, mmm)
+ mappersByClass.put(mmm.clazz,mmm)
+ }
+
+ /**
+ * add Groovy helpers to MongoDB objects.
+ */
+ void afterPropertiesSet()
+ {
+
+ Mongo.metaClass.propertyMissing = { String name ->
+ return ((Mongo) delegate).getDB(name)
+ }
+
+ DBApiLayer.metaClass.propertyMissing = { String name ->
+ return ((DBApiLayer) delegate).getCollection(name)
+ }
+
+ BasicDBList.metaClass.toObject = {
+ List oList = new ArrayList((int) delegate.size())
+ delegate.each {
+ if (it instanceof BasicDBObject || it instanceof BasicDBList) oList.add(it.toObject())
+ else oList.add(it)
+ }
+ }
+
+ BasicDBObject.metaClass.toObject = {
+ BasicDBObject _self = (BasicDBObject) delegate
+
+ def typeName = _self.get("_t")
+
+ if (!typeName)
+ {
+ return delegate
+ }
+
+ MongoMapperModel mmm = mappersByTypeName.get(typeName)
+
+ if (!mmm)
+ {
+ return delegate
+ }
+
+ // begin mapping process to create a new object using
+ // information stored inthe MongoMapperModel
+
+ def obj = mmm.clazz.newInstance()
+
+ for (MongoMapperField mmf: mmm.fields)
+ {
+
+ switch (mmf.fieldType)
+ {
+ case String:
+ obj."${mmf.domainFieldName}" = _self.get(mmf.mongoFieldName)
+ break
+
+ case List:
+ BasicDBList mongoList = (BasicDBList) _self.get(mmf.mongoFieldName)
+ if (!mongoList)
+ {
+ obj."${mmf.domainFieldName}" = new ArrayList()
+ }
+ else
+ {
+ obj."${mmf.domainFieldName}" = mongoList.toObject()
+ }
+
+ break
+
+ }
+
+
+ }
+
+ return obj
+
+
+ }
+
+ grailsApplication.config.mongo.shortcuts.each { shortcutName, String path ->
+ def pathParts = path.split("\\/")
+ def obj = this."${pathParts[0]}"."${pathParts[1]}"."${pathParts[2]}"
+ if (obj) shortcuts.put(shortcutName, obj)
+ }
+
+ grailsApplication.config.mongo.mappings.each { type, className ->
+ Class c = grailsApplication.getClassForName(className)
+ typeMappings.put(type, c)
+ }
+
+ }
+
+
+}
@@ -0,0 +1,28 @@
+package com.iolog.mongodbtools
+
+/**
+ * User: mark
+ * Date: Mar 1, 2010
+ * Time: 12:23:44 AM
+ */
+class MongoMapperField
+{
+ String domainFieldName
+ String mongoFieldName
+ Class fieldType
+
+ /**
+ * flag which indicates that this field's class type has a registered mapper.
+ */
+ boolean isMapped
+
+
+ public String toString()
+ {
+ return "MongoMapperField{" +
+ "mongoFieldName='" + mongoFieldName + '\'' +
+ ", domainFieldName='" + domainFieldName + '\'' +
+ ", fieldType=" + fieldType +
+ '}';
+ }
+}
Oops, something went wrong.

0 comments on commit 3e57518

Please sign in to comment.