1
|
package eu.dnetlib.server;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Invoice;
|
4
|
import eu.dnetlib.goldoa.domain.InvoiceFile;
|
5
|
import eu.dnetlib.goldoa.domain.ManagerException;
|
6
|
import eu.dnetlib.goldoa.service.EligibilityManager;
|
7
|
import eu.dnetlib.goldoa.service.InvoiceManager;
|
8
|
import org.apache.commons.fileupload.FileItem;
|
9
|
import org.apache.commons.fileupload.FileItemFactory;
|
10
|
import org.apache.commons.fileupload.FileItemIterator;
|
11
|
import org.apache.commons.fileupload.FileItemStream;
|
12
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
13
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.springframework.context.ApplicationContext;
|
16
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
17
|
|
18
|
import javax.servlet.ServletConfig;
|
19
|
import javax.servlet.ServletException;
|
20
|
import javax.servlet.http.HttpServlet;
|
21
|
import javax.servlet.http.HttpServletRequest;
|
22
|
import javax.servlet.http.HttpServletResponse;
|
23
|
import java.io.*;
|
24
|
import java.text.SimpleDateFormat;
|
25
|
import java.util.Date;
|
26
|
import java.util.List;
|
27
|
|
28
|
/**
|
29
|
* Created by stefania on 3/19/15.
|
30
|
*/
|
31
|
public class FileUpload extends HttpServlet {
|
32
|
|
33
|
private InvoiceManager invoiceManager;
|
34
|
|
35
|
@Override
|
36
|
public void init(ServletConfig config) throws ServletException {
|
37
|
|
38
|
super.init(config);
|
39
|
|
40
|
ApplicationContext context = WebApplicationContextUtils
|
41
|
.getWebApplicationContext(getServletContext());
|
42
|
|
43
|
this.invoiceManager = (InvoiceManager) context.getBean("invoiceManager");
|
44
|
}
|
45
|
|
46
|
@Override
|
47
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
48
|
try {
|
49
|
FileItemFactory factory = new DiskFileItemFactory();
|
50
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
51
|
List<FileItem> items = upload.parseRequest(request);
|
52
|
|
53
|
Invoice invoice = new Invoice();
|
54
|
|
55
|
invoice.setSource("portal");
|
56
|
invoice = invoiceManager.saveInvoice(invoice);
|
57
|
|
58
|
for (FileItem item:items) {
|
59
|
if (item.isFormField()) {
|
60
|
String fieldName = item.getFieldName();
|
61
|
String value = item.getString();
|
62
|
|
63
|
if (fieldName.equals("invoiceNumber")) {
|
64
|
invoice.setNumber(value);
|
65
|
} else if (fieldName.equals("dateIssued")) {
|
66
|
|
67
|
invoice.setDate(new SimpleDateFormat("yyyy/MM/dd").parse(value));
|
68
|
}
|
69
|
} else {
|
70
|
String contentType = item.getContentType();
|
71
|
|
72
|
invoiceManager.uploadInvoice(invoice.getId(), contentType, item.getInputStream());
|
73
|
}
|
74
|
}
|
75
|
|
76
|
invoiceManager.saveInvoice(invoice);
|
77
|
|
78
|
response.setContentType("text/plain");
|
79
|
response.setContentLength(invoice.getId().length());
|
80
|
|
81
|
response.getOutputStream().print(invoice.getId());
|
82
|
}
|
83
|
catch(Exception e){
|
84
|
throw new RuntimeException(e);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
@Override
|
89
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
90
|
String invoiceId = req.getParameter("invoiceId");
|
91
|
|
92
|
try {
|
93
|
InvoiceFile file = invoiceManager.downloadInvoice(invoiceId);
|
94
|
|
95
|
resp.setContentType(file.getMimetype());
|
96
|
resp.setContentLength(file.getFile().length);
|
97
|
|
98
|
IOUtils.copy(new ByteArrayInputStream(file.getFile()), resp.getOutputStream());
|
99
|
} catch (ManagerException e) {
|
100
|
|
101
|
switch (e.getErrorCause()) {
|
102
|
case NOT_EXISTS:
|
103
|
resp.setStatus(404);
|
104
|
resp.getOutputStream().println("Invoice with id " + invoiceId + " not found");
|
105
|
break;
|
106
|
case UNKNOWN:
|
107
|
resp.setStatus(500);
|
108
|
resp.getOutputStream().println("Error getting invoice with id " + invoiceId);
|
109
|
break;
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
}
|
114
|
|