001    package market;
002    
003    import market.stdform.ButtonIDs;
004    import market.stdform.FSCustomerConfirmSelection;
005    import market.stdform.FSCustomerOfferTable;
006    import market.stdform.MSLogOff;
007    import sale.FormSheet;
008    import sale.Gate;
009    import sale.GateChangeTransition;
010    import sale.SaleProcess;
011    import sale.SalesPoint;
012    import sale.Transition;
013    import sale.UIGate;
014    import sale.stdforms.MsgForm;
015    import users.User;
016    import data.CountingStock;
017    import data.IntegerValue;
018    import data.Value;
019    
020    /**
021     * The customer process. This process handles a customer's purchase.
022     */
023    public class SProcessCustomer extends SProcessMarket{
024    
025        /**
026         * The customer who interacts with this SaleProcess.
027         */
028        private UCustomer uc_customer;
029    
030        /**
031         * Gate for selecting items to purchase.
032         */
033        private UIGate uig_selection = new UIGate(null, null);
034    
035        /**
036         * Gate for affirming or canceling the purchase.
037         */
038        private UIGate uig_commit = new UIGate(null, null);
039    
040        /**
041         * Gate for displaying the successful purchase.
042         */
043        private UIGate uig_ok = new UIGate(null, null);
044    
045        /**
046         * @param user the customer who performs the purchase.
047         */
048        public SProcessCustomer(User user){
049            super("buy");
050            uc_customer = (UCustomer)(user);
051        }
052    
053        // ################################## Gates ##########################################################
054    
055        /**
056         * Attaches {@link FSCustomerOfferTable}, its actions and the menu to {@link #uig_selection}.
057         * @return the set up {@link #uig_selection}.
058         */
059        protected Gate getInitialGate() {
060    
061            final FormSheet fs = FSCustomerOfferTable.getOfferFormSheet((CountingStock)SMarket.getOffer(),
062                    uc_customer.getShoppingBasket(),
063                    this.getBasket(),
064                    uig_selection);
065            setAction(fs, new sale.Action(){
066                public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
067                    if(uc_customer.getShoppingBasket().size(getBasket())==0){
068                        JDDShowMessage.showMessageDialog(fs, "Bitte wählen sie Artikel zum Kauf aus.",
069                                "Keine Auswahl getroffen");
070                    }
071                    else uig_selection.setNextTransition(GateChangeTransition.CHANGE_TO_COMMIT_GATE);
072                }
073            }, ButtonIDs.BTN_BUY);
074            setTransition(fs, GateChangeTransition.CHANGE_TO_ROLLBACK_GATE, ButtonIDs.BTN_BACK);
075            uig_selection.setFormSheet(fs);
076            uig_selection.setMenuSheet(new MSLogOff());
077            return uig_selection;
078        }
079    
080        /**
081         * Attaches {@link FSCustomerConfirmSelection}, its actions and the menu to {@link #uig_commit}.
082         * @return the set up {@link #uig_commit}.
083         */
084        public Gate getCommitGate() {
085            FormSheet fs = new FSCustomerConfirmSelection(getAmount());
086            setTransition(fs, commit(), ButtonIDs.BTN_BUY);
087            setTransition(fs, new GateChangeTransition(getInitialGate()), ButtonIDs.BTN_BACK);
088            uig_commit.setFormSheet(fs);
089            return uig_commit;
090        }
091    
092        /**
093         * Attaches a {@link MsgForm} and its OK-action to {@link #uig_ok}.
094         * @return the set up {@link #uig_ok}.
095         */
096        public Gate getOkGate() {
097            FormSheet fs = new MsgForm("Auswahl bestätigt!", "Bitte begeben Sie sich zur Kasse!");
098            setTransition(fs, changeToStopGate(), FormSheet.BTNID_OK);
099            uig_ok.setFormSheet(fs);
100            return uig_ok;
101        }
102    
103    
104        // ############################ Transitions ###########################################################
105    
106        /**
107         * @return a Transition that changes to the {@link #getStopGate()} and adds the customer to the global till-queue.
108         */
109        private Transition changeToStopGate(){
110            return new Transition(){
111                public Gate perform(SaleProcess pOwner, User usr) {
112                    SICustomer.addToTillQueue(uc_customer);
113                    return getStopGate();
114                }
115            };
116        }
117    
118        /**
119         * @return a transition that commits the databasket and changes to the {@link #getOkGate()}.
120         */
121        private Transition commit(){
122            return new Transition(){
123                public Gate perform(SaleProcess pOwner, User usr) {
124                    pOwner.getBasket().commit();
125                    return getOkGate();
126                }
127            };
128        }
129    
130        //################################# private methods ########################################################
131    
132        /**
133         * @return the Value of the articles in the customers shoppingbasket.
134         */
135        private Value getAmount(){
136            return uc_customer.getShoppingBasket().sumStock(this.getBasket(), CIArticle.getCatalogItemValue(), new IntegerValue(0));
137        }
138    }