KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > store > Store


1 /*
2  * Created on 16.10.2004
3  */

4 package com.nightlabs.ipanema.store;
5
6 import java.util.Collection JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import javax.jdo.JDOHelper;
12 import javax.jdo.PersistenceManager;
13
14 import com.nightlabs.ipanema.organisation.LocalOrganisation;
15 import com.nightlabs.ipanema.security.User;
16 import com.nightlabs.ipanema.transfer.Anchor;
17 import com.nightlabs.ipanema.transfer.Transfer;
18 import com.nightlabs.ipanema.transfer.TransferRegistry;
19
20 /**
21  * The Store is responsible for managing all ProductTypes and Products
22  * within the store. There exists exactly one instance.
23  *
24  * @author marco at nightlabs dot de
25  *
26  * @jdo.persistence-capable
27  * identity-type = "datastore"
28  * detachable = "true"
29  *
30  * @jdo.inheritance strategy = "new-table"
31  */

32 public class Store
33     implements TransferRegistry
34 {
35     /**
36      * This method returns the singleton instance of Store. If there is
37      * no instance of Store in the datastore, yet, it will be created.
38      *
39      * @param pm
40      * @return
41      */

42     public static Store getStore(PersistenceManager pm)
43     {
44         Iterator JavaDoc it = pm.getExtent(Store.class).iterator();
45         if (it.hasNext())
46             return (Store)it.next();
47
48         Store store = new Store();
49
50         // initialize the organisationID
51
it = pm.getExtent(LocalOrganisation.class).iterator();
52         if (!it.hasNext())
53             throw new IllegalStateException JavaDoc("LocalOrganisation undefined in datastore!");
54         LocalOrganisation localOrganisation = (LocalOrganisation) it.next();
55         store.organisationID = localOrganisation.getOrganisation().getOrganisationID();
56
57         pm.makePersistent(store);
58         return store;
59     }
60
61     /**
62      * @jdo.field persistence-modifier="persistent"
63      * @jdo.column length="100"
64      */

65     private String JavaDoc organisationID;
66     
67     private long nextProductID = 0;
68
69     protected Store() { }
70
71     /**
72      * key: String productPrimaryKey {organisationID + / + productID}<br/>
73      * value: Product productType
74      *
75      * @jdo.field
76      * persistence-modifier="persistent"
77      * collection-type="map"
78      * key-type="java.lang.String"
79      * value-type="Product"
80      * dependent="true"
81      *
82      * @jdo.join
83      *
84      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 201"
85      */

86     protected Map JavaDoc productTypes = new HashMap JavaDoc();
87     
88     /**
89      * key: String productPrimaryKey {organisationID + / + productID}<br/>
90      * value: ProductStatusTracker productStatusTracker
91      *
92      * @jdo.field
93      * persistence-modifier="persistent"
94      * collection-type="map"
95      * key-type="java.lang.String"
96      * value-type="ProductStatusTracker"
97      * dependent="true"
98      *
99      * @jdo.join
100      *
101      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 201"
102      */

103     protected Map JavaDoc productTypeStatusTrackers = new HashMap JavaDoc();
104
105     /**
106      * key: String productPrimaryKey {organisationID + / + productID}<br/>
107      * value: Product product
108      *
109      * @jdo.field
110      * persistence-modifier="persistent"
111      * collection-type="map"
112      * key-type="java.lang.String"
113      * value-type="Product"
114      * dependent="true"
115      *
116      * @jdo.join
117      *
118      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 201"
119      */

120     protected Map JavaDoc products = new HashMap JavaDoc();
121     
122     /**
123      * key: String productPrimaryKey {organisationID + / + productID}<br/>
124      * value: ProductStatusTracker productStatusTracker
125      *
126      * @jdo.field
127      * persistence-modifier="persistent"
128      * collection-type="map"
129      * key-type="java.lang.String"
130      * value-type="ProductStatusTracker"
131      * dependent="true"
132      *
133      * @jdo.join
134      *
135      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 201"
136      */

137     protected Map JavaDoc productStatusTrackers = new HashMap JavaDoc();
138
139     /**
140      * key: String productPrimaryKey {organisationID + / + productID}<br/>
141      * value: ProductTransferTracker productTransferTracker
142      *
143      * @jdo.field
144      * persistence-modifier="persistent"
145      * collection-type="map"
146      * key-type="java.lang.String"
147      * value-type="ProductTransferTracker"
148      * dependent="true"
149      *
150      * @jdo.join
151      *
152      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 201"
153      */

154     protected Map JavaDoc productTransferTrackers = new HashMap JavaDoc();
155
156     /**
157      * @return Returns the organisationID.
158      */

159     public String JavaDoc getOrganisationID()
160     {
161         return organisationID;
162     }
163     /**
164      * @param organisationID The organisationID to set.
165      */

166     protected void setOrganisationID(String JavaDoc organisationID)
167     {
168         this.organisationID = organisationID;
169     }
170     
171     protected synchronized long createProductID()
172     {
173         long res = nextProductID++;
174         return res;
175     }
176
177     /**
178      *
179      * @param user Which user is adding this productType.
180      * @param productType
181      */

182     public void addProductType(User user, Product productType)
183     {
184         if (productType.getProductID() == null) {
185             if (productType.getOrganisationID() != null &&
186                     !this.getOrganisationID().equals(productType.getOrganisationID()))
187                 throw new IllegalArgumentException JavaDoc("productType.productID is null, but productType.organisationID is not null and a foreign organisation! Cannot generate an ID for a foreign organisation!");
188
189             productType.setOrganisationID(getOrganisationID());
190             productType.setProductID(Long.toHexString(createProductID()));
191         };
192
193         if (!Product.NATURE_TYPE.equals(productType.getNature()))
194             throw new IllegalArgumentException JavaDoc("productType has not nature Product.NATURE_TYPE!");
195
196         String JavaDoc key = productType.getPrimaryKey();
197         if (productTypes.containsKey(key))
198             throw new IllegalStateException JavaDoc("Cannot add productType, because a productType with the same key already exists!");
199         
200         // makePersistent is necessary because of a map bug in JPOX.
201
// Otherwise the Map.put fails.
202
// if (!JDOHelper.isPersistent(productType))
203
// JDOHelper.getPersistenceManager(this).makePersistent(productType);
204

205         boolean hollow = !this.getOrganisationID().equals(productType.getOrganisationID());
206
207         ProductStatusTracker productStatusTracker = new ProductStatusTracker(user, productType, hollow);
208         productTypes.put(key, productType);
209         productTypeStatusTrackers.put(key, productStatusTracker);
210     }
211
212     public Product getProductType(String JavaDoc organisationID, String JavaDoc productTypeID)
213     {
214         return (Product)productTypes.get(Product.getPrimaryKey(organisationID, productTypeID));
215     }
216     public ProductStatusTracker getProductTypeStatusTracker(String JavaDoc organisationID, String JavaDoc productTypeID)
217     {
218         return (ProductStatusTracker)productTypeStatusTrackers.get(Product.getPrimaryKey(organisationID, productTypeID));
219     }
220     public ProductStatusTracker getProductTypeStatusTracker(Product productType)
221     {
222         if (productType == null)
223             throw new NullPointerException JavaDoc("productType must not be null!");
224         return (ProductStatusTracker)productTypeStatusTrackers.get(productType.getPrimaryKey());
225     }
226
227     public void addProduct(User user, Product product)
228     {
229         if (product.getProductID() == null) {
230             if (product.getOrganisationID() != null &&
231                     !this.getOrganisationID().equals(product.getOrganisationID()))
232                 throw new IllegalArgumentException JavaDoc("product.productID is null, but product.organisationID is not null and a foreign organisation! Cannot generate an ID for a foreign organisation!");
233
234             product.setOrganisationID(getOrganisationID());
235             product.setProductID(Long.toHexString(createProductID()));
236         };
237
238         if (!Product.NATURE_INSTANCE.equals(product.getNature()))
239             throw new IllegalArgumentException JavaDoc("product has not nature Product.NATURE_INSTANCE!");
240
241         String JavaDoc key = product.getPrimaryKey();
242         if (products.containsKey(key))
243             throw new IllegalStateException JavaDoc("Cannot add product, because a product with the same key already exists!");
244
245         // makePersistent is necessary because of a map bug in JPOX.
246
// Otherwise the Map.put fails.
247
// if (!JDOHelper.isPersistent(product))
248
// JDOHelper.getPersistenceManager(this).makePersistent(product);
249

250         boolean hollow = !this.getOrganisationID().equals(product.getOrganisationID());
251
252         ProductStatusTracker productStatusTracker = new ProductStatusTracker(user, product, hollow);
253         ProductTransferTracker productTransferTracker = new ProductTransferTracker(product);
254         products.put(key, product);
255         productStatusTrackers.put(key, productStatusTracker);
256         productTransferTrackers.put(key, productTransferTracker);
257     }
258
259     public Product getProduct(String JavaDoc organisationID, String JavaDoc productID)
260     {
261         return (Product)products.get(Product.getPrimaryKey(organisationID, productID));
262     }
263     public ProductStatusTracker getProductStatusRegistry(String JavaDoc organisationID, String JavaDoc productID)
264     {
265         return (ProductStatusTracker)productStatusTrackers.get(Product.getPrimaryKey(organisationID, productID));
266     }
267     public ProductStatusTracker getProductStatusTracker(Product product)
268     {
269         if (product == null)
270             throw new NullPointerException JavaDoc("product must not be null!");
271         return (ProductStatusTracker)productStatusTrackers.get(product.getPrimaryKey());
272     }
273     public ProductTransferTracker getProductTransferTracker(String JavaDoc organisationID, String JavaDoc productID)
274     {
275         return (ProductTransferTracker)productTransferTrackers.get(Product.getPrimaryKey(organisationID, productID));
276     }
277     public ProductTransferTracker getProductTransferTracker(Product product)
278     {
279         return (ProductTransferTracker)productTransferTrackers.get(product.getPrimaryKey());
280     }
281
282     protected ProductTransfer transferProducts(ProductTransfer container, User initiator, Anchor from, Anchor to, Collection JavaDoc products)
283     {
284         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
285         if (pm == null)
286             throw new IllegalStateException JavaDoc("This store is not persistent! I don't have a PersistenceManager!");
287
288         ProductTransfer productTransfer = new ProductTransfer(this, null, initiator, from, to, products);
289         for (Iterator JavaDoc it = products.iterator(); it.hasNext(); ) {
290             Product product = (Product)it.next();
291             getProductTransferTracker(product).addProductTransfer(productTransfer);
292         }
293         // TODO We should make sure that all products can be transferred (e.g. to check packaged products).
294
// And probably we need to do much more...
295
return productTransfer;
296     }
297
298     public ProductTransfer transferProducts(User initiator, Anchor from, Anchor to, ProductTransfer container)
299     {
300         return transferProducts(container, initiator, from, to, container.products.values());
301     }
302
303     public ProductTransfer transferProducts(User initiator, Anchor from, Anchor to, Collection JavaDoc products)
304     {
305         return transferProducts(null, initiator, from, to, products);
306     }
307
308     
309 /////////// begin implementation of TransferRegistry /////////////
310
private long nextTransferID = 0;
311
312     /**
313      * key: String transferPrimaryKey {organisationID + / + transferTypeID + / + transferID}<br/>
314      * value: Transfer transfer
315      *
316      * @jdo.field
317      * persistence-modifier="persistent"
318      * collection-type="map"
319      * key-type="java.lang.String"
320      * value-type="Transfer"
321      * dependent="true"
322      *
323      * @jdo.join
324      *
325      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 201"
326      */

327     protected Map JavaDoc transfers = new HashMap JavaDoc();
328
329     /**
330      * This method adds an instance of Transfer. This is not necessary, if the Transfer has been created
331      * by this organisation, because every Transfers does a self-registration.
332      *
333      * @param transfer
334      */

335     public void addTransfer(Transfer transfer)
336     {
337         if (transfer == null)
338             throw new NullPointerException JavaDoc("transfer is null!");
339         
340         if (transfer.getOrganisationID() == null)
341             throw new NullPointerException JavaDoc("transfer.organisationID is null!");
342
343         if (transfer.getTransferID() < 0)
344             throw new NullPointerException JavaDoc("transfer.transferID < 0!");
345
346         String JavaDoc key = transfer.getPrimaryKey();
347         if (transfers.containsKey(key))
348             throw new IllegalStateException JavaDoc("Cannot add transfer, because a transfer with the same key already exists!");
349
350         transfers.put(transfer.getPrimaryKey(), transfer);
351     }
352
353     public synchronized long createTransferID(String JavaDoc organisationID, String JavaDoc transferTypeID)
354     {
355         if (organisationID.equals(getOrganisationID()))
356             throw new IllegalArgumentException JavaDoc("This instance of Store can only create transferIDs for the organisation \""+getOrganisationID()+"\"!");
357
358         if (!ProductTransfer.TRANSFERTYPEID.equals(transferTypeID))
359             throw new IllegalArgumentException JavaDoc("This implementation of TransferRegistry manages only Transfers with transferTypeID=\""+ProductTransfer.TRANSFERTYPEID+"\"!");
360         long res = nextTransferID++;
361         return res;
362     }
363 /////////// end implementation of TransferRegistry /////////////
364
}
365
Popular Tags