KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 30.10.2004
3  */

4 package com.nightlabs.ipanema.trade;
5
6 import java.util.Iterator JavaDoc;
7
8 import javax.jdo.JDOHelper;
9 import javax.jdo.PersistenceManager;
10
11 import com.nightlabs.ipanema.accounting.Accounting;
12 import com.nightlabs.ipanema.accounting.AccountingPriceConfig;
13 import com.nightlabs.ipanema.accounting.PriceFragment;
14
15
16 /**
17  * This OfferItemPrice is used within OfferItemS. On creation, it copies all data from the original
18  * price. Thus, the price grid can be changed without the offers to change. The offer items
19  * call OfferItemPrice.assign(...) only if the offer changes.
20  *
21  * @author Marco Schulze - marco at nightlabs dot de
22  *
23  * @jdo.persistence-capable
24  * identity-type = "application"
25  * persistence-capable-superclass = "com.nightlabs.ipanema.accounting.Price"
26  * detachable = "true"
27  *
28  * @jdo.inheritance strategy = "new-table"
29  */

30 public class OfferItemPrice extends com.nightlabs.ipanema.accounting.Price
31 {
32
33     private com.nightlabs.ipanema.accounting.Price origPrice;
34     
35     protected OfferItemPrice() { }
36
37     /**
38      * @param origPrice The original price from which to copy all data.
39      * @param priceID The ID for the new price.
40      */

41     public OfferItemPrice(
42             com.nightlabs.ipanema.accounting.Price origPrice,
43             String JavaDoc organisationID, long priceConfigID,
44             long priceID, boolean refund)
45     {
46         super(organisationID, priceConfigID, priceID, origPrice.getCurrency());
47
48         this.origPrice = origPrice;
49         assign(origPrice);
50         
51         if (refund)
52             negate();
53     }
54
55     /**
56      * Copies all data (amount, fragments) from the given origPrice.
57      *
58      * @param origPrice
59      */

60     protected void assign(com.nightlabs.ipanema.accounting.Price origPrice)
61     {
62         if (!getCurrency().getCurrencyID().equals(origPrice.getCurrency().getCurrencyID()))
63             throw new IllegalArgumentException JavaDoc("Currencies do not match!");
64
65         this.setAmount(origPrice.getAmount());
66
67         for (Iterator JavaDoc it = origPrice.getFragments().iterator(); it.hasNext(); ) {
68             PriceFragment origpf = (PriceFragment)it.next();
69             PriceFragment pf = new PriceFragment(this, origpf);
70             // TODO Does the following "put" really delete an old object?
71
// FIXME Uncomment:
72
// this.fragments.put(pf.getPriceFragmentTypeID(), pf);
73
}
74     }
75
76     protected void negate()
77     {
78         this.setAmount(-this.getAmount());
79         for (Iterator JavaDoc it = this.getFragments().iterator(); it.hasNext(); ) {
80             PriceFragment pf = (PriceFragment)it.next();
81             pf.setAmount(-pf.getAmount());
82         }
83     }
84
85     /**
86      * This method creates a new instance of OfferItemPrice in which
87      * the original price represented by this instance of OfferItemPrice
88      * is negated.
89      *
90      * @param priceConfig The priceConfig is used to create an ID for the
91      * new OfferItemPrice
92      *
93      * @return
94      */

95     public OfferItemPrice createRefundPrice()
96     {
97         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
98         if (pm == null)
99             throw new IllegalStateException JavaDoc("This instance of OfferItemPrice is currently not persistent!");
100
101         Accounting accounting = Accounting.getAccounting(pm);
102         AccountingPriceConfig accountingPriceConfig = accounting.getAccountingPriceConfig();
103
104         OfferItemPrice offerItemPrice = new OfferItemPrice(
105                 this,
106                 accounting.getOrganisationID(),
107                 accountingPriceConfig.getPriceConfigID(),
108                 accountingPriceConfig.createPriceID(), true);
109         return offerItemPrice;
110     }
111
112 }
113
Popular Tags