リスト8 CastorJDOTest.java

  1 package com.netpotlet.test;
  2
  3 import java.io.FileNotFoundException;
  4 import java.io.FileReader;
  5 import java.io.IOException;
  6 import java.io.StringWriter;
  7 import java.util.Date;
  8
  9 import javax.xml.parsers.FactoryConfigurationError;
 10 import javax.xml.parsers.ParserConfigurationException;
 11 import javax.xml.transform.TransformerConfigurationException;
 12 import javax.xml.transform.TransformerException;
 13
 14 import org.exolab.castor.jdo.Database;
 15 import org.exolab.castor.jdo.DatabaseNotFoundException;
 16 import org.exolab.castor.jdo.JDO;
 17 import org.exolab.castor.jdo.OQLQuery;
 18 import org.exolab.castor.jdo.PersistenceException;
 19 import org.exolab.castor.jdo.QueryResults;
 20 import org.exolab.castor.mapping.MappingException;
 21 import org.exolab.castor.xml.MarshalException;
 22 import org.exolab.castor.xml.Marshaller;
 23 import org.exolab.castor.xml.ValidationException;
 24
 25 import com.netpotlet.nymph.*;
 26
 27 public class CastorJDOTest {
 28     private CastorJDOTest(String xmlfile, String conffile,
 29                           String xsltfile, String outputfile)
 30         throws MappingException,
 31                DatabaseNotFoundException,
 32                PersistenceException,
 33                MarshalException,
 34                ValidationException,
 35                TransformerConfigurationException,
 36                IOException,
 37                FactoryConfigurationError,
 38                ParserConfigurationException, TransformerException {
 39
            //アンマーシャル
 40         GoodsList goodsList = unmarshall(xmlfile);

            //config.xml と mapping.xml の読み込み
 41         JDO jdo = new JDO();
 42         jdo.setConfiguration(getClass().getResource(conffile).toString());
 43         jdo.setDatabaseName("nymph");

            //アンマーシャルしたオブジェクトをデータベースへ
 44         Database database = jdo.getDatabase();
 45         create(database, goodsList);

            //プログラム内で作ったオブジェクトをデータベースへ
 46         addData(database);

            //データベースの情報をオブジェクトへ
 47         GoodsList selectedList = select(database);

            //マーシャル
 48         String marshalledString = getMarshalledString(selectedList);

            //XML + XSLT で HTMLへ
 49         transform(xsltfile, marshalledString, outputfile);
 50     }
 51     
 52     private GoodsList unmarshall(String filename)
 53         throws MarshalException,
 54                ValidationException,
 55                FileNotFoundException {
 56         
 57         GoodsList goodsList =
 58             (GoodsList)GoodsList.unmarshal(new FileReader(filename));
 59         return goodsList;
 60     }
 61
 62     private void create(Database database, GoodsList goodsList)
 63         throws PersistenceException {
 64         Goods[] goodsArray = goodsList.getGoods();
 65         for (int i=0; i<goodsArray.length; i++) {
 66             JdoGoods jdoGoods = new JdoGoods(goodsArray[i]);
 67             create(database, jdoGoods);
 68         }
 69     }
 70
 71     private void create(Database database, JdoGoods goods)
 72         throws PersistenceException {
 73         Price price = goods.getPrice();
 74         Category category = goods.getCategory();

            //トランザクション開始
 75         database.begin();

            //INSERT文相当の処理
 76         database.create(goods);
 77         database.create(price);
 78         database.create(category);

            //トランザクション終了
 79         database.commit();
 80     }
 81     
 82     private void addData(Database database)
 83         throws PersistenceException {
 84         int id = 5;
 85         JdoGoods goods = new JdoGoods();
 86         goods.setId(5);
 87         goods.setSample("river.gif");
 88         goods.setName("フォトカード1");
 89         goods.setDate(2003, 12-1, 12);
 90         goods.setDescription("浅草橋");
 91         Price price = new Price();
 92         price.setId(id);
 93         price.setCost(100);
 94         price.setRetail(150);
 95         goods.setPrice(price);
 96         Category category = new Category();
 97         category.setId(id);
 98         category.setName("文具");
 99         goods.setCategory(category);
100         create(database, goods);
101     }
102     
103     private GoodsList select(Database database)
104         throws PersistenceException {
105         GoodsList selectedList = new GoodsList();

            //トランザクション開始
106         database.begin();

            //OQLクエリー生成
107         OQLQuery oqlQuery =
108           database.getOQLQuery("SELECT goods FROM com.netpotlet.nymph.JdoGoods goods");

            //OQLクエリー実行
109         QueryResults results = oqlQuery.execute();

            //クエリー実行結果の取得
110         while (results.hasMoreElements()) {
111             JdoGoods jdoGoods = (JdoGoods)results.next();
112             int id = jdoGoods.getId();
113             String sample = (String)jdoGoods.getSample();
114             String name = jdoGoods.getName();
115             Date date = jdoGoods.getDate();
116             String description = jdoGoods.getDescription();
117             Price price = jdoGoods.getPrice();
118             Category category = jdoGoods.getCategory();
119             System.out.println(id+", "+sample+", "+name+", "+description);
120             System.out.println(price.getCost()+", "+price.getRetail());
121             System.out.println(category.getName());
122             selectedList.addGoods(jdoGoods.getGoods());
123         }
124         results.close();
125         oqlQuery.close();

            //トランザクション終了
126         database.commit();
127         return selectedList;
128     }
129     
130     private String getMarshalledString(GoodsList goodsList)
131         throws IOException,
132                MarshalException,
133                ValidationException {
134         StringWriter writer = new StringWriter();
135         Marshaller marshaller = new Marshaller(writer);
136         marshaller.setEncoding("euc-jp");
137         marshaller.marshal(goodsList);
138         return writer.toString();
139     }
140     
141     private void transform(String xsltfile, String document, String outputfile)
142         throws TransformerConfigurationException,
143                IOException,
144                FactoryConfigurationError,
145                ParserConfigurationException,
146                TransformerException {
147         DocumentTransformer transformer =
148             new DocumentTransformer("euc-jp", xsltfile);
149         transformer.setSource(document);
150         transformer.setResult(outputfile);
151         transformer.transformDocument();
152     }
153
154     public static void main(String[] args) throws Exception {
155         new CastorJDOTest(args[0], args[1], args[2], args[3]);
156     }
157 }