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:02 cwl Exp $
007: */
008:
009: package collections.ship.sentity;
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; a "tricky" binding
044: // that uses transient fields is used--see PartBinding, etc, for
045: // details. For keys, a one-to-one binding is implemented with
046: // EntryBinding classes to bind the stored tuple entry to a key Object.
047: //
048: ClassCatalog catalog = db.getClassCatalog();
049: EntryBinding partKeyBinding = new PartKeyBinding();
050: EntityBinding partDataBinding = new PartBinding(catalog,
051: Part.class);
052: EntryBinding supplierKeyBinding = new SupplierKeyBinding();
053: EntityBinding supplierDataBinding = new SupplierBinding(
054: catalog, Supplier.class);
055: EntryBinding shipmentKeyBinding = new ShipmentKeyBinding();
056: EntityBinding shipmentDataBinding = new ShipmentBinding(
057: catalog, Shipment.class);
058: EntryBinding cityKeyBinding = TupleBinding
059: .getPrimitiveBinding(String.class);
060:
061: // Create map views for all stores and indices.
062: // StoredSortedMap is used since the stores and indices are ordered
063: // (they use the DB_BTREE access method).
064: //
065: partMap = new StoredSortedMap(db.getPartDatabase(),
066: partKeyBinding, partDataBinding, true);
067: supplierMap = new StoredSortedMap(db.getSupplierDatabase(),
068: supplierKeyBinding, supplierDataBinding, true);
069: shipmentMap = new StoredSortedMap(db.getShipmentDatabase(),
070: shipmentKeyBinding, shipmentDataBinding, true);
071: shipmentByPartMap = new StoredSortedMap(db
072: .getShipmentByPartDatabase(), partKeyBinding,
073: shipmentDataBinding, true);
074: shipmentBySupplierMap = new StoredSortedMap(db
075: .getShipmentBySupplierDatabase(), supplierKeyBinding,
076: shipmentDataBinding, true);
077: supplierByCityMap = new StoredSortedMap(db
078: .getSupplierByCityDatabase(), cityKeyBinding,
079: supplierDataBinding, true);
080: }
081:
082: // The views returned below can be accessed using the java.util.Map or
083: // java.util.Set interfaces, or using the StoredSortedMap and
084: // StoredValueSet classes, which provide additional methods. The entity
085: // sets could be obtained directly from the Map.values() method but
086: // convenience methods are provided here to return them in order to avoid
087: // down-casting elsewhere.
088:
089: /**
090: * Return a map view of the part storage container.
091: */
092: public StoredSortedMap getPartMap() {
093:
094: return partMap;
095: }
096:
097: /**
098: * Return a map view of the supplier storage container.
099: */
100: public StoredSortedMap getSupplierMap() {
101:
102: return supplierMap;
103: }
104:
105: /**
106: * Return a map view of the shipment storage container.
107: */
108: public StoredSortedMap getShipmentMap() {
109:
110: return shipmentMap;
111: }
112:
113: /**
114: * Return an entity set view of the part storage container.
115: */
116: public StoredSortedValueSet getPartSet() {
117:
118: return (StoredSortedValueSet) partMap.values();
119: }
120:
121: /**
122: * Return an entity set view of the supplier storage container.
123: */
124: public StoredSortedValueSet getSupplierSet() {
125:
126: return (StoredSortedValueSet) supplierMap.values();
127: }
128:
129: /**
130: * Return an entity set view of the shipment storage container.
131: */
132: public StoredSortedValueSet getShipmentSet() {
133:
134: return (StoredSortedValueSet) shipmentMap.values();
135: }
136:
137: /**
138: * Return a map view of the shipment-by-part index.
139: */
140: public StoredSortedMap getShipmentByPartMap() {
141:
142: return shipmentByPartMap;
143: }
144:
145: /**
146: * Return a map view of the shipment-by-supplier index.
147: */
148: public StoredSortedMap getShipmentBySupplierMap() {
149:
150: return shipmentBySupplierMap;
151: }
152:
153: /**
154: * Return a map view of the supplier-by-city index.
155: */
156: public final StoredSortedMap getSupplierByCityMap() {
157:
158: return supplierByCityMap;
159: }
160:
161: /**
162: * PartKeyBinding is used to bind the stored key tuple entry for a part to
163: * a key object representation.
164: */
165: private static class PartKeyBinding extends TupleBinding {
166:
167: /**
168: * Construct the binding object.
169: */
170: private PartKeyBinding() {
171: }
172:
173: /**
174: * Create the key object from the stored key tuple entry.
175: */
176: public Object entryToObject(TupleInput input) {
177:
178: String number = input.readString();
179: return new PartKey(number);
180: }
181:
182: /**
183: * Create the stored key tuple entry from the key object.
184: */
185: public void objectToEntry(Object object, TupleOutput output) {
186:
187: PartKey key = (PartKey) object;
188: output.writeString(key.getNumber());
189: }
190: }
191:
192: /**
193: * PartBinding is used to bind the stored key/data entry pair for a part
194: * to a combined data object (entity).
195: *
196: * <p> The binding is "tricky" in that it uses the Part class for both the
197: * stored data entry and the combined entity object. To do this, Part's
198: * key field(s) are transient and are set by the binding after the data
199: * object has been deserialized. This avoids the use of a PartData class
200: * completely. </p>
201: */
202: private static class PartBinding extends TupleSerialBinding {
203:
204: /**
205: * Construct the binding object.
206: */
207: private PartBinding(ClassCatalog classCatalog, Class dataClass) {
208:
209: super (classCatalog, dataClass);
210: }
211:
212: /**
213: * Create the entity by combining the stored key and data.
214: * This "tricky" binding returns the stored data as the entity, but
215: * first it sets the transient key fields from the stored key.
216: */
217: public Object entryToObject(TupleInput keyInput,
218: Object dataInput) {
219:
220: String number = keyInput.readString();
221: Part part = (Part) dataInput;
222: part.setKey(number);
223: return part;
224: }
225:
226: /**
227: * Create the stored key from the entity.
228: */
229: public void objectToKey(Object object, TupleOutput output) {
230:
231: Part part = (Part) object;
232: output.writeString(part.getNumber());
233: }
234:
235: /**
236: * Return the entity as the stored data. There is nothing to do here
237: * since the entity's key fields are transient.
238: */
239: public Object objectToData(Object object) {
240:
241: return object;
242: }
243: }
244:
245: /**
246: * SupplierKeyBinding is used to bind the stored key tuple entry for a
247: * supplier to a key object representation.
248: */
249: private static class SupplierKeyBinding extends TupleBinding {
250:
251: /**
252: * Construct the binding object.
253: */
254: private SupplierKeyBinding() {
255: }
256:
257: /**
258: * Create the key object from the stored key tuple entry.
259: */
260: public Object entryToObject(TupleInput input) {
261:
262: String number = input.readString();
263: return new SupplierKey(number);
264: }
265:
266: /**
267: * Create the stored key tuple entry from the key object.
268: */
269: public void objectToEntry(Object object, TupleOutput output) {
270:
271: SupplierKey key = (SupplierKey) object;
272: output.writeString(key.getNumber());
273: }
274: }
275:
276: /**
277: * SupplierBinding is used to bind the stored key/data entry pair for a
278: * supplier to a combined data object (entity).
279: *
280: * <p> The binding is "tricky" in that it uses the Supplier class for both
281: * the stored data entry and the combined entity object. To do this,
282: * Supplier's key field(s) are transient and are set by the binding after
283: * the data object has been deserialized. This avoids the use of a
284: * SupplierData class completely. </p>
285: */
286: private static class SupplierBinding extends TupleSerialBinding {
287:
288: /**
289: * Construct the binding object.
290: */
291: private SupplierBinding(ClassCatalog classCatalog,
292: Class dataClass) {
293:
294: super (classCatalog, dataClass);
295: }
296:
297: /**
298: * Create the entity by combining the stored key and data.
299: * This "tricky" binding returns the stored data as the entity, but
300: * first it sets the transient key fields from the stored key.
301: */
302: public Object entryToObject(TupleInput keyInput,
303: Object dataInput) {
304:
305: String number = keyInput.readString();
306: Supplier supplier = (Supplier) dataInput;
307: supplier.setKey(number);
308: return supplier;
309: }
310:
311: /**
312: * Create the stored key from the entity.
313: */
314: public void objectToKey(Object object, TupleOutput output) {
315:
316: Supplier supplier = (Supplier) object;
317: output.writeString(supplier.getNumber());
318: }
319:
320: /**
321: * Return the entity as the stored data. There is nothing to do here
322: * since the entity's key fields are transient.
323: */
324: public Object objectToData(Object object) {
325:
326: return object;
327: }
328: }
329:
330: /**
331: * ShipmentKeyBinding is used to bind the stored key tuple entry for a
332: * shipment to a key object representation.
333: */
334: private static class ShipmentKeyBinding extends TupleBinding {
335:
336: /**
337: * Construct the binding object.
338: */
339: private ShipmentKeyBinding() {
340: }
341:
342: /**
343: * Create the key object from the stored key tuple entry.
344: */
345: public Object entryToObject(TupleInput input) {
346:
347: String partNumber = input.readString();
348: String supplierNumber = input.readString();
349: return new ShipmentKey(partNumber, supplierNumber);
350: }
351:
352: /**
353: * Create the stored key tuple entry from the key object.
354: */
355: public void objectToEntry(Object object, TupleOutput output) {
356:
357: ShipmentKey key = (ShipmentKey) object;
358: output.writeString(key.getPartNumber());
359: output.writeString(key.getSupplierNumber());
360: }
361: }
362:
363: /**
364: * ShipmentBinding is used to bind the stored key/data entry pair for a
365: * shipment to a combined data object (entity).
366: *
367: * <p> The binding is "tricky" in that it uses the Shipment class for both
368: * the stored data entry and the combined entity object. To do this,
369: * Shipment's key field(s) are transient and are set by the binding after
370: * the data object has been deserialized. This avoids the use of a
371: * ShipmentData class completely. </p>
372: */
373: private static class ShipmentBinding extends TupleSerialBinding {
374:
375: /**
376: * Construct the binding object.
377: */
378: private ShipmentBinding(ClassCatalog classCatalog,
379: Class dataClass) {
380:
381: super (classCatalog, dataClass);
382: }
383:
384: /**
385: * Create the entity by combining the stored key and data.
386: * This "tricky" binding returns the stored data as the entity, but
387: * first it sets the transient key fields from the stored key.
388: */
389: public Object entryToObject(TupleInput keyInput,
390: Object dataInput) {
391:
392: String partNumber = keyInput.readString();
393: String supplierNumber = keyInput.readString();
394: Shipment shipment = (Shipment) dataInput;
395: shipment.setKey(partNumber, supplierNumber);
396: return shipment;
397: }
398:
399: /**
400: * Create the stored key from the entity.
401: */
402: public void objectToKey(Object object, TupleOutput output) {
403:
404: Shipment shipment = (Shipment) object;
405: output.writeString(shipment.getPartNumber());
406: output.writeString(shipment.getSupplierNumber());
407: }
408:
409: /**
410: * Return the entity as the stored data. There is nothing to do here
411: * since the entity's key fields are transient.
412: */
413: public Object objectToData(Object object) {
414:
415: return object;
416: }
417: }
418: }
|