KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > trade > OfferItem


1 /*
2  * Created on 30.10.2004
3  */

4 package com.nightlabs.ipanema.trade;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.Date JavaDoc;
8
9 import javax.jdo.JDOHelper;
10 import javax.jdo.PersistenceManager;
11
12 import com.nightlabs.ModuleException;
13 import com.nightlabs.ipanema.accounting.Accounting;
14 import com.nightlabs.ipanema.accounting.AccountingPriceConfig;
15 import com.nightlabs.ipanema.accounting.Currency;
16 import com.nightlabs.ipanema.accounting.Invoice;
17 import com.nightlabs.ipanema.accounting.ProductInfo;
18 import com.nightlabs.ipanema.security.User;
19 import com.nightlabs.ipanema.store.Product;
20
21 /**
22  * @author Marco Schulze - marco at nightlabs dot de
23  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
24  *
25  * @jdo.persistence-capable
26  * identity-type = "application"
27  * objectid-class = "com.nightlabs.ipanema.trade.id.OfferItemID"
28  * detachable = "true"
29  *
30  * @jdo.inheritance strategy = "new-table"
31  */

32 public class OfferItem
33     implements Serializable JavaDoc
34 {
35     /**
36      * @jdo.field primary-key="true"
37      * @jdo.column length="100"
38      */

39     private String JavaDoc organisationID;
40
41     /**
42      * @jdo.field primary-key="true"
43      */

44     private long offerID;
45
46     // A product can only occur once within an Offer, because an Offer must either be rejected
47
// or accepted completely and you can't buy the same product twice.
48
/**
49      * @jdo.field primary-key="true"
50      * @jdo.column length="100"
51      */

52     private String JavaDoc productOrganisationID;
53
54     /**
55      * @jdo.field primary-key="true"
56      * @jdo.column length="100"
57      */

58     private String JavaDoc productID;
59
60     /**
61      * @jdo.field persistence-modifier="persistent"
62      */

63     private String JavaDoc primaryKey;
64
65     /**
66      * @jdo.field persistence-modifier="persistent"
67      */

68     private Offer offer;
69
70     /**
71      * An <tt>OfferItem</tt> can only be part of none or exactly one <tt>Invoice</tt>.
72      *
73      * @jdo.field persistence-modifier="persistent"
74      */

75     private Invoice invoice = null;
76
77     /**
78      * @jdo.field persistence-modifier="persistent"
79      */

80     private Product product;
81
82     /**
83      * @jdo.field persistence-modifier="persistent"
84      */

85     private OfferItemPrice price;
86
87     /**
88      * This is set according to the result of PriceConfig.isDependentOnOffer().
89      *
90      * @jdo.field persistence-modifier="persistent"
91      */

92     private boolean priceDependentOnOffer;
93
94     /**
95      * Whether or not this OfferItem is refunding a previously sold OfferItem.
96      * If true, originalOfferItemToRefund is assigned. This flag exists mainly
97      * for detached use of OfferItem in which you can't execute
98      * <tt>originalOfferItemToRefund != null</tt>.
99      *
100      * @see #originalOfferItemToRefund
101      *
102      * @jdo.field persistence-modifier="persistent"
103      */

104     private boolean refund = false;
105
106     /**
107      * If this OfferItem refunds a previously sold OfferItem, this member points
108      * to the original OfferItem.
109      *
110      * @see #refund
111      *
112      * @jdo.field persistence-modifier="persistent"
113      */

114     private OfferItem originalOfferItemToRefund = null;
115     
116     private Date JavaDoc autoReleaseDT = null;
117     
118     /**
119      * If the offerItem is allocated, it still does not mean, that
120      * everything is fine, because another organisation might just have
121      * auto-released. Thus, it is necessary to execute <tt>allocate(...)</tt>
122      * again (with a timestamp at least 30 minutes in the future) to be sure.
123      * <br/><br/>
124      * The only thing that can be sure is that the product which is embraced
125      * by this OfferItem is NOT allocated by THIS OfferItem if allocated is
126      * false. But it can be allocated by another OfferItem!
127      *
128      * @jdo.field persistence-modifier="persistent"
129      */

130     private boolean allocated = false;
131
132     protected OfferItem() { }
133
134     protected void initOffer(Offer offer)
135     {
136         if (offer == null)
137             throw new NullPointerException JavaDoc("offer");
138         this.offer = offer;
139         this.organisationID = offer.getOrganisationID();
140         this.offerID = offer.getOfferID();
141     }
142     
143     public OfferItem(Offer offer, OfferItem offerItemToRefund)
144     {
145         initOffer(offer);
146
147         if (offerItemToRefund == null)
148             throw new NullPointerException JavaDoc("offerItemToRefund");
149         this.originalOfferItemToRefund = offerItemToRefund;
150         this.refund = true;
151         this.price = originalOfferItemToRefund.getPrice().createRefundPrice();
152         this.primaryKey = getPrimaryKey(organisationID, offerID, productOrganisationID, productID);
153     }
154
155     public OfferItem(Offer offer, Product product, Currency currency)
156     {
157         initOffer(offer);
158
159         if (product == null)
160             throw new NullPointerException JavaDoc("product");
161         this.product = product;
162         this.productOrganisationID = product.getOrganisationID();
163         this.productID = product.getProductID();
164
165         this.primaryKey = getPrimaryKey(organisationID, offerID, productOrganisationID, productID);
166
167         PersistenceManager pm = JDOHelper.getPersistenceManager(offer);
168         if (pm == null)
169             throw new IllegalStateException JavaDoc("offer is currently not persistent! Can only create an OfferItem with a persistent offer.");
170
171         Accounting accounting = Accounting.getAccounting(pm);
172         ProductInfo productInfo = accounting.getProductInfo(product, true);
173         AccountingPriceConfig accountingPriceConfig = accounting.getAccountingPriceConfig();
174
175         this.price = new OfferItemPrice(
176                 productInfo.getPriceConfig().getPrice(this),
177                 accounting.getOrganisationID(),
178                 accountingPriceConfig.getPriceConfigID(),
179                 accountingPriceConfig.createPriceID(), false);
180     }
181
182     /**
183      * @return Returns the organisationID.
184      */

185     public String JavaDoc getOrganisationID()
186     {
187         return organisationID;
188     }
189     /**
190      * @return Returns the offerID.
191      */

192     public long getOfferID()
193     {
194         return offerID;
195     }
196     /**
197      * @return Returns the productOrganisationID.
198      */

199     public String JavaDoc getProductOrganisationID()
200     {
201         return productOrganisationID;
202     }
203     /**
204      * @return Returns the productID.
205      */

206     public String JavaDoc getProductID()
207     {
208         return productID;
209     }
210     public static String JavaDoc getPrimaryKey(
211             String JavaDoc organisationID, long offerID, String JavaDoc productOrganisationID, String JavaDoc productID)
212     {
213         return organisationID + '/' + Long.toHexString(offerID) + '/' + productOrganisationID + '/' + productID;
214     }
215     public String JavaDoc getPrimaryKey() {
216         return primaryKey;
217     }
218     /**
219      * @return Returns the product.
220      */

221     public Product getProduct()
222     {
223         return product;
224     }
225     /**
226      * @return Returns the price.
227      */

228     public OfferItemPrice getPrice()
229     {
230         return price;
231     }
232     /**
233      * @return Returns the priceDependentOnOffer.
234      */

235     public boolean isPriceDependentOnOffer()
236     {
237         return priceDependentOnOffer;
238     }
239     /**
240      * @param priceDependentOnOffer The priceDependentOnOffer to set.
241      */

242     protected void setPriceDependentOnOffer(boolean priceDependentOnOffer)
243     {
244         this.priceDependentOnOffer = priceDependentOnOffer;
245     }
246     /**
247      * @return Returns the offer.
248      */

249     public Offer getOffer()
250     {
251         return offer;
252     }
253     /**
254      * @return Returns the originalOfferItemToRefund.
255      */

256     public OfferItem getOriginalOfferItemToRefund()
257     {
258         return originalOfferItemToRefund;
259     }
260     /**
261      * @return Returns the refund.
262      */

263     public boolean isRefund()
264     {
265         return refund;
266     }
267     
268     /**
269      * This method returns the date and time when the OfferItem will
270      * automatically be released. To change this, you need to call
271      * <tt>allocate(..)</tt>.
272      *
273      * @return Returns the autoReleaseDT.
274      * @see #allocate(User, Date)
275      */

276     public Date JavaDoc getAutoReleaseDT()
277     {
278         return autoReleaseDT;
279     }
280
281     /**
282      * This method allocates the product which is embraced by this OfferItem.
283      * The allocation work is delegated to the Trader, because it is necessary
284      * to create Order-s & Offer-s to get the requirement-products.
285      * @throws ModuleException
286      */

287     public void allocate(User user, Date JavaDoc autoReleaseDT)
288             throws ModuleException
289     {
290         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
291         if (pm == null)
292             throw new IllegalStateException JavaDoc("This instance of OfferItem is currently not persistent!");
293         
294         Trader trader = Trader.getTrader(pm);
295         trader.allocateOfferItem(user, this, autoReleaseDT);
296
297         this.autoReleaseDT = autoReleaseDT;
298         this.allocated = true;
299     }
300     
301     public void release(User user)
302             throws ModuleException
303     {
304         if (!allocated)
305             return;
306
307         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
308         if (pm == null)
309             throw new IllegalStateException JavaDoc("This instance of OfferItem is currently not persistent!");
310
311         Trader trader = Trader.getTrader(pm);
312         trader.releaseOfferItem(user, this);
313         this.allocated = false;
314     }
315
316     public Invoice getInvoice() {
317         return invoice;
318     }
319     public void setInvoice(Invoice invoice) {
320         this.invoice = invoice;
321     }
322 }
323
Popular Tags