1 /*-
2  * Copyright (c) 2002, 2020 Oracle and/or its affiliates.  All rights reserved.
3  *
4  * See the file EXAMPLES-LICENSE for license information.
5  *
6  */
7 
8 package collections.ship.entity;
9 
10 import com.sleepycat.bind.EntityBinding;
11 import com.sleepycat.bind.serial.ClassCatalog;
12 import com.sleepycat.bind.serial.SerialBinding;
13 import com.sleepycat.bind.serial.SerialSerialBinding;
14 import com.sleepycat.collections.StoredSortedMap;
15 import com.sleepycat.collections.StoredValueSet;
16 
17 /**
18  * SampleViews defines the data bindings and collection views for the sample
19  * database.
20  *
21  * @author Mark Hayes
22  */
23 public class SampleViews {
24 
25     private final StoredSortedMap<PartKey, Part> partMap;
26     private final StoredSortedMap<SupplierKey, Supplier> supplierMap;
27     private final StoredSortedMap<ShipmentKey, Shipment> shipmentMap;
28     private final StoredSortedMap<PartKey, Shipment> shipmentByPartMap;
29     private final StoredSortedMap<SupplierKey, Shipment> shipmentBySupplierMap;
30     private final StoredSortedMap<String, Supplier> supplierByCityMap;
31 
32     /**
33      * Create the data bindings and collection views.
34      * @param db
35      */
SampleViews(SampleDatabase db)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<PartKey> partKeyBinding =
46             new SerialBinding<>(catalog, PartKey.class);
47         EntityBinding<Part> partDataBinding =
48             new PartBinding(catalog, PartKey.class, PartData.class);
49         SerialBinding<SupplierKey> supplierKeyBinding =
50             new SerialBinding<>(catalog, SupplierKey.class);
51         EntityBinding<Supplier> supplierDataBinding =
52             new SupplierBinding(catalog, SupplierKey.class,
53                                 SupplierData.class);
54         SerialBinding<ShipmentKey> shipmentKeyBinding =
55             new SerialBinding<>(catalog, ShipmentKey.class);
56         EntityBinding<Shipment> shipmentDataBinding =
57             new ShipmentBinding(catalog, ShipmentKey.class,
58                                 ShipmentData.class);
59         SerialBinding<String> cityKeyBinding =
60             new SerialBinding<>(catalog, String.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      * @return
97      */
getPartMap()98     public StoredSortedMap<PartKey, Part> getPartMap() {
99 
100         return partMap;
101     }
102 
103     /**
104      * Return a map view of the supplier storage container.
105      * @return
106      */
getSupplierMap()107     public StoredSortedMap<SupplierKey, Supplier> getSupplierMap() {
108 
109         return supplierMap;
110     }
111 
112     /**
113      * Return a map view of the shipment storage container.
114      * @return
115      */
getShipmentMap()116     public StoredSortedMap<ShipmentKey, Shipment> getShipmentMap() {
117 
118         return shipmentMap;
119     }
120 
121     /**
122      * Return an entity set view of the part storage container.
123      * @return
124      */
getPartSet()125     public StoredValueSet<Part> getPartSet() {
126 
127         return (StoredValueSet<Part>) partMap.values();
128     }
129 
130     /**
131      * Return an entity set view of the supplier storage container.
132      * @return
133      */
getSupplierSet()134     public StoredValueSet<Supplier> getSupplierSet() {
135 
136         return (StoredValueSet<Supplier>) supplierMap.values();
137     }
138 
139     /**
140      * Return an entity set view of the shipment storage container.
141      * @return
142      */
getShipmentSet()143     public StoredValueSet<Shipment> getShipmentSet() {
144 
145         return (StoredValueSet<Shipment>) shipmentMap.values();
146     }
147 
148     /**
149      * Return a map view of the shipment-by-part index.
150      * @return
151      */
getShipmentByPartMap()152     public StoredSortedMap<PartKey, Shipment> getShipmentByPartMap() {
153 
154         return shipmentByPartMap;
155     }
156 
157     /**
158      * Return a map view of the shipment-by-supplier index.
159      * @return
160      */
getShipmentBySupplierMap()161     public StoredSortedMap<SupplierKey, Shipment> getShipmentBySupplierMap() {
162 
163         return shipmentBySupplierMap;
164     }
165 
166     /**
167      * Return a map view of the supplier-by-city index.
168      * @return
169      */
getSupplierByCityMap()170     public final StoredSortedMap<String, Supplier> getSupplierByCityMap() {
171 
172         return supplierByCityMap;
173     }
174 
175     /**
176      * PartBinding is used to bind the stored key/data entry pair for a part
177      * to a combined data object (entity).
178      */
179     private static class PartBinding
180             extends SerialSerialBinding<PartKey, PartData, Part> {
181 
182         /**
183          * Construct the binding object.
184          */
PartBinding(ClassCatalog classCatalog, Class<PartKey> keyClass, Class<PartData> dataClass)185         private PartBinding(ClassCatalog classCatalog,
186                             Class<PartKey> keyClass,
187                             Class<PartData> dataClass) {
188 
189             super(classCatalog, keyClass, dataClass);
190         }
191 
192         /**
193          * Create the entity by combining the stored key and data.
194          */
195         @Override
entryToObject(PartKey keyInput, PartData dataInput)196         public Part entryToObject(PartKey keyInput, PartData dataInput) {
197 
198             return new Part(keyInput.getNumber(), dataInput.getName(), dataInput.getColor(),
199                             dataInput.getWeight(), dataInput.getCity());
200         }
201 
202         /**
203          * Create the stored key from the entity.
204          */
205         @Override
objectToKey(Part object)206         public PartKey objectToKey(Part object) {
207 
208             return new PartKey(object.getNumber());
209         }
210 
211         /**
212          * Create the stored data from the entity.
213          */
214         @Override
objectToData(Part object)215         public PartData objectToData(Part object) {
216 
217             return new PartData(object.getName(), object.getColor(),
218                                  object.getWeight(), object.getCity());
219         }
220     }
221 
222     /**
223      * SupplierBinding is used to bind the stored key/data entry pair for a
224      * supplier to a combined data object (entity).
225      */
226     private static class SupplierBinding
227             extends SerialSerialBinding<SupplierKey, SupplierData, Supplier> {
228 
229         /**
230          * Construct the binding object.
231          */
SupplierBinding(ClassCatalog classCatalog, Class<SupplierKey> keyClass, Class<SupplierData> dataClass)232         private SupplierBinding(ClassCatalog classCatalog,
233                                 Class<SupplierKey> keyClass,
234                                 Class<SupplierData> dataClass) {
235 
236             super(classCatalog, keyClass, dataClass);
237         }
238 
239         /**
240          * Create the entity by combining the stored key and data.
241          */
242         @Override
entryToObject(SupplierKey keyInput, SupplierData dataInput)243         public Supplier entryToObject(SupplierKey keyInput, SupplierData dataInput) {
244 
245             SupplierKey key = (SupplierKey) keyInput;
246             SupplierData data = (SupplierData) dataInput;
247             return new Supplier(keyInput.getNumber(), dataInput.getName(),
248                                 dataInput.getStatus(), dataInput.getCity());
249         }
250 
251         /**
252          * Create the stored key from the entity.
253          */
254         @Override
objectToKey(Supplier object)255         public SupplierKey objectToKey(Supplier object) {
256 
257             return new SupplierKey(object.getNumber());
258         }
259 
260         /**
261          * Create the stored data from the entity.
262          */
263         @Override
objectToData(Supplier object)264         public SupplierData objectToData(Supplier object) {
265 
266             return new SupplierData(object.getName(), object.getStatus(),
267                                      object.getCity());
268         }
269     }
270 
271     /**
272      * ShipmentBinding is used to bind the stored key/data entry pair for a
273      * shipment to a combined data object (entity).
274      */
275     private static class ShipmentBinding
276             extends SerialSerialBinding<ShipmentKey, ShipmentData, Shipment> {
277 
278         /**
279          * Construct the binding object.
280          */
ShipmentBinding(ClassCatalog classCatalog, Class<ShipmentKey> keyClass, Class<ShipmentData> dataClass)281         private ShipmentBinding(ClassCatalog classCatalog,
282                                 Class<ShipmentKey> keyClass,
283                                 Class<ShipmentData> dataClass) {
284 
285             super(classCatalog, keyClass, dataClass);
286         }
287 
288         /**
289          * Create the entity by combining the stored key and data.
290          */
291         @Override
entryToObject(ShipmentKey keyInput, ShipmentData dataInput)292         public Shipment entryToObject(ShipmentKey keyInput, ShipmentData dataInput) {
293 
294             return new Shipment(keyInput.getPartNumber(), keyInput.getSupplierNumber(),
295                                 dataInput.getQuantity());
296         }
297 
298         /**
299          * Create the stored key from the entity.
300          */
301         @Override
objectToKey(Shipment object)302         public ShipmentKey objectToKey(Shipment object) {
303 
304             return new ShipmentKey(object.getPartNumber(),
305                                    object.getSupplierNumber());
306         }
307 
308         /**
309          * Create the stored data from the entity.
310          */
311         @Override
objectToData(Shipment object)312         public ShipmentData objectToData(Shipment object) {
313 
314             return new ShipmentData(object.getQuantity());
315         }
316     }
317 }
318