25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 19 ■ <strong>Spring</strong> Boot, Simplifying Everything<br />

Listing 19-7 shows the model.groovy script that contains the definition of the two classes, the Document class and<br />

the Type class. Next, see Listing 19-8 for the repo.groovy script.<br />

Listing 19-8. repo.groovy<br />

import groovy.sql.Sql<br />

import org.springframework.core.io.Resource<br />

import javax.annotation.Resource as R<br />

@Repository<br />

class MyDocumentsRepo {<br />

def sqlEngine = Sql.newInstance("jdbc:hsqldb:mem:testdb", "sa", "", "org.hsqldb.jdbcDriver")<br />

@Autowired<br />

Resource schema<br />

@Autowired<br />

Resource data<br />

@R<br />

Map sql<br />

@PostConstruct<br />

def init(){<br />

schema.inputStream.eachLine{<br />

sqlEngine.execute(it)<br />

}<br />

data.inputStream.eachLine{<br />

sqlEngine.execute(it)<br />

}<br />

}<br />

def getAllDocuments(){<br />

def result = []<br />

def type, document<br />

sqlEngine.rows(sql.query).each { row -><br />

type = new Type(<br />

typeId:row.typeId,<br />

name:row.type_name,<br />

desc:row.type_desc,<br />

extension:row.extension)<br />

document = new Document(<br />

documentId:row.documentId,<br />

name:row.name,<br />

location:row.location,<br />

created:row.created,<br />

modified:row.modified,<br />

description:row.doc_desc,<br />

type:type)<br />

273

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!