-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTaskController.java
More file actions
200 lines (158 loc) · 7.09 KB
/
Copy pathAddTaskController.java
File metadata and controls
200 lines (158 loc) · 7.09 KB
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
package com;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.dao.GuestDA;
import com.model.Guest;
import com.model.Task;
import com.utils.SubTitle;
/**
* Servlet implementation class AddTaskController
*/
@WebServlet("/AddTaskController")
public class AddTaskController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIRECTORY = "User";
private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
/**
* @see HttpServlet#HttpServlet()
*/
public AddTaskController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Guest aGuest = (Guest)request.getSession().getAttribute("guest");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// Get guest ID from client side JSP
Task t = new Task();
int score = aGuest.getScore();
float reward = 0;
String sourceLang = null;
String targetLang = null;
String dstPath = null;
t.setSourceID(aGuest.getGuestID());
// Set Date
t.setSubmitTime(new java.util.Date());
// File Upload
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(MEMORY_THRESHOLD);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(MAX_REQUEST_SIZE);
String uploadPath = getServletContext().getRealPath("./") + File.separator + UPLOAD_DIRECTORY;
String userPath = uploadPath + File.separator + aGuest.getGuestID()+File.separator+"upload";
File uploadDir = new File(userPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
try {
List<FileItem> formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
for (FileItem item : formItems) {
if (item.isFormField()) {
switch(item.getFieldName()){
case "TNameField":
t.setName(new String(item.getString().getBytes("ISO-8859-1"),"UTF-8"));
break;
case "PayField":
reward = Float.parseFloat(item.getString());
t.setReward(reward);
break;
case "DueTimeField":
try {
t.setDueTime(new SimpleDateFormat("yyyy-MM-dd").parse(item.getString()));
} catch (ParseException e) {
e.printStackTrace();
}
break;
case "sourceLangField":
sourceLang = new String(item.getString().getBytes("ISO-8859-1"),"UTF-8");
t.setSourceLang(sourceLang);
break;
case "targetLangField":
targetLang = new String(item.getString().getBytes("ISO-8859-1"),"UTF-8");
t.setTargetLang(targetLang);
break;
case "sourceText":
if(!item.getString().equals(""))
{
t.setText(new String(item.getString().getBytes("ISO-8859-1"),"UTF-8"));
t.setFileType(0);
}
break;
}
}
else{
try {
if(item.getName()!="")
{
if(item.getName().endsWith(".srt"))
{
t.setFileType(2);
}
else {
t.setFileType(1);
}
}
String fileName = new File(item.getName()).getName();
String filePath = userPath + File.separator + fileName;
File storeFile = new File(filePath);
// System.out.println(filePath);
item.write(storeFile);
t.setTextFilePath(fileName);
if(t.getFileType()==2)
{
dstPath = userPath + File.separator + "DST_"+ fileName;
SubTitle.translate(filePath, dstPath);
}
} catch (Exception e) {
System.out.println("空");
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
t.setDestinationID(null);
Guest.initialize();
aGuest.addNewTask(t);
score = (int)(score - reward);
aGuest.setScore(score);
GuestDA.updateScore(aGuest.getGuestID(), score);
// Update task related information for the guest
if(aGuest.getNbProposedTasks() > 0){
aGuest.setProposedTaskList(aGuest.findProposedTasks());
}
aGuest.setReceivedTaskList(aGuest.findReceivedTasks());
if(aGuest.getNbAcceptedTasks() > 0){
aGuest.setAcceptedTaskList(aGuest.findAcceptedTasks());
}
// Close the database connection
Guest.terminate();
request.getSession().setAttribute("guest", aGuest);
RequestDispatcher dispatcher = request.getRequestDispatcher("/JSP/guestHome.jsp");
dispatcher.forward(request, response);
//response.sendRedirect("MicroTasks/JSP/guestHome.jsp");
}
}