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.18.2.2 2008/01/07 15:14:00 cwl Exp $
007: */
008:
009: package collections.ship.entity;
010:
011: import com.sleepycat.bind.EntityBinding;
012: import com.sleepycat.bind.serial.ClassCatalog;
013: import com.sleepycat.bind.serial.SerialBinding;
014: import com.sleepycat.bind.serial.SerialSerialBinding;
015: import com.sleepycat.collections.StoredSortedMap;
016: import com.sleepycat.collections.StoredValueSet;
017:
018: /**
019: * SampleViews defines the data bindings and collection views for the sample
020: * database.
021: *
022: * @author Mark Hayes
023: */
024: public class SampleViews {
025:
026: private StoredSortedMap partMap;
027: private StoredSortedMap supplierMap;
028: private StoredSortedMap shipmentMap;
029: private StoredSortedMap shipmentByPartMap;
030: private StoredSortedMap shipmentBySupplierMap;
031: private StoredSortedMap supplierByCityMap;
032:
033: /**
034: * Create the data bindings and collection views.
035: */
036: public SampleViews(SampleDatabase db) {
037:
038: // Create the data bindings.
039: // In this sample, EntityBinding classes are used to bind the stored
040: // key/data entry pair to a combined data object. For keys, however,
041: // the stored entry is used directly via a SerialBinding and no
042: // special binding class is needed.
043: //
044: ClassCatalog catalog = db.getClassCatalog();
045: SerialBinding partKeyBinding = new SerialBinding(catalog,
046: PartKey.class);
047: EntityBinding partDataBinding = new PartBinding(catalog,
048: PartKey.class, PartData.class);
049: SerialBinding supplierKeyBinding = new SerialBinding(catalog,
050: SupplierKey.class);
051: EntityBinding supplierDataBinding = new SupplierBinding(
052: catalog, SupplierKey.class, SupplierData.class);
053: SerialBinding shipmentKeyBinding = new SerialBinding(catalog,
054: ShipmentKey.class);
055: EntityBinding shipmentDataBinding = new ShipmentBinding(
056: catalog, ShipmentKey.class, ShipmentData.class);
057: SerialBinding cityKeyBinding = new SerialBinding(catalog,
058: String.class);
059:
060: // Create map views for all stores and indices.
061: // StoredSortedMap is not used since the stores and indices are
062: // ordered by serialized key objects, which do not provide a very
063: // useful ordering.
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 StoredValueSet getPartSet() {
117:
118: return (StoredValueSet) partMap.values();
119: }
120:
121: /**
122: * Return an entity set view of the supplier storage container.
123: */
124: public StoredValueSet getSupplierSet() {
125:
126: return (StoredValueSet) supplierMap.values();
127: }
128:
129: /**
130: * Return an entity set view of the shipment storage container.
131: */
132: public StoredValueSet getShipmentSet() {
133:
134: return (StoredValueSet) 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: * PartBinding is used to bind the stored key/data entry pair for a part
163: * to a combined data object (entity).
164: */
165: private static class PartBinding extends SerialSerialBinding {
166:
167: /**
168: * Construct the binding object.
169: */
170: private PartBinding(ClassCatalog classCatalog, Class keyClass,
171: Class dataClass) {
172:
173: super (classCatalog, keyClass, dataClass);
174: }
175:
176: /**
177: * Create the entity by combining the stored key and data.
178: */
179: public Object entryToObject(Object keyInput, Object dataInput) {
180:
181: PartKey key = (PartKey) keyInput;
182: PartData data = (PartData) dataInput;
183: return new Part(key.getNumber(), data.getName(), data
184: .getColor(), data.getWeight(), data.getCity());
185: }
186:
187: /**
188: * Create the stored key from the entity.
189: */
190: public Object objectToKey(Object object) {
191:
192: Part part = (Part) object;
193: return new PartKey(part.getNumber());
194: }
195:
196: /**
197: * Create the stored data from the entity.
198: */
199: public Object objectToData(Object object) {
200:
201: Part part = (Part) object;
202: return new PartData(part.getName(), part.getColor(), part
203: .getWeight(), part.getCity());
204: }
205: }
206:
207: /**
208: * SupplierBinding is used to bind the stored key/data entry pair for a
209: * supplier to a combined data object (entity).
210: */
211: private static class SupplierBinding extends SerialSerialBinding {
212:
213: /**
214: * Construct the binding object.
215: */
216: private SupplierBinding(ClassCatalog classCatalog,
217: Class keyClass, Class dataClass) {
218:
219: super (classCatalog, keyClass, dataClass);
220: }
221:
222: /**
223: * Create the entity by combining the stored key and data.
224: */
225: public Object entryToObject(Object keyInput, Object dataInput) {
226:
227: SupplierKey key = (SupplierKey) keyInput;
228: SupplierData data = (SupplierData) dataInput;
229: return new Supplier(key.getNumber(), data.getName(), data
230: .getStatus(), data.getCity());
231: }
232:
233: /**
234: * Create the stored key from the entity.
235: */
236: public Object objectToKey(Object object) {
237:
238: Supplier supplier = (Supplier) object;
239: return new SupplierKey(supplier.getNumber());
240: }
241:
242: /**
243: * Create the stored data from the entity.
244: */
245: public Object objectToData(Object object) {
246:
247: Supplier supplier = (Supplier) object;
248: return new SupplierData(supplier.getName(), supplier
249: .getStatus(), supplier.getCity());
250: }
251: }
252:
253: /**
254: * ShipmentBinding is used to bind the stored key/data entry pair for a
255: * shipment to a combined data object (entity).
256: */
257: private static class ShipmentBinding extends SerialSerialBinding {
258:
259: /**
260: * Construct the binding object.
261: */
262: private ShipmentBinding(ClassCatalog classCatalog,
263: Class keyClass, Class dataClass) {
264:
265: super (classCatalog, keyClass, dataClass);
266: }
267:
268: /**
269: * Create the entity by combining the stored key and data.
270: */
271: public Object entryToObject(Object keyInput, Object dataInput) {
272:
273: ShipmentKey key = (ShipmentKey) keyInput;
274: ShipmentData data = (ShipmentData) dataInput;
275: return new Shipment(key.getPartNumber(), key
276: .getSupplierNumber(), data.getQuantity());
277: }
278:
279: /**
280: * Create the stored key from the entity.
281: */
282: public Object objectToKey(Object object) {
283:
284: Shipment shipment = (Shipment) object;
285: return new ShipmentKey(shipment.getPartNumber(), shipment
286: .getSupplierNumber());
287: }
288:
289: /**
290: * Create the stored data from the entity.
291: */
292: public Object objectToData(Object object) {
293:
294: Shipment shipment = (Shipment) object;
295: return new ShipmentData(shipment.getQuantity());
296: }
297: }
298: }
|