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