1 package com.netpotlet.test;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.sql.Connection;
6 import java.sql.SQLException;
7
8 import javax.xml.parsers.FactoryConfigurationError;
9 import javax.xml.parsers.ParserConfigurationException;
10 import javax.xml.transform.TransformerConfigurationException;
11 import javax.xml.transform.TransformerException;
12
13 import org.xml.sax.SAXException;
14
15 import com.netpotlet.elf.ElfDetailCategoryCategory;
16 import com.netpotlet.elf.ElfDetailPricePrice;
17 import com.netpotlet.elf.ElfGoods;
18 import com.netpotlet.elf.ElfGoodsList;
19 import com.netpotlet.elf.GoodsTable;
20 import com.netpotlet.elf.GoodsTableView;
21
22 public class RelaxerJDBCTest {
23 private RelaxerJDBCTest(String xmlfile,
24 String
drivername,
25 String
jdbcurl,
26 String
username,
27 String
password,
28 String
tablename,
29 String
xsltfile,
30 String
outputfile)
31 throws SAXException,
32 ClassNotFoundException,
33 RuntimeException,
34 SQLException,
35 TransformerConfigurationException,
36 IOException,
37 FactoryConfigurationError,
38 ParserConfigurationException,
TransformerException {
//アンマーシャル
39 ElfGoodsList elfGoodsList
= unmarshal(xmlfile);
//データベースとの接続作成
40 ConnectionManager connectionManager
=
41 ConnectionManager.getInstance(drivername,
42 jdbcurl,
43 username,
44 password);
//アンマーシャルしたオブジェクトをデータベースへ
45 create(connectionManager,
tablename, elfGoodsList);
//プログラム内で作ったオブジェクトをデータベースへ
46 addData(connectionManager,
tablename);
//データベースの情報をオブジェクトへ
47 ElfGoodsList selected
=
48 select(connectionManager, "select
* from "+tablename+";");
//マーシャル
49 String marshalledString
= getMarshalledString(selected);
//XML+XSLTでHTMLへ
50 transform(xsltfile,
marshalledString, outputfile);
51 }
52
53 private ElfGoodsList unmarshal(String xmlfile)
54 throws IOException,
55 SAXException,
56 ParserConfigurationException
{
57 return new ElfGoodsList(new
File(xmlfile));
58 }
59
60 private void create(ConnectionManager manager,
61 String
tablename,
62 ElfGoodsList
elfGoodsList)
63 throws SQLException
{
//コネクションプールから1つ取得
64 Connection connection
= manager.getConnection();
//goodsテーブル操作クラスのインスタンス生成
65 GoodsTable goodsTable
=
66 new
GoodsTable(connection, tablename);
//INSERTO文相当の処理
67 goodsTable.insert(elfGoodsList.getGoods());
//プールにコネクションを返す
68 manager.returnConnection(connection);
69 }
70
71 private void addData(ConnectionManager manager,
72 String
tablename)
73 throws SQLException
{
74 String id = "5";
75 ElfGoods elfGoods =
new ElfGoods();
76 elfGoods.setIdByString(id);
77 elfGoods.setSample("river.gif");
78 elfGoods.setName("フォトカード1");
79 elfGoods.setXmlDateByString("2003-12-12");
80 elfGoods.setDescription("浅草橋");
81 ElfDetailPricePrice
price = new ElfDetailPricePrice();
82 price.setIdByString(id);
83 price.setCostByString("100");
84 price.setRetailByString("150");
85 elfGoods.setDetailPricePrice(price);
86 ElfDetailCategoryCategory
category =
87 new
ElfDetailCategoryCategory();
88 category.setIdByString(id);
89 category.setName("文具");
90 elfGoods.setDetailCategoryCategory(category);
91 Connection connection
= manager.getConnection();
92 GoodsTable goodsTable
=
93 new
GoodsTable(connection, tablename);
94 goodsTable.insert(elfGoods);
95 manager.returnConnection(connection);
96 }
97
98 private ElfGoodsList select(ConnectionManager
manager,
99 String
selectClause)
100 throws SQLException {
//コネクションプールから1つ取得
101 Connection connection = manager.getConnection();
//データベース参照クラスのインスタンス生成
102 GoodsTableView goodsTableView
=
103 new
GoodsTableView(connection, selectClause);
//クエリー実行
104 ElfGoods[] elfGoodsArray
= goodsTableView.select();
//goodsListがルートのツリーの作成
105 ElfGoodsList elfGoodsList
= new ElfGoodsList();
106 elfGoodsList.setGoods(elfGoodsArray);
//プールにコネクションを返す
107 manager.returnConnection(connection);
108 return elfGoodsList;
109 }
110
111 private String getMarshalledString(ElfGoodsList elfGoodsList)
{
112 return elfGoodsList.makeTextDocument();
113 }
114
115 private void transform(String xsltfile,
116 String
document,
117 String
outputfile)
118 throws TransformerConfigurationException,
119 IOException,
120 FactoryConfigurationError,
121 ParserConfigurationException,
122 TransformerException
{
123 DocumentTransformer transformer
=
124 new
DocumentTransformer("euc-jp", xsltfile);
125 transformer.setSource(document);
126 transformer.setResult(outputfile);
127 transformer.transformDocument();
128 }
129
130 public static void main(String[] args) throws Exception
{
131 new RelaxerJDBCTest(args[0],
args[1], args[2], args[3],
132 args[4],
args[5], args[6], args[7]);
133 }
134 }