This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucketsRepositoryDAO.java
246 lines (210 loc) · 7.69 KB
/
BucketsRepositoryDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package databases.mysql.daos;
import databases.mysql.CloudEntityData;
import databases.mysql.DAO;
import databases.mysql.MySQLConnect;
import utility.PropertiesManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* Data Access Object for cloud buckets related information
*/
@SuppressWarnings({"DuplicatedCode", "SqlResolve", "RedundantSuppression"})
public class BucketsRepositoryDAO extends DAO {
/**
* Queries
*/
private static final String CREATE_GOOGLE_BUCKETS_TABLE = "CREATE TABLE IF NOT EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_buckets (" +
"bucket_name varchar(100) NOT NULL, " +
"PRIMARY KEY (bucket_name)" +
")";
private static final String CREATE_AMAZON_BUCKETS_TABLE = "CREATE TABLE IF NOT EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_buckets (" +
"bucket_name varchar(100) NOT NULL, " +
"region varchar(15) NOT NULL, " +
"PRIMARY KEY (bucket_name)" +
")";
private static final String INSERT_GOOGLE_BUCKET = "INSERT INTO " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_buckets " +
"(bucket_name) " + "VALUES (?) " +
"ON DUPLICATE KEY UPDATE bucket_name=VALUES(bucket_name)";
private static final String INSERT_AMAZON_BUCKET = "INSERT INTO " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_buckets " +
"(bucket_name, region) " + "VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE bucket_name=VALUES(bucket_name), region=VALUES(region)";
private static final String SELECT_GOOGLE_BUCKETS = "SELECT bucket_name FROM " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_buckets";
private static final String SELECT_AMAZON_BUCKETS = "SELECT bucket_name, region FROM " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_buckets";
private static final String DROP_GOOGLE_BUCKETS = "DROP TABLE IF EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_buckets";
private static final String DROP_AMAZON_BUCKETS = "DROP TABLE IF EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_buckets";
/**
* Initializes tables
* @param connection database connection to use
* @param provider select which provider is needed to initialize corresponding tables
* @throws SQLException query definition and execution related problems
*/
private static void initTables(Connection connection, String provider) throws SQLException {
if (connection != null) {
initDatabase(connection);
Statement statement = connection.createStatement();
switch (provider) {
case GOOGLE:
statement.executeUpdate(CREATE_GOOGLE_BUCKETS_TABLE);
break;
case AMAZON:
statement.executeUpdate(CREATE_AMAZON_BUCKETS_TABLE);
break;
default:
statement.executeUpdate(CREATE_AMAZON_BUCKETS_TABLE);
statement.executeUpdate(CREATE_GOOGLE_BUCKETS_TABLE);
break;
}
statement.close();
} else {
System.err.println("Could not initialize database");
}
}
/**
* Drop every table associated to Google Cloud Platform Buckets
*/
public static void dropGoogle() {
dropTable(GOOGLE);
}
/**
* Drop every table associated to Amazon Web Services Buckets
*/
public static void dropAmazon() {
dropTable(AMAZON);
}
/**
* Generic drop table function
* @param provider select which provider is needed to delete corresponding tables
*/
private static void dropTable(String provider) {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return;
}
Statement statement = connection.createStatement();
switch (provider) {
case GOOGLE:
statement.executeUpdate(DROP_GOOGLE_BUCKETS);
break;
case AMAZON:
statement.executeUpdate(DROP_AMAZON_BUCKETS);
break;
default:
System.err.println("Provider not supported! Could not perform DB drop");
}
statement.close();
MySQLConnect.closeConnection(connection);
} catch (SQLException e) {
System.err.println("Could not drop table(s): " + e.getMessage());
}
}
/**
* Persists a new Google Cloud Storage bucket to database
* @param bucketName name of the bucket
*/
public static void persistGoogle(String bucketName) {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return;
}
initTables(connection, GOOGLE);
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_GOOGLE_BUCKET);
preparedStatement.setString(1, bucketName);
preparedStatement.execute();
preparedStatement.close();
MySQLConnect.closeConnection(connection);
} catch (SQLException e) {
System.err.println("Could not perform insertion: " + e.getMessage());
}
}
/**
* Persists a new Amazon S3 bucket to database
* @param bucketName name of the bucket
* @param region bucket region
*/
public static void persistAmazon(String bucketName, String region) {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return;
}
initTables(connection, AMAZON);
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_AMAZON_BUCKET);
preparedStatement.setString(1, bucketName);
preparedStatement.setString(2, region);
preparedStatement.execute();
preparedStatement.close();
MySQLConnect.closeConnection(connection);
} catch (SQLException e) {
System.err.println("Could not perform insertion: " + e.getMessage());
}
}
/**
* List every Google Cloud Storage bucket
* @return list of buckets (CloudEntityData)
*/
public static List<CloudEntityData> getGoogles() {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return null;
}
initTables(connection, GOOGLE);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SELECT_GOOGLE_BUCKETS);
List<CloudEntityData> result = new ArrayList<>();
while (resultSet.next()) {
result.add(new CloudEntityData(resultSet.getString("bucket_name")));
}
statement.close();
resultSet.close();
MySQLConnect.closeConnection(connection);
return result;
} catch (SQLException e) {
System.err.println("Could not perform select: " + e.getMessage());
return null;
}
}
/**
* List every Amazon S3 bucket
* @return list of buckets (CloudEntityData)
*/
public static List<CloudEntityData> getAmazons() {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return null;
}
initTables(connection, AMAZON);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SELECT_AMAZON_BUCKETS);
List<CloudEntityData> result = new ArrayList<>();
while (resultSet.next()) {
result.add(new CloudEntityData(resultSet.getString("bucket_name"),
resultSet.getString("region")));
}
statement.close();
resultSet.close();
MySQLConnect.closeConnection(connection);
return result;
} catch (SQLException e) {
System.err.println("Could not perform select: " + e.getMessage());
return null;
}
}
}