Project

General

Profile

1
package eu.dnetlib.server;
2

    
3
import eu.dnetlib.goldoa.domain.*;
4
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
            String personId = null;
55

    
56
            String action = null;
57

    
58
			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
					if (fieldName.equals("apcPaid") && value != null && !value.equals("")) {
66
						apcPaid = Float.parseFloat(value);
67
					} else if (fieldName.equals("transferCosts") && value != null && !value.equals("")) {
68
						transferCost = Float.parseFloat(value);
69
					} else if (fieldName.equals("otherExpenses") && value != null && !value.equals("")) {
70
						otherCost = Float.parseFloat(value);
71
					} else if (fieldName.equals("comment") && value!=null && !value.trim().equals("")) {
72
						comment = value;
73
                    } else if (fieldName.equals("personId") && value!=null && !value.trim().equals("")) {
74
                        personId = value;
75
					} else if (fieldName.equals("requestId")) {
76
						requestId = value;
77
					} else if (fieldName.equals("dateOfTransfer")) {
78
						datePaid = new SimpleDateFormat("yyyy/MM/dd").parse(value);
79
					} else if (fieldName.equals("action")) {
80
                        action = value;
81
                    }
82
				}
83
			}
84

    
85
            if(action!=null && action.equals("edit")) {
86

    
87
                Request r = requestManager.getById(requestId);
88
                if(r!=null) {
89

    
90
                    /*Request savedRequest = request.getRequest();
91
                    savedRequest.setApc_paid(apcPaid);
92
                    savedRequest.setTransfer_cost(transferCost);
93
                    savedRequest.setOther_cost(otherCost);
94
                    savedRequest.setDate_paid(datePaid);*/
95

    
96
                    requestManager.saveRequest(r);
97
                }
98

    
99
            } else {
100
                requestManager.paidRequest(requestId, personId, comment, null, apcPaid, transferCost, otherCost, datePaid);
101
            }
102

    
103
			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
}
(5-5/5)