-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbConnect.java
352 lines (295 loc) · 12.7 KB
/
DbConnect.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package hello;
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.PutItemResult;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;
import com.amazonaws.services.dynamodbv2.model.UpdateItemResult;
public class DbConnect {
static AmazonDynamoDBClient dynamoDB;
public static void init() throws Exception
{
AWSCredentials credentials = null;
try {
credentials = new DefaultAWSCredentialsProviderChain().getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. " +
"Please make sure that your credentials file is at the correct " +
"location (C:\\Users\\nas\\.aws\\credentials), and is in valid format.",
e);
}
dynamoDB = new AmazonDynamoDBClient(credentials);
Region usWest2 = Region.getRegion(Regions.EU_WEST_1);
dynamoDB.setRegion(usWest2);
}
public static void addEntry(String name,String Email, String password, boolean activated,byte[] hash) throws Exception
{ init();
User user = new User();
user.setName(name);
user.setEmail(Email);
user.setPassword(password);
user.setRole("USER");
user.setActivated(activated);
user.setHashKey(hash);
System.out.println(user);
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
mapper.save(user);
}
public static User getUser(String hashKey) throws Exception
{ init();
User user = new User();
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
user = mapper.load(User.class, hashKey);
return user;
}
public static boolean checkName(String checkName){
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String tableName = "usr_reg2";
Map<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("name", new Condition().withAttributeValueList(new AttributeValue(checkName)).withComparisonOperator(ComparisonOperator.EQ));
ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
ScanResult scanResult = dynamoDB.scan(scanRequest);
if (scanResult.getCount()!=0)
{
return false;
}
else
{
return true;
}
}
public static boolean activateEmails(String hashKey)
{
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
User user = mapper.load(User.class, hashKey);
boolean emailBoolean = user.getEmails();
System.out.println("emails before change: "+emailBoolean);
emailBoolean = !emailBoolean;
System.out.println("emails after change: "+emailBoolean);
user.setEmails(emailBoolean);
mapper.save(user);
return emailBoolean;
}
public static boolean checkEmail(String checkEmail){
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String tableName = "usr_reg2";
Map<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("email", new Condition().withAttributeValueList(new AttributeValue(checkEmail)).withComparisonOperator(ComparisonOperator.EQ));
ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
ScanResult scanResult = dynamoDB.scan(scanRequest);
if (scanResult.getCount()!=0)
{
return false;
}
else
{
return true;
}
}
public static void addVideoDesc(String userID,String desc,String name,String File,String Completed, String MCLink) throws Exception
{ init();
try {
String tableName = "video_table";
// Add an item
Map<String, AttributeValue> item = newItem(userID,desc,name,File,Completed,MCLink);
PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
System.out.println("Result: " + putItemResult);
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it "
+ "to AWS, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
}
}
public static void addMCDesc(String nameMC,String bMC,String categMC,String descMC,String extentMC,String linkMC) {
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, AttributeValue> item = mcItem(nameMC,bMC,categMC,descMC,extentMC,linkMC);
PutItemRequest putItemRequest = new PutItemRequest("MCDatabase", item);
PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
}
public static void getVideoItems(String userID)
{
String tableName = "video_table";
AWSCredentials credentials = null;
try {
credentials = new DefaultAWSCredentialsProviderChain().getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. " +
"Please make sure that your credentials file is at the correct " +
"location (C:\\Users\\nas\\.aws\\credentials), and is in valid format.",
e);
}
dynamoDB = new AmazonDynamoDBClient(credentials);
Region usWest2 = Region.getRegion(Regions.EU_WEST_1);
dynamoDB.setRegion(usWest2);
Map<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("userID", new Condition().withAttributeValueList(new AttributeValue(userID)).withComparisonOperator(ComparisonOperator.EQ));
ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
ScanResult scanResult = dynamoDB.scan(scanRequest);
System.out.println("Result: " + scanResult);
}
public static void updateUserData(String hashKey){
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
User user = mapper.load(User.class, hashKey);
user.setActivated(true);
user.setHashKey(null);
user.setEmailActive(true);
mapper.save(user);
}
public static void updateEmail(String hashKey,String Value){
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
User user = mapper.load(User.class, hashKey);
user.setEmail(Value);
mapper.save(user);
}
public static void RemoveFromMailingList(String Email){
String tableName = "usr_reg2";
AWSCredentials credentials = null;
try {
credentials = new DefaultAWSCredentialsProviderChain().getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. " +
"Please make sure that your credentials file is at the correct " +
"location (C:\\Users\\nas\\.aws\\credentials), and is in valid format.",
e);
}
dynamoDB = new AmazonDynamoDBClient(credentials);
Region usWest2 = Region.getRegion(Regions.EU_WEST_1);
dynamoDB.setRegion(usWest2);
Map<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("email", new Condition().withAttributeValueList(new AttributeValue(Email)).withComparisonOperator(ComparisonOperator.EQ));
ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
ScanResult scanResult = dynamoDB.scan(scanRequest);
System.out.println("Result: " + scanResult);
if(scanResult.getCount()!=0) {
System.out.println(scanResult.getItems().get(0).get("name").getS());
DynamoDBMapper dbmapper = new DynamoDBMapper(dynamoDB);
User user = dbmapper.load(User.class, scanResult.getItems().get(0).get("name").getS());
user.setEmail("");
dbmapper.save(user);
}
}
public static void updateHash(String hashKey,byte[] HASHKEY){
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
User user = mapper.load(User.class, hashKey);
user.setHashKey(HASHKEY);
mapper.save(user);
}
public static String stringifyHash(byte[] theHash)
{
String hexStr = "";
for (int i = 0; i < theHash.length; i++) {
hexStr += Integer.toString( ( theHash[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return hexStr;
}
public static void UpdateItem(String tableName,String attValue,String KeyToUpdate, String updateValue) {
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String,AttributeValueUpdate> updates=new HashMap<String,AttributeValueUpdate>();
AttributeValueUpdate update = new AttributeValueUpdate().withValue(new AttributeValue(updateValue));
Map<String,AttributeValue> key2 = new HashMap<String,AttributeValue>();
key2.put("userID", new AttributeValue(attValue));
updates.put(KeyToUpdate, update);
try {
UpdateItemRequest req=new UpdateItemRequest(tableName,key2,updates);
System.out.println("requeest"+req);
UpdateItemResult res=dynamoDB.updateItem(req);
System.out.println("result"+res);
}
catch ( AmazonServiceException ase) {
System.err.println("Failed to update item: " + ase.getMessage());
}
}
private static Map<String, AttributeValue> newItem(String name,String email,String Password) {
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("userID", new AttributeValue(name));
item.put("email", new AttributeValue(email));
item.put("Password", new AttributeValue(Password));
return item;
}
private static Map<String, AttributeValue> newItem(String userID,String desc,String videoName,String File,String Completed, String MCLink) {
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("userID", new AttributeValue(videoName));
item.put("desc", new AttributeValue(desc));
item.put("name", new AttributeValue(userID));
item.put("File", new AttributeValue(File));
item.put("Completed", new AttributeValue(Completed));
item.put("MCLink", new AttributeValue(MCLink));
return item;
}
private static Map<String, AttributeValue> mcItem(String MCName, String MCBodyparts, String MCCategory, String MCDescription,String MCExtention,String MCLink) {
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("MCName", new AttributeValue(MCName));
item.put("MCBodyparts", new AttributeValue(MCBodyparts));
item.put("MCCategory", new AttributeValue(MCCategory));
item.put("MCDescription", new AttributeValue(MCDescription));
item.put("MCExtention", new AttributeValue(MCExtention));
item.put("MCLink", new AttributeValue(MCLink));
return item;
}
}