001    package market;
002    
003    import java.awt.Rectangle;
004    
005    import market.stdform.ButtonIDs;
006    import market.stdform.FSCheckable;
007    import market.stdform.FSEditPersonData;
008    import market.stdform.FSEmpty;
009    import sale.Action;
010    import sale.FormSheet;
011    import sale.Gate;
012    import sale.GateChangeTransition;
013    import sale.SaleProcess;
014    import sale.SalesPoint;
015    import sale.Shop;
016    import sale.Transition;
017    import sale.UIGate;
018    import sale.stdforms.MsgForm;
019    import users.User;
020    
021    /**
022     * SaleProcess that handles the new registration of a customer or
023     * the editing of personal data of an existing customer.
024     */
025    public class SProcessCustomerEditProfile extends SProcessMarket{
026    
027        /**
028             * ID for serialization.
029             */
030            private static final long serialVersionUID = 7586907211554408930L;
031    
032            /**
033         * Gate for editing profile.
034         */
035        private UIGate uig_editProfile = new UIGate(null, null);
036    
037        /**
038         * Gate for displaying the successful change of data.
039         */
040        private UIGate uig_confirmation = new UIGate(null, null);
041    
042        /**
043         * Gate for displaying the change of data was unsuccessful.
044         */
045        private UIGate uig_passwordFailed = new UIGate(null, null);
046    
047        /**
048         * The customer who interacts with this SaleProcess.
049         */
050        private UCustomer customer;
051    
052        private FSCheckable fsc_profile;
053    
054        /**
055         * Shows whether it is the first registration
056         * or only the correction of existing data.
057         */
058        private boolean firstRegistration;
059    
060        /**
061         * @param user the UCustomer who interacts with this process.
062         */
063        public SProcessCustomerEditProfile(User user){
064            super("Edit-Profile");
065            customer = (UCustomer)user;
066            if(user == null) firstRegistration = true;
067            else firstRegistration = false;
068        }
069    
070    
071        // ################################## Gates ##########################################################
072    
073        /**
074         * Attaches {@link FSEditPersonData}, its actions and the menu to {@link #uig_editProfile}.
075         * @return the set up {@link #uig_editProfile}.
076         */
077        protected Gate getInitialGate() {
078            fsc_profile = FSEditPersonData.getCustomerProfileForCustomer(customer);
079            setAction(fsc_profile, new sale.Action(){
080                            private static final long serialVersionUID = 531801823323661120L;
081                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
082                    if(fsc_profile.checkTextFields(FSCheckable.ALL_ERRORMESSAGES_AT_ONCE, true)){
083                        User usr = null;
084                        if (customer == null) {
085                            String userID = fsc_profile.getEntry(FSEditPersonData.JTFC_LOGIN);
086                            usr = UMUserBase.getGlobalBase().getUser(userID);
087                        }
088                        if (usr != null) {
089                            JDDShowMessage.showMessageDialog(fsc_profile, "Dieses Login wurde bereits " +
090                                    "vergeben, wählen sie bitte ein anderes.", "Doppeltes Login!");
091                        }
092                        else {
093                            uig_editProfile.setNextTransition(commitEdits());
094                        }
095                    }
096                }
097            }, ButtonIDs.BTN_ACCEPT);
098            setAction(fsc_profile, new sale.Action(){
099                            private static final long serialVersionUID = -7131341325569598405L;
100                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
101                    uig_editProfile.setNextTransition(GateChangeTransition.CHANGE_TO_STOP_GATE);
102                    if(firstRegistration){
103                        if(sp.getCurrentProcess()!=null) sp.processFinished(sp.getCurrentProcess());
104                        SMarket.getTheMarket().removeSalesPoint(sp);
105                    }
106                }
107            }, ButtonIDs.BTN_BACK);
108            uig_editProfile.setFormSheet(fsc_profile);
109            return uig_editProfile;
110        }
111    
112        /**
113         * Attaches a {@link MsgForm} and its OK-action to {@link #uig_confirmation}.
114         * @return the set up {@link #uig_confirmation}.
115         */
116        private Gate getConfirmationGate(){
117            FormSheet fs = new MsgForm("Daten gespeichert", "Ihre Daten wurden erfolgreich geändert!");
118            setAction(fs, new sale.Action(){
119                            private static final long serialVersionUID = 3573136633975782479L;
120                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
121                    uig_confirmation.setNextTransition(GateChangeTransition.CHANGE_TO_STOP_GATE);
122                    if(firstRegistration){
123                        if(sp.getCurrentProcess()!=null) sp.processFinished(sp.getCurrentProcess());
124                        SMarket.getTheMarket().removeSalesPoint(sp);
125                    }
126                }
127            }, FormSheet.BTNID_OK);
128            uig_confirmation.setFormSheet(fs);
129            return uig_confirmation;
130        }
131    
132        /**
133         * Attaches a {@link MsgForm} and its OK-action to {@link #uig_passwordFailed}.
134         * @return the set up {@link #uig_passwordFailed}.
135         */
136        private Gate getPasswordFailedGate(){
137            FormSheet fs = new MsgForm("Passwort überprüfen", "Sie haben entweder kein Passwort eingegeben\n"+
138            "oder die Passwörter stimmen nicht überein");
139            setTransition(fs, new GateChangeTransition(uig_editProfile), FormSheet.BTNID_OK);
140            uig_passwordFailed.setFormSheet(fs);
141            return uig_passwordFailed;
142        }
143    
144    
145        // ############################ Transitions ###########################################################
146    
147        /**
148         * @return a transition that changes to the {@link #getConfirmationGate()} if all new passwords are equal,
149         * otherwise to the {@link #getPasswordFailedGate()}.
150         */
151        private Transition commitEdits(){
152            return new Transition(){
153                            private static final long serialVersionUID = 4397760929774283052L;
154                            public Gate perform(SaleProcess pOwner, User usr) {
155                    FSEditPersonData fs = ((FSEditPersonData)fsc_profile.getFormSheet());
156                    if(fs.passwordsEqual()){
157                        if(firstRegistration){
158                            if(fs.getPassword()==null) return getPasswordFailedGate();
159                            String userID = fsc_profile.getEntry(FSEditPersonData.JTFC_LOGIN);
160                            customer = (UCustomer)UMUserBase.createUser(userID, UMUserBase.CUSTOMER, null);
161                        }
162                        customer.setSalutation(fs.getSalutation());
163                        customer.setFirstName(fsc_profile.getEntry(FSEditPersonData.JTFC_FIRSTNAME));
164                        customer.setSurname(fsc_profile.getEntry(FSEditPersonData.JTFC_NAME));
165                        customer.setTelephone(fsc_profile.getEntry(FSEditPersonData.JTFC_TELEPHONE));
166                        customer.setStreet(fsc_profile.getEntry(FSEditPersonData.JTFC_STREET));
167                        customer.setPostcode(Integer.parseInt(fsc_profile.getEntry(FSEditPersonData.JTFC_POSTCODE)));
168                        customer.setCity(fsc_profile.getEntry(FSEditPersonData.JTFC_CITY));
169                        customer.setCompany(fsc_profile.getEntry(FSEditPersonData.JTFC_COMPANY));
170                        if(fs.getPassword()!=null) customer.setPassWd(User.garblePassWD(fs.getPassword()));
171                        return getConfirmationGate();
172                     }
173                     return getPasswordFailedGate();
174                }
175            };
176        }
177    
178    
179        //################################### public methods #######################################################
180    
181        /**
182         *@return an action that creates a new {@link SPListenable} and runs a new SProcessCustomerEditProfile on it
183         */
184        public static Action create(){
185            return new sale.Action(){
186                            private static final long serialVersionUID = 8073958925405897685L;
187                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
188                    SPListenable point = new SPListenable("Sohn&Sohn"){
189                                            private static final long serialVersionUID = -2189766551487673920L;
190                                            protected FormSheet getDefaultFormSheet() {
191                            return new FSEmpty();
192                        }
193                        // Quit the SalesPoint without any question
194                        /*protected boolean onCanQuit(){
195                            return getCurrentProcess() == null;
196                        }*/
197                    };
198                    point.setSalesPointFrameBounds(new Rectangle(0,0,640,540));
199                    Shop.getTheShop().addSalesPoint(point);
200                    point.runProcess(new  SProcessCustomerEditProfile(null));
201                }
202            };
203        }
204    }