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!



No hay comentarios: