Java Cart .

Java Cart ItemBuilder java

package cart;

import cart.dbUtils.DBConnection;
import cart.dbUtils.ItemGroupDBUtil;
import cart.dbUtils.ItemSizeColorDBUtil;
import java.sql.ResultSet;

//ItemBuilder - Builds Item and it's subclasses
// This class is something of a necessary evil as a bridge between the 
//     relational tables and the different item types.
// The ItemType comes from the table, and using it we determine what 
//     kind of item needs to be built.
// For some item types additional dbUtils must be called.
// I decided that having this all in one place makes maintence for 
//     new item types, or even different data input types
//     such as XML, easiest to manage and will effect the other 
//     code as little as possible.  While every new ItemType
//     will need to have this Class changed, new ItemTypes 
//     shouldn't be too common.
// It also means that anyone wanting to change this scheme for their 
//     implementation has to only replace this class.
//author - Lawrence Truett - FluffyCat.com
//date - July 7th, 2003 - San Diego, CA
public class ItemBuilder {
    //make an item using dbc and an Item table ResultSet, 
    //    used by ItemDBUtil
    public static Item buildItem(DBConnection dbc, 
                                 ResultSet resultSet) {
        Item itemToReturn = null;
        try {
            ItemType itemType = new ItemType(resultSet.getString(4));
            switch (itemType.getTypeChar0()) {
                case 'S' :
                    switch (itemType.getTypeChar1()) {
                        case 'C' :
                            itemToReturn = 
                                buildItemOfStuffWSizeColor(dbc,
                                                           resultSet,
                                                           itemType);
                            break;
                        default :
                            itemToReturn = 
                                buildItemOfStuff(dbc, 
                                                 resultSet, 
                                                 itemType);
                            break;
                    }
                    break;
                case 'M' :
                    itemToReturn = 
                        buildItemOfMedia(dbc, 
                                         resultSet, 
                                         itemType);
                    break;
                case 'G' :
                    itemToReturn = 
                        buildItemOfGroup(dbc, 
                                         resultSet, 
                                         itemType);
                    break;
                default  :  itemToReturn = 
                    buildBaseItem(dbc, 
                                  resultSet, 
                                  itemType);
            }
            
        } catch (java.sql.SQLException sqlException) {
            System.out.println(
                "The following error occured in "+
                "ItemBuilder.buildItem: " +
                sqlException);
        }
        return itemToReturn;
    }
    
    public static Item buildBaseItem(DBConnection dbc, 
                                     ResultSet resultSet, 
                                     ItemType itemTypeIn) {
        Item itemToReturn = null;
        try {
            itemToReturn = new Item(resultSet.getLong(1), //key
            resultSet.getDouble(2),//price
            resultSet.getString(3),//description
            itemTypeIn,
            resultSet.getString(5),//short image
            resultSet.getString(6)//large image
            );
        } catch (java.sql.SQLException sqlException) {
            System.out.println(
                "The following error occured in "+
                "ItemBuilder.buildBaseItem: " +
                 sqlException);
        }
        return itemToReturn;
    }
    
    public static Item buildItemOfGroup(DBConnection dbc, 
                                        ResultSet resultSet, 
                                        ItemType itemTypeIn) {
        ItemOfGroup itemToReturn = null;
        try {
            long[] itemsInGroup = 
                ItemGroupDBUtil.getItemsInGroup(resultSet.getLong(1),
                                                dbc);
            itemToReturn = 
                new ItemOfGroup(resultSet.getLong(1), //key
            resultSet.getDouble(2),//price
            resultSet.getString(3),//description
            itemTypeIn,
            resultSet.getString(5),//short image
            resultSet.getString(6),//large image
            itemsInGroup
            );
        } catch (java.sql.SQLException sqlException) {
            System.out.println(
                "The following error occured in "+
                "ItemBuilder.buildItemOfGroup: " + 
                 sqlException);
        }
        return itemToReturn;
    }
    
    public static Item buildItemOfMedia(DBConnection dbc, 
                                        ResultSet resultSet, 
                                        ItemType itemTypeIn) {
        ItemOfMedia itemToReturn = null;
        try {
            itemToReturn = new ItemOfMedia(resultSet.getLong(1), //key
            resultSet.getDouble(2),//price
            resultSet.getString(3),//description
            itemTypeIn,
            resultSet.getString(5),//short image
            resultSet.getString(6)//large image
            );
        } catch (java.sql.SQLException sqlException) {
            System.out.println(
                "The following error occured in "+
                "ItemBuilder.buildItemOfStuffWSizeColor: " + 
                sqlException);
        }
        return itemToReturn;
    }
    
    public static Item buildItemOfStuff(DBConnection dbc, 
                                        ResultSet resultSet, 
                                        ItemType itemTypeIn) {
        ItemOfStuff itemToReturn = null;
        try {
            itemToReturn = new ItemOfStuff(resultSet.getLong(1), //key
            resultSet.getDouble(2),//price
            resultSet.getString(3),//description
            itemTypeIn,
            resultSet.getString(5),//short image
            resultSet.getString(6)//large image
            );
        } catch (java.sql.SQLException sqlException) {
            System.out.println(
                "The following error occured in "+
                 "ItemBuilder.buildItemOfStuffWSizeColor: " +
                 sqlException);
        }
        return itemToReturn;
    }
    
    public static Item buildItemOfStuffWSizeColor(DBConnection dbc, 
                                                  ResultSet resultSet, 
                                                  ItemType itemTypeIn) {
        ItemOfStuffWSizeColor itemToReturn = null;
        
        try {
            
            long itemKey = resultSet.getLong(1);
            
            ItemSizeColor itemSizeColor = 
                ItemSizeColorDBUtil.getItemSizeColorForItemKey(itemKey, 
                                                               dbc);
            
            itemToReturn = new ItemOfStuffWSizeColor(itemKey, //key
            resultSet.getDouble(2),//price
            resultSet.getString(3),//description
            itemTypeIn,
            resultSet.getString(5),//short image
            resultSet.getString(6),//large image
            itemSizeColor.getSize(),
            itemSizeColor.getColor()
            );
        } catch (java.sql.SQLException sqlException) {
            System.out.println(
                "The following error occured in " +
                "ItemBuilder.buildItemOfStuffWSizeColor: " + 
                sqlException);
        }
        return itemToReturn;
    }
}
To download source right-click here and "Save As...".
Comments
Sign in to be the first to comment on Java Cart ItemBuilder java.