KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > accounting > Price


1 /*
2  * Created on 27.10.2004
3  */

4 package com.nightlabs.ipanema.accounting;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import com.nightlabs.ipanema.organisation.Organisation;
13
14 /**
15  * @author Marco Schulze - marco at nightlabs dot de
16  *
17  * @jdo.persistence-capable
18  * identity-type = "application"
19  * objectid-class = "com.nightlabs.ipanema.accounting.id.PriceID"
20  * detachable = "true"
21  *
22  * @jdo.inheritance strategy = "new-table"
23  */

24 public class Price
25     implements Serializable JavaDoc
26 {
27     /**
28      * @jdo.field primary-key="true"
29      * @jdo.column length="100"
30      */

31     private String JavaDoc organisationID;
32
33     /**
34      * @jdo.field primary-key="true"
35      */

36     private long priceConfigID;
37
38     /**
39      * @jdo.field primary-key="true"
40      */

41     private long priceID;
42     /////// end PK /////
43

44
45     /////// begin normal fields ///////
46
private Currency currency;
47
48     private long amount = 0;
49
50     /**
51      * key: String priceFragmentTypePK<br/>
52      * value: PriceFragment priceFragment
53      * <p>
54      * A total price can contain various fragments that need to be known for
55      * legal reasons. E.g. the VAT is managed as a fragment with a certain key
56      * like "vat-de-16" or "vat-de-7". Other data that one trade partner
57      * wants/allows the other to be known might be transferred in here as well
58      * E.g. the system fees might be declared like this. The price fragments
59      * declared here are used by an Accountant to book the right amounts on the
60      * right Accounts.
61      * <p>
62      * Note, that PriceFragments can overlap or be incomplete and therefore their sum is NOT
63      * the <tt>Price.amount</tt> (or only by accident).
64      *
65      * @jdo.field
66      * persistence-modifier="persistent"
67      * collection-type="map"
68      * key-type="java.lang.String"
69      * value-type="PriceFragment"
70      * dependent="true"
71      * mapped-by="price"
72      *
73      * @jdo.map-vendor-extension vendor-name="jpox" key="key-field" value="priceFragmentTypeID"
74      *
75      * // This is not necessary if there's no join: @!jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 100"
76      */

77     protected Map JavaDoc fragments = new HashMap JavaDoc();
78
79     /////// end normal fields ////////
80

81     protected Price() { }
82
83     /**
84      * This constructor is used to create a price within a TariffPrice object
85      * in the general price grid.
86      *
87      * @param tariffPrice
88      * @param currency
89      */

90     public Price(String JavaDoc organisationID, long priceConfigID, long priceID, Currency currency)
91     {
92         this.organisationID = organisationID;
93         this.priceConfigID = priceConfigID;
94         this.priceID = priceID;
95         if (currency == null)
96             throw new NullPointerException JavaDoc("currency");
97         this.currency = currency;
98     }
99
100     /**
101      * @return Returns the organisationID.
102      */

103     public String JavaDoc getOrganisationID()
104     {
105         return organisationID;
106     }
107     /**
108      * @return Returns the priceConfigID.
109      */

110     public long getPriceConfigID()
111     {
112         return priceConfigID;
113     }
114     /**
115      * @return Returns the priceID.
116      */

117     public long getPriceID()
118     {
119         return priceID;
120     }
121     /**
122      * @return Returns the currency.
123      */

124     public Currency getCurrency()
125     {
126         return currency;
127     }
128
129     /**
130      * @return Returns a Collection of PriceFragment.
131      */

132     public Collection JavaDoc getFragments()
133     {
134         return fragments.values();
135     }
136
137     /**
138      * @return Returns the amount.
139      */

140     public long getAmount()
141     {
142         return amount;
143     }
144     /**
145      * @param amount The amount to set.
146      */

147     public void setAmount(long amount)
148     {
149         this.amount = amount;
150     }
151     
152     /**
153      * Returns the mathematical absolute value of this price's amount.
154      */

155     public long getAmountAbsoluteValue() {
156         return Math.abs(amount);
157     }
158
159     public PriceFragment getPriceFragment(
160             String JavaDoc priceFragmentTypePK, boolean throwExceptionIfNotExistent)
161     {
162         PriceFragment fragment = (PriceFragment) fragments.get(priceFragmentTypePK);
163         if (fragment == null && throwExceptionIfNotExistent)
164             throw new IllegalArgumentException JavaDoc("No PriceFragment registered with priceFragmentTypePK=\""+priceFragmentTypePK+"\"!");
165         return fragment;
166     }
167     
168     public PriceFragment getPriceFragment(
169             String JavaDoc priceFragmentTypeOrganisationID,
170             String JavaDoc priceFragmentTypeID, boolean throwExceptionIfNotExistent)
171     {
172         PriceFragment fragment = (PriceFragment) fragments.get(
173                 PriceFragmentType.getPrimaryKey(priceFragmentTypeOrganisationID, priceFragmentTypeID));
174         if (fragment == null && throwExceptionIfNotExistent)
175             throw new IllegalArgumentException JavaDoc("No PriceFragment registered with priceFragmentTypeOrganisationID=\""+priceFragmentTypeOrganisationID+"\", priceFragmentTypeID=\""+priceFragmentTypeID+"\"!");
176         return fragment;
177     }
178
179     public PriceFragment createPriceFragment(PriceFragmentType priceFragmentType)
180     {
181         if (priceFragmentType == null)
182             throw new NullPointerException JavaDoc("priceFragmentType");
183
184         PriceFragment fragment = (PriceFragment) fragments.get(priceFragmentType.getPriceFragmentTypeID());
185         if (fragment == null) {
186             fragment = new PriceFragment(this, priceFragmentType);
187             fragments.put(priceFragmentType.getPrimaryKey(), fragment);
188         }
189         return fragment;
190     }
191
192     public long getAmount(PriceFragmentType priceFragmentType)
193     {
194         return getAmount(priceFragmentType.getOrganisationID(), priceFragmentType.getPriceFragmentTypeID());
195     }
196     public long getAmount(String JavaDoc priceFragmentTypePK)
197     {
198         if (priceFragmentTypePK == null ||
199                 PriceFragmentType.getPrimaryKey(Organisation.ROOT_ORGANISATIONID, PriceFragmentType.TOTAL_PRICEFRAGMENTTYPEID).equals(priceFragmentTypePK))
200             return amount;
201
202         PriceFragment priceFragment = getPriceFragment(priceFragmentTypePK, false);
203         if (priceFragment != null)
204             return priceFragment.getAmount();
205
206         return 0;
207     }
208     
209     /**
210      * @return Returns the amount.
211      */

212     public long getAmount(String JavaDoc priceFragmentTypeOrganisationID, String JavaDoc priceFragmentTypeID)
213     {
214         if (priceFragmentTypeOrganisationID != null && priceFragmentTypeID == null)
215             throw new IllegalArgumentException JavaDoc("priceFragmentTypeOrganisationID is not null, but priceFragmentTypeID is null! Either none or both must be null!");
216
217         if (priceFragmentTypeOrganisationID == null && priceFragmentTypeID != null)
218             throw new IllegalArgumentException JavaDoc("priceFragmentTypeOrganisationID is null, but priceFragmentTypeID is not null! Either none or both must be null!");
219
220         if (priceFragmentTypeID == null ||
221                 (Organisation.ROOT_ORGANISATIONID.equals(priceFragmentTypeOrganisationID) &&
222                  PriceFragmentType.TOTAL_PRICEFRAGMENTTYPEID.equals(priceFragmentTypeID)))
223             return amount;
224
225         PriceFragment priceFragment = getPriceFragment(priceFragmentTypeOrganisationID, priceFragmentTypeID, false);
226         if (priceFragment != null)
227             return priceFragment.getAmount();
228
229         return 0;
230     }
231     /**
232      * @param amount The amount to set.
233      */

234     public void setAmount(PriceFragmentType priceFragmentType, long amount)
235     {
236         if (priceFragmentType == null ||
237                 (Organisation.ROOT_ORGANISATIONID.equals(priceFragmentType.getOrganisationID()) &&
238                  PriceFragmentType.TOTAL_PRICEFRAGMENTTYPEID.equals(priceFragmentType.getPriceFragmentTypeID())))
239             this.amount = amount;
240         PriceFragment priceFragment = createPriceFragment(priceFragmentType);
241         priceFragment.setAmount(amount);
242     }
243
244     public void clearFragments()
245     {
246         fragments.clear();
247     }
248     /**
249      * This method finds the local PriceFragment with the same priceFragmentID and
250      * adds the amount of the given PriceFragment to it. If there is no local PriceFragment
251      * existing for the given PriceFragmentType, it will be created.
252      *
253      * @param priceFragment
254      */

255     public void sumPriceFragment(PriceFragment priceFragment)
256     {
257         PriceFragmentType priceFragmentType = priceFragment.getPriceFragmentType();
258         String JavaDoc priceFragmentTypePK = priceFragmentType.getPrimaryKey();
259         PriceFragment localPriceFragment = (PriceFragment) fragments.get(priceFragmentTypePK);
260         if (localPriceFragment == null) {
261             localPriceFragment = new PriceFragment(this, priceFragmentType);
262             fragments.put(priceFragmentTypePK, localPriceFragment);
263         }
264         localPriceFragment.setAmount(localPriceFragment.getAmount() + priceFragment.getAmount());
265     }
266     
267     public void sumPrice(Price price)
268     {
269         this.amount += price.getAmount();
270         for (Iterator JavaDoc it = price.getFragments().iterator(); it.hasNext(); ) {
271             PriceFragment fragment = (PriceFragment) it.next();
272             sumPriceFragment(fragment);
273         }
274     }
275 }
276
Popular Tags