-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminBean.java
456 lines (370 loc) · 12.6 KB
/
AdminBean.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package hello;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import com.sun.mail.util.MailLogger;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.IOUtils;
import org.primefaces.model.UploadedFile;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
@ManagedBean(name="AdminBean")
@ViewScoped
public class AdminBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
static AmazonDynamoDBClient dynamoDB;
static List<VideoData> videoData ;
List<VideoData> filteredData;
private User UserID;
private UploadedFile uploadedFile;
private UploadedFile mcFile;
private String key;
private String tableName = "video_table";
private String MCName;
private String MCCategory;
private String MCBodyparts;
private String MCDescription;
private String MCExtention;
private String setEditMCName;
public AdminBean() {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
UserID=(User) session.getAttribute("user");
try {
videoData = populateList();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void init() throws Exception {
AWSCredentials credentials = null;
try {
credentials = new ProfileCredentialsProvider("default").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 class VideoData{
String userID;
String desc;
String videoName;
String videoFile;
String completed;
String mCLink;
private boolean editable;
public VideoData(String userID,String desc,String videoName,String File,String Completed,String MCLink)
{
this.userID = userID;
this.desc = desc;
this.videoName = videoName;
this.videoFile = File;
this.completed = Completed;
this.mCLink = MCLink;
}
public String getUserID()
{
return userID;
}
public void setUserID(String userID)
{
this.userID = userID;
}
public String getdesc()
{
return desc;
}
public void setdesc(String desc)
{
this.desc = desc;
}
public String getvideoName()
{
return videoName;
}
public void setvideoName(String videoName)
{
this.videoName = videoName;
}
public String getvideoFile()
{
return videoFile;
}
public void setvideoFile(String File)
{
this.videoFile = File;
}
public String getcompleted()
{
return completed;
}
public void setcompleted(String Completed)
{
this.completed = Completed;
}
public String getmCLink()
{
return mCLink;
}
public void setmCLink(String MCLink)
{
this.mCLink = MCLink;
}
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
}
public String editAction(VideoData videoData) {
videoData.setEditable(true);
return null;
}
public String hideAction(VideoData videoData) {
videoData.setEditable(false);
return null;
}
public List<VideoData> getvideoData()
{
return videoData;
}
public UploadedFile getMcFile() {
return mcFile;
}
public void setMcFile(UploadedFile mcFile) {
this.mcFile = mcFile;
}
public String getMCName() {
return MCName;
}
public void setMCName(String mCName) {
MCName = mCName;
}
public String getMCCategory() {
return MCCategory;
}
public void setMCCategory(String mCCategory) {
MCCategory = mCCategory;
}
public String getMCBodyparts() {
return MCBodyparts;
}
public void setMCBodyparts(String mCBodyparts) {
MCBodyparts = mCBodyparts;
}
public String getMCDescription() {
return MCDescription;
}
public void setMCDescription(String mCDescription) {
MCDescription = mCDescription;
}
public String getMCExtention() {
return MCExtention;
}
public void setMCExtention(String mCExtention) {
MCExtention = mCExtention;
}
public List<VideoData> populateList() throws Exception {
init();
videoData = new ArrayList<VideoData>();
String completed = "no";
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("Completed", new Condition().withAttributeValueList(new AttributeValue(completed)).withComparisonOperator(ComparisonOperator.EQ));
ScanRequest scanRequestFilter = new ScanRequest(tableName).withScanFilter(scanFilter);
ScanRequest scanRequest = new ScanRequest(tableName).withTableName(tableName);
ScanResult scanResult = dynamoDB.scan(scanRequest);
for(int i=0;i<scanResult.getCount();i++){
String tempUser = scanResult.getItems().get(i).get("name").getS();
String tempDesc = scanResult.getItems().get(i).get("desc").getS();
String tempFile = scanResult.getItems().get(i).get("File").getS();
String tempVideoName = scanResult.getItems().get(i).get("userID").getS();
String tempCompleted = scanResult.getItems().get(i).get("Completed").getS();
String tempMCLink = scanResult.getItems().get(i).get("MCLink").getS();
videoData.add(new VideoData(tempUser,tempDesc,tempVideoName,tempFile,tempCompleted,tempMCLink));
}
for (VideoData p : videoData){
if(p.userID=="nas2"){
System.out.println("ArrayList: " + p.userID) ;
}
}
return videoData;
}
public User getUserID() {
return UserID;
}
public void setUserID(User userID) {
UserID = userID;
}
public void saveAction(String attValue) throws Exception{
init();
String fileExtention = getExtension(uploadedFile.getFileName());
System.out.println(fileExtention);
if(allowedExtensions(fileExtention)) {
key ="MotionCapture/"+ MCName+ "."+fileExtention;
InputStream input = uploadedFile.getInputstream();
try {
DbConnect.UpdateItem(tableName,attValue,"MCLink",key);
DbConnect.UpdateItem(tableName, attValue,"Completed", "yes");
s3methods.uploadFile(key, createSampleFile(input,fileExtention));
DbConnect.addMCDesc(MCName, MCBodyparts, MCCategory, MCDescription,MCExtention, key);
if(DbConnect.getUser(UserID.getName()).getEmails() && DbConnect.getUser(UserID.getName()).getEmailActive() ){
System.out.println(UserID.getEmails());
for(VideoData video : videoData)
{
if(video.getvideoName().equals(attValue)){
AmazonSES.sendCompletionEmail(DbConnect.getUser(video.getUserID()).getEmail(),MCName,s3methods.getURL(key));
AmazonSES.ComplaintHandleRequest();
AmazonSES.BounceHandleRequest();
}
}
} else {
System.out.println("nomail");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
{
FacesContext.getCurrentInstance().addMessage("uploadForm:file", new FacesMessage("Please upload a video in .avi,.wmv or .mp4 format"));
}
}
public void uploadMC() throws Exception
{
init();
String fileExtention = getExtension(mcFile.getFileName());
System.out.println(fileExtention);
InputStream input = mcFile.getInputstream();
if(allowedExtensions(fileExtention)) {
key ="MotionCapture/"+ MCName+ "."+fileExtention;
try {
s3methods.uploadFile(key, createSampleFile(input,fileExtention));
DbConnect.addMCDesc(MCName, MCBodyparts, MCCategory, MCDescription,MCExtention, key);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
FacesContext.getCurrentInstance().addMessage("uploadForm:file", new FacesMessage("Please upload a video in .avi,.wmv or .mp4 format"));
}
}
private static File createSampleFile(InputStream input,String extention) throws IOException {
File temp = File.createTempFile("afsafasa", "."+extention);
temp.deleteOnExit();
OutputStream output = new FileOutputStream(temp);
try {
IOUtils.copy(input, output);
System.out.println(temp);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
}
return temp;
}
public static String getExtension(String filename) {
if (filename == null) {
return null;
}
int extensionPos = filename.lastIndexOf('.');
int lastUnixPos = filename.lastIndexOf('/');
int lastWindowsPos = filename.lastIndexOf('\\');
int lastSeparator = Math.max(lastUnixPos, lastWindowsPos);
int index = lastSeparator > extensionPos ? -1 : extensionPos;
if (index == -1) {
return "";
} else {
return filename.substring(index + 1);
}
}
public static boolean allowedExtensions(String ext)
{
String[] extentions = {"fbx", "bvh","FBX","BVH"};
if (Arrays.asList(extentions).contains(ext))
{
return true;
} else {
return false;
}
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public List<VideoData> getFilteredData() {
return filteredData;
}
public void setFilteredData(List<VideoData> filteredData) {
this.filteredData = filteredData;
}
public String getSetEditMCName() {
return setEditMCName;
}
public void setSetEditMCName(String SetEditMCName) {
setEditMCName = SetEditMCName;
}
}