001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SampleViews.java,v 1.20.2.2 2008/01/07 15:14:03 cwl Exp $
007: */
008:
009: package collections.ship.tuple;
010:
011: import com.sleepycat.bind.EntityBinding;
012: import com.sleepycat.bind.EntryBinding;
013: import com.sleepycat.bind.serial.ClassCatalog;
014: import com.sleepycat.bind.serial.TupleSerialBinding;
015: import com.sleepycat.bind.tuple.TupleBinding;
016: import com.sleepycat.bind.tuple.TupleInput;
017: import com.sleepycat.bind.tuple.TupleOutput;
018: import com.sleepycat.collections.StoredSortedMap;
019: import com.sleepycat.collections.StoredSortedValueSet;
020:
021: /**
022: * SampleViews defines the data bindings and collection views for the sample
023: * database.
024: *
025: * @author Mark Hayes
026: */
027: public class SampleViews {
028:
029: private StoredSortedMap partMap;
030: private StoredSortedMap supplierMap;
031: private StoredSortedMap shipmentMap;
032: private StoredSortedMap shipmentByPartMap;
033: private StoredSortedMap shipmentBySupplierMap;
034: private StoredSortedMap supplierByCityMap;
035:
036: /**
037: * Create the data bindings and collection views.
038: */
039: public SampleViews(SampleDatabase db) {
040:
041: // Create the data bindings.
042: // In this sample, EntityBinding classes are used to bind the stored
043: // key/data entry pair to a combined data object. For keys, a
044: // one-to-one binding is implemented with EntryBinding classes to bind
045: // the stored tuple entry to a key Object.
046: //
047: ClassCatalog catalog = db.getClassCatalog();
048: EntryBinding partKeyBinding = new PartKeyBinding();
049: EntityBinding partDataBinding = new PartBinding(catalog,
050: PartData.class);
051: EntryBinding supplierKeyBinding = new SupplierKeyBinding();
052: EntityBinding supplierDataBinding = new SupplierBinding(
053: catalog, SupplierData.class);
054: EntryBinding shipmentKeyBinding = new ShipmentKeyBinding();
055: EntityBinding shipmentDataBinding = new ShipmentBinding(
056: catalog, ShipmentData.class);
057: EntryBinding cityKeyBinding = TupleBinding
058: .getPrimitiveBinding(String.class);
059:
060: // Create map views for all stores and indices.
061: // StoredSortedMap is used since the stores and indices are ordered
062: // (they use the DB_BTREE access method).
063: //
064: partMap = new StoredSortedMap(db.getPartDatabase(),
065: partKeyBinding, partDataBinding, true);
066: supplierMap = new StoredSortedMap(db.getSupplierDatabase(),
067: supplierKeyBinding, supplierDataBinding, true);
068: shipmentMap = new StoredSortedMap(db.getShipmentDatabase(),
069: shipmentKeyBinding, shipmentDataBinding, true);
070: shipmentByPartMap = new StoredSortedMap(db
071: .getShipmentByPartDatabase(), partKeyBinding,
072: shipmentDataBinding, true);
073: shipmentBySupplierMap = new StoredSortedMap(db
074: .getShipmentBySupplierDatabase(), supplierKeyBinding,
075: shipmentDataBinding, true);
076: supplierByCityMap = new StoredSortedMap(db
077: .getSupplierByCityDatabase(), cityKeyBinding,
078: supplierDataBinding, true);
079: }
080:
081: // The views returned below can be accessed using the java.util.Map or
082: // java.util.Set interfaces, or using the StoredSortedMap and
083: // StoredValueSet classes, which provide additional methods. The entity
084: // sets could be obtained directly from the Map.values() method but
085: // convenience methods are provided here to return them in order to avoid
086: // down-casting elsewhere.
087:
088: /**
089: * Return a map view of the part storage container.
090: */
091: public StoredSortedMap getPartMap() {
092:
093: return partMap;
094: }
095:
096: /**
097: * Return a map view of the supplier storage container.
098: */
099: public StoredSortedMap getSupplierMap() {
100:
101: return supplierMap;
102: }
103:
104: /**
105: * Return a map view of the shipment storage container.
106: */
107: public StoredSortedMap getShipmentMap() {
108:
109: return shipmentMap;
110: }
111:
112: /**
113: * Return an entity set view of the part storage container.
114: */
115: public StoredSortedValueSet getPartSet() {
116:
117: return (StoredSortedValueSet) partMap.values();
118: }
119:
120: /**
121: * Return an entity set view of the supplier storage container.
122: */
123: public StoredSortedValueSet getSupplierSet() {
124:
125: return (StoredSortedValueSet) supplierMap.values();
126: }
127:
128: /**
129: * Return an entity set view of the shipment storage container.
130: */
131: public StoredSortedValueSet getShipmentSet() {
132:
133: return (StoredSortedValueSet) shipmentMap.values();
134: }
135:
136: /**
137: * Return a map view of the shipment-by-part index.
138: */
139: public StoredSortedMap getShipmentByPartMap() {
140:
141: return shipmentByPartMap;
142: }
143:
144: /**
145: * Return a map view of the shipment-by-supplier index.
146: */
147: public StoredSortedMap getShipmentBySupplierMap() {
148:
149: return shipmentBySupplierMap;
150: }
151:
152: /**
153: * Return a map view of the supplier-by-city index.
154: */
155: public final StoredSortedMap getSupplierByCityMap() {
156:
157: return supplierByCityMap;
158: }
159:
160: /**
161: * PartKeyBinding is used to bind the stored key tuple entry for a part to
162: * a key object representation.
163: */
164: private static class PartKeyBinding extends TupleBinding {
165:
166: /**
167: * Construct the binding object.
168: */
169: private PartKeyBinding() {
170: }
171:
172: /**
173: * Create the key object from the stored key tuple entry.
174: */
175: public Object entryToObject(TupleInput input) {
176:
177: String number = input.readString();
178: return new PartKey(number);
179: }
180:
181: /**
182: * Create the stored key tuple entry from the key object.
183: */
184: public void objectToEntry(Object object, TupleOutput output) {
185:
186: PartKey key = (PartKey) object;
187: output.writeString(key.getNumber());
188: }
189: }
190:
191: /**
192: * PartBinding is used to bind the stored key/data entry pair for a part
193: * to a combined data object (entity).
194: */
195: private static class PartBinding extends TupleSerialBinding {
196:
197: /**
198: * Construct the binding object.
199: */
200: private PartBinding(ClassCatalog classCatalog, Class dataClass) {
201:
202: super (classCatalog, dataClass);
203: }
204:
205: /**
206: * Create the entity by combining the stored key and data.
207: */
208: public Object entryToObject(TupleInput keyInput,
209: Object dataInput) {
210:
211: String number = keyInput.readString();
212: PartData data = (PartData) dataInput;
213: return new Part(number, data.getName(), data.getColor(),
214: data.getWeight(), data.getCity());
215: }
216:
217: /**
218: * Create the stored key from the entity.
219: */
220: public void objectToKey(Object object, TupleOutput output) {
221:
222: Part part = (Part) object;
223: output.writeString(part.getNumber());
224: }
225:
226: /**
227: * Create the stored data from the entity.
228: */
229: public Object objectToData(Object object) {
230:
231: Part part = (Part) object;
232: return new PartData(part.getName(), part.getColor(), part
233: .getWeight(), part.getCity());
234: }
235: }
236:
237: /**
238: * SupplierKeyBinding is used to bind the stored key tuple entry for a
239: * supplier to a key object representation.
240: */
241: private static class SupplierKeyBinding extends TupleBinding {
242:
243: /**
244: * Construct the binding object.
245: */
246: private SupplierKeyBinding() {
247: }
248:
249: /**
250: * Create the key object from the stored key tuple entry.
251: */
252: public Object entryToObject(TupleInput input) {
253:
254: String number = input.readString();
255: return new SupplierKey(number);
256: }
257:
258: /**
259: * Create the stored key tuple entry from the key object.
260: */
261: public void objectToEntry(Object object, TupleOutput output) {
262:
263: SupplierKey key = (SupplierKey) object;
264: output.writeString(key.getNumber());
265: }
266: }
267:
268: /**
269: * SupplierBinding is used to bind the stored key/data entry pair for a
270: * supplier to a combined data object (entity).
271: */
272: private static class SupplierBinding extends TupleSerialBinding {
273:
274: /**
275: * Construct the binding object.
276: */
277: private SupplierBinding(ClassCatalog classCatalog,
278: Class dataClass) {
279:
280: super (classCatalog, dataClass);
281: }
282:
283: /**
284: * Create the entity by combining the stored key and data.
285: */
286: public Object entryToObject(TupleInput keyInput,
287: Object dataInput) {
288:
289: String number = keyInput.readString();
290: SupplierData data = (SupplierData) dataInput;
291: return new Supplier(number, data.getName(), data
292: .getStatus(), data.getCity());
293: }
294:
295: /**
296: * Create the stored key from the entity.
297: */
298: public void objectToKey(Object object, TupleOutput output) {
299:
300: Supplier supplier = (Supplier) object;
301: output.writeString(supplier.getNumber());
302: }
303:
304: /**
305: * Create the stored data from the entity.
306: */
307: public Object objectToData(Object object) {
308:
309: Supplier supplier = (Supplier) object;
310: return new SupplierData(supplier.getName(), supplier
311: .getStatus(), supplier.getCity());
312: }
313: }
314:
315: /**
316: * ShipmentKeyBinding is used to bind the stored key tuple entry for a
317: * shipment to a key object representation.
318: */
319: private static class ShipmentKeyBinding extends TupleBinding {
320:
321: /**
322: * Construct the binding object.
323: */
324: private ShipmentKeyBinding() {
325: }
326:
327: /**
328: * Create the key object from the stored key tuple entry.
329: */
330: public Object entryToObject(TupleInput input) {
331:
332: String partNumber = input.readString();
333: String supplierNumber = input.readString();
334: return new ShipmentKey(partNumber, supplierNumber);
335: }
336:
337: /**
338: * Create the stored key tuple entry from the key object.
339: */
340: public void objectToEntry(Object object, TupleOutput output) {
341:
342: ShipmentKey key = (ShipmentKey) object;
343: output.writeString(key.getPartNumber());
344: output.writeString(key.getSupplierNumber());
345: }
346: }
347:
348: /**
349: * ShipmentBinding is used to bind the stored key/data entry pair for a
350: * shipment to a combined data object (entity).
351: */
352: private static class ShipmentBinding extends TupleSerialBinding {
353:
354: /**
355: * Construct the binding object.
356: */
357: private ShipmentBinding(ClassCatalog classCatalog,
358: Class dataClass) {
359:
360: super (classCatalog, dataClass);
361: }
362:
363: /**
364: * Create the entity by combining the stored key and data.
365: */
366: public Object entryToObject(TupleInput keyInput,
367: Object dataInput) {
368:
369: String partNumber = keyInput.readString();
370: String supplierNumber = keyInput.readString();
371: ShipmentData data = (ShipmentData) dataInput;
372: return new Shipment(partNumber, supplierNumber, data
373: .getQuantity());
374: }
375:
376: /**
377: * Create the stored key from the entity.
378: */
379: public void objectToKey(Object object, TupleOutput output) {
380:
381: Shipment shipment = (Shipment) object;
382: output.writeString(shipment.getPartNumber());
383: output.writeString(shipment.getSupplierNumber());
384: }
385:
386: /**
387: * Create the stored data from the entity.
388: */
389: public Object objectToData(Object object) {
390:
391: Shipment shipment = (Shipment) object;
392: return new ShipmentData(shipment.getQuantity());
393: }
394: }
395: }
|