001: package com.completex.objective.persistency.examples.ex003.app;
002:
003: import com.completex.objective.components.persistency.OdalPersistencyException;
004: import com.completex.objective.components.persistency.Persistency;
005: import com.completex.objective.persistency.examples.ex003.domain.PoliceCar;
006: import com.completex.objective.persistency.examples.ex003.domain.PoliceCarAttr;
007: import com.completex.objective.persistency.examples.ex003.domain.Product;
008: import com.completex.objective.persistency.examples.ex003.domain.ProductAttr;
009: import com.completex.objective.persistency.examples.ex003.domain.TaxiCar;
010: import com.completex.objective.persistency.examples.ex003.domain.TaxiCarAttr;
011:
012: import java.util.Collection;
013: import java.util.List;
014: import java.util.ArrayList;
015:
016: /**
017: * @author Gennady Krizhevsky
018: */
019: public class ProductDAO {
020: private Persistency persistency;
021:
022: public ProductDAO(Persistency persistency) {
023: this .persistency = persistency;
024: }
025:
026: public Product loadProduct(final Long productId)
027: throws ProductException {
028: try {
029: ProductAttr productAttr = (ProductAttr) persistency
030: .load(new ProductAttr(productId));
031: return loadProduct(productAttr);
032: } catch (OdalPersistencyException e) {
033: throw new ProductException(e);
034: }
035: }
036:
037: private Product loadProduct(ProductAttr productAttr)
038: throws OdalPersistencyException {
039: Product product = null;
040: Long productId = productAttr.getProductId();
041: if (productAttr != null) {
042: if (PoliceCar.TYPE.equals(productAttr.getName())) {
043: PoliceCarAttr policeCarAttr = (PoliceCarAttr) persistency
044: .load(new PoliceCarAttr(productId));
045: product = new PoliceCar(productAttr, policeCarAttr);
046: } else if (TaxiCar.TYPE.equals(productAttr.getName())) {
047: TaxiCarAttr taxiCarAttr = (TaxiCarAttr) persistency
048: .load(new TaxiCarAttr(productId));
049: product = new TaxiCar(productAttr, taxiCarAttr);
050: } else {
051: throw new RuntimeException("Unsupported type "
052: + productAttr.getName());
053: }
054: }
055: return product;
056: }
057:
058: public Product[] loadAllProducts() throws ProductException {
059: try {
060: ArrayList products = new ArrayList();
061: List productAttrs = (List) persistency
062: .select(new ProductAttr());
063: for (int i = 0; i < productAttrs.size(); i++) {
064: ProductAttr productAttr = (ProductAttr) productAttrs
065: .get(i);
066: products.add(loadProduct(productAttr));
067: }
068: return (Product[]) products.toArray(new Product[products
069: .size()]);
070: } catch (OdalPersistencyException e) {
071: throw new ProductException(e);
072: }
073: }
074:
075: //
076: // Police car:
077: //
078: private void insertPoliceCar(final PoliceCar policeCar)
079: throws ProductException {
080: try {
081: persistency.insert(policeCar.getProductAttr());
082: policeCar.getPoliceCarAttr().setProductId(
083: policeCar.getProductAttr().getProductId());
084: persistency.insert(policeCar.getPoliceCarAttr());
085: } catch (OdalPersistencyException e) {
086: throw new ProductException(e);
087: }
088: }
089:
090: private void updatePoliceCar(final PoliceCar policeCar)
091: throws ProductException {
092: try {
093: persistency.update(policeCar.getProductAttr());
094: persistency.update(policeCar.getPoliceCarAttr());
095: } catch (OdalPersistencyException e) {
096: throw new ProductException(e);
097: }
098: }
099:
100: //
101: // Taxi car:
102: //
103: private void insertTaxiCar(final TaxiCar taxiCar)
104: throws ProductException {
105: try {
106: persistency.insert(taxiCar.getProductAttr());
107: taxiCar.getTaxiCarAttr().setProductId(
108: taxiCar.getProductAttr().getProductId());
109: persistency.insert(taxiCar.getTaxiCarAttr());
110: } catch (OdalPersistencyException e) {
111: throw new ProductException(e);
112: }
113: }
114:
115: private void updateTaxiCar(final TaxiCar taxiCar)
116: throws ProductException {
117: try {
118: persistency.update(taxiCar.getProductAttr());
119: persistency.update(taxiCar.getTaxiCarAttr());
120: } catch (OdalPersistencyException e) {
121: throw new ProductException(e);
122: }
123: }
124:
125: //
126: // Dispatchers:
127: //
128: public void insertProduct(final Product product)
129: throws ProductException {
130: if (product instanceof PoliceCar) {
131: insertPoliceCar(((PoliceCar) product));
132: } else if (product instanceof TaxiCar) {
133: insertTaxiCar(((TaxiCar) product));
134: } else {
135: throw new RuntimeException("Unsupported product type "
136: + product);
137: }
138: }
139:
140: public void updateProduct(final Product product)
141: throws ProductException {
142: if (product instanceof PoliceCar) {
143: updatePoliceCar(((PoliceCar) product));
144: } else if (product instanceof TaxiCar) {
145: updateTaxiCar(((TaxiCar) product));
146: } else {
147: throw new RuntimeException("Unsupported product type "
148: + product);
149: }
150: }
151:
152: }
|