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