1 4 package com.nightlabs.ipanema.accounting; 5 6 import java.io.Serializable ; 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import com.nightlabs.ipanema.organisation.Organisation; 13 14 24 public class Price 25 implements Serializable 26 { 27 31 private String organisationID; 32 33 36 private long priceConfigID; 37 38 41 private long priceID; 42 44 45 private Currency currency; 47 48 private long amount = 0; 49 50 77 protected Map fragments = new HashMap (); 78 79 81 protected Price() { } 82 83 90 public Price(String 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 ("currency"); 97 this.currency = currency; 98 } 99 100 103 public String getOrganisationID() 104 { 105 return organisationID; 106 } 107 110 public long getPriceConfigID() 111 { 112 return priceConfigID; 113 } 114 117 public long getPriceID() 118 { 119 return priceID; 120 } 121 124 public Currency getCurrency() 125 { 126 return currency; 127 } 128 129 132 public Collection getFragments() 133 { 134 return fragments.values(); 135 } 136 137 140 public long getAmount() 141 { 142 return amount; 143 } 144 147 public void setAmount(long amount) 148 { 149 this.amount = amount; 150 } 151 152 155 public long getAmountAbsoluteValue() { 156 return Math.abs(amount); 157 } 158 159 public PriceFragment getPriceFragment( 160 String priceFragmentTypePK, boolean throwExceptionIfNotExistent) 161 { 162 PriceFragment fragment = (PriceFragment) fragments.get(priceFragmentTypePK); 163 if (fragment == null && throwExceptionIfNotExistent) 164 throw new IllegalArgumentException ("No PriceFragment registered with priceFragmentTypePK=\""+priceFragmentTypePK+"\"!"); 165 return fragment; 166 } 167 168 public PriceFragment getPriceFragment( 169 String priceFragmentTypeOrganisationID, 170 String priceFragmentTypeID, boolean throwExceptionIfNotExistent) 171 { 172 PriceFragment fragment = (PriceFragment) fragments.get( 173 PriceFragmentType.getPrimaryKey(priceFragmentTypeOrganisationID, priceFragmentTypeID)); 174 if (fragment == null && throwExceptionIfNotExistent) 175 throw new IllegalArgumentException ("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 ("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 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 212 public long getAmount(String priceFragmentTypeOrganisationID, String priceFragmentTypeID) 213 { 214 if (priceFragmentTypeOrganisationID != null && priceFragmentTypeID == null) 215 throw new IllegalArgumentException ("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 ("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 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 255 public void sumPriceFragment(PriceFragment priceFragment) 256 { 257 PriceFragmentType priceFragmentType = priceFragment.getPriceFragmentType(); 258 String 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 it = price.getFragments().iterator(); it.hasNext(); ) { 271 PriceFragment fragment = (PriceFragment) it.next(); 272 sumPriceFragment(fragment); 273 } 274 } 275 } 276 | Popular Tags |