package collections.ship.entity;
import com.sleepycat.bind.EntityBinding;
import com.sleepycat.bind.serial.ClassCatalog;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.SerialSerialBinding;
import com.sleepycat.collections.StoredSortedMap;
import com.sleepycat.collections.StoredValueSet;
public class SampleViews {
private StoredSortedMap partMap;
private StoredSortedMap supplierMap;
private StoredSortedMap shipmentMap;
private StoredSortedMap shipmentByPartMap;
private StoredSortedMap shipmentBySupplierMap;
private StoredSortedMap supplierByCityMap;
public SampleViews(SampleDatabase db) {
ClassCatalog catalog = db.getClassCatalog();
SerialBinding partKeyBinding =
new SerialBinding(catalog, PartKey.class);
EntityBinding partDataBinding =
new PartBinding(catalog, PartKey.class, PartData.class);
SerialBinding supplierKeyBinding =
new SerialBinding(catalog, SupplierKey.class);
EntityBinding supplierDataBinding =
new SupplierBinding(catalog, SupplierKey.class,
SupplierData.class);
SerialBinding shipmentKeyBinding =
new SerialBinding(catalog, ShipmentKey.class);
EntityBinding shipmentDataBinding =
new ShipmentBinding(catalog, ShipmentKey.class,
ShipmentData.class);
SerialBinding cityKeyBinding =
new SerialBinding(catalog, String.class);
partMap =
new StoredSortedMap(db.getPartDatabase(),
partKeyBinding, partDataBinding, true);
supplierMap =
new StoredSortedMap(db.getSupplierDatabase(),
supplierKeyBinding, supplierDataBinding, true);
shipmentMap =
new StoredSortedMap(db.getShipmentDatabase(),
shipmentKeyBinding, shipmentDataBinding, true);
shipmentByPartMap =
new StoredSortedMap(db.getShipmentByPartDatabase(),
partKeyBinding, shipmentDataBinding, true);
shipmentBySupplierMap =
new StoredSortedMap(db.getShipmentBySupplierDatabase(),
supplierKeyBinding, shipmentDataBinding, true);
supplierByCityMap =
new StoredSortedMap(db.getSupplierByCityDatabase(),
cityKeyBinding, supplierDataBinding, true);
}
public StoredSortedMap getPartMap() {
return partMap;
}
public StoredSortedMap getSupplierMap() {
return supplierMap;
}
public StoredSortedMap 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 StoredSortedMap getShipmentByPartMap() {
return shipmentByPartMap;
}
public StoredSortedMap getShipmentBySupplierMap() {
return shipmentBySupplierMap;
}
public final StoredSortedMap getSupplierByCityMap() {
return supplierByCityMap;
}
private static class PartBinding extends SerialSerialBinding {
private PartBinding(ClassCatalog classCatalog,
Class keyClass,
Class dataClass) {
super(classCatalog, keyClass, dataClass);
}
public Object entryToObject(Object keyInput, Object dataInput) {
PartKey key = (PartKey) keyInput;
PartData data = (PartData) dataInput;
return new Part(key.getNumber(), data.getName(), data.getColor(),
data.getWeight(), data.getCity());
}
public Object objectToKey(Object object) {
Part part = (Part) object;
return new PartKey(part.getNumber());
}
public Object objectToData(Object object) {
Part part = (Part) object;
return new PartData(part.getName(), part.getColor(),
part.getWeight(), part.getCity());
}
}
private static class SupplierBinding extends SerialSerialBinding {
private SupplierBinding(ClassCatalog classCatalog,
Class keyClass,
Class dataClass) {
super(classCatalog, keyClass, dataClass);
}
public Object entryToObject(Object keyInput, Object dataInput) {
SupplierKey key = (SupplierKey) keyInput;
SupplierData data = (SupplierData) dataInput;
return new Supplier(key.getNumber(), data.getName(),
data.getStatus(), data.getCity());
}
public Object objectToKey(Object object) {
Supplier supplier = (Supplier) object;
return new SupplierKey(supplier.getNumber());
}
public Object objectToData(Object object) {
Supplier supplier = (Supplier) object;
return new SupplierData(supplier.getName(), supplier.getStatus(),
supplier.getCity());
}
}
private static class ShipmentBinding extends SerialSerialBinding {
private ShipmentBinding(ClassCatalog classCatalog,
Class keyClass,
Class dataClass) {
super(classCatalog, keyClass, dataClass);
}
public Object entryToObject(Object keyInput, Object dataInput) {
ShipmentKey key = (ShipmentKey) keyInput;
ShipmentData data = (ShipmentData) dataInput;
return new Shipment(key.getPartNumber(), key.getSupplierNumber(),
data.getQuantity());
}
public Object objectToKey(Object object) {
Shipment shipment = (Shipment) object;
return new ShipmentKey(shipment.getPartNumber(),
shipment.getSupplierNumber());
}
public Object objectToData(Object object) {
Shipment shipment = (Shipment) object;
return new ShipmentData(shipment.getQuantity());
}
}
}