View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.command.IO;
21  
22  import java.util.Date;
23  
24  import ui.command.Command;
25  import ui.panel.UserPanel;
26  import base.Control;
27  import base.InternetCafeManager;
28  import base.user.Document;
29  import base.user.User;
30  import base.user.UserFactory;
31  
32  public class SaveUserCommand extends Command {
33  
34  	private UserPanel userPanel;
35  
36  	private User user;
37  
38  	private boolean createNewUser;
39  
40  	private String name;
41  
42  	private String surname;
43  
44  	private String gender;
45  
46  	private Date birthday;
47  
48  	private String nickname;
49  
50  	private String password;
51  
52  	private String retypedPassword;
53  
54  	private String credential;
55  
56  	private String imagePath;
57  
58  	private Document document;
59  
60  	/***
61  	 * @param userPanel
62  	 *            The UserPanel instance from wich must retrieve the User's
63  	 *            data.
64  	 * @param user
65  	 *            The instance of user that will be the result of this command.
66  	 * @param createNewUser
67  	 *            States if that Command must create a new user or modify the
68  	 *            User's data of an existing User instance.
69  	 */
70  	public SaveUserCommand(UserPanel userPanel, User user, boolean createNewUser) {
71  		this.userPanel = userPanel;
72  		this.user = user;
73  		this.createNewUser = createNewUser;
74  	}
75  
76  	/*
77  	 * (non-Javadoc)
78  	 * 
79  	 * @see ui.command.Command#prologo()
80  	 */
81  	@Override
82  	protected void prologo() {
83  		name = getUserPanel().getUserName();
84  		surname = getUserPanel().getUserSurname();
85  		gender = getUserPanel().getUserGender();
86  		birthday = getUserPanel().getUserBirthday();
87  		nickname = getUserPanel().getUserNickname();
88  		password = getUserPanel().getUserPassword();
89  		retypedPassword = getUserPanel().getRetypedPassword();
90  		credential = getUserPanel().getUserCredential();
91  		imagePath = getUserPanel().getUserImagePath();
92  		document = getUserPanel().getUserDocument();
93  		if (isValidUserData(name, surname, gender, birthday, nickname,
94  				password, retypedPassword, credential, imagePath, document))
95  			setStatus(EXECUTE_STATUS);
96  		else
97  			setStatus(ERROR_STATUS);
98  	}
99  
100 	private boolean isValidUserData(String name, String surname, String gender,
101 			Date birthday, String nickname, String password,
102 			String retypedPassword, String credential, String imagePath,
103 			Document document) {
104 		if (!Control.isValidUserName(name)) {
105 			getUserPanel().setUserNameError(true);
106 			return false;
107 		} else
108 			getUserPanel().setUserNameError(false);
109 
110 		if (!Control.isValidUserSurname(surname)) {
111 			getUserPanel().setUserSurnameError(true);
112 			return false;
113 		} else
114 			getUserPanel().setUserSurnameError(false);
115 
116 		if (!Control.isValidBirthdayDate(birthday)) {
117 			getUserPanel().setUserBirthdayError(true);
118 			return false;
119 		} else
120 			getUserPanel().setUserBirthdayError(false);
121 
122 		if (!Control.isValidUserNickname(nickname)) {
123 			getUserPanel().setUserNicknameError(true);
124 			return false;
125 		} else
126 			getUserPanel().setUserNicknameError(false);
127 
128 		if (!password.equals(retypedPassword)) {
129 			getUserPanel().setUserPasswordError(true);
130 			return false;
131 		} else
132 			getUserPanel().setUserPasswordError(false);
133 
134 		if (!Control.isValidUserPassword(password)) {
135 			getUserPanel().setUserPasswordError(true);
136 			return false;
137 		} else
138 			getUserPanel().setUserPasswordError(false);
139 
140 		if (!Control.isValidUserDocument(document)) {
141 			getUserPanel().setUserDocumentError(true);
142 			return false;
143 		} else
144 			getUserPanel().setUserDocumentError(false);
145 
146 		return true;
147 	}
148 
149 	/*
150 	 * (non-Javadoc)
151 	 * 
152 	 * @see ui.command.Command#execution()
153 	 */
154 	@Override
155 	protected void execution() throws Exception {
156 		switch (getStatus()) {
157 		case ERROR_STATUS:
158 			break;
159 		case VETOED_STATUS:
160 			break;
161 		case EXECUTE_STATUS:
162 			if (createNewUser) {
163 				user = UserFactory.newUser(name, surname, gender, birthday,
164 						nickname, password, credential, document, imagePath);
165 				InternetCafeManager.getInstance().addUser(user);
166 				setStatus(SUCCESS_STATUS);
167 			} else {
168 				user.setName(name);
169 				user.setSurname(surname);
170 				user.setGender(gender);
171 				user.setBirthday(birthday);
172 				user.setNickname(nickname);
173 				user.setPassword(password);
174 				user.setCredential(credential);
175 				user.setImagePath(imagePath);
176 				user.setDocument(document);
177 				setStatus(SUCCESS_STATUS);
178 			}
179 			break;
180 		}
181 	}
182 
183 	/***
184 	 * @return Returns the userPanel.
185 	 */
186 	private UserPanel getUserPanel() {
187 		return userPanel;
188 	}
189 
190 }