View Javadoc

1   package net.sourceforge.jdbdump.gui;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.sql.SQLException;
6   import java.util.ArrayList;
7   import java.util.Date;
8   import java.util.Hashtable;
9   
10  import javax.faces.model.SelectItem;
11  
12  import net.sourceforge.jdbdump.connect.Configuration;
13  import net.sourceforge.jdbdump.connect.DatabaseConnectionData;
14  import net.sourceforge.jdbdump.connect.DatabaseConnector;
15  import net.sourceforge.jdbdump.connect.DatabaseConnectorFactory;
16  import net.sourceforge.jdbdump.connect.Mailer;
17  import net.sourceforge.jdbdump.dump.BinaryFileManager;
18  import net.sourceforge.jdbdump.dump.Dump;
19  import net.sourceforge.jdbdump.dump.DumpFileManager;
20  import net.sourceforge.jdbdump.dump.DumpFileManager.CompressionMethod;
21  
22  import org.apache.log4j.Logger;
23  
24  public class DbBqpBean {
25  	private String database;
26  	private String compression;
27  	private boolean notifyEmails;
28  	private ArrayList<SelectItem> compressionMethods;
29  	private ArrayList<SelectItem> databases;
30  	
31  	private Logger logger = Logger.getLogger(DbBqpBean.class);
32  	
33  	public DbBqpBean() throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
34  		compressionMethods = new ArrayList<SelectItem>();
35  		databases = new ArrayList<SelectItem>();
36  		
37  		for(CompressionMethod tmp: CompressionMethod.values()) {
38  			compressionMethods.add(new SelectItem(tmp.toString()));
39  		}
40  		
41  		for (DatabaseConnectionData tmp: Configuration.getInstance().getConnections().values()) {
42  			databases.add(new SelectItem(tmp.getTitle()));
43  		}		
44  	}
45  	
46  	public String getCompression() {
47  		return compression;
48  	}
49  	public void setCompression(String compression) {
50  		this.compression = compression;
51  	}
52  	public String getDatabase() {
53  		return database;
54  	}
55  	public void setDatabase(String database) {
56  		this.database = database;
57  	}
58  	public boolean getNotifyEmails() {
59  		return notifyEmails;
60  	}
61  	public void setNotifyEmails(boolean notifyEmails) {
62  		this.notifyEmails = notifyEmails;
63  	}
64  
65  	public ArrayList<SelectItem> getCompressionMethods() {
66  		return compressionMethods;
67  	}
68  
69  	public void setCompressionMethods(ArrayList<SelectItem> compressionMethods) {
70  		this.compressionMethods = compressionMethods;
71  	}
72  
73  	public ArrayList<SelectItem> getDatabases() {
74  		return databases;
75  	}
76  
77  	public void setDatabases(ArrayList<SelectItem> databases) {
78  		this.databases = databases;
79  	}
80  	
81  	public String backupDatabase() {
82  					
83  		try {
84  			Hashtable<String, DatabaseConnectionData> hash = Configuration.getInstance().getConnections();
85  			DatabaseConnectionData data = hash.get(database);
86  			String databaseType = data.getDatabaseType();
87  			
88  			DatabaseConnector connector = DatabaseConnectorFactory.getInstance().createConnector(databaseType);
89  				
90  			String url = data.getURL();
91  			if (url.equals("")) url = connector.createURL(data);
92  			String login = data.getLogin();
93  			String passwd = data.getPassword();
94  			
95  			connector.connect(url, login, passwd);
96  			
97  			DumpFileManager saver = new BinaryFileManager();
98  			logger.info("Now creating dump...");
99  			Dump dump = connector.dump();
100 			
101 			logger.info("Saving dump...");
102 			String backupDir = new String();//Configuration.getInstance().getBackupDir();
103 			Date date = new Date();			
104 			String fileName = "bqp-" + date.getTime();
105 			CompressionMethod cm = CompressionMethod.valueOf(compression);			
106 			
107 			saver.exportDump(dump, new File(backupDir + fileName), cm);
108 			logger.info("Dump saved to " + fileName + ".");
109 			
110 			if (notifyEmails) {
111 				Mailer mailer = new Mailer();
112 				mailer.sendBackupNotification(fileName);
113 			}
114 		}
115 		catch (SQLException e) {
116 			e.printStackTrace();
117 		} catch (ClassNotFoundException e) { 
118 			e.printStackTrace();
119 		} catch (InstantiationException e) {
120 			e.printStackTrace();
121 		} catch (IllegalAccessException e) {
122 			e.printStackTrace();
123 		} catch (IOException e) {
124 			e.printStackTrace();
125 		}
126 		return "dbList";
127 	}
128 	
129 	public String restoreDatabase() {
130 				
131 		try {
132 			Hashtable<String, DatabaseConnectionData> hash = Configuration.getInstance().getConnections();
133 			DatabaseConnectionData data = hash.get(database);
134 			String databaseType = data.getDatabaseType();
135 			
136 			DatabaseConnector connector = DatabaseConnectorFactory.getInstance().createConnector(databaseType);
137 			
138 			String url = data.getURL();
139 			if (url.equals("")) url = connector.createURL(data);
140 			String login = data.getLogin();
141 			String passwd = data.getPassword();
142 			
143 			connector.connect(url, login, passwd);
144 			
145 			String dumpFile = new String();
146 			
147 			DumpFileManager saver = new BinaryFileManager();
148 			logger.info("Loading dump from " + dumpFile + "...");
149 			CompressionMethod cm = CompressionMethod.valueOf(compression);
150 			
151 			Dump dump = (Dump) saver.importDump(new File(dumpFile), cm);
152 			logger.info("Now restoring...");
153 			connector.restore(dump);
154 			dump.closeFileReader();
155 			connector.disconnect();
156 			
157 			if (notifyEmails) {
158 				Mailer mailer = new Mailer();
159 				mailer.sendRestoreNotification(dumpFile);
160 			}
161 		}
162 		catch (SQLException ex) {
163 			ex.printStackTrace();
164 		} catch (ClassNotFoundException e) {
165 			e.printStackTrace();
166 		} catch (InstantiationException e) {
167 			e.printStackTrace();
168 		} catch (IllegalAccessException e) {
169 			e.printStackTrace();
170 		} catch (IOException e) {
171 			e.printStackTrace();
172 		}
173 		return "dbList";
174 	}
175 }