001    package videoautomat.transition;
002    
003    import java.util.Arrays;
004    
005    import sale.FormSheet;
006    import sale.Gate;
007    import sale.SaleProcess;
008    import sale.Transition;
009    import sale.UIGate;
010    import users.User;
011    import users.UserManager;
012    import videoautomat.AutomatUser;
013    import videoautomat.contentcreator.RegisterContentCreator;
014    /**
015     * Transition to register a new {@link users.User}.
016     *
017     * @author Tobias Ruch
018     */
019    public class RegisterOKTransition implements Transition {
020    
021       /**
022             * ID for Serialization.
023             */
024            private static final long serialVersionUID = -600112154098222643L;
025    /** Referenz the the given {@link RegisterContentCreator}*/
026    
027       private RegisterContentCreator creator;
028    
029       /**
030        * Construts a new <code>RegisterOKTransition</code> with the given ContentCreator
031        * from wich the Transition is called.
032        * @param creator - <code>RegisterContentCreator</code> which calls this {@link sale.Transition}
033        */
034       public RegisterOKTransition(RegisterContentCreator creator) {
035          this.creator = creator;
036       }
037       /**
038        * Implemeneted from the <code>Transition</code>-Interface to perform the registration
039        * @param sp - the process that triggered the Transition
040        * @param user - the user currently active in the process' ProcessContext
041        */
042       public Gate perform(SaleProcess sp, User user) {
043          //input validation
044    
045          StringBuffer errors = new StringBuffer("");
046    
047          // if no user name
048          if ("".equals(creator.getUserName())){
049             errors.append("You have to choose a user name!\n");
050          }
051    
052          //different passwords
053          if (!Arrays.equals(creator.getPassword(), creator.getConfirmedPassword())){
054             errors.append("The passwords are different!\n");
055    
056          }
057    
058          if (UserManager.getGlobalUM().getUserNames().contains(creator.getUserName())){
059             errors.append("User already exists!\n");
060          }
061    
062          // an error occurred
063          if (errors.length() != 0){
064             // set error message
065             creator.setErrorMessage(errors.toString());
066             // create new formsheet and reuse the old Content Creator
067             FormSheet register = new FormSheet("Register", creator, false);
068             // return the gate
069             return new UIGate(register, null);
070          }
071    
072    
073          //everything is ok: create the new user
074          UserManager.getGlobalUM().addUser(new AutomatUser(creator.getUserName(), creator.getPassword(), false));
075    
076          // return the commit gate
077          return sp.getCommitGate();
078       }
079    
080    }