package collections.ship.tuple;
import com.sleepycat.bind.EntityBinding;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.ClassCatalog;
import com.sleepycat.bind.serial.TupleSerialBinding;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.collections.StoredSortedMap;
import com.sleepycat.collections.StoredSortedValueSet;
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();
EntryBinding partKeyBinding =
new PartKeyBinding();
EntityBinding partDataBinding =
new PartBinding(catalog, PartData.class);
EntryBinding supplierKeyBinding =
new SupplierKeyBinding();
EntityBinding supplierDataBinding =
new SupplierBinding(catalog, SupplierData.class);
EntryBinding shipmentKeyBinding =
new ShipmentKeyBinding();
EntityBinding shipmentDataBinding =
new ShipmentBinding(catalog, ShipmentData.class);
EntryBinding cityKeyBinding =
TupleBinding.getPrimitiveBinding(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 StoredSortedValueSet getPartSet() {
return (StoredSortedValueSet) partMap.values();
}
public StoredSortedValueSet getSupplierSet() {
return (StoredSortedValueSet) supplierMap.values();
}
public StoredSortedValueSet getShipmentSet() {
return (StoredSortedValueSet) shipmentMap.values();
}
public StoredSortedMap getShipmentByPartMap() {
return shipmentByPartMap;
}
public StoredSortedMap getShipmentBySupplierMap() {
return shipmentBySupplierMap;
}
public final StoredSortedMap getSupplierByCityMap() {
return supplierByCityMap;
}
private static class PartKeyBinding extends TupleBinding {
private PartKeyBinding() {
}
public Object entryToObject(TupleInput input) {
String number = input.readString();
return new PartKey(number);
}
public void objectToEntry(Object object, TupleOutput output) {
PartKey key = (PartKey) object;
output.writeString(key.getNumber());
}
}
private static class PartBinding extends TupleSerialBinding {
private PartBinding(ClassCatalog classCatalog, Class dataClass) {
super(classCatalog, dataClass);
}
public Object entryToObject(TupleInput keyInput, Object dataInput) {
String number = keyInput.readString();
PartData data = (PartData) dataInput;
return new Part(number, data.getName(), data.getColor(),
data.getWeight(), data.getCity());
}
public void objectToKey(Object object, TupleOutput output) {
Part part = (Part) object;
output.writeString(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 SupplierKeyBinding extends TupleBinding {
private SupplierKeyBinding() {
}
public Object entryToObject(TupleInput input) {
String number = input.readString();
return new SupplierKey(number);
}
public void objectToEntry(Object object, TupleOutput output) {
SupplierKey key = (SupplierKey) object;
output.writeString(key.getNumber());
}
}
private static class SupplierBinding extends TupleSerialBinding {
private SupplierBinding(ClassCatalog classCatalog, Class dataClass) {
super(classCatalog, dataClass);
}
public Object entryToObject(TupleInput keyInput, Object dataInput) {
String number = keyInput.readString();
SupplierData data = (SupplierData) dataInput;
return new Supplier(number, data.getName(),
data.getStatus(), data.getCity());
}
public void objectToKey(Object object, TupleOutput output) {
Supplier supplier = (Supplier) object;
output.writeString(supplier.getNumber());
}
public Object objectToData(Object object) {
Supplier supplier = (Supplier) object;
return new SupplierData(supplier.getName(), supplier.getStatus(),
supplier.getCity());
}
}
private static class ShipmentKeyBinding extends TupleBinding {
private ShipmentKeyBinding() {
}
public Object entryToObject(TupleInput input) {
String partNumber = input.readString();
String supplierNumber = input.readString();
return new ShipmentKey(partNumber, supplierNumber);
}
public void objectToEntry(Object object, TupleOutput output) {
ShipmentKey key = (ShipmentKey) object;
output.writeString(key.getPartNumber());
output.writeString(key.getSupplierNumber());
}
}
private static class ShipmentBinding extends TupleSerialBinding {
private ShipmentBinding(ClassCatalog classCatalog, Class dataClass) {
super(classCatalog, dataClass);
}
public Object entryToObject(TupleInput keyInput, Object dataInput) {
String partNumber = keyInput.readString();
String supplierNumber = keyInput.readString();
ShipmentData data = (ShipmentData) dataInput;
return new Shipment(partNumber, supplierNumber,
data.getQuantity());
}
public void objectToKey(Object object, TupleOutput output) {
Shipment shipment = (Shipment) object;
output.writeString(shipment.getPartNumber());
output.writeString(shipment.getSupplierNumber());
}
public Object objectToData(Object object) {
Shipment shipment = (Shipment) object;
return new ShipmentData(shipment.getQuantity());
}
}
}