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