1
|
package unitest;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.*;
|
4
|
import eu.dnetlib.goldoa.service.AlternativeFundingBidManager;
|
5
|
import eu.dnetlib.goldoa.service.InvoiceManager;
|
6
|
import eu.dnetlib.goldoa.service.OrganizationManager;
|
7
|
import org.apache.commons.io.IOUtils;
|
8
|
import org.junit.Test;
|
9
|
import org.junit.runner.RunWith;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.test.annotation.Rollback;
|
12
|
import org.springframework.test.context.ContextConfiguration;
|
13
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
14
|
|
15
|
import javax.transaction.Transactional;
|
16
|
import java.io.*;
|
17
|
import java.io.File;
|
18
|
import java.nio.file.Files;
|
19
|
import java.nio.file.Path;
|
20
|
import java.nio.file.Paths;
|
21
|
import java.sql.Timestamp;
|
22
|
import java.text.ParseException;
|
23
|
import java.text.SimpleDateFormat;
|
24
|
import java.util.Date;
|
25
|
import java.util.List;
|
26
|
|
27
|
import static eu.dnetlib.goldoa.domain.Constants.*;
|
28
|
|
29
|
@RunWith( SpringJUnit4ClassRunner.class )
|
30
|
@ContextConfiguration(locations = "classpath:applicationContext-goldoa-service-test.xml")
|
31
|
public class AlternativeFundingBidsTest {
|
32
|
|
33
|
@Autowired
|
34
|
OrganizationManager organizationManager;
|
35
|
@Autowired
|
36
|
InvoiceManager invoiceManager;
|
37
|
@Autowired
|
38
|
AlternativeFundingBidManager alternativeFundingBidManager;
|
39
|
|
40
|
@Test
|
41
|
@Transactional
|
42
|
@Rollback(true)
|
43
|
public void importAlternativeFundingBids(){
|
44
|
|
45
|
alternativeFundingBidManager.importAlternativeFundingBids();
|
46
|
/*String path = "/home/panagiotis/Desktop/uploadFiles/";
|
47
|
BufferedReader br = null;
|
48
|
String s = "";
|
49
|
String cvsSplitBy = ",";
|
50
|
AlternativeFundingBid bid = null;
|
51
|
|
52
|
try {
|
53
|
br = new BufferedReader(new FileReader(path + "Alternative Funding Bids - First round.csv"));
|
54
|
s = br.readLine();
|
55
|
while ((s = br.readLine()) != null) {
|
56
|
|
57
|
System.out.println("Line -> " + s);
|
58
|
|
59
|
String[] line = s.split(cvsSplitBy);
|
60
|
String id = !line[0].equals("")?line[0].trim():null;
|
61
|
String funding_name = !line[1].equals("")?line[1].trim():null;
|
62
|
String organization = !line[2].equals("")?line[2].trim():null;
|
63
|
String country = !line[3].equals("")?line[3].trim():null;
|
64
|
String totalAmount = !line[4].equals("")?line[4].trim():null;
|
65
|
String contractStartDate = !line[5].equals("")?line[5].trim():null;
|
66
|
String contractEndDate = !line[6].equals("")?line[6].trim():null;
|
67
|
|
68
|
String firstInvoiceDate = !line[7].equals("")?line[7].trim():null;
|
69
|
String firstInvoiceNumber = !line[8].equals("")?line[8].trim():null;
|
70
|
|
71
|
String secondInvoiceDate = !line[9].equals("")?line[9].trim():null;
|
72
|
String secondInvoiceNumber = !line[10].equals("")?line[10].trim():null;
|
73
|
|
74
|
String firstPaymentAmount = !line[11].equals("")?line[11].trim():null;
|
75
|
String firstPaymentDate = !line[12].equals("")?line[12].trim():null;
|
76
|
String firstPaymentOtherCosts = !line[13].equals("")?line[13].trim():null;
|
77
|
String firstPaymentTransferCost = !line[14].equals("")?line[14].trim():null;
|
78
|
|
79
|
String secondPaymentAmount = !line[15].equals("")?line[15].trim():null;
|
80
|
String secondPaymentDate = !line[16].equals("")?line[16].trim():null;
|
81
|
String secondPaymentOtherCosts = !line[17].equals("")?line[17].trim():null;
|
82
|
String secondPaymentTransferCost = !line[18].equals("")?line[18].trim():null;
|
83
|
|
84
|
|
85
|
List<Object> orgs = organizationManager.search(organization);
|
86
|
if(orgs == null)
|
87
|
System.out.println("Organization " + organization + " not exists!");
|
88
|
|
89
|
Organization org = (Organization) organizationManager.search(organization).get(0);
|
90
|
bid = this.initializeBid(funding_name,id,country,totalAmount,contractStartDate,contractEndDate);
|
91
|
System.out.println("Bid initialized!");
|
92
|
|
93
|
bid.setFirstInvoice(this.uploadInvoiceFile(path,funding_name,FIRST_INVOICE_PDF,
|
94
|
firstInvoiceDate,firstInvoiceNumber));
|
95
|
System.out.println("First invoice ready!");
|
96
|
|
97
|
bid.setSecondInvoice(this.uploadInvoiceFile(path,funding_name,SECOND_INVOICE_PDF,
|
98
|
secondInvoiceDate,secondInvoiceNumber));
|
99
|
System.out.println("Second invoice ready!");
|
100
|
|
101
|
bid.setFirstPaymentDocument(this.uploadReceiptFile(path,funding_name,FIRST_PAYMENT_PDF,
|
102
|
firstPaymentAmount,firstPaymentDate,firstPaymentOtherCosts,firstPaymentTransferCost));
|
103
|
System.out.println("First payment ready!");
|
104
|
|
105
|
bid.setSecondPaymentDocument(this.uploadReceiptFile(path,funding_name,SECOND_PAYMENT_PDF,
|
106
|
secondPaymentAmount,secondPaymentDate,secondPaymentOtherCosts,secondPaymentTransferCost));
|
107
|
System.out.println("Second payment ready!");
|
108
|
|
109
|
bid.setContract(this.uploadContract(path,funding_name,CONTRACT_PDF));
|
110
|
System.out.println("Contract ready!");
|
111
|
//alternativeFundingBidManager.save(bid);
|
112
|
}
|
113
|
|
114
|
} catch (IOException | ParseException e) {
|
115
|
e.printStackTrace();
|
116
|
}*/
|
117
|
}
|
118
|
|
119
|
private eu.dnetlib.goldoa.domain.File uploadContract(String basePath, String funding_name, String contract) {
|
120
|
|
121
|
String fullPath = basePath + funding_name + "/" + contract;
|
122
|
File file = new File(basePath + funding_name + "/" + contract);
|
123
|
InputStream inputStream = null;
|
124
|
Path source = Paths.get(fullPath);
|
125
|
String contentType = null;
|
126
|
eu.dnetlib.goldoa.domain.File contractFile = null;
|
127
|
try {
|
128
|
inputStream = new FileInputStream(file);
|
129
|
contentType = Files.probeContentType(source);
|
130
|
|
131
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
132
|
IOUtils.copy(inputStream, baos);
|
133
|
IOUtils.closeQuietly(baos);
|
134
|
|
135
|
contractFile = new eu.dnetlib.goldoa.domain.File();
|
136
|
contractFile.setId(invoiceManager.getFileId());
|
137
|
contractFile.setMimetype(contentType);
|
138
|
contractFile.setFile(baos.toByteArray());
|
139
|
|
140
|
|
141
|
|
142
|
} catch (IOException e) {
|
143
|
e.printStackTrace();
|
144
|
}
|
145
|
|
146
|
return contractFile;
|
147
|
}
|
148
|
|
149
|
private BankTransferReceipt uploadReceiptFile(String basePath, String funding_name, String receipt,
|
150
|
String paymentAmount, String paymentDate,
|
151
|
String paymentOtherCosts, String paymentTransferCost) throws ParseException {
|
152
|
|
153
|
String fullPath = basePath + funding_name + "/" + receipt;
|
154
|
File file = new File(basePath + funding_name + "/" + receipt);
|
155
|
InputStream inputStream = null;
|
156
|
Path source = Paths.get(fullPath);
|
157
|
String contentType = null;
|
158
|
Invoice invoice = null;
|
159
|
BankTransferReceipt bankTransferReceipt = null;
|
160
|
try {
|
161
|
inputStream = new FileInputStream(file);
|
162
|
contentType = Files.probeContentType(source);
|
163
|
|
164
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
165
|
IOUtils.copy(inputStream, baos);
|
166
|
IOUtils.closeQuietly(baos);
|
167
|
bankTransferReceipt = new BankTransferReceipt();
|
168
|
bankTransferReceipt.setContent(baos.toByteArray());
|
169
|
bankTransferReceipt.setContentType(contentType);
|
170
|
bankTransferReceipt.setAmount(Double.valueOf(paymentAmount));
|
171
|
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd").parse(paymentDate);
|
172
|
Timestamp st = new java.sql.Timestamp(parsedDate.getTime());
|
173
|
bankTransferReceipt.setDate(st);
|
174
|
bankTransferReceipt.setOtherCosts(Double.valueOf(paymentOtherCosts));
|
175
|
bankTransferReceipt.setTransferCost(Double.valueOf(paymentTransferCost));
|
176
|
} catch (IOException e) {
|
177
|
e.printStackTrace();
|
178
|
}
|
179
|
return bankTransferReceipt;
|
180
|
|
181
|
}
|
182
|
|
183
|
private AlternativeFundingBid initializeBid(String funding_name, String id, String country,
|
184
|
String totalAmount, String contractStartDate,
|
185
|
String contractEndDate) {
|
186
|
|
187
|
AlternativeFundingBid bid = new AlternativeFundingBid();
|
188
|
bid.setBidName(funding_name);
|
189
|
bid.setId(id);
|
190
|
bid.setCountry(alternativeFundingBidManager.getCountry(country));
|
191
|
bid.setBidNo(Integer.valueOf(id));
|
192
|
bid.setTotalAmountRequested(Double.parseDouble(totalAmount));
|
193
|
bid.setFirstPayment(Double.parseDouble(totalAmount)/2);
|
194
|
bid.setSecondPayment(Double.parseDouble(totalAmount)/2);
|
195
|
bid.setRound(FIRST_ROUND);
|
196
|
try {
|
197
|
if(contractStartDate != null){
|
198
|
Date parsedStartDate = new SimpleDateFormat("yyyy-MM-dd").parse(contractStartDate);
|
199
|
Timestamp start = new java.sql.Timestamp(parsedStartDate.getTime());
|
200
|
bid.setContractStartDate(start);
|
201
|
}
|
202
|
|
203
|
if(contractEndDate != null){
|
204
|
Date parsedEndDate = new SimpleDateFormat("yyyy-MM-dd").parse(contractEndDate);
|
205
|
Timestamp end = new java.sql.Timestamp(parsedEndDate.getTime());
|
206
|
bid.setContractEndDate(end);
|
207
|
}
|
208
|
} catch (ParseException e) {
|
209
|
System.out.println("Start or end date null! ");
|
210
|
}
|
211
|
return bid;
|
212
|
|
213
|
}
|
214
|
|
215
|
private Invoice uploadInvoiceFile(String basePath, String funding_name, String invoicePdf,
|
216
|
String invoiceDate, String invoiceNumber) throws ParseException {
|
217
|
|
218
|
String fullPath = basePath + funding_name + "/" + invoicePdf;
|
219
|
File file = new File(basePath + funding_name + "/" + invoicePdf);
|
220
|
InputStream inputStream = null;
|
221
|
Path source = Paths.get(fullPath);
|
222
|
String contentType = null;
|
223
|
Invoice invoice = null;
|
224
|
try {
|
225
|
inputStream = new FileInputStream(file);
|
226
|
contentType = Files.probeContentType(source);
|
227
|
invoice = new Invoice();
|
228
|
invoice.setSource("portal");
|
229
|
invoice.setNumber(invoiceNumber);
|
230
|
Date parsedStartDate = new SimpleDateFormat("yyyy-MM-dd").parse(invoiceDate);
|
231
|
Timestamp st = new java.sql.Timestamp(parsedStartDate.getTime());
|
232
|
invoice.setDate(st);
|
233
|
invoice = invoiceManager.saveInvoice(invoice);
|
234
|
invoice = invoiceManager.uploadInvoice(invoice.getId(), contentType, inputStream);
|
235
|
invoiceManager.saveInvoice(invoice);
|
236
|
} catch (IOException | ManagerException e) {
|
237
|
e.printStackTrace();
|
238
|
}
|
239
|
return invoice;
|
240
|
}
|
241
|
|
242
|
|
243
|
}
|