Project

General

Profile

1 38128 antonis.le
package eu.dnetlib.server;
2
3 39416 stefania.m
import eu.dnetlib.goldoa.domain.*;
4 38128 antonis.le
import eu.dnetlib.goldoa.service.RequestManager;
5
import org.apache.commons.fileupload.FileItem;
6
import org.apache.commons.fileupload.FileItemFactory;
7
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
8
import org.apache.commons.fileupload.servlet.ServletFileUpload;
9
import org.apache.commons.io.IOUtils;
10
import org.springframework.context.ApplicationContext;
11
import org.springframework.web.context.support.WebApplicationContextUtils;
12
13
import javax.servlet.ServletConfig;
14
import javax.servlet.ServletException;
15
import javax.servlet.http.HttpServlet;
16
import javax.servlet.http.HttpServletRequest;
17
import javax.servlet.http.HttpServletResponse;
18
import java.io.ByteArrayInputStream;
19
import java.io.IOException;
20
import java.text.SimpleDateFormat;
21
import java.util.Date;
22
import java.util.List;
23
24
/**
25
 * Created by antleb on 7/9/15.
26
 */
27
public class RequestServlet extends HttpServlet {
28
	private RequestManager requestManager;
29
30
	@Override
31
	public void init(ServletConfig config) throws ServletException {
32
33
		super.init(config);
34
35
		ApplicationContext context = WebApplicationContextUtils
36
				.getWebApplicationContext(getServletContext());
37
38
		this.requestManager = (RequestManager) context.getBean("requestManager");
39
	}
40
41
	@Override
42
	public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
43
		try {
44
			FileItemFactory factory = new DiskFileItemFactory();
45
			ServletFileUpload upload = new ServletFileUpload(factory);
46
			List<FileItem> items = upload.parseRequest(request);
47
48
			String requestId = null;
49
			float apcPaid = 0;
50
			float transferCost = 0;
51
			float otherCost = 0;
52
			Date datePaid = null;
53
			String comment = null;
54 39074 stefania.m
            String personId = null;
55 38128 antonis.le
56 39416 stefania.m
            String action = null;
57
58 38128 antonis.le
			for (FileItem item:items) {
59
				if (item.isFormField()) {
60
					String fieldName = item.getFieldName();
61
					String value = item.getString();
62
63
					System.out.println(value);
64
65 38813 antonis.le
					if (fieldName.equals("apcPaid") && value != null && !value.equals("")) {
66 38128 antonis.le
						apcPaid = Float.parseFloat(value);
67 38813 antonis.le
					} else if (fieldName.equals("transferCosts") && value != null && !value.equals("")) {
68 38128 antonis.le
						transferCost = Float.parseFloat(value);
69 38813 antonis.le
					} else if (fieldName.equals("otherExpenses") && value != null && !value.equals("")) {
70 38128 antonis.le
						otherCost = Float.parseFloat(value);
71 38814 stefania.m
					} else if (fieldName.equals("comment") && value!=null && !value.trim().equals("")) {
72 38128 antonis.le
						comment = value;
73 39074 stefania.m
                    } else if (fieldName.equals("personId") && value!=null && !value.trim().equals("")) {
74
                        personId = value;
75 38128 antonis.le
					} else if (fieldName.equals("requestId")) {
76
						requestId = value;
77
					} else if (fieldName.equals("dateOfTransfer")) {
78
						datePaid = new SimpleDateFormat("yyyy/MM/dd").parse(value);
79 39416 stefania.m
					} else if (fieldName.equals("action")) {
80
                        action = value;
81
                    }
82 38128 antonis.le
				}
83
			}
84
85 39416 stefania.m
            if(action!=null && action.equals("edit")) {
86 38128 antonis.le
87 45767 panagiotis
                Request r = requestManager.getById(requestId);
88
                if(r!=null) {
89 39416 stefania.m
90 45767 panagiotis
                    /*Request savedRequest = request.getRequest();
91 39416 stefania.m
                    savedRequest.setApc_paid(apcPaid);
92
                    savedRequest.setTransfer_cost(transferCost);
93
                    savedRequest.setOther_cost(otherCost);
94 45767 panagiotis
                    savedRequest.setDate_paid(datePaid);*/
95 39416 stefania.m
96 45767 panagiotis
                    requestManager.saveRequest(r);
97 39416 stefania.m
                }
98
99
            } else {
100 41562 stefania.m
                requestManager.paidRequest(requestId, personId, comment, null, apcPaid, transferCost, otherCost, datePaid);
101 39416 stefania.m
            }
102
103 38128 antonis.le
			for (FileItem item:items) {
104
				if (!item.isFormField()) {
105
					String contentType = item.getContentType();
106
107
					requestManager.uploadTransferReceipt(requestId, contentType, item.getInputStream());
108
				}
109
			}
110
111
			response.setStatus(200);
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 requestId = req.getParameter("requestId");
121
122
		try {
123
			BankTransferReceipt file = requestManager.downloadBankTransferReceipt(requestId);
124
125
			resp.setContentType(file.getContentType());
126
			resp.setContentLength(file.getContent().length);
127
128
			IOUtils.copy(new ByteArrayInputStream(file.getContent()), resp.getOutputStream());
129
		} catch (RequestManagerException e) {
130
131
			switch (e.getErrorCause()) {
132
				case NOT_EXISTS:
133
					resp.setStatus(404);
134
					resp.getOutputStream().println("Bank transfer receipt for request " + requestId + " not found");
135
					break;
136
				case UNKNOWN:
137
				default:
138
					resp.setStatus(500);
139
					resp.getOutputStream().println("Error getting transfer receipt for request " + requestId);
140
					break;
141
			}
142
		}
143
	}
144
}