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.io.File;
23  import java.io.PrintStream;
24  
25  import javax.swing.JOptionPane;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.transform.Transformer;
28  import javax.xml.transform.TransformerFactory;
29  import javax.xml.transform.dom.DOMSource;
30  import javax.xml.transform.stream.StreamResult;
31  
32  import org.apache.log4j.Logger;
33  import org.w3c.dom.Document;
34  
35  import ui.Messages;
36  import ui.command.Command;
37  import ui.command.CommandExecutor;
38  import base.ConfigurationManager;
39  import base.InternetCafe;
40  import base.InternetCafeManager;
41  
42  public class ExitCommand extends Command {
43  
44  	private static final transient Logger logger = Logger
45  			.getLogger(ExitCommand.class.getName());
46  
47  	private boolean saveChanges = true;
48  
49  	public void prologo() {
50  		InternetCafe.setStatusBarMessage(Messages.getString("command.exitcommand.exiting")); //$NON-NLS-1$
51  		Object[] options = { Messages.getString("command.option.exit.yesdiscard"), //$NON-NLS-1$
52  				Messages.getString("command.option.exit.yessaving"), Messages.getString("command.option.exit.cancel") }; //$NON-NLS-1$ //$NON-NLS-2$
53  		int result = JOptionPane.showOptionDialog(null,
54  				Messages.getString("command.exitcommand.message1"), Messages.getString("common.exit"), //$NON-NLS-1$ //$NON-NLS-2$
55  				JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
56  				null, options, options[1]);
57  		if (result == JOptionPane.YES_OPTION) {
58  			saveChanges = false;
59  			setStatus(EXECUTE_STATUS);
60  		} else if (result == JOptionPane.NO_OPTION) {
61  			saveChanges = true;
62  			setStatus(EXECUTE_STATUS);
63  		} else
64  			setStatus(ABORT_STATUS);
65  	}
66  
67  	public void execution() throws Exception {
68  		switch (getStatus()) {
69  		case ABORT_STATUS:
70  			break;
71  		case VETOED_STATUS:
72  			break;
73  		case EXECUTE_STATUS:
74  			if (saveChanges) {
75  				try {
76  					// Save the current configuration...
77  					File configurationFile = new File(
78  							ConfigurationManager.CONFIGURATION_FILE);
79  					if (configurationFile.exists()) {
80  						configurationFile.delete();
81  						configurationFile.createNewFile();
82  					}
83  					Document doc = DocumentBuilderFactory.newInstance()
84  							.newDocumentBuilder().newDocument();
85  					doc.appendChild(ConfigurationManager.getInstance().toXml(
86  							doc));
87  					String fileName = configurationFile.getAbsolutePath();
88  					if (!fileName.endsWith("."+Messages.getString("common.filetype.xml"))) //$NON-NLS-1$ //$NON-NLS-2$
89  						fileName += "."+Messages.getString("common.filetype.xml"); //$NON-NLS-1$ //$NON-NLS-2$
90  					Transformer transformer = TransformerFactory.newInstance()
91  							.newTransformer();
92  					DOMSource source = new DOMSource(doc);
93  					StreamResult streamResult = new StreamResult(
94  							new PrintStream(fileName));
95  					transformer.transform(source, streamResult);
96  
97  					// We must save all to the database
98  					InternetCafeManager.getInstance().store();
99  					
100 					//If JDBS is integrated we must call its ExitCommand
101 					CommandExecutor.getInstance().executeCommand(new base.jdbs.ui.command.ExitCommand(),true);
102 					
103 					setStatus(SUCCESS_STATUS);
104 					System.exit(0);
105 				} catch (Exception ex) {
106 					logger.error(ex.getMessage());
107 					ex.printStackTrace();
108 					setStatus(ERROR_STATUS);
109 					
110 				}
111 			} else{
112 				setStatus(SUCCESS_STATUS);
113 				System.exit(0);
114 			}
115 			break;
116 		}
117 	}
118 }