lunes, mayo 19, 2014

Grails, reusing domains with a binary plugin

How should I do in order to reuse my domains?
Use your own domain's plugin, this is how.

Just create your plugin:

grails create-plugin book-remote
...{work as usual with your domains}...

Change your groupId/version and set binary as default type of packaging:

BookRemoteGrailsPlugin.groovy:

String groupId = 'com.nortia.book'
String version = '0.1'
String packaging = 'binary'

Install into local maven repo (or wherever you want to):

grails  maven install --binary


Then, you have include your plugin as a dependency for your grails app:

BuildConfig.groovy:
    dependencies {
    //Since it is  binary plugin is goes here instead of in the 'plugins' section
compile 'com.nortia.book:book-remote:0.1'
    }


That's it! :D
PD: There is no need for the plugin to be 'binary' unless you're planning to use those domains in another groovy (no grails) project (which was my case).

miércoles, mayo 14, 2014

Integrando Gorm en Griffon

Con los últimos anuncios de Graeme Rocher sobre la integración de Gorm en cualquier script Groovy:(ver https://gist.github.com/graemerocher/c25ec929d9bcd1adcbea):

Lo que pedía el cuerpo era hacer un intento para incluir Gorm en un ejemplo sencillo de Griffon.
El código completo esta en el siguiente repo (https://github.com/ivanarrizabalaga/BookGorm), y las partes relevantes son:

BuildConfig.groovy:

    repositories {
        griffonHome()
        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/"
        mavenRepo "http://repo.spring.io/milestone"
        mavenRepo "http://repo.spring.io/release"
    }
    dependencies {
        runtime "org.grails:grails-datastore-gorm-hibernate4:3.0.0.RELEASE"
        runtime "org.grails:grails-spring:2.3.6"
        runtime "com.h2database:h2:1.3.164"
        compile "org.springframework:spring-expression:3.2.4.RELEASE"
    }

Initialize.groovy:

//TODO Should be a Spring bean
new HibernateDatastoreSpringInitializer("bookgorm").
configureForDataSource(new DriverManagerDataSource(
Driver.name,
"jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE", 'sa', ''
)
)

src/main/bookgorm/Book.groovy:

package bookgorm
import grails.persistence.Entity

@Entity
class Book{
String title
String author
  static constraints = {
        title blank:false
        author blank:false
  }
}

BookController.groovy:

    void loadData(){
        List books=Book.list()
        edt{
            model.books.addAll(books)
        }
        
    }
    def addBook= {evt=null->
        Book b=new Book(title:view.titleField.text,author:view.authorField.text)
        b.save()
        edt{
            model.books.add(b)
            view.titleField.text=""
            view.authorField.text=""
        }
    }
    def clearAll= {evt=null->
        Book.executeUpdate("delete Book")
        edt{
            model.books.clear()
        }
    }

Mola, no?
¡A cuidarse!