June 28, 2010

Mapping current grails domain classes to use mongodb

I have a grails app that I am building and wanted try out using mongodb sense i have heard so many good things about it. The quick start guide doesn’t give you and helpful hints on how to map current domain classes to use mongodb. It only helps you create new domain classes to use mongo.

The excerpt below is strait from the wiki of the mongodb plugin for grails github account. Hope this helps some of you out.

 

The MongoDB Grails plugin is primarily exposed to Grails applications as a Spring bean called ‘mongo’.
Grails classes can then easily make use of it throughout their code by simply adding a ‘mongo’
property to their classes which support dependency injection (domain/controllers/services), eg:

class UserController{ def mongo }

Configuration

At startup the MongoDB plugin will look for a ‘mongo’ configuration property in your
Config.groovy file which defines your MongoDB databases and related configuration
properties.

#Config.groovy  mongo{ databases{ server1{ host = "localhost" port = 1234  // if ommited, will use the default MongoDB port }  server2{ host = "192.168.1.2" } }  shortcuts{ users = "server1/app/users" comments = "server2/app/comments" } }

The above example registers two different database hosts which can then be accessed using the mongo
bean: mongo.server1.<dbname>.<collection>. ‘dbname’ and ‘collection’ will
return the corrisponding Java MongoDB driver equivalents (DB, DBCollection).

Shortcuts

Shortcuts can also be defined to shorten the syntax required to access a collection by registering a
root-level mongo property directly with a collection. For example, in the above
example we mapped the ‘users’ shortcut to “server1/app/users”, which lets us use mongo.users
instead of mongo.server1.app.users.

Shortcuts have the added benefit of making it easier to change your server topology without having to
change your code; if you were to move your users collection to a different server, you would just update
your alias.

Shortcuts will also be used in the future to reference server-pools.

Mapping Domain Objects

The MongoDbWrapper makes it easy to save and restore your Grails domain objects
to a collection by using mappers which convert your domain objects to BasicDBObjects,
and vice-versa.

To register your Domain class with a mapper you need to add two static helper fields
to your class:

class User{ String firstName String lastName  static mongoTypeName = "user" static mongoFields = ['fn':firstName','ln':'lastName'] }

When your objects are converted to documents a property ‘_t’ is added to help identify the type.
This type identifier is specified with mongoTypeName. You then specify which fields
should be saved, and their respective keys.

Domain objects can then be coverted to docs by calling the MOP added method “toMongoDoc()”.

def user = new User( firstName:"mark", lastName:"priatel" ) def userDoc = user.toMongoDoc()

You can also convert documents retured from queries back into their Domain equivilants
using the toObject() method added to BasicDBObject and BasicDBList (via MOP):

mongo.users.find().each{ doc -> def userDomainObject = doc.toObject() }

The mapper will also process mapped properties and Lists:

class Address{ String city String country  static mongoTypeName = "address" static mongoFields = [ 'ci':'city' , 'co' : 'country' ] }  class User{ String firstName String lastName Address address  static mongoTypeName = "user" static mongoFields = ['fn':firstName','ln':'lastName','adrs':'address'] }  def adrs = new Address( city:'ottawa' , country:'canada' ) def user = new User( firstName:'mark', lastName:'priatel',address:adrs)  mongo.users.save( user.toMongoDoc() )  (bson) { "_id" : ObjectId("4b952284d8e992502c9629e3"), "_t" : "u", "fn" : "mark", "ln" : "priatel", "adrs" : { "_t" : "a", "ci" : "ottawa", "co" : "canada" } }

Of course, you can still save your domain objects using GORM.

def user = new User( firstname:"mark" ) user.save() mongo.users.save( user.toMongoDoc() )

MongoDB Document Builder

The mongo bean exposes a special root-level property ‘doc’ which can be used to create
BasicDBObjectS (which are used by the Java driver to represent MongoDB documents) using a
Groovy builder syntax:

def userData = mongo.doc{ firstName("mark") lastName("priatel") company("iolog") address{ city("ottawa") country("canada") } }  mongo.user.save(userData)  println userData._id

-

 

Posted via email from Matthew Sloan Wallace

May 16, 2008

Flash Player 10 Released – Code Name “Astro”

Flash Player 10 beta, code name Astro is out.

Astro Image

I just checked out a great video by Lee Brimelow on http://gotoandlearn.com that will get you up and running with Flash Player 10. Here is the link. http://www.gotoandlearn.com/player.php?id=73.

You can check out the Adobe Labs site for instructions for setting this up and using Flex Builder if you would like to go that route.

http://labs.adobe.com/technologies/flashplayer10/

September 11, 2007

New Site Developed For Cincinnati Band Bluf

Bluf Site Screen Shot

About a week or so ago I launched the new Bluf web site. If you have not heard of this band Bluf you should really check them out. Bluf writes their own original music and the bottom line is ….. they know how to rock for sure!

The guys relied on there myspace page for a log time as for their web site, and still maintain a large following on that site. Now that the bad is looking ahead and there needs grew the felt that a flash based web site was the way to go. This gives them the ability to post there music, videos and also have a really interactive site.

On this site they wanted a look that stuck with the theme they had be using on there myspace page, but after getting to know the guys and seeing them play I felt they where edgier than that. I felt that it was important to take the new site a few steps further. Using a lot of distressed textures and elements I was able to design and implement the site in a way that was simple, unique to the Band, and also really show the fans who Bluf really is. From what I could tell the guys where really excited about the look of the new site and seamed to think that I totally nailed the look and feel they where wanting. Of course I had a little help from the guys. They supplied there logo, some pictures and the red tree element was all Bluf’s doing. The red tree and lighter white background was the starting point and as we moved forward it was clear what the site needed. Hop on over and check out the new Bluf Site and shoot the guys an email and let them know what you think. blufmusic.com

Read on to hear more about Bluf.

Read the rest of this entry »