Project

General

Profile

1 35969 stefania.m
package eu.dnetlib.server;
2
3 35987 antonis.le
import eu.dnetlib.goldoa.domain.*;
4
import eu.dnetlib.goldoa.service.BudgetManager;
5 35969 stefania.m
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 35987 antonis.le
    private BudgetManager budgetManager;
31 35969 stefania.m
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 35987 antonis.le
        this.budgetManager = (BudgetManager) context.getBean("budgetManager");
41 35969 stefania.m
    }
42
43
    @Override
44
    public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
45 35987 antonis.le
        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 37436 stefania.m
            BankAccount bankAccount = null;
52 35987 antonis.le
53 37436 stefania.m
            budget.setCurrency(Currency.EUR);
54
55 35987 antonis.le
            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 46067 panagiotis
                        //TODO fix!
64
                        //budget.setOrganization(value);
65 37436 stefania.m
                    } else if (fieldName.equals("amountRequested")) {
66
                        budget.setAmountRequested(Float.parseFloat(value));
67 35987 antonis.le
                    } else if (fieldName.equals("userId")) {
68 46067 panagiotis
                        //budget.setUser(value);
69 37436 stefania.m
                    } else if (fieldName.equals("startDate")) {
70 45767 panagiotis
                        budget.setStartdate(new SimpleDateFormat("yyyy/MM/dd").parse(value));
71 37436 stefania.m
                    } else if (fieldName.equals("endDate")) {
72 45767 panagiotis
                        budget.setEnddate(new SimpleDateFormat("yyyy/MM/dd").parse(value));
73 37436 stefania.m
                    } 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 35987 antonis.le
                    }
94 37436 stefania.m
95 35987 antonis.le
                } else {
96
                    String contentType = item.getContentType();
97
98
99 43258 stefania.m
//                    budgetManager.uploadTerms(budget.getId(), contentType, item.getInputStream());
100 35987 antonis.le
                }
101
            }
102
103 37436 stefania.m
            budget.setBankAccount(bankAccount);
104
105 45767 panagiotis
            budget.setStatus(Budget.Status.SUBMITTED);
106 35987 antonis.le
            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 35969 stefania.m
    }
117
118
    @Override
119
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
120 35987 antonis.le
        String budgetId = req.getParameter("budgetId");
121
122
        try {
123 43258 stefania.m
//            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 35987 antonis.le
        } catch (Exception e) {
130
            resp.setStatus(500);
131
            resp.getOutputStream().println("Error getting signed terms of agreement of budget with id " + budgetId);
132
        }
133 35969 stefania.m
    }
134
}