package collections.ship.marshal;
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;
import com.sleepycat.util.RuntimeExceptionWrapper;
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 MarshalledKeyBinding(PartKey.class);
EntityBinding partDataBinding =
new MarshalledEntityBinding(catalog, Part.class);
EntryBinding supplierKeyBinding =
new MarshalledKeyBinding(SupplierKey.class);
EntityBinding supplierDataBinding =
new MarshalledEntityBinding(catalog, Supplier.class);
EntryBinding shipmentKeyBinding =
new MarshalledKeyBinding(ShipmentKey.class);
EntityBinding shipmentDataBinding =
new MarshalledEntityBinding(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 MarshalledKeyBinding extends TupleBinding {
private Class keyClass;
private MarshalledKeyBinding(Class keyClass) {
if (!MarshalledKey.class.isAssignableFrom(keyClass)) {
throw new IllegalArgumentException(keyClass.toString() +
" does not implement MarshalledKey");
}
this.keyClass = keyClass;
}
public Object entryToObject(TupleInput input) {
try {
MarshalledKey key = (MarshalledKey) keyClass.newInstance();
key.unmarshalKey(input);
return key;
} catch (IllegalAccessException e) {
throw new RuntimeExceptionWrapper(e);
} catch (InstantiationException e) {
throw new RuntimeExceptionWrapper(e);
}
}
public void objectToEntry(Object object, TupleOutput output) {
MarshalledKey key = (MarshalledKey) object;
key.marshalKey(output);
}
}
private static class MarshalledEntityBinding extends TupleSerialBinding {
private MarshalledEntityBinding(ClassCatalog classCatalog,
Class entityClass) {
super(classCatalog, entityClass);
if (!MarshalledEnt.class.isAssignableFrom(entityClass)) {
throw new IllegalArgumentException(entityClass.toString() +
" does not implement MarshalledEnt");
}
}
public Object entryToObject(TupleInput tupleInput, Object javaInput) {
MarshalledEnt entity = (MarshalledEnt) javaInput;
entity.unmarshalPrimaryKey(tupleInput);
return entity;
}
public void objectToKey(Object object, TupleOutput output) {
MarshalledEnt entity = (MarshalledEnt) object;
entity.marshalPrimaryKey(output);
}
public Object objectToData(Object object) {
return object;
}
}
}