001 package videoautomat.transition; 002 import sale.Gate; 003 import sale.SaleProcess; 004 import sale.Transition; 005 import users.User; 006 import videoautomat.SaleProcessRent; 007 import videoautomat.VideoShop; 008 import data.BasketEntryValue; 009 import data.CatalogItem; 010 import data.DataBasket; 011 import data.DataBasketEntry; 012 import data.IntegerValue; 013 import data.NumberValue; 014 import data.QuoteValue; 015 import data.Value; 016 import data.events.VetoException; 017 018 /** 019 * <code>Transition</code> that sums up the prices of the selected videos and leads to the 020 * {@link SaleProcessRent#getPayGate()}, if no video is selected it leads to the 021 * {@link SaleProcessRent#getInitialGate()} 022 * 023 * @author Tobias Ruch 024 */ 025 public class RentSumUpTransition implements Transition { 026 027 /** 028 * ID for Serialization. 029 */ 030 private static final long serialVersionUID = 4239789398578904163L; 031 032 /** 033 * Performes the transition. 034 * @param sp - current process 035 * @param user - current user of this process 036 * 037 * @return the new <code>Gate</code> which should be shown after the transition 038 */ 039 public Gate perform(SaleProcess sp, User user) { 040 041 042 NumberValue nv_sum = (NumberValue) sp.getBasket().sumSubBasket( 043 SaleProcessRent.SUB_SHOP_VIDEO, 044 null, 045 new SumBasketEntryValue(sp.getBasket()), 046 new IntegerValue(0)); 047 ((SaleProcessRent)sp).setSumNumberValue(nv_sum); 048 if (nv_sum.isAddZero()){ 049 return ((SaleProcessRent)sp).restart(); 050 } 051 052 sp.getBasket().setCurrentSubBasket(SaleProcessRent.SUB_TMP_MONEY); 053 return ((SaleProcessRent)sp).getPayGate(); 054 } 055 056 /** 057 * Inner class to create a <code>BasketEntryValue</code> implemenation 058 * and to avoid anonymous classes and inline declarations. 059 * 060 * @author Tobias Ruch 061 */ 062 private static class SumBasketEntryValue implements BasketEntryValue{ 063 /** DataBasket which sould be summed */ 064 private DataBasket dataBasket; 065 066 /** 067 * Creates a new instance 068 * @param db - DataBasket, which should be summed up. 069 */ 070 public SumBasketEntryValue(DataBasket db){ 071 this.dataBasket = db; 072 } 073 074 public Value getEntryValue(DataBasketEntry dbe) { 075 try { 076 CatalogItem ci = VideoShop.getVideoCatalog().get( 077 dbe.getSecondaryKey(),null, false); 078 079 int count = ((Integer) dbe.getValue()).intValue(); 080 081 return ((QuoteValue) ci.getValue()).getOffer().multiply(count); 082 083 } catch (VetoException e) { 084 e.printStackTrace(); 085 dataBasket.rollback(); 086 } 087 return null; 088 } 089 } 090 }