package com.sun.j2ee.blueprints.petstore.tools.populate;
import java.util.*;
import java.sql.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class ItemPopulator {
public static final String XML_ITEMS = "Items";
static final String XML_ITEM = "Item";
static final String XML_ID = "Item/@id";
private static final String XML_PRODUCT = "Item/@product";
private static final String[] PARAMETER_NAMES = { XML_ID, XML_PRODUCT };
private String rootTag;
private Map sqlStatements;
private ItemDetailsPopulator itemDetailsPopulator;
public ItemPopulator(Map sqlStatements) throws PopulateException {
this(XML_ITEMS, sqlStatements);
return;
}
public ItemPopulator(String rootTag, Map sqlStatements) throws PopulateException {
this.rootTag = rootTag;
this.sqlStatements = sqlStatements;
itemDetailsPopulator = new ItemDetailsPopulator(XML_ITEM, sqlStatements);
return;
}
public XMLFilter setup(XMLReader reader, final Connection connection) {
return new XMLDBHandler(itemDetailsPopulator.setup(reader, connection), rootTag, XML_ITEM, true) {
public void update() throws PopulateException {}
public void create() throws PopulateException {
if (connection == null) {
PopulateUtils.printSQLStatement(sqlStatements, PopulateUtils.makeSQLStatementKey(PopulateUtils.INSERT_OPERATION, "item"), PARAMETER_NAMES, this);
} else {
PopulateUtils.executeSQLStatement(connection, sqlStatements, PopulateUtils.makeSQLStatementKey(PopulateUtils.INSERT_OPERATION, "item"), PARAMETER_NAMES, this);
}
}
};
}
public boolean check(Connection connection) throws PopulateException {
return PopulateUtils.executeSQLStatement(connection, sqlStatements, PopulateUtils.makeSQLStatementKey(PopulateUtils.CHECK_OPERATION, "item"), null, null) && itemDetailsPopulator.check(connection);
}
public void dropTables(Connection connection) throws PopulateException {
try {
itemDetailsPopulator.dropTables(connection);
} catch (PopulateException exception) {}
PopulateUtils.executeSQLStatement(connection, sqlStatements, PopulateUtils.makeSQLStatementKey(PopulateUtils.DROP_OPERATION, "item"), null, null);
return;
}
public void createTables(Connection connection) throws PopulateException {
PopulateUtils.executeSQLStatement(connection, sqlStatements, PopulateUtils.makeSQLStatementKey(PopulateUtils.CREATE_OPERATION, "item"), null, null);
itemDetailsPopulator.createTables(connection);
return;
}
}