00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package collections.ship.marshal;
00011
00012 import com.sleepycat.bind.EntityBinding;
00013 import com.sleepycat.bind.EntryBinding;
00014 import com.sleepycat.bind.serial.ClassCatalog;
00015 import com.sleepycat.bind.serial.TupleSerialBinding;
00016 import com.sleepycat.bind.tuple.TupleBinding;
00017 import com.sleepycat.bind.tuple.TupleInput;
00018 import com.sleepycat.bind.tuple.TupleOutput;
00019 import com.sleepycat.collections.StoredSortedMap;
00020 import com.sleepycat.collections.StoredSortedValueSet;
00021 import com.sleepycat.util.RuntimeExceptionWrapper;
00022
00029 public class SampleViews {
00030
00031 private StoredSortedMap partMap;
00032 private StoredSortedMap supplierMap;
00033 private StoredSortedMap shipmentMap;
00034 private StoredSortedMap shipmentByPartMap;
00035 private StoredSortedMap shipmentBySupplierMap;
00036 private StoredSortedMap supplierByCityMap;
00037
00041 public SampleViews(SampleDatabase db) {
00042
00043
00044
00045
00046
00047
00048
00049
00050 ClassCatalog catalog = db.getClassCatalog();
00051 EntryBinding partKeyBinding =
00052 new MarshalledKeyBinding(PartKey.class);
00053 EntityBinding partDataBinding =
00054 new MarshalledEntityBinding(catalog, Part.class);
00055 EntryBinding supplierKeyBinding =
00056 new MarshalledKeyBinding(SupplierKey.class);
00057 EntityBinding supplierDataBinding =
00058 new MarshalledEntityBinding(catalog, Supplier.class);
00059 EntryBinding shipmentKeyBinding =
00060 new MarshalledKeyBinding(ShipmentKey.class);
00061 EntityBinding shipmentDataBinding =
00062 new MarshalledEntityBinding(catalog, Shipment.class);
00063 EntryBinding cityKeyBinding =
00064 TupleBinding.getPrimitiveBinding(String.class);
00065
00066
00067
00068
00069
00070 partMap =
00071 new StoredSortedMap(db.getPartDatabase(),
00072 partKeyBinding, partDataBinding, true);
00073 supplierMap =
00074 new StoredSortedMap(db.getSupplierDatabase(),
00075 supplierKeyBinding, supplierDataBinding, true);
00076 shipmentMap =
00077 new StoredSortedMap(db.getShipmentDatabase(),
00078 shipmentKeyBinding, shipmentDataBinding, true);
00079 shipmentByPartMap =
00080 new StoredSortedMap(db.getShipmentByPartDatabase(),
00081 partKeyBinding, shipmentDataBinding, true);
00082 shipmentBySupplierMap =
00083 new StoredSortedMap(db.getShipmentBySupplierDatabase(),
00084 supplierKeyBinding, shipmentDataBinding, true);
00085 supplierByCityMap =
00086 new StoredSortedMap(db.getSupplierByCityDatabase(),
00087 cityKeyBinding, supplierDataBinding, true);
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00100 public StoredSortedMap getPartMap() {
00101
00102 return partMap;
00103 }
00104
00108 public StoredSortedMap getSupplierMap() {
00109
00110 return supplierMap;
00111 }
00112
00116 public StoredSortedMap getShipmentMap() {
00117
00118 return shipmentMap;
00119 }
00120
00124 public StoredSortedValueSet getPartSet() {
00125
00126 return (StoredSortedValueSet) partMap.values();
00127 }
00128
00132 public StoredSortedValueSet getSupplierSet() {
00133
00134 return (StoredSortedValueSet) supplierMap.values();
00135 }
00136
00140 public StoredSortedValueSet getShipmentSet() {
00141
00142 return (StoredSortedValueSet) shipmentMap.values();
00143 }
00144
00148 public StoredSortedMap getShipmentByPartMap() {
00149
00150 return shipmentByPartMap;
00151 }
00152
00156 public StoredSortedMap getShipmentBySupplierMap() {
00157
00158 return shipmentBySupplierMap;
00159 }
00160
00164 public final StoredSortedMap getSupplierByCityMap() {
00165
00166 return supplierByCityMap;
00167 }
00168
00174 private static class MarshalledKeyBinding extends TupleBinding {
00175
00176 private Class keyClass;
00177
00181 private MarshalledKeyBinding(Class keyClass) {
00182
00183
00184
00185 if (!MarshalledKey.class.isAssignableFrom(keyClass)) {
00186 throw new IllegalArgumentException(keyClass.toString() +
00187 " does not implement MarshalledKey");
00188 }
00189 this.keyClass = keyClass;
00190 }
00191
00195 public Object entryToObject(TupleInput input) {
00196
00197 try {
00198 MarshalledKey key = (MarshalledKey) keyClass.newInstance();
00199 key.unmarshalKey(input);
00200 return key;
00201 } catch (IllegalAccessException e) {
00202 throw new RuntimeExceptionWrapper(e);
00203 } catch (InstantiationException e) {
00204 throw new RuntimeExceptionWrapper(e);
00205 }
00206 }
00207
00211 public void objectToEntry(Object object, TupleOutput output) {
00212
00213 MarshalledKey key = (MarshalledKey) object;
00214 key.marshalKey(output);
00215 }
00216 }
00217
00229 private static class MarshalledEntityBinding extends TupleSerialBinding {
00230
00234 private MarshalledEntityBinding(ClassCatalog classCatalog,
00235 Class entityClass) {
00236
00237 super(classCatalog, entityClass);
00238
00239
00240
00241 if (!MarshalledEnt.class.isAssignableFrom(entityClass)) {
00242 throw new IllegalArgumentException(entityClass.toString() +
00243 " does not implement MarshalledEnt");
00244 }
00245 }
00246
00252 public Object entryToObject(TupleInput tupleInput, Object javaInput) {
00253
00254 MarshalledEnt entity = (MarshalledEnt) javaInput;
00255 entity.unmarshalPrimaryKey(tupleInput);
00256 return entity;
00257 }
00258
00262 public void objectToKey(Object object, TupleOutput output) {
00263
00264 MarshalledEnt entity = (MarshalledEnt) object;
00265 entity.marshalPrimaryKey(output);
00266 }
00267
00272 public Object objectToData(Object object) {
00273
00274 return object;
00275 }
00276 }
00277 }