KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > entity > SampleViews


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SampleViews.java,v 1.18 2006/10/30 21:13:58 bostic Exp $
7  */

8
9 package collections.ship.entity;
10
11 import com.sleepycat.bind.EntityBinding;
12 import com.sleepycat.bind.serial.ClassCatalog;
13 import com.sleepycat.bind.serial.SerialBinding;
14 import com.sleepycat.bind.serial.SerialSerialBinding;
15 import com.sleepycat.collections.StoredSortedMap;
16 import com.sleepycat.collections.StoredValueSet;
17
18 /**
19  * SampleViews defines the data bindings and collection views for the sample
20  * database.
21  *
22  * @author Mark Hayes
23  */

24 public class SampleViews {
25
26     private StoredSortedMap partMap;
27     private StoredSortedMap supplierMap;
28     private StoredSortedMap shipmentMap;
29     private StoredSortedMap shipmentByPartMap;
30     private StoredSortedMap shipmentBySupplierMap;
31     private StoredSortedMap supplierByCityMap;
32
33     /**
34      * Create the data bindings and collection views.
35      */

36     public SampleViews(SampleDatabase db) {
37
38         // Create the data bindings.
39
// In this sample, EntityBinding classes are used to bind the stored
40
// key/data entry pair to a combined data object. For keys, however,
41
// the stored entry is used directly via a SerialBinding and no
42
// special binding class is needed.
43
//
44
ClassCatalog catalog = db.getClassCatalog();
45         SerialBinding partKeyBinding =
46             new SerialBinding(catalog, PartKey.class);
47         EntityBinding partDataBinding =
48             new PartBinding(catalog, PartKey.class, PartData.class);
49         SerialBinding supplierKeyBinding =
50             new SerialBinding(catalog, SupplierKey.class);
51         EntityBinding supplierDataBinding =
52             new SupplierBinding(catalog, SupplierKey.class,
53                                 SupplierData.class);
54         SerialBinding shipmentKeyBinding =
55             new SerialBinding(catalog, ShipmentKey.class);
56         EntityBinding shipmentDataBinding =
57             new ShipmentBinding(catalog, ShipmentKey.class,
58                                 ShipmentData.class);
59         SerialBinding cityKeyBinding =
60             new SerialBinding(catalog, String JavaDoc.class);
61
62         // Create map views for all stores and indices.
63
// StoredSortedMap is not used since the stores and indices are
64
// ordered by serialized key objects, which do not provide a very
65
// useful ordering.
66
//
67
partMap =
68             new StoredSortedMap(db.getPartDatabase(),
69                                 partKeyBinding, partDataBinding, true);
70         supplierMap =
71             new StoredSortedMap(db.getSupplierDatabase(),
72                                 supplierKeyBinding, supplierDataBinding, true);
73         shipmentMap =
74             new StoredSortedMap(db.getShipmentDatabase(),
75                                 shipmentKeyBinding, shipmentDataBinding, true);
76         shipmentByPartMap =
77             new StoredSortedMap(db.getShipmentByPartDatabase(),
78                                 partKeyBinding, shipmentDataBinding, true);
79         shipmentBySupplierMap =
80             new StoredSortedMap(db.getShipmentBySupplierDatabase(),
81                                 supplierKeyBinding, shipmentDataBinding, true);
82         supplierByCityMap =
83             new StoredSortedMap(db.getSupplierByCityDatabase(),
84                                 cityKeyBinding, supplierDataBinding, true);
85     }
86
87     // The views returned below can be accessed using the java.util.Map or
88
// java.util.Set interfaces, or using the StoredSortedMap and
89
// StoredValueSet classes, which provide additional methods. The entity
90
// sets could be obtained directly from the Map.values() method but
91
// convenience methods are provided here to return them in order to avoid
92
// down-casting elsewhere.
93

94     /**
95      * Return a map view of the part storage container.
96      */

97     public StoredSortedMap getPartMap() {
98
99         return partMap;
100     }
101
102     /**
103      * Return a map view of the supplier storage container.
104      */

105     public StoredSortedMap getSupplierMap() {
106
107         return supplierMap;
108     }
109
110     /**
111      * Return a map view of the shipment storage container.
112      */

113     public StoredSortedMap getShipmentMap() {
114
115         return shipmentMap;
116     }
117
118     /**
119      * Return an entity set view of the part storage container.
120      */

121     public StoredValueSet getPartSet() {
122
123         return (StoredValueSet) partMap.values();
124     }
125
126     /**
127      * Return an entity set view of the supplier storage container.
128      */

129     public StoredValueSet getSupplierSet() {
130
131         return (StoredValueSet) supplierMap.values();
132     }
133
134     /**
135      * Return an entity set view of the shipment storage container.
136      */

137     public StoredValueSet getShipmentSet() {
138
139         return (StoredValueSet) shipmentMap.values();
140     }
141
142     /**
143      * Return a map view of the shipment-by-part index.
144      */

145     public StoredSortedMap getShipmentByPartMap() {
146
147         return shipmentByPartMap;
148     }
149
150     /**
151      * Return a map view of the shipment-by-supplier index.
152      */

153     public StoredSortedMap getShipmentBySupplierMap() {
154
155         return shipmentBySupplierMap;
156     }
157
158     /**
159      * Return a map view of the supplier-by-city index.
160      */

161     public final StoredSortedMap getSupplierByCityMap() {
162
163         return supplierByCityMap;
164     }
165
166     /**
167      * PartBinding is used to bind the stored key/data entry pair for a part
168      * to a combined data object (entity).
169      */

170     private static class PartBinding extends SerialSerialBinding {
171
172         /**
173          * Construct the binding object.
174          */

175         private PartBinding(ClassCatalog classCatalog,
176                             Class JavaDoc keyClass,
177                             Class JavaDoc dataClass) {
178
179             super(classCatalog, keyClass, dataClass);
180         }
181
182         /**
183          * Create the entity by combining the stored key and data.
184          */

185         public Object JavaDoc entryToObject(Object JavaDoc keyInput, Object JavaDoc dataInput) {
186
187             PartKey key = (PartKey) keyInput;
188             PartData data = (PartData) dataInput;
189             return new Part(key.getNumber(), data.getName(), data.getColor(),
190                             data.getWeight(), data.getCity());
191         }
192
193         /**
194          * Create the stored key from the entity.
195          */

196         public Object JavaDoc objectToKey(Object JavaDoc object) {
197
198             Part part = (Part) object;
199             return new PartKey(part.getNumber());
200         }
201
202         /**
203          * Create the stored data from the entity.
204          */

205         public Object JavaDoc objectToData(Object JavaDoc object) {
206
207             Part part = (Part) object;
208             return new PartData(part.getName(), part.getColor(),
209                                  part.getWeight(), part.getCity());
210         }
211     }
212
213     /**
214      * SupplierBinding is used to bind the stored key/data entry pair for a
215      * supplier to a combined data object (entity).
216      */

217     private static class SupplierBinding extends SerialSerialBinding {
218
219         /**
220          * Construct the binding object.
221          */

222         private SupplierBinding(ClassCatalog classCatalog,
223                                 Class JavaDoc keyClass,
224                                 Class JavaDoc dataClass) {
225
226             super(classCatalog, keyClass, dataClass);
227         }
228
229         /**
230          * Create the entity by combining the stored key and data.
231          */

232         public Object JavaDoc entryToObject(Object JavaDoc keyInput, Object JavaDoc dataInput) {
233
234             SupplierKey key = (SupplierKey) keyInput;
235             SupplierData data = (SupplierData) dataInput;
236             return new Supplier(key.getNumber(), data.getName(),
237                                 data.getStatus(), data.getCity());
238         }
239
240         /**
241          * Create the stored key from the entity.
242          */

243         public Object JavaDoc objectToKey(Object JavaDoc object) {
244
245             Supplier supplier = (Supplier) object;
246             return new SupplierKey(supplier.getNumber());
247         }
248
249         /**
250          * Create the stored data from the entity.
251          */

252         public Object JavaDoc objectToData(Object JavaDoc object) {
253
254             Supplier supplier = (Supplier) object;
255             return new SupplierData(supplier.getName(), supplier.getStatus(),
256                                      supplier.getCity());
257         }
258     }
259
260     /**
261      * ShipmentBinding is used to bind the stored key/data entry pair for a
262      * shipment to a combined data object (entity).
263      */

264     private static class ShipmentBinding extends SerialSerialBinding {
265
266         /**
267          * Construct the binding object.
268          */

269         private ShipmentBinding(ClassCatalog classCatalog,
270                                 Class JavaDoc keyClass,
271                                 Class JavaDoc dataClass) {
272
273             super(classCatalog, keyClass, dataClass);
274         }
275
276         /**
277          * Create the entity by combining the stored key and data.
278          */

279         public Object JavaDoc entryToObject(Object JavaDoc keyInput, Object JavaDoc dataInput) {
280
281             ShipmentKey key = (ShipmentKey) keyInput;
282             ShipmentData data = (ShipmentData) dataInput;
283             return new Shipment(key.getPartNumber(), key.getSupplierNumber(),
284                                 data.getQuantity());
285         }
286
287         /**
288          * Create the stored key from the entity.
289          */

290         public Object JavaDoc objectToKey(Object JavaDoc object) {
291
292             Shipment shipment = (Shipment) object;
293             return new ShipmentKey(shipment.getPartNumber(),
294                                    shipment.getSupplierNumber());
295         }
296
297         /**
298          * Create the stored data from the entity.
299          */

300         public Object JavaDoc objectToData(Object JavaDoc object) {
301
302             Shipment shipment = (Shipment) object;
303             return new ShipmentData(shipment.getQuantity());
304         }
305     }
306 }
307
Popular Tags