1
|
package eu.dnetlib.server;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.*;
|
4
|
import eu.dnetlib.goldoa.service.BudgetManager;
|
5
|
import eu.dnetlib.goldoa.service.InvoiceManager;
|
6
|
import org.apache.commons.fileupload.FileItem;
|
7
|
import org.apache.commons.fileupload.FileItemFactory;
|
8
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
9
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
10
|
import org.apache.commons.io.IOUtils;
|
11
|
import org.springframework.context.ApplicationContext;
|
12
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
13
|
|
14
|
import javax.servlet.ServletConfig;
|
15
|
import javax.servlet.ServletException;
|
16
|
import javax.servlet.http.HttpServlet;
|
17
|
import javax.servlet.http.HttpServletRequest;
|
18
|
import javax.servlet.http.HttpServletResponse;
|
19
|
import java.io.ByteArrayInputStream;
|
20
|
import java.io.IOException;
|
21
|
import java.text.SimpleDateFormat;
|
22
|
import java.util.List;
|
23
|
|
24
|
/**
|
25
|
* Created by stefania on 4/3/15.
|
26
|
*/
|
27
|
public class BudgetServlet extends HttpServlet {
|
28
|
|
29
|
// private InvoiceManager invoiceManager;
|
30
|
private BudgetManager budgetManager;
|
31
|
|
32
|
@Override
|
33
|
public void init(ServletConfig config) throws ServletException {
|
34
|
|
35
|
super.init(config);
|
36
|
|
37
|
ApplicationContext context = WebApplicationContextUtils
|
38
|
.getWebApplicationContext(getServletContext());
|
39
|
|
40
|
this.budgetManager = (BudgetManager) context.getBean("budgetManager");
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
45
|
try {
|
46
|
FileItemFactory factory = new DiskFileItemFactory();
|
47
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
48
|
List<FileItem> items = upload.parseRequest(request);
|
49
|
|
50
|
Budget budget = new Budget();
|
51
|
BankAccount bankAccount = null;
|
52
|
|
53
|
budget.setCurrency(Currency.EUR);
|
54
|
|
55
|
budget = budgetManager.saveBudget(budget);
|
56
|
|
57
|
for (FileItem item:items) {
|
58
|
if (item.isFormField()) {
|
59
|
String fieldName = item.getFieldName();
|
60
|
String value = item.getString();
|
61
|
|
62
|
if (fieldName.equals("institution")) {
|
63
|
//TODO fix!
|
64
|
//budget.setOrganization(value);
|
65
|
} else if (fieldName.equals("amountRequested")) {
|
66
|
budget.setAmountRequested(Float.parseFloat(value));
|
67
|
} else if (fieldName.equals("userId")) {
|
68
|
//budget.setUser(value);
|
69
|
} else if (fieldName.equals("startDate")) {
|
70
|
budget.setStartdate(new SimpleDateFormat("yyyy/MM/dd").parse(value));
|
71
|
} else if (fieldName.equals("endDate")) {
|
72
|
budget.setEnddate(new SimpleDateFormat("yyyy/MM/dd").parse(value));
|
73
|
} else if (fieldName.equals("bankName")) {
|
74
|
if(bankAccount==null)
|
75
|
bankAccount = new BankAccount();
|
76
|
bankAccount.setBankName(value);
|
77
|
} else if (fieldName.equals("bankAddress")) {
|
78
|
if(bankAccount==null)
|
79
|
bankAccount = new BankAccount();
|
80
|
bankAccount.setBankAddress(value);
|
81
|
} else if (fieldName.equals("accountHolder")) {
|
82
|
if(bankAccount==null)
|
83
|
bankAccount = new BankAccount();
|
84
|
bankAccount.setAccountHolder(value);
|
85
|
} else if (fieldName.equals("accountNumber")) {
|
86
|
if(bankAccount==null)
|
87
|
bankAccount = new BankAccount();
|
88
|
bankAccount.setIban(value);
|
89
|
} else if (fieldName.equals("bankCode")) {
|
90
|
if(bankAccount==null)
|
91
|
bankAccount = new BankAccount();
|
92
|
bankAccount.setBankCode(value);
|
93
|
}
|
94
|
|
95
|
} else {
|
96
|
String contentType = item.getContentType();
|
97
|
|
98
|
|
99
|
// budgetManager.uploadTerms(budget.getId(), contentType, item.getInputStream());
|
100
|
}
|
101
|
}
|
102
|
|
103
|
budget.setBankAccount(bankAccount);
|
104
|
|
105
|
budget.setStatus(Budget.Status.SUBMITTED);
|
106
|
budgetManager.saveBudget(budget);
|
107
|
|
108
|
response.setContentType("text/plain");
|
109
|
response.setContentLength(budget.getId().length());
|
110
|
|
111
|
response.getOutputStream().print(budget.getId());
|
112
|
}
|
113
|
catch(Exception e){
|
114
|
throw new RuntimeException(e);
|
115
|
}
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
120
|
String budgetId = req.getParameter("budgetId");
|
121
|
|
122
|
try {
|
123
|
// BudgetFile file = budgetManager.downloadBudgetTerms(budgetId);
|
124
|
//
|
125
|
// resp.setContentType(file.getContentType());
|
126
|
// resp.setContentLength(file.getContent().length);
|
127
|
//
|
128
|
// IOUtils.copy(new ByteArrayInputStream(file.getContent()), resp.getOutputStream());
|
129
|
} catch (Exception e) {
|
130
|
resp.setStatus(500);
|
131
|
resp.getOutputStream().println("Error getting signed terms of agreement of budget with id " + budgetId);
|
132
|
}
|
133
|
}
|
134
|
}
|