package collections.ship.sentity;
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, Part.class);
EntryBinding supplierKeyBinding =
new SupplierKeyBinding();
EntityBinding supplierDataBinding =
new SupplierBinding(catalog, Supplier.class);
EntryBinding shipmentKeyBinding =
new ShipmentKeyBinding();
EntityBinding shipmentDataBinding =
new ShipmentBinding(catalog, Shipment.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();
Part part = (Part) dataInput;
part.setKey(number);
return part;
}
public void objectToKey(Object object, TupleOutput output) {
Part part = (Part) object;
output.writeString(part.getNumber());
}
public Object objectToData(Object object) {
return object;
}
}
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();
Supplier supplier = (Supplier) dataInput;
supplier.setKey(number);
return supplier;
}
public void objectToKey(Object object, TupleOutput output) {
Supplier supplier = (Supplier) object;
output.writeString(supplier.getNumber());
}
public Object objectToData(Object object) {
return object;
}
}
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();
Shipment shipment = (Shipment) dataInput;
shipment.setKey(partNumber, supplierNumber);
return shipment;
}
public void objectToKey(Object object, TupleOutput output) {
Shipment shipment = (Shipment) object;
output.writeString(shipment.getPartNumber());
output.writeString(shipment.getSupplierNumber());
}
public Object objectToData(Object object) {
return object;
}
}
}