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 base;
21
22 import java.io.File;
23 import java.util.Date;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import base.user.Document;
28 import base.user.User;
29 import base.user.UserCredential;
30
31 public class Control {
32 public static boolean isValidUserName(String name) {
33 if ((name == null) || (name.length() == 0)) {
34 return false;
35 }
36
37 Pattern pattern = Pattern
38 .compile("([A-Za-z]*[ //t//n//x0B//f//r]{0,1})*");
39 Matcher matcher = pattern.matcher(name);
40
41 return matcher.matches();
42 }
43
44 public static boolean isValidUserSurname(String surname) {
45 if ((surname == null) || (surname.length() == 0)) {
46 return false;
47 }
48
49 Pattern pattern = Pattern
50 .compile("([A-Za-z]*[ //t//n//x0B//f//r]{0,1})*");
51 Matcher matcher = pattern.matcher(surname);
52
53 return matcher.matches();
54 }
55
56 public static boolean isValidUserNickname(String nickname) {
57 if ((nickname == null) || (nickname.length() == 0)) {
58 return false;
59 }
60
61 return isValidCharacterOnlyString(nickname);
62 }
63
64 public static boolean isValidBirthdayDate(Date birthday) {
65 return (birthday != null) && birthday.before(new Date());
66 }
67
68 public static boolean isValidCharacterOnlyString(String string) {
69 if ((string == null) || (string.length() == 0)) {
70 return false;
71 }
72
73 Pattern pattern = Pattern.compile("[A-Za-z]*");
74 Matcher matcher = pattern.matcher(string);
75
76 return matcher.matches();
77 }
78
79 public static boolean isValidDoubleString(String string) {
80 if ((string == null) || (string.length() == 0)) {
81 return false;
82 }
83
84 Pattern pattern = Pattern.compile("[+-]?[0-9]*[//.]{0,1}[0-9]*");
85 Matcher matcher = pattern.matcher(string);
86
87 return matcher.matches();
88 }
89
90 public static boolean isValidIntegerOnlyString(String string) {
91 if ((string == null) || (string.length() == 0)) {
92 return false;
93 }
94
95 Pattern pattern = Pattern.compile("[0-9]*");
96 Matcher matcher = pattern.matcher(string);
97
98 return matcher.matches();
99 }
100
101 public static boolean isValidEMailAddress(String email) {
102 Pattern pattern = Pattern
103 .compile("[A-Za-z]*[//.]{0,1}[A-Za-z0-9]*@[A-Za-z0-9]*//.[A-Za-z]*");
104 Matcher matcher = pattern.matcher(email);
105
106 return matcher.matches();
107 }
108
109 public static boolean isValidUDPPort(int port) {
110 return (port > 1024) && (port < 65536);
111 }
112
113 public static boolean isValidDocumentDate(Date releaseDate,
114 Date expirationDate) {
115 Date currentDate = new Date();
116
117 return (releaseDate != null) && (expirationDate != null)
118 && releaseDate.before(expirationDate)
119 && releaseDate.before(currentDate);
120 }
121
122 public static boolean isValidSessionDate(Date startTime, Date endTime) {
123 return (startTime != null) && (endTime != null)
124 && startTime.before(endTime);
125 }
126
127 public static boolean isValidIPAddress(String address) {
128 Pattern pattern = Pattern
129 .compile("([0-9]{1,3})//.([0-9]{1,3})//.([0-9]{1,3})//.([0-9]{1,3})");
130 Matcher matcher = pattern.matcher(address);
131
132 return matcher.matches();
133 }
134
135 public static boolean isValidUserPassword(String password) {
136 return (password != null) && (password.length() > 0);
137 }
138
139 public static boolean isValidUserDocument(Document document) {
140 if (document == null) {
141 return false;
142 }
143
144 if (!isValidDocumentDate(document.getRelease(), document
145 .getExpiration())
146 || !isValidDocumentNumber(document.getType(), document
147 .getNumber())) {
148 return false;
149 }
150
151 return true;
152 }
153
154 public static boolean isValidDocumentImage(String imagePath) {
155 if ((imagePath == null) || (imagePath == "")
156 || !new File(imagePath).exists()) {
157 return false;
158 }
159
160 return true;
161 }
162
163 public static boolean isValidDocumentNumber(String documentType,
164 String documentNumber) {
165
166 if ((documentNumber == null) || (documentNumber.length() == 0)) {
167 return false;
168 }
169
170 return true;
171 }
172
173 public static boolean isValidServiceMinMaxDuration(
174 int minDurationInMinutes, boolean careAboutMinDuration,
175 int maxDurationInMinutes, boolean careAboutMaxDuration) {
176 if ((minDurationInMinutes < 0) || (maxDurationInMinutes < 0)) {
177 return false;
178 }
179
180 if (careAboutMinDuration && !(minDurationInMinutes >= 0)) {
181 return false;
182 }
183
184 if (careAboutMaxDuration && !(maxDurationInMinutes > 0)) {
185 return false;
186 }
187
188 if (careAboutMinDuration && careAboutMaxDuration) {
189 return maxDurationInMinutes >= minDurationInMinutes;
190 }
191
192 return true;
193 }
194
195 public static boolean areValidAdministratorCredentials(String nickname,
196 String password) {
197 User[] user = InternetCafeManager.getInstance().getUser();
198
199 for (int i = 0; i < user.length; i++) {
200 if (user[i].getNickname().equals(nickname)) {
201 return user[i]
202 .getCredential()
203 .equals(
204 UserCredential.USER_CREDENTIAL[UserCredential.ADMINISTRATOR])
205 && user[i].getPassword().equals(
206 User.buildUserPassword(password));
207 }
208 }
209
210 return false;
211 }
212 }