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 base;
21  
22  import java.io.File;
23  import java.util.Date;
24  
25  import javax.swing.UIManager;
26  
27  import org.apache.log4j.Logger;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.Node;
31  import org.w3c.dom.NodeList;
32  
33  import ui.dialog.ServerLoginDialog;
34  
35  public class ConfigurationManager implements IXMLSaveable, IXMLLoadable {
36  	private static final transient Logger logger = Logger
37  			.getLogger(ConfigurationManager.class.getName());
38  
39  	public static final boolean debugMode = true;
40  	
41  	public final boolean enableJDBS = false;
42  
43  	public static final String COPYRIGHT = "InternetCafe version Apha 1.1, Copyright (C) 2006 Guido Angelo Ingenito"
44  			+ "\n"
45  			+ "InternetCafe comes with ABSOLUTELY NO WARRANTY; for details click"
46  			+ "\n"
47  			+ "'License' from the 'Help' menu. This is free software, and you are welcome"
48  			+ "\n"
49  			+ "to redistribute it under the GPL (General Public License) conditions.";
50  
51  	public final static String LICENSE_FILE = "LICENSE.txt";
52  
53  	public final static String BACKUP_DETAILS_FILE = "BackupDetails.xml";
54  
55  	public final static String RESOURCES_DIRECTORY = ((debugMode == true) ? ".."
56  			: ".")
57  			+ File.separatorChar + "InternetCafeResources";
58  
59  	public final static String CONFIGURATION_FILE = RESOURCES_DIRECTORY
60  			+ File.separatorChar + "configuration.xml";
61  
62  	public static String USER_LIST_HTML_FILE_NAME = "UserList.html";
63  
64  	public static String USER_LIST_CSS_FILE_NAME = "user.css";
65  
66  	public static String USER_LIST_XSL_FILE_NAME = "user.xsl";
67  
68  	public static String SESSION_LIST_HTML_FILE_NAME = "SessionList.html";
69  
70  	public static String SESSION_LIST_CSS_FILE_NAME = "session.css";
71  
72  	public static String SESSION_LIST_XSL_FILE_NAME = "session.xsl";
73  
74  	private static ConfigurationManager instance;
75  
76  	private boolean passwordProtect = false;
77  
78  	private boolean displayTipOfDay = true;
79  
80  	private boolean automaticPasswordProtected = true;
81  
82  	private Class internetCafeLookAndFeel = UIManager.getLookAndFeel()
83  			.getClass();
84  
85  	private int logoutScheduleRate = 60; // * 10; // The system will try a to
86  											// log
87  
88  	// out after 10 minutes by default;
89  	private Date lastSeenUserWorking = new Date();
90  
91  	private ServerLoginDialog serverLoginDialog;
92  
93  	private String internetCafeLogoPath = RESOURCES_DIRECTORY
94  			+ File.separatorChar + "ztl.jpg";
95  
96  	private String internetCafeName = "ZTL Cafe";
97  
98  	private String dataBasePath = RESOURCES_DIRECTORY + File.separatorChar
99  			+ "ztl.yap";
100 
101 	private String userImageDirectoryPath = RESOURCES_DIRECTORY
102 			+ File.separatorChar + "images" + File.separatorChar + "users";
103 
104 	private String documentImageDirectoryPath = RESOURCES_DIRECTORY
105 			+ File.separatorChar + "images" + File.separatorChar + "documents";
106 
107 	private String htmlDirectoryPath = RESOURCES_DIRECTORY + File.separatorChar
108 			+ "html";
109 
110 	public static ConfigurationManager getInstance() {
111 		return (instance == null) ? (instance = new ConfigurationManager())
112 				: instance;
113 	}
114 
115 	/***
116 	 * This method reads from an XML document various configuration parameters
117 	 * for the Internet Cafe.
118 	 * 
119 	 * @param document
120 	 *            An XML document that contains a list of configuration
121 	 *            parameters for the Internet Cafe.
122 	 */
123 	public Object fromXml(Document document) {
124 		NodeList configurationNodeList = document
125 				.getElementsByTagName(ICXmlTags.IC_CONFIGURATION_TAG);
126 		Node configurationNode = configurationNodeList.item(0);
127 
128 		for (int i = 0; i < configurationNode.getChildNodes().getLength(); i++) {
129 			try {
130 				if (configurationNode.getChildNodes().item(i).getNodeName()
131 						.equals(ICXmlTags.IC_NAME_TAG)) {
132 					internetCafeName = configurationNode.getChildNodes()
133 							.item(i).getAttributes().getNamedItem(
134 									ICXmlTags.IC_VALUE_ATTRIBUTE)
135 							.getNodeValue();
136 				}
137 
138 				if (configurationNode.getChildNodes().item(i).getNodeName()
139 						.equals(ICXmlTags.IC_LOGO_TAG)) {
140 					internetCafeLogoPath = configurationNode.getChildNodes()
141 							.item(i).getAttributes().getNamedItem(
142 									ICXmlTags.IC_VALUE_ATTRIBUTE)
143 							.getNodeValue();
144 				}
145 
146 				if (configurationNode.getChildNodes().item(i).getNodeName()
147 						.equals(ICXmlTags.IC_DATABASE_TAG)) {
148 					dataBasePath = configurationNode.getChildNodes().item(i)
149 							.getAttributes().getNamedItem(
150 									ICXmlTags.IC_VALUE_ATTRIBUTE)
151 							.getNodeValue();
152 				}
153 
154 				if (configurationNode.getChildNodes().item(i).getNodeName()
155 						.equals(ICXmlTags.IC_USER_IMAGE_DIRECTORY_TAG)) {
156 					userImageDirectoryPath = configurationNode.getChildNodes()
157 							.item(i).getAttributes().getNamedItem(
158 									ICXmlTags.IC_VALUE_ATTRIBUTE)
159 							.getNodeValue();
160 				}
161 
162 				if (configurationNode.getChildNodes().item(i).getNodeName()
163 						.equals(ICXmlTags.IC_DOCUMENT_IMAGE_DIRECTORY_TAG)) {
164 					documentImageDirectoryPath = configurationNode
165 							.getChildNodes().item(i).getAttributes()
166 							.getNamedItem(ICXmlTags.IC_VALUE_ATTRIBUTE)
167 							.getNodeValue();
168 				}
169 
170 				if (configurationNode.getChildNodes().item(i).getNodeName()
171 						.equals(ICXmlTags.IC_DISPLAY_TIP_OF_DAY_TAG)) {
172 					displayTipOfDay = new Boolean(configurationNode
173 							.getChildNodes().item(i).getAttributes()
174 							.getNamedItem(ICXmlTags.IC_VALUE_ATTRIBUTE)
175 							.getNodeValue());
176 				}
177 
178 				if (configurationNode.getChildNodes().item(i).getNodeName()
179 						.equals(ICXmlTags.IC_LOOK_AND_FEEL_TAG)) {
180 					try {
181 						internetCafeLookAndFeel = Class
182 								.forName(configurationNode.getChildNodes()
183 										.item(i).getAttributes().getNamedItem(
184 												ICXmlTags.IC_VALUE_ATTRIBUTE)
185 										.getNodeValue());
186 					} catch (Exception ex) {
187 						logger.error(ex.getMessage());
188 						ex.printStackTrace();
189 					}
190 				}
191 			} catch (Exception ex) {
192 				ex.printStackTrace();
193 			}
194 		}
195 
196 		return this;
197 	}
198 
199 	/***
200 	 * This method writes to an XML document various configuration parameters of
201 	 * the Internet Cafe.
202 	 * 
203 	 * @param document
204 	 *            An XML document that will contain a list of configuration
205 	 *            parameters for the Internet Cafe.
206 	 */
207 	public Node toXml(Document document) {
208 		Element configurationElement = document
209 				.createElement(ICXmlTags.IC_CONFIGURATION_TAG);
210 
211 		Element nameElement = document.createElement(ICXmlTags.IC_NAME_TAG);
212 
213 		nameElement
214 				.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, internetCafeName);
215 		configurationElement.appendChild(nameElement);
216 
217 		Element logoElement = document.createElement(ICXmlTags.IC_LOGO_TAG);
218 
219 		logoElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
220 				internetCafeLogoPath);
221 		configurationElement.appendChild(logoElement);
222 
223 		Element dbElement = document.createElement(ICXmlTags.IC_DATABASE_TAG);
224 
225 		dbElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, dataBasePath);
226 		configurationElement.appendChild(dbElement);
227 
228 		Element userImageDirectoryElement = document
229 				.createElement(ICXmlTags.IC_USER_IMAGE_DIRECTORY_TAG);
230 
231 		userImageDirectoryElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
232 				userImageDirectoryPath);
233 		configurationElement.appendChild(userImageDirectoryElement);
234 
235 		Element documentImageDirectoryElement = document
236 				.createElement(ICXmlTags.IC_DOCUMENT_IMAGE_DIRECTORY_TAG);
237 
238 		documentImageDirectoryElement.setAttribute(
239 				ICXmlTags.IC_VALUE_ATTRIBUTE, documentImageDirectoryPath);
240 		configurationElement.appendChild(documentImageDirectoryElement);
241 
242 		Element tipOfDayElement = document
243 				.createElement(ICXmlTags.IC_DISPLAY_TIP_OF_DAY_TAG);
244 
245 		tipOfDayElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, ""
246 				+ displayTipOfDay);
247 		configurationElement.appendChild(tipOfDayElement);
248 
249 		Element internetCafeLookandFeelElement = document
250 				.createElement(ICXmlTags.IC_LOOK_AND_FEEL_TAG);
251 
252 		internetCafeLookandFeelElement.setAttribute(
253 				ICXmlTags.IC_VALUE_ATTRIBUTE, internetCafeLookAndFeel
254 						.getCanonicalName());
255 		configurationElement.appendChild(internetCafeLookandFeelElement);
256 
257 		return configurationElement;
258 	}
259 
260 	/***
261 	 * @return Returns the internetCafeLogoPath.
262 	 */
263 	public String getInternetCafeLogoPath() {
264 		return internetCafeLogoPath;
265 	}
266 
267 	/***
268 	 * @return Returns the internetCafeName.
269 	 */
270 	public String getInternetCafeName() {
271 		return internetCafeName;
272 	}
273 
274 	/***
275 	 * @param internetCafeLogoPath
276 	 *            The internetCafeLogoPath to set.
277 	 */
278 	public void setInternetCafeLogoPath(String internetCafeLogoPath) {
279 		this.internetCafeLogoPath = internetCafeLogoPath;
280 	}
281 
282 	/***
283 	 * @param internetCafeName
284 	 *            The internetCafeName to set.
285 	 */
286 	public void setInternetCafeName(String internetCafeName) {
287 		this.internetCafeName = internetCafeName;
288 	}
289 
290 	/***
291 	 * @return Returns the dataBasePath.
292 	 */
293 	public String getDataBasePath() {
294 		return dataBasePath;
295 	}
296 
297 	/***
298 	 * @param dataBasePath
299 	 *            The dataBasePath to set.
300 	 */
301 	public void setDataBasePath(String dataBasePath) {
302 		this.dataBasePath = dataBasePath;
303 	}
304 
305 	/***
306 	 * @return Returns the documentImageDirectoryPath.
307 	 */
308 	public String getDocumentImageDirectoryPath() {
309 		return documentImageDirectoryPath;
310 	}
311 
312 	/***
313 	 * @param documentImageDirectoryPath
314 	 *            The documentImageDirectoryPath to set.
315 	 */
316 	public void setDocumentImageDirectoryPath(String documentImageDirectoryPath) {
317 		this.documentImageDirectoryPath = documentImageDirectoryPath;
318 	}
319 
320 	/***
321 	 * @return Returns the userImageDirectoryPath.
322 	 */
323 	public String getUserImageDirectoryPath() {
324 		return userImageDirectoryPath;
325 	}
326 
327 	/***
328 	 * @param userImageDirectoryPath
329 	 *            The userImageDirectoryPath to set.
330 	 */
331 	public void setUserImageDirectoryPath(String userImageDirectoryPath) {
332 		this.userImageDirectoryPath = userImageDirectoryPath;
333 	}
334 
335 	/***
336 	 * @return Returns the passwordProtect.
337 	 */
338 	public boolean isPasswordProtect() {
339 		return passwordProtect;
340 	}
341 
342 	/***
343 	 * @param passwordProtect
344 	 *            The passwordProtect to set.
345 	 */
346 	public void setPasswordProtect(boolean passwordProtect) {
347 		logger.debug("Password Protect Setted To:" + passwordProtect);
348 		this.passwordProtect = passwordProtect;
349 	}
350 
351 	/***
352 	 * @return Returns the automaticPasswordProtected.
353 	 */
354 	public boolean isAutomaticPasswordProtected() {
355 		return automaticPasswordProtected;
356 	}
357 
358 	/***
359 	 * @param automaticPasswordProtected
360 	 *            The automaticPasswordProtected to set.
361 	 */
362 	public void setAutomaticPasswordProtect(boolean automaticPasswordProtected) {
363 		logger.debug("Automatic Password Protect Setted To:"
364 				+ automaticPasswordProtected);
365 		this.automaticPasswordProtected = automaticPasswordProtected;
366 	}
367 
368 	/***
369 	 * @return Returns the internetCafeLookAndFeel.
370 	 */
371 	public Class getInternetCafeLookAndFeel() {
372 		return internetCafeLookAndFeel;
373 	}
374 
375 	/***
376 	 * @param internetCafeLookAndFeel
377 	 *            The internetCafeLookAndFeel to set.
378 	 */
379 	public void setInternetCafeLookAndFeel(Class internetCafeLookAndFeel) {
380 		this.internetCafeLookAndFeel = internetCafeLookAndFeel;
381 	}
382 
383 	/***
384 	 * @return Returns the logoutScheduleRate.
385 	 */
386 	public int getLogoutScheduleRate() {
387 		return logoutScheduleRate;
388 	}
389 
390 	/***
391 	 * @param logoutScheduleRate
392 	 *            The logoutScheduleRate to set.
393 	 */
394 	public void setLogoutScheduleRate(int logoutScheduleRate) {
395 		logger.debug("Logout Schedule Rate Setted To:" + logoutScheduleRate
396 				+ "(s)");
397 		this.logoutScheduleRate = logoutScheduleRate;
398 	}
399 
400 	/***
401 	 * @return Returns the lastSeenUserWorking.
402 	 */
403 	public Date getLastSeenUserWorking() {
404 		return lastSeenUserWorking;
405 	}
406 
407 	/***
408 	 * @param lastSeenUserWorking
409 	 *            The lastSeenUserWorking to set.
410 	 */
411 	public void setLastSeenUserWorking(Date lastSeenUserWorking) {
412 		this.lastSeenUserWorking = lastSeenUserWorking;
413 	}
414 
415 	/***
416 	 * @return Returns the serverLoginDialog.
417 	 */
418 	public ServerLoginDialog getServerLoginDialog() {
419 		if (serverLoginDialog == null) {
420 			serverLoginDialog = new ServerLoginDialog();
421 			serverLoginDialog.setModal(true);
422 		}
423 
424 		return serverLoginDialog;
425 	}
426 
427 	/***
428 	 * @return Returns the displayTipOfDay.
429 	 */
430 	public boolean isDisplayTipOfDay() {
431 		return displayTipOfDay;
432 	}
433 
434 	/***
435 	 * @param displayTipOfDay
436 	 *            The displayTipOfDay to set.
437 	 */
438 	public void setDisplayTipOfDay(boolean displayTipOfDay) {
439 		this.displayTipOfDay = displayTipOfDay;
440 	}
441 
442 	/***
443 	 * @return Returns the htmlDirectoryPath.
444 	 */
445 	public String getHtmlDirectoryPath() {
446 		return htmlDirectoryPath;
447 	}
448 
449 	/***
450 	 * @return Returns the enableJDBS.
451 	 */
452 	public boolean isJDBSEnabled() {
453 		return enableJDBS;
454 	}
455 }