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        * Performes the transition.
029        * @param sp - current process
030        * @param user - current user of this process
031        * 
032        * @return the new <code>Gate</code> which should be shown after the transition    
033        */ 
034       public Gate perform(SaleProcess sp, User user) {
035          
036         
037          NumberValue nv_sum = (NumberValue) sp.getBasket().sumSubBasket(
038                                 SaleProcessRent.SUB_SHOP_VIDEO,
039                                 null, 
040                                 new SumBasketEntryValue(sp.getBasket()), 
041                                 new IntegerValue(0));
042          sp.getContext().setProcessData(SaleProcessRent.SUM_KEY, nv_sum);
043          if (nv_sum.isAddZero()){
044             return ((SaleProcessRent)sp).restart();
045          }
046          
047          sp.getBasket().setCurrentSubBasket(SaleProcessRent.SUB_TMP_MONEY);
048          return ((SaleProcessRent)sp).getPayGate();
049       }   
050       
051       /**
052        * Inner class to create a <code>BasketEntryValue</code> implemenation
053        * and to avoid anonymous classes and inline declarations.
054        *
055        * @author Tobias Ruch
056        */
057       private static class SumBasketEntryValue implements BasketEntryValue{
058          /** DataBasket which sould be summed */
059          private DataBasket dataBasket;
060          
061          /**
062           * Creates a new instance
063           * @param db - DataBasket, which should be summed up.
064           */
065          public SumBasketEntryValue(DataBasket db){
066             this.dataBasket = db;
067          }
068          
069          public Value getEntryValue(DataBasketEntry dbe) {
070             try {
071                CatalogItem ci = VideoShop.getVideoCatalog().get(
072                      dbe.getSecondaryKey(),null, false);
073                
074                int count = ((Integer) dbe.getValue()).intValue();
075                
076                return ((QuoteValue) ci.getValue()).getOffer().multiply(count);
077                
078             } catch (VetoException e) {
079                e.printStackTrace();
080                dataBasket.rollback();
081             }
082             return null;
083          }
084       }      
085    }