| 1 4 package com.nightlabs.ipanema.trade; 5 6 import java.io.Serializable ; 7 import java.util.Date ; 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 32 public class OfferItem 33 implements Serializable  34 { 35 39 private String organisationID; 40 41 44 private long offerID; 45 46 52 private String productOrganisationID; 53 54 58 private String productID; 59 60 63 private String primaryKey; 64 65 68 private Offer offer; 69 70 75 private Invoice invoice = null; 76 77 80 private Product product; 81 82 85 private OfferItemPrice price; 86 87 92 private boolean priceDependentOnOffer; 93 94 104 private boolean refund = false; 105 106 114 private OfferItem originalOfferItemToRefund = null; 115 116 private Date autoReleaseDT = null; 117 118 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 ("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 ("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 ("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 ("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 185 public String getOrganisationID() 186 { 187 return organisationID; 188 } 189 192 public long getOfferID() 193 { 194 return offerID; 195 } 196 199 public String getProductOrganisationID() 200 { 201 return productOrganisationID; 202 } 203 206 public String getProductID() 207 { 208 return productID; 209 } 210 public static String getPrimaryKey( 211 String organisationID, long offerID, String productOrganisationID, String productID) 212 { 213 return organisationID + '/' + Long.toHexString(offerID) + '/' + productOrganisationID + '/' + productID; 214 } 215 public String getPrimaryKey() { 216 return primaryKey; 217 } 218 221 public Product getProduct() 222 { 223 return product; 224 } 225 228 public OfferItemPrice getPrice() 229 { 230 return price; 231 } 232 235 public boolean isPriceDependentOnOffer() 236 { 237 return priceDependentOnOffer; 238 } 239 242 protected void setPriceDependentOnOffer(boolean priceDependentOnOffer) 243 { 244 this.priceDependentOnOffer = priceDependentOnOffer; 245 } 246 249 public Offer getOffer() 250 { 251 return offer; 252 } 253 256 public OfferItem getOriginalOfferItemToRefund() 257 { 258 return originalOfferItemToRefund; 259 } 260 263 public boolean isRefund() 264 { 265 return refund; 266 } 267 268 276 public Date getAutoReleaseDT() 277 { 278 return autoReleaseDT; 279 } 280 281 287 public void allocate(User user, Date autoReleaseDT) 288 throws ModuleException 289 { 290 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 291 if (pm == null) 292 throw new IllegalStateException ("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 ("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 |