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  
25  import javax.swing.JFileChooser;
26  
27  import org.apache.log4j.Logger;
28  
29  import ui.Messages;
30  import ui.command.Command;
31  import ui.util.FileExtensionFilter;
32  import base.InternetCafe;
33  import base.InternetCafeManager;
34  import base.backup.Backup;
35  import base.backup.BackupFactory;
36  
37  /***
38   * @author skunk
39   * 
40   */
41  public class LoadBackupCommand extends Command {
42  
43  	private static final transient Logger logger = Logger
44  			.getLogger(LoadBackupCommand.class.getName());
45  
46  	private File backupFile;
47  
48  	/*
49  	 * (non-Javadoc)
50  	 * 
51  	 * @see ui.command.Command#prologo()
52  	 */
53  	@Override
54  	protected void prologo() {
55  		InternetCafe.setStatusBarMessage(Messages.getString("command.loadbackupcommand.loadingbackup")); //$NON-NLS-1$
56  		JFileChooser chooser = new JFileChooser();
57  		chooser.setDialogTitle(Messages.getString("command.loadbackupcommand.loadbackup")); //$NON-NLS-1$
58  		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
59  		chooser.setApproveButtonText(Messages.getString("button.load")); //$NON-NLS-1$
60  		chooser.setFileFilter(new FileExtensionFilter(Messages.getString("common.filetype.zip"), //$NON-NLS-1$
61  				Messages.getString("common.filetype.zip"))); //$NON-NLS-1$
62  		if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
63  			backupFile = new File(chooser.getSelectedFile().getAbsolutePath());
64  			setStatus(EXECUTE_STATUS);
65  		} else
66  			setStatus(ABORT_STATUS);
67  	}
68  
69  	/*
70  	 * (non-Javadoc)
71  	 * 
72  	 * @see ui.command.Command#execution()
73  	 */
74  	@Override
75  	protected void execution() throws Exception {
76  		switch (getStatus()) {
77  		case ABORT_STATUS:
78  			break;
79  		case VETOED_STATUS:
80  			break;
81  		case EXECUTE_STATUS:
82  			Backup backup = null;
83  			try {
84  				backup = BackupFactory.newBackupFromZipArchive(backupFile);
85  				InternetCafeManager.getInstance().addBackup(backup);
86  			} catch (Exception ex) {
87  				logger.error(ex.getMessage());
88  				ex.printStackTrace();
89  			} finally {
90  				if (backup != null)
91  					setStatus(SUCCESS_STATUS);
92  				else
93  					setStatus(ERROR_STATUS);
94  			}
95  			break;
96  		}
97  	}
98  }