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 /** Referenz the the given {@link RegisterContentCreator}*/
022
023 private RegisterContentCreator creator;
024
025 /**
026 * Construts a new <code>RegisterOKTransition</code> with the given ContentCreator
027 * from wich the Transition is called.
028 * @param creator - <code>RegisterContentCreator</code> which calls this {@link sale.Transition}
029 */
030 public RegisterOKTransition(RegisterContentCreator creator) {
031 this.creator = creator;
032 }
033 /**
034 * Implemeneted from the <code>Transition</code>-Interface to perform the registration
035 * @param sp - the process that triggered the Transition
036 * @param user - the user currently active in the process' ProcessContext
037 */
038 public Gate perform(SaleProcess sp, User user) {
039 //input validation
040
041 StringBuffer errors = new StringBuffer("");
042
043 // if no user name
044 if ("".equals(creator.getUserName())){
045 errors.append("You have to choose a user name!\n");
046 }
047
048 //different passwords
049 if (!Arrays.equals(creator.getPassword(), creator.getConfirmedPassword())){
050 errors.append("The passwords are different!\n");
051
052 }
053
054 if (UserManager.getGlobalUM().getUserNames().contains(creator.getUserName())){
055 errors.append("User already exists!\n");
056 }
057
058 // an error occurred
059 if (errors.length() != 0){
060 // set error message
061 creator.setErrorMessage(errors.toString());
062 // create new formsheet and reuse the old Content Creator
063 FormSheet register = new FormSheet("Register", creator, false);
064 // return the gate
065 return new UIGate(register, null);
066 }
067
068
069 //everything is ok: create the new user
070 UserManager.getGlobalUM().addUser(new AutomatUser(creator.getUserName(), creator.getPassword(), false));
071
072 // return the commit gate
073 return sp.getCommitGate();
074 }
075
076 }