001 package videoautomat.transition; 002 003 import java.util.Iterator; 004 005 import sale.Gate; 006 import sale.SaleProcess; 007 import sale.SalesPoint; 008 import sale.Transition; 009 import users.User; 010 import videoautomat.AutomatUser; 011 import videoautomat.DisplayMoneyStockError; 012 import videoautomat.SaleProcessRent; 013 import videoautomat.UserVideoStock; 014 import videoautomat.VideoCassette; 015 import videoautomat.VideoShop; 016 import data.CatalogItemValue; 017 import data.DataBasket; 018 import data.DataBasketConditionImpl; 019 import data.IntegerValue; 020 import data.MoneyBag; 021 import data.NotEnoughMoneyException; 022 import data.NumberValue; 023 import data.ooimpl.CountingStockItemDBEntry; 024 025 /** 026 *<code>Transition</code> that temporary adds the selected videos to the 027 *{@link AutomatUser#getVideoStock()}, the inserted money to the {@link VideoShop#getVideoStock()} 028 *and from there transacts the change money 029 * @author Tobias Ruch 030 */ 031 032 public class RentPayConfirmTransition implements Transition { 033 034 /** 035 * ID for Serialization. 036 */ 037 private static final long serialVersionUID = -7502900682335096830L; 038 039 /** 040 * Performes the transition. 041 * @param sp - current process 042 * @param user - current user of this process 043 * 044 * @return the new <code>Gate</code> which should be shown after the transition 045 */ 046 047 public Gate perform(SaleProcess sp, User user) { 048 049 050 SaleProcessRent saleProcess = (SaleProcessRent) sp; 051 DataBasket dataBasket = saleProcess.getBasket(); 052 MoneyBag mb_temp = saleProcess.getTemporaryMoneyBag(); 053 /* 054 * first add new rent-cassettes to the user`s stock 055 */ 056 057 058 dataBasket.setCurrentSubBasket(SaleProcessRent.SUB_USER_VIDEO); 059 UserVideoStock ss_user = 060 ((AutomatUser) ((SalesPoint) saleProcess.getContext()).getUser()) 061 .getVideoStock(); 062 063 Iterator i = 064 dataBasket.subBasketIterator( 065 SaleProcessRent.SUB_SHOP_VIDEO, 066 DataBasketConditionImpl.ALL_ENTRIES); 067 while (i.hasNext()) { 068 VideoCassette vc = 069 new VideoCassette( 070 ((CountingStockItemDBEntry) i.next()) 071 .getSecondaryKey()); 072 ss_user.add(vc, dataBasket); 073 } 074 /* 075 * calculate what is in the temporar moneybag 076 */ 077 078 dataBasket.setCurrentSubBasket(SaleProcessRent.SUB_TMP_MONEY); 079 NumberValue nv = (NumberValue)((NumberValue) mb_temp.sumStock(dataBasket, 080 new CatalogItemValue(), 081 new IntegerValue(0))). 082 subtract(saleProcess.getSumNumberValue()); 083 /* 084 * this prevents an exception that is caused by removing all items from the table 085 * while it is shown 086 */ 087 try { 088 saleProcess.getContext().setFormSheet(saleProcess, null); 089 } catch (InterruptedException e1) { 090 e1.printStackTrace(); 091 } 092 /* 093 * put the content of the temporar moneybag to the shop`s one and get the change 094 */ 095 VideoShop.getMoneyBag().addStock(mb_temp, dataBasket, true); 096 dataBasket.setCurrentSubBasket(SaleProcessRent.SUB_SHOP_MONEY); 097 try{ 098 VideoShop.getMoneyBag().transferMoney(mb_temp, dataBasket, nv); 099 }catch(NotEnoughMoneyException e){ 100 dataBasket.rollbackSubBasket(SaleProcessRent.SUB_USER_VIDEO); 101 dataBasket.rollbackSubBasket(SaleProcessRent.SUB_TMP_MONEY); 102 dataBasket.rollbackSubBasket(SaleProcessRent.SUB_SHOP_MONEY); 103 new DisplayMoneyStockError(); 104 dataBasket.setCurrentSubBasket(SaleProcessRent.SUB_TMP_MONEY); 105 return saleProcess.getPayGate(); 106 } 107 108 return saleProcess.getConfirmGate(); 109 110 } 111 112 }