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.FileWriter;
24  import java.io.Writer;
25  
26  import org.apache.log4j.Logger;
27  
28  import ui.Messages;
29  import ui.command.Command;
30  import base.InternetCafe;
31  import base.user.User;
32  import base.user.jcard.JCard;
33  
34  
35   public class ExportUserAsVCardCommand extends Command {
36    
37   private static final transient Logger logger = Logger.getLogger(ExportUserAsVCardCommand.class.getName());
38   
39   private final User user;
40   private File vCardFile = null;
41  
42  	public ExportUserAsVCardCommand(User user) {
43  		this.user = user;
44  	}
45  
46  	/*
47  	 * (non-Javadoc)
48  	 * 
49  	 * @see ui.command.Command#prologo()
50  	 */
51  	@Override
52  	protected void prologo() {
53  		InternetCafe
54  				.setStatusBarMessage(Messages
55  						.getString("command.exportuserasvcardcommand.exportinguserasvcard")); //$NON-NLS-1$
56  		javax.swing.JFileChooser repositoryChooser = new javax.swing.JFileChooser();
57  		repositoryChooser.setDialogTitle(Messages
58  				.getString("command.exportuserasvcardcommand.message1")); //$NON-NLS-1$
59  		repositoryChooser
60  				.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
61  		repositoryChooser.setApproveButtonText(Messages
62  				.getString("button.select")); //$NON-NLS-1$
63  		repositoryChooser.setMultiSelectionEnabled(true);
64  
65  		if (repositoryChooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) {
66  			File outputDirectory = repositoryChooser.getSelectedFile();
67  			File vCardFile = new File(
68  					outputDirectory.getAbsolutePath()
69  							+ File.separatorChar
70  							+ user.getName()
71  							+ " " + user.getSurname() + "." + Messages.getString("common.filetype.vcard")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
72  			logger.debug("Output directory: " + outputDirectory); //$NON-NLS-1$
73  			logger.debug("vCard file: " + vCardFile); //$NON-NLS-1$
74  			setStatus(EXECUTE_STATUS);
75  		} else
76  			setStatus(ABORT_STATUS);
77  	}
78  
79  	/*
80  	 * (non-Javadoc)
81  	 * 
82  	 * @see ui.command.Command#execution()
83  	 */
84  	@Override
85  	protected void execution() throws Exception {
86  		switch (getStatus()) {
87  		case ERROR_STATUS:
88  			break;
89  		case VETOED_STATUS:
90  			break;
91  		case EXECUTE_STATUS:
92  			JCard jcard = new JCard();
93  			Writer writer = new FileWriter(vCardFile);
94  			jcard.newJCard(this.user).write(writer);
95  			writer.close();
96  			setStatus(SUCCESS_STATUS);
97  			break;
98  		}
99  	}
100 }