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