001    package market;
002    
003    import java.awt.Rectangle;
004    
005    import market.stdform.FSLogOn;
006    import sale.Action;
007    import sale.FormSheet;
008    import sale.Gate;
009    import sale.GateChangeTransition;
010    import sale.SaleProcess;
011    import sale.SalesPoint;
012    import sale.Shop;
013    import sale.UIGate;
014    import sale.stdforms.MsgForm;
015    import users.User;
016    import users.UserManager;
017    import users.stdforms.LogOnForm;
018    import users.swing.UserFilter;
019    
020    /**
021     * SaleProcess used to log on a UPerson to the system.
022     */
023    public class SProcessLogOn extends SProcessMarket {
024    
025        /**
026             * ID for serialization.
027             */
028            private static final long serialVersionUID = 3637352363865291661L;
029    
030            /**
031         * Stores which kind of person wants to log on, look also at constants in UMUserBase
032         */
033        private int userType;
034    
035        /**
036         * UserFilter used to show the right Users in a LogOnForm
037         */
038        private UserFilter uf_filter;
039    
040        /**
041         * LogOnForm used by select-user-gate
042         */
043        private LogOnForm lof_selection;
044    
045        /**
046         * Gate for selecting username.
047         */
048        private UIGate uig_selectUser = new UIGate(null, null);
049    
050        /**
051         * Gate for displaying the log on was unsuccessful.
052         */
053        private UIGate uig_logOnFailed = new UIGate(null, null);
054    
055        /**
056         * Gate for displaying the user has already logged on.
057         */
058        private UIGate uig_userIsLogged = new UIGate(null, null);
059    
060        /**
061         * Create a new SProcessLogOn
062         *
063         * @param i the kind of User who wants to log on
064         */
065        private SProcessLogOn(int i){
066            super("Log-on-Process");
067            this.userType = i;
068    
069            // choice what kind of user wants to log on
070            switch(userType){
071                case UMUserBase.CUSTOMER:
072                    uf_filter = ((UMUserBase)UserManager.getGlobalUM()).getCustomers();
073                    break;
074                case UMUserBase.WAREHOUSE_WORKER:
075                    uf_filter = ((UMUserBase)UserManager.getGlobalUM()).getWarehouseWorker();
076                    break;
077                case UMUserBase.SELLER:
078                    uf_filter = ((UMUserBase)UserManager.getGlobalUM()).getSeller();
079                    break;
080                case UMUserBase.MANAGER:
081                    uf_filter = ((UMUserBase)UserManager.getGlobalUM()).getManager();
082            }
083        }
084    
085        /**
086         * Attaches a {@link FSLogOn} and its actions to {@link #uig_selectUser}.
087         * @return the set up {@link #uig_selectUser}.
088         */
089        protected Gate getInitialGate() {
090            lof_selection = new FSLogOn(uf_filter);
091            setAction(lof_selection, new Action(){
092                            private static final long serialVersionUID = 8349332623530161898L;
093                            public void doAction(SaleProcess process, SalesPoint point) {
094                    int error = 0;
095                    lof_selection.ok();
096                    User user = lof_selection.getResult();
097                    if(UMUserBase.isLoggedOn(user)) error = 1;
098                    if(user == null) error = 2;
099                    switch (error) {
100                        case 1:
101                            uig_selectUser.setNextTransition(
102                                    new GateChangeTransition(getUserIsLoggedGate()));
103                            break;
104                        case 2:
105                            uig_selectUser.setNextTransition(
106                                    new GateChangeTransition(getLogOnFailedGate()));
107                            break;
108                        default:
109                            process.detachContext();
110                            switch(userType){
111                                case UMUserBase.CUSTOMER:
112                                    // TODO: ????????????????
113                                    //SMarket.getTheMarket().removeSalesPoint(point);
114                                    point.attach(user);
115                                    point.setSalesPointFrameBounds(new Rectangle(0,0,640,480));
116                                    point.runProcess(new SProcessCustomer((UCustomer)user));
117                                    uig_selectUser.setNextTransition(GateChangeTransition.CHANGE_TO_STOP_GATE);
118                                    break;
119                                case UMUserBase.WAREHOUSE_WORKER:
120                                    point.attach(user);
121                                    point.setSalesPointFrameBounds(new Rectangle(0,0,640,480));
122                                    SMarket.fireUpdateWorkerScreen();
123                                    point.runProcess(new SProcessWorker(((UStaffer)user).getFullName()));
124                                    uig_selectUser.setNextTransition(GateChangeTransition.CHANGE_TO_STOP_GATE);
125                                    break;
126                                case UMUserBase.SELLER:
127                                    point.attach(user);
128                                    point.setSalesPointFrameBounds(new Rectangle(0,0,640,480));
129                                    point.runProcess(new SProcessSeller(((UStaffer)user).getFullName()));
130                                    uig_selectUser.setNextTransition(GateChangeTransition.CHANGE_TO_STOP_GATE);
131                                    break;
132                                case UMUserBase.MANAGER:
133                                    point.attach(user);
134                                    point.setSalesPointFrameBounds(new java.awt.Rectangle(0,0,720,540));
135                                    point.runProcess(new SProcessManager());
136                                    uig_selectUser.setNextTransition(GateChangeTransition.CHANGE_TO_STOP_GATE);
137                                    break;
138                                }
139                            break;
140                    }
141                }}, FormSheet.BTNID_OK);
142    
143            setAction(lof_selection, new Action(){
144                            private static final long serialVersionUID = -7900596031071636204L;
145                            public void doAction(SaleProcess process, SalesPoint point) throws Throwable {
146                    uig_selectUser.setNextTransition(GateChangeTransition.CHANGE_TO_STOP_GATE);
147                    point.processFinished(process);
148                    SMarket.getTheMarket().removeSalesPoint(point);
149                }
150            }, FormSheet.BTNID_CANCEL);
151    
152            uig_selectUser.setFormSheet(lof_selection);
153            return uig_selectUser;
154        }
155    
156        /**
157         * Attaches a {@link MsgForm} and its OK-action to {@link #uig_logOnFailed}.
158         * @return the set up {@link #uig_logOnFailed}.
159         */
160        private Gate getLogOnFailedGate(){
161            FormSheet fs = new MsgForm("Anmeldung fehlgeschlagen", "Das System konnte Sie nicht anmelden!\n"
162                    + "Bitte überprüfen Sie Nutzernamen und Passwort.");
163            setTransition(fs, new GateChangeTransition(getInitialGate()), FormSheet.BTNID_OK);
164            uig_logOnFailed.setFormSheet(fs);
165            return uig_logOnFailed;
166        }
167    
168        /**
169         * Attaches a {@link MsgForm} and its OK-action to {@link #uig_userIsLogged}.
170         * @return the set up {@link #uig_userIsLogged}.
171         */
172        private Gate getUserIsLoggedGate(){
173            FormSheet fs = new MsgForm("Anmeldung fehlgeschlagen", "Sie sind bereits am System angemeldet!");
174            setTransition(fs, new GateChangeTransition(getInitialGate()), FormSheet.BTNID_OK);
175            uig_userIsLogged.setFormSheet(fs);
176            return uig_userIsLogged;
177        }
178    
179    
180        // ################################### public methods ##################################################
181    
182        /**
183         * Returns an Action that initiates a SProcessLogOn
184         *
185         * @param i what kind of user wants to log on, look at the constants of UMUserBase
186         *
187         * @see UMUserBase
188         */
189        public static Action createLogOnProcess(final int i){
190            return new Action(){
191                            private static final long serialVersionUID = 3040152719423537743L;
192                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
193                    SProcessLogOn splo = new SProcessLogOn(i);
194                    SPListenable point = new SPListenable("Sohn&Sohn");
195                     point.setSalesPointFrameBounds(new Rectangle(0,0,500,125));
196                     Shop.getTheShop().addSalesPoint(point);
197                     point.runProcess(splo);
198                }
199            };
200        }
201    }
202