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