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.FileReader;
25  import java.io.Reader;
26  
27  import org.apache.log4j.Logger;
28  
29  import ui.Messages;
30  import ui.command.Command;
31  import base.InternetCafe;
32  import base.InternetCafeManager;
33  import base.user.User;
34  import base.user.UserFactory;
35  import base.user.jcard.JCard;
36  
37  public class ImportUserFromVCardCommand extends Command {
38  	
39  	private static final transient Logger logger = Logger.getLogger(ImportUserFromVCardCommand.class
40  			.getName());
41  	private File vCardFile = null;
42  
43  	public ImportUserFromVCardCommand() {
44  		
45  	}
46  	
47  	/*
48  	 * (non-Javadoc)
49  	 * 
50  	 * @see ui.command.Command#prologo()
51  	 */
52  	@Override
53  	protected void prologo() {
54  		InternetCafe.setStatusBarMessage(Messages.getString("command.importuserfromvcardcommand.importinguserfromvcard")); //$NON-NLS-1$
55  		javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
56  		chooser.setDialogTitle(Messages.getString("command.importuserfromvcardcommand.dialog.title")); //$NON-NLS-1$
57  		chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
58  		chooser.setApproveButtonText(Messages.getString("button.select")); //$NON-NLS-1$
59  		if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
60  			this.vCardFile = new File(chooser.getSelectedFile().getAbsolutePath());
61  			logger.debug("vCard file: " + vCardFile); //$NON-NLS-1$
62  			setStatus(EXECUTE_STATUS);
63  		}else setStatus(ABORT_STATUS);		
64  	}
65  	
66  	/*
67  	 * (non-Javadoc)
68  	 * 
69  	 * @see ui.command.Command#execution()
70  	 */
71  	@Override
72  	protected void execution() throws Exception {
73  		switch(getStatus()){
74  		case ERROR_STATUS:
75  			break;
76  		case VETOED_STATUS:
77  			break;
78  		case EXECUTE_STATUS:
79  			JCard jcard = new JCard();
80  			Reader fileReader = new FileReader(this.vCardFile);
81  			jcard.read(fileReader);
82  			fileReader.close();
83  			User user = UserFactory.newUser(jcard,"password"); //$NON-NLS-1$
84  			InternetCafeManager.getInstance().addUser(user);
85  			setStatus(SUCCESS_STATUS);
86  			break;
87  		}
88  	}
89  }