package com.sleepycat.examples.bdb.shipment.entity;
import com.sleepycat.bdb.bind.DataBinding;
import com.sleepycat.bdb.bind.EntityBinding;
import com.sleepycat.bdb.bind.serial.SerialBinding;
import com.sleepycat.bdb.bind.serial.SerialFormat;
import com.sleepycat.bdb.bind.serial.SerialSerialBinding;
import com.sleepycat.bdb.collection.StoredMap;
import com.sleepycat.bdb.collection.StoredValueSet;
import java.io.IOException;
public class SampleViews {
private StoredMap partMap;
private StoredMap supplierMap;
private StoredMap shipmentMap;
private StoredMap shipmentByPartMap;
private StoredMap shipmentBySupplierMap;
private StoredMap supplierByCityMap;
public SampleViews(SampleDatabase db) {
DataBinding partKeyBinding =
new SerialBinding(db.getPartKeyFormat());
EntityBinding partValueBinding =
new PartBinding(db.getPartKeyFormat(), db.getPartValueFormat());
DataBinding supplierKeyBinding =
new SerialBinding(db.getSupplierKeyFormat());
EntityBinding supplierValueBinding =
new SupplierBinding(db.getSupplierKeyFormat(),
db.getSupplierValueFormat());
DataBinding shipmentKeyBinding =
new SerialBinding(db.getShipmentKeyFormat());
EntityBinding shipmentValueBinding =
new ShipmentBinding(db.getShipmentKeyFormat(),
db.getShipmentValueFormat());
DataBinding cityKeyBinding =
new SerialBinding(db.getCityKeyFormat());
partMap =
new StoredMap(db.getPartStore(),
partKeyBinding, partValueBinding, true);
supplierMap =
new StoredMap(db.getSupplierStore(),
supplierKeyBinding, supplierValueBinding, true);
shipmentMap =
new StoredMap(db.getShipmentStore(),
shipmentKeyBinding, shipmentValueBinding, true);
shipmentByPartMap =
new StoredMap(db.getShipmentByPartIndex(),
partKeyBinding, shipmentValueBinding, true);
shipmentBySupplierMap =
new StoredMap(db.getShipmentBySupplierIndex(),
supplierKeyBinding, shipmentValueBinding, true);
supplierByCityMap =
new StoredMap(db.getSupplierByCityIndex(),
cityKeyBinding, supplierValueBinding, true);
}
public StoredMap getPartMap() {
return partMap;
}
public StoredMap getSupplierMap() {
return supplierMap;
}
public StoredMap getShipmentMap() {
return shipmentMap;
}
public StoredValueSet getPartSet() {
return (StoredValueSet) partMap.values();
}
public StoredValueSet getSupplierSet() {
return (StoredValueSet) supplierMap.values();
}
public StoredValueSet getShipmentSet() {
return (StoredValueSet) shipmentMap.values();
}
public StoredMap getShipmentByPartMap() {
return shipmentByPartMap;
}
public StoredMap getShipmentBySupplierMap() {
return shipmentBySupplierMap;
}
public final StoredMap getSupplierByCityMap() {
return supplierByCityMap;
}
private static class PartBinding extends SerialSerialBinding {
private PartBinding(SerialFormat keyFormat,
SerialFormat valueFormat) {
super(keyFormat, valueFormat);
}
public Object dataToObject(Object keyInput, Object valueInput)
throws IOException {
PartKey key = (PartKey) keyInput;
PartValue value = (PartValue) valueInput;
return new Part(key.getNumber(), value.getName(), value.getColor(),
value.getWeight(), value.getCity());
}
public Object objectToKey(Object object)
throws IOException {
Part part = (Part) object;
return new PartKey(part.getNumber());
}
public Object objectToValue(Object object)
throws IOException {
Part part = (Part) object;
return new PartValue(part.getName(), part.getColor(),
part.getWeight(), part.getCity());
}
}
private static class SupplierBinding extends SerialSerialBinding {
private SupplierBinding(SerialFormat keyFormat,
SerialFormat valueFormat) {
super(keyFormat, valueFormat);
}
public Object dataToObject(Object keyInput, Object valueInput)
throws IOException {
SupplierKey key = (SupplierKey) keyInput;
SupplierValue value = (SupplierValue) valueInput;
return new Supplier(key.getNumber(), value.getName(),
value.getStatus(), value.getCity());
}
public Object objectToKey(Object object)
throws IOException {
Supplier supplier = (Supplier) object;
return new SupplierKey(supplier.getNumber());
}
public Object objectToValue(Object object)
throws IOException {
Supplier supplier = (Supplier) object;
return new SupplierValue(supplier.getName(), supplier.getStatus(),
supplier.getCity());
}
}
private static class ShipmentBinding extends SerialSerialBinding {
private ShipmentBinding(SerialFormat keyFormat,
SerialFormat valueFormat) {
super(keyFormat, valueFormat);
}
public Object dataToObject(Object keyInput, Object valueInput)
throws IOException {
ShipmentKey key = (ShipmentKey) keyInput;
ShipmentValue value = (ShipmentValue) valueInput;
return new Shipment(key.getPartNumber(), key.getSupplierNumber(),
value.getQuantity());
}
public Object objectToKey(Object object)
throws IOException {
Shipment shipment = (Shipment) object;
return new ShipmentKey(shipment.getPartNumber(),
shipment.getSupplierNumber());
}
public Object objectToValue(Object object)
throws IOException {
Shipment shipment = (Shipment) object;
return new ShipmentValue(shipment.getQuantity());
}
}
}