001    package videoautomat;
002    import sale.*;
003    import users.ActionCapability;
004    import users.User;
005    import users.UserManager;
006    import users.stdforms.LogOnForm;
007    import videoautomat.gui.LogOn;
008    import data.ooimpl.DataBasketImpl;
009    
010    /**
011     * This class implements a <code>SaleProcess</code> used to log on and choose which activity should follow.
012     *  
013     */
014    public class SaleProcessLogOn extends SaleProcess {
015            
016            /*
017             * The initial Gate of this process, at which the user selects it`s ID and type in the password
018             */
019            private UIGate uig_log_on = new UIGate(null, null);
020    
021            /*
022             * This Gate is set if the user selected no name or typed in the wrong password
023             */
024            private UIGate uig_fault = new UIGate(null, null);
025    
026            /*
027             * At this Gate the user selects his/her further activities e.g. renting/handing back a video or administrative
028             * stuff
029             */
030            private UIGate uig_main = new UIGate(null, null);
031            
032            /*
033             * A LogOnForm used for the uig_log_on
034             */
035            private LogOnForm lof_initial = null;
036            /*
037             * Refers to the current user
038             */
039            private User u_current = null;
040            /**
041             * Constructs a new <code>SaleProcessLogOn</code>
042             *  
043             */
044            public SaleProcessLogOn() {
045                    super("SaleProcessLogOn");
046            }
047    
048            /**
049             * Implementation of the inherited abstract method.
050             * 
051             * @return a <code>Gate</code> where the user selects it`s user-name and is asked for his/her password.
052             * 
053             * @see sale.SaleProcess#getInitialGate()
054             */
055            protected Gate getInitialGate() {
056                    lof_initial = LogOn.getLogOnForm(true, UserManager.getGlobalUM(), null, null);
057                    lof_initial.addContentCreator(new FormSheetContentCreator() {
058                            protected void createFormSheetContent(FormSheet sheet) {
059                                    sheet.getButton(FormSheet.BTNID_OK).setAction(new sale.Action() {
060                                            public void doAction(SaleProcess process, SalesPoint point) {
061                                                    uig_log_on.setNextTransition(logOn());
062                                            }
063                                    });
064                                    sheet.getButton(FormSheet.BTNID_CANCEL).setAction(new sale.Action() {
065                                            public void doAction(SaleProcess process, SalesPoint point) {
066                                                    uig_log_on.setNextTransition(
067                                                            GateChangeTransition.CHANGE_TO_STOP_GATE);
068                                            }
069                                    });
070                            }
071                    });
072                    uig_log_on.setFormSheet(lof_initial);
073                    return uig_log_on;
074            }
075            /**
076             * @return a <code>Gate</code> that shows an error-message.
077             */
078            private Gate getFaultGate() {
079                    FormSheet fs_fault = LogOn.getFaultFormSheet();
080    
081                    fs_fault.addContentCreator(new FormSheetContentCreator() {
082                            protected void createFormSheetContent(FormSheet sheet) {
083                                    sheet.getButton(FormSheet.BTNID_OK).setAction(new sale.Action() {
084                                            public void doAction(SaleProcess process, SalesPoint point) {
085                                                    uig_fault.setNextTransition(
086                                                            new GateChangeTransition(getInitialGate()));
087                                            }
088                                    });
089                            }
090                    });
091    
092                    uig_fault.setFormSheet(fs_fault);
093                    return uig_fault;
094            }
095            /**
096             * @return a <code>Gate</code> where the user can select the next activity, like renting a video.
097             */
098            private Gate getMainGate() {
099                    FormSheet fs_main =
100                            LogOn.getMainFormSheet(
101                                    VideoShop.getVideoStock(),
102                                    uig_main,
103                                    false,
104                                    new TEDVideoStock());
105                    fs_main.addContentCreator(new FormSheetContentCreator() {
106                            protected void createFormSheetContent(FormSheet fs) {
107                                    fs.getButton(LogOn.FB_RENT).setAction(new sale.Action() {
108                                            public void doAction(SaleProcess p, SalesPoint sp) {
109                                                    sp.runProcess(new SaleProcessRent(), new DataBasketImpl());
110                                            }
111                                    });
112                                    fs.getButton(LogOn.FB_HANDBACK).setAction(new sale.Action() {
113                                            public void doAction(SaleProcess p, SalesPoint sp) {
114                                                    sp.runProcess(new SaleProcessHandBack(), new DataBasketImpl());
115                                            }
116                                    });
117                                    fs.getButton(LogOn.FB_ADMIN).setAction(
118                                            (ActionCapability) u_current.getCapability(
119                                                    AutomatUser.CAPABILITY_ADMIN));
120                                    fs.getButton(LogOn.FB_LOGOUT).setAction(new sale.Action() {
121                                            public void doAction(SaleProcess p, SalesPoint sp) {
122                                                    sp.detachUser();
123                                                    uig_main.setNextTransition(
124                                                            GateChangeTransition.CHANGE_TO_STOP_GATE);
125                                            }
126                                    });
127                            }
128                    });
129                    uig_main.setFormSheet(fs_main);
130                    return uig_main;
131            }
132            /**
133             * @return a <code>Transition</code> that proves the selected name and password and with success leads to the
134             *              {@link SaleProcessLogOn#getMainGate()}, otherwise it returns the
135             *              {@link SaleProcessLogOn#getFaultGate()}.
136             */
137            private Transition logOn() {
138                    return new Transition() {
139                            public Gate perform(SaleProcess process, User user) {
140                                    lof_initial.ok();
141                                    u_current = lof_initial.getResult();
142                                    if (u_current != null) {
143                                            ((SalesPoint) process.getContext()).attach(u_current);                                  
144                                            return getMainGate();
145                                    }
146                                    return getFaultGate();
147                            }
148                    };
149            }
150    }