UUID as ID in Grails MongoDB

Domain :

import static java.util.UUID.randomUUID

class Example {
    
    String id = randomUUID() as String
    static mapping = {
        id generator:'assigned'
    }
}

In ExampleController, we need to use

example.insert(flush:true,failOnError: true)

instead of

example.save(flush:true,failOnError: true)
Advertisements

Sorting Map inside Groovy , Javascript

Groovy sorting by multiple properties:

http://www.tothenew.com/blog/groovier-way-of-sorting-over-multiple-fields-in-a-list-of-maps-in-groovy/

List list = [
    [id:0, firstName: 'Sachin', lastName: 'Tendulkar', age: 40 ],
    [id:1, firstName: 'Sachin', lastName: 'Tendulkar', age: 103 ],
    [id:2, firstName: 'Ajay', lastName: 'Tendulkar', age: 48 ],
    [id:3, firstName: 'Virendra', lastName: 'Sehwag', age: 5 ],
    [id:4, firstName: 'Virendra', lastName: 'Sehwag', age: 50 ],
    [id:5, firstName: 'Sachin', lastName: 'Nayyar', age: 15 ]
]

def ids = list.sort { a,b ->
   a.firstName <=> b.firstName ?: a.lastName <=> b.lastName ?: a.age <=> b.age
}*.id

assert ids == [2, 5, 0, 1, 3, 4]

Javascript sorting by multiple properties:

http://stackoverflow.com/questions/9175268/javascript-sort-function-sort-by-first-then-by-second

objects = []
object[0] = {strength: 3, name: "Leo"}
object[1] = {strength: 3, name: "Mike"}

function cmp(a, b) {
    if (a > b) return +1;
    if (a < b) return -1;
    return 0;
}
array.sort(function(a, b) {
    return cmp(a.strength,b.strength) || cmp(a.name,b.name)
})

Grails: ERROR plugins.DefaultGrailsPluginManager – Error configuring dynamic methods for plugin [springSecurityCore:2.0-RC4]: null Message: null

error message:
Error |
2016-04-25 11:12:49,121 [localhost-startStop-1] ERROR plugins.DefaultGrailsPluginManager – Error configuring dynamic methods for plugin [springSecurityCore:2.0-RC3]: null
Message: null
Line | Method
->> 312 | compileStaticRules in grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
| 222 | initialize in ”
| 747 | initializeFromAnnotations in SpringSecurityCoreGrailsPlugin
| 601 | doCall in SpringSecurityCoreGrailsPlugin$_closure3
| 262 | run . . . . . . . . . . . in java.util.concurrent.FutureTask
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread

solution:

./gradlew grails-refresh-dependencies

Virtual Box inner OS screen size

https://www.virtualbox.org/manual/ch04.html

My OS: Debian 8.4.0

Follow the guide above,  below is the solution:

Click “Insert Guest Additions CD Image..”, then in console type:

apt-get install dkms
apt-get update
apt-get upgrade

Then restart, and type in console:

mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
cd /mnt/cdrom
sh ./VBoxLinuxAdditions.run

Then restart again, and select “Auto-resize Guest Display”, click full screen and exit full screen, then you can resize the debian window.

Done!

Grails 2- pass temporary data from front to controller

To pass temporary data from html to controller, we need to define transient property inside domain. But transient property in domain is not bindable by default, so to solve the problem , we can do :

In domain class

String t1
static transient =['t1']
static constraints = {
   t1 bindable:true
}

We also need to remember to add the t1 property into map inside CustomMarshallerRegistrar for translation between json and java object.

Starting mongod failed

By checking  /var/log/mongodb/mongod.log, we can see the error message:


Detected unclean shutdown - /var/lib/mongo/mongod.lock is not empty.

The solution for this problem can be found at http://stackoverflow.com/questions/9884233/mongodb-service-is-not-starting-up

sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start

It solved the problem.

These commands might be also useful:

kill current mongod process:
1. Find pid

ps -ef | grep mongo

2. kill pid

kill -9 PID

Grails + AngularJS as web framework

Recently I am trying to build a website with backend grails and front-end Angularjs. By struggling with folder structure to start , I found a good plugin by Craig:

https://github.com/craigburke/lazybones-angular-grails

To just use it , these commands are usually required:


lazybones create angular-grails myProject

./gradlew grails-create-domain-class -PgrailsArgs=MyDomainClassName

lazybones generate module -PmoduleName=myDomainClassName -PdomainClass=MyDomainClassName

./gradlew run

To open the Gradle project in IntelliJ IDEA, simply run the following task from the root:


./gradlew idea

The first build of Gradle from IDEA will fail. Launching a second build fixes the compilation error. From http://gradle.org/

To change the template which is used to generate code inside the plugin, we need to change the code inside the src/templates (Sources:templates). We can refer to lazybones template guide. The lazybones template reference is https://github.com/pledbrook/lazybones/wiki/Template-developers-guide

To use spring sercurity quick start , we can use the command:

 ./gradlew grails-s2-quickstart -PgrailsArgs='com.company User Role'

 

production phase:

./gradlew -PgrailsEnv=production grails-run-app

If you want to pass in some arguments, you can do so via the grailsArgs project property:

./gradlew -PgrailsArgs='--inplace solr' grails-create-plugin

 

Change grails server url:

in grails-app/conf/Config.groovy

environments {
production {

grails.serverURL = "http://localhost:8080" // Specify the root of your link
....
}

development {
grails.serverURL = "http://localhost:8080/${appName}"
...
}
...
}

And add another line into Config.groovy:

grails.app.context = '/'

In order to access from port 80 , please refer https://workdiaryyy.wordpress.com/2015/12/18/port-80-permission-denied-for-web-service/

References:

https://github.com/pledbrook/lazybones/wiki/Template-developers-guide

http://gradle.org/

https://github.com/grails/grails-gradle-plugin

https://github.com/craigburke/lazybones-angular-grails

http://stackoverflow.com/questions/4989528/launching-a-grails-app-through-intelli-j-with-root-set-to-localhost8080-instea