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  
21  package ui.command.IO;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.FileOutputStream;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.transform.Transformer;
32  import javax.xml.transform.TransformerFactory;
33  import javax.xml.transform.dom.DOMSource;
34  import javax.xml.transform.stream.StreamResult;
35  import javax.xml.transform.stream.StreamSource;
36  
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  
40  import ui.Messages;
41  import ui.command.Command;
42  import base.ConfigurationManager;
43  import base.ICXmlTags;
44  import base.IXMLSaveable;
45  import base.InternetCafe;
46  import base.InternetCafeManager;
47  
48  public class ExportUserListCommand extends Command {
49  	
50  	private final IXMLSaveable[] data;
51  	private InputStream xsltSource = null;
52  	private InputStream cssSource = null;
53  	private File outputDir = null;
54  	
55  	public ExportUserListCommand() {
56  		super();
57  		try {
58  			xsltSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.USER_LIST_XSL_FILE_NAME);
59  			cssSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.USER_LIST_CSS_FILE_NAME);
60  		} catch (FileNotFoundException e) {
61  			// TODO Auto-generated catch block
62  			e.printStackTrace();
63  		}
64  		
65  		this.data = InternetCafeManager.getInstance().getUser();
66  	}
67  	
68  	@Override
69  	protected void prologo() {
70  		InternetCafe.setStatusBarMessage(Messages.getString("command.exportuserlistcommand.exportinguserlist")); //$NON-NLS-1$
71  		
72  		javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
73  		chooser.setDialogTitle(Messages.getString("command.exportuserlistcommand.message1")); //$NON-NLS-1$
74  		chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
75  		chooser.setApproveButtonText(Messages.getString("button.select")); //$NON-NLS-1$
76  		chooser.setMultiSelectionEnabled(true);
77  		
78  		if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
79  			outputDir = chooser.getSelectedFile();
80  		
81  			setStatus(EXECUTE_STATUS);
82  		} else
83  			setStatus(ABORT_STATUS);
84  	}
85  	
86  	@Override
87  	protected void execution() throws Exception {
88  		switch (getStatus()) {
89  		case ABORT_STATUS:
90  			break;
91  		case VETOED_STATUS:
92  			break;
93  		case EXECUTE_STATUS:
94  			if (outputDir != null) {
95  				Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
96  				Element userListElement = document.createElement(ICXmlTags.IC_USER_LIST_TAG);
97  				for (int i = 0; i < data.length; i++)
98  					userListElement.appendChild(data[i].toXml(document));
99  				document.appendChild(userListElement);
100 				Transformer transformer = TransformerFactory.newInstance().newTransformer();
101 				DOMSource xmlSource = new DOMSource(document);
102 	
103 				TransformerFactory factory = TransformerFactory.newInstance();
104 				transformer = factory.newTransformer(new StreamSource(xsltSource));
105 				
106 				OutputStream htmlOutput = new FileOutputStream(""+outputDir+File.separatorChar+ConfigurationManager.USER_LIST_HTML_FILE_NAME); //$NON-NLS-1$
107 
108 				transformer.transform(xmlSource,new StreamResult(htmlOutput));
109 				htmlOutput.close();
110 				xsltSource.close();
111 				
112 				FileOutputStream cssOutput = new FileOutputStream(""+outputDir+File.separatorChar + ConfigurationManager.USER_LIST_CSS_FILE_NAME); //$NON-NLS-1$
113 				byte[] buff = new byte[1024];
114 				int l = 0;
115 				while((l = cssSource.read(buff)) > 0){
116 					cssOutput.write(buff,0,l);
117 				}
118 				cssOutput.close();
119 				cssSource.close();
120 				
121 				setStatus(SUCCESS_STATUS);
122 			} else
123 				setStatus(ERROR_STATUS);
124 			break;
125 		}
126 		
127 	}
128 }