| 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.Map ; 10 11 import javax.jdo.JDOHelper; 12 import javax.jdo.PersistenceManager; 13 import javax.jdo.StoreCallback; 14 15 import com.nightlabs.ipanema.trade.OfferItem; 16 17 35 public abstract class PriceConfig implements Serializable , StoreCallback 36 { 37 41 private String organisationID = null; 42 43 46 private long priceConfigID = -1; 47 48 51 private String primaryKey; 52 53 55 protected PriceConfig() { } 56 public PriceConfig(String organisationID, long priceConfigID) 57 { 58 if (organisationID == null) 59 throw new NullPointerException ("organisationID must not be null!"); 60 if (priceConfigID < 0) 61 throw new IllegalArgumentException ("priceConfigID < 0!"); 62 63 this.organisationID = organisationID; 64 this.priceConfigID = priceConfigID; 65 this.primaryKey = getPrimaryKey(organisationID, priceConfigID); 66 } 67 68 71 public String getOrganisationID() 72 { 73 return organisationID; 74 } 75 78 public long getPriceConfigID() 79 { 80 return priceConfigID; 81 } 82 public static String getPrimaryKey(String organisationID, long priceConfigID) 83 { 84 if (organisationID == null) 85 throw new NullPointerException ("organisationID must not be null!"); 86 if (priceConfigID < 0) 87 throw new IllegalArgumentException ("priceConfigID < 0!"); 88 89 return organisationID + '/' + Long.toHexString(priceConfigID); 90 } 91 92 public String getPrimaryKey() 93 { 94 return primaryKey; 95 } 96 97 115 private Map currencies = new HashMap (); 116 117 public Collection getCurrencies() 118 { 119 return currencies.values(); 120 } 121 122 128 public void addCurrency(Currency currency) 129 { 130 currencies.put(currency.getCurrencyID(), currency); 131 } 132 133 137 public Currency getCurrency(String currencyID, boolean throwExceptionIfNotRegistered) 138 { 139 Currency res = (Currency) currencies.get(currencyID); 140 if (res == null && throwExceptionIfNotRegistered) 141 throw new IndexOutOfBoundsException ("There is no Currency registered in this PriceConfig with the currencyID "+currencyID); 142 return res; 143 } 144 public boolean containsCurrency(String currencyID) 145 { 146 return currencies.containsKey(currencyID); 147 } 148 public boolean containsCurrency(Currency currency) 149 { 150 return currencies.containsKey(currency.getCurrencyID()); 151 } 152 public Currency removeCurrency(String currencyID) 153 { 154 return (Currency) currencies.remove(currencyID); 155 } 156 157 169 private Map priceFragmentTypes = new HashMap (); 170 171 public Collection getPriceFragmentTypes() 172 { 173 return priceFragmentTypes.values(); 174 } 175 public void addPriceFragmentType(PriceFragmentType priceFragmentType) 176 { 177 priceFragmentTypes.put(priceFragmentType.getPrimaryKey(), priceFragmentType); 178 } 179 public PriceFragmentType getPriceFragmentType(String organisationID, String priceFragmentTypeID, boolean throwExceptionIfNotExistent) 180 { 181 return getPriceFragmentType( 182 PriceFragmentType.getPrimaryKey(organisationID, priceFragmentTypeID), 183 throwExceptionIfNotExistent); 184 } 185 public PriceFragmentType getPriceFragmentType(String priceFragmentTypePK, boolean throwExceptionIfNotExistent) 186 { 187 PriceFragmentType res = (PriceFragmentType) priceFragmentTypes.get(priceFragmentTypePK); 188 if (throwExceptionIfNotExistent && res == null) 189 throw new IllegalArgumentException ("No PriceFragmentType registered with \""+priceFragmentTypePK+"\"!"); 190 return res; 191 } 192 public boolean containsPriceFragmentType(PriceFragmentType priceFragmentType) 193 { 194 return priceFragmentTypes.containsKey(priceFragmentType.getPrimaryKey()); 195 } 196 public boolean containsPriceFragmentType(String priceFragmentTypePK) 197 { 198 return priceFragmentTypes.containsKey(priceFragmentTypePK); 199 } 200 public boolean containsPriceFragmentType(String organisationID, String priceFragmentTypeID) 201 { 202 return priceFragmentTypes.containsKey( 203 PriceFragmentType.getPrimaryKey(organisationID, priceFragmentTypeID)); 204 } 205 212 public PriceFragmentType removePriceFragmentType(String organisationID, String priceFragmentTypeID) 213 { 214 return removePriceFragmentType( 215 PriceFragmentType.getPrimaryKey(organisationID, priceFragmentTypeID)); 216 } 217 223 public PriceFragmentType removePriceFragmentType(String priceFragmentTypePK) 224 { 225 return (PriceFragmentType) priceFragmentTypes.remove(priceFragmentTypePK); 226 } 227 228 241 258 public abstract Price getPrice(OfferItem offerItem); 259 260 public abstract boolean isDependentOnOffer(); 261 262 273 public abstract boolean isDependentOnProductPackage(); 274 275 278 private long nextPriceID = 0; 279 280 287 public synchronized long createPriceID() 288 { 289 long res = nextPriceID; 290 nextPriceID = res + 1; 291 return res; 292 } 293 294 297 public void jdoPreStore() 298 { 299 if (JDOHelper.isNew(this)) { 300 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 301 302 PriceFragmentType totalPFT = PriceFragmentType.getTotalPriceFragmentType(pm); 303 addPriceFragmentType(totalPFT); 304 } 305 } 306 307 } 324 | Popular Tags |