-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce datasource holder for console applications bookstore2 test
- Loading branch information
Showing
9 changed files
with
271 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
core/modules/core/src/com/haulmont/yarg/console/DatasourceHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.haulmont.yarg.console; | ||
|
||
import javax.sql.DataSource; | ||
|
||
/** | ||
* @author degtyarjov | ||
* @version $Id$ | ||
*/ | ||
public final class DatasourceHolder { | ||
private DatasourceHolder() { | ||
} | ||
|
||
public static volatile DataSource dataSource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
core/modules/core/test/sample/bookstore2/BookStore2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package sample.bookstore2; | ||
|
||
import com.haulmont.yarg.console.ConsoleRunner; | ||
import com.haulmont.yarg.console.DatasourceHolder; | ||
import com.haulmont.yarg.formatters.factory.DefaultFormatterFactory; | ||
import com.haulmont.yarg.loaders.factory.DefaultLoaderFactory; | ||
import com.haulmont.yarg.loaders.impl.GroovyDataLoader; | ||
import com.haulmont.yarg.loaders.impl.SqlDataLoader; | ||
import com.haulmont.yarg.reporting.ReportOutputDocument; | ||
import com.haulmont.yarg.reporting.Reporting; | ||
import com.haulmont.yarg.reporting.RunParams; | ||
import com.haulmont.yarg.structure.Report; | ||
import com.haulmont.yarg.structure.xml.impl.DefaultXmlReader; | ||
import com.haulmont.yarg.util.groovy.DefaultScriptingImpl; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.Test; | ||
import utils.TestDatabase; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* @author degtyarjov | ||
* @version $Id$ | ||
*/ | ||
|
||
public class BookStore2Test { | ||
@Test | ||
public void testBookStoreReport() throws Exception { | ||
TestDatabase testDatabase = new TestDatabase(); | ||
testDatabase.setUpDatabase(); | ||
|
||
Connection connection = testDatabase.getDs().getConnection(); | ||
DatasourceHolder.dataSource = testDatabase.getDs(); | ||
try { | ||
connection.createStatement().executeUpdate("drop table store;"); | ||
} catch (SQLException e) { | ||
//ignore | ||
} | ||
try { | ||
connection.createStatement().executeUpdate("drop table book;"); | ||
} catch (SQLException e) { | ||
//ignore | ||
} | ||
|
||
connection.createStatement().executeUpdate("create table store (id integer, name varchar, address varchar);"); | ||
connection.createStatement().executeUpdate("create table book(id integer, name varchar, author varchar, price decimal, store_id integer);"); | ||
|
||
connection.createStatement().executeUpdate("insert into store values(1, 'Main store', 'Some street');"); | ||
connection.createStatement().executeUpdate("insert into store values(2, 'Secondary store', 'Another street');"); | ||
|
||
connection.createStatement().executeUpdate("insert into book values(1, 'Concurrency in practice', 'Brian Goetz', 10.0, 1);"); | ||
connection.createStatement().executeUpdate("insert into book values(2, 'Concurrency in practice', 'Brian Goetz', 10.0, 1);"); | ||
connection.createStatement().executeUpdate("insert into book values(2, 'Concurrency in practice', 'Brian Goetz', 10.0, 1);"); | ||
connection.createStatement().executeUpdate("insert into book values(3, 'Effective Java', 'Josh Bloch', 20.0, 2);"); | ||
connection.createStatement().executeUpdate("insert into book values(4, 'Effective Java', 'Josh Bloch', 20.0, 2);"); | ||
connection.createStatement().executeUpdate("insert into book values(4, 'Effective Java', 'Josh Bloch', 20.0, 2);"); | ||
connection.createStatement().executeUpdate("insert into book values(5, 'Effective Java', 'Josh Bloch', 20.0, 1);"); | ||
connection.createStatement().executeUpdate("insert into book values(5, 'Effective Java', 'Josh Bloch', 20.0, 1);"); | ||
connection.createStatement().executeUpdate("insert into book values(6, 'Concurrency in practice', 'Brian Goetz', 10.0, 2);"); | ||
connection.createStatement().executeUpdate("insert into book values(7, 'Refactoring', 'Martin Fowler', 15.0, 2);"); | ||
connection.createStatement().executeUpdate("insert into book values(8, 'Refactoring', 'Martin Fowler', 15.0, 2);"); | ||
connection.createStatement().executeUpdate("insert into book values(8, 'Refactoring', 'Martin Fowler', 15.0, 2);"); | ||
connection.createStatement().executeUpdate("insert into book values(9, 'Refactoring', 'Martin Fowler', 15.0, 1);"); | ||
|
||
connection.commit(); | ||
|
||
Report report = new DefaultXmlReader().parseXml(FileUtils.readFileToString(new File("./modules/core/test/sample/bookstore2/bookstore2.xml"))); | ||
System.out.println(); | ||
|
||
Reporting reporting = new Reporting(); | ||
reporting.setFormatterFactory(new DefaultFormatterFactory()); | ||
reporting.setLoaderFactory(new DefaultLoaderFactory() | ||
.setGroovyDataLoader(new GroovyDataLoader(new DefaultScriptingImpl())) | ||
.setSqlDataLoader(new SqlDataLoader(testDatabase.getDs()))); | ||
|
||
ReportOutputDocument reportOutputDocument = reporting.runReport(new RunParams(report), new FileOutputStream("./result/sample/bookstore2.xls")); | ||
|
||
testDatabase.stop(); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<report name="report"> | ||
<templates> | ||
<template code="DEFAULT" documentName="bookstore.xls" | ||
documentPath="./modules/core/test/sample/bookstore2/bookstore2.xls" outputType="xls" | ||
outputNamePattern="bookstore.xls"/> | ||
</templates> | ||
<rootBand name="Root" orientation="H"> | ||
<bands> | ||
<band name="Header" orientation="H"/> | ||
<band name="Shop" orientation="H"> | ||
<bands> | ||
<band name="Book" orientation="H"> | ||
<queries> | ||
<query name="Book" type="groovy"> | ||
<script> | ||
return params['Books'].asMap().get(parentBand.getParameterValue('id')) | ||
</script> | ||
</query> | ||
</queries> | ||
</band> | ||
</bands> | ||
<queries> | ||
<query name="Shop" type="groovy"> | ||
<script> | ||
return params['Shops'] | ||
</script> | ||
</query> | ||
</queries> | ||
</band> | ||
</bands> | ||
<queries> | ||
<query name="Root" type="groovy"> | ||
<script> | ||
package sample.bookstore2.bookstore | ||
|
||
import com.google.common.collect.ArrayListMultimap | ||
import com.google.common.collect.Multimap | ||
import com.haulmont.yarg.console.DatasourceHolder | ||
import com.haulmont.yarg.util.db.QueryRunner | ||
import com.haulmont.yarg.util.db.ResultSetHandler | ||
|
||
import java.sql.ResultSet | ||
import java.sql.SQLException | ||
|
||
QueryRunner runner = new QueryRunner(DatasourceHolder.dataSource); | ||
def query = | ||
'select shop.id as "id", shop.name as "name", shop.address as "address", ' + | ||
' book.author as "author", book.name as "name", book.price as "price", count(*) as "count" ' + | ||
'from store shop, book ' + | ||
'where book.store_id = shop.id ' + | ||
'group by shop.id, shop.name, shop.address, book.author, book.name, book.price'; | ||
def shops = new HashSet() | ||
def booksByShopId = ArrayListMultimap.create() | ||
|
||
runner.query(query, new ResultSetHandler() { | ||
@Override | ||
Object handle(ResultSet rs) throws SQLException { | ||
while (rs.next()) { | ||
def shop = [:] | ||
shop['id'] = rs.getInt(1) | ||
shop['name'] = rs.getString(2) | ||
shop['address'] = rs.getString(3) | ||
|
||
shops.add(shop) | ||
def book = [:] | ||
book['author'] = rs.getString(4) | ||
book['name'] = rs.getString(5) | ||
book['price'] = rs.getBigDecimal(6) | ||
book['count'] = rs.getLong(7) | ||
|
||
booksByShopId.put(rs.getInt(1), book) | ||
} | ||
return null | ||
} | ||
}) | ||
|
||
params['Shops'] = new ArrayList(shops) | ||
params['Books'] = booksByShopId | ||
|
||
return [[:]] | ||
</script> | ||
</query> | ||
</queries> | ||
</rootBand> | ||
</report> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package sample.bookstore2.bookstore | ||
|
||
import com.google.common.collect.ArrayListMultimap | ||
import com.google.common.collect.Multimap | ||
import com.haulmont.yarg.console.ConsoleRunner | ||
import com.haulmont.yarg.util.db.QueryRunner | ||
import com.haulmont.yarg.util.db.ResultSetHandler | ||
|
||
import java.sql.ResultSet | ||
import java.sql.SQLException | ||
|
||
QueryRunner runner = new QueryRunner(ConsoleRunner.dataSource); | ||
def query = | ||
'select shop.id as "id", shop.name as "name", shop.address as "address", ' + | ||
' book.author as "author", book.name as "name", book.price as "price", count(*) as "count" ' + | ||
'from shop, book book ' + | ||
'where book.store_id = shop.id ' + | ||
'group by shop.id, shop.name, shop.address book.author, book.name, book.price'; | ||
def shops = new HashSet() | ||
def booksByShopId = ArrayListMultimap.create() | ||
|
||
runner.query(query, new ResultSetHandler() { | ||
@Override | ||
Object handle(ResultSet rs) throws SQLException { | ||
while (rs.next()) { | ||
def shop = [:] | ||
shop['id'] = rs.getInt(1) | ||
shop['name'] = rs.getString(2) | ||
shop['address'] = rs.getString(3) | ||
|
||
shops.add(shop) | ||
def book = [:] | ||
book['author'] = rs.getString(4) | ||
book['name'] = rs.getString(5) | ||
book['price'] = rs.getBigDecimal(6) | ||
book['count'] = rs.getLong(7) | ||
|
||
booksByShopId.put(rs.getInt(1), book) | ||
} | ||
return null | ||
} | ||
}) | ||
|
||
params['Shops'] = shops | ||
params['Books'] = booksByShopId | ||
|
||
return [[:]] |