1
|
package eu.dnetlib.goldoa.service.dao;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.BankAccount;
|
4
|
import eu.dnetlib.goldoa.domain.Budget;
|
5
|
import eu.dnetlib.goldoa.domain.BudgetComment;
|
6
|
import eu.dnetlib.goldoa.domain.Comment;
|
7
|
import eu.dnetlib.goldoa.domain.Currency;
|
8
|
import org.apache.commons.codec.digest.DigestUtils;
|
9
|
import org.apache.commons.io.IOUtils;
|
10
|
import org.hibernate.criterion.Order;
|
11
|
import org.hibernate.criterion.Restrictions;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
import org.springframework.dao.EmptyResultDataAccessException;
|
14
|
import org.springframework.jdbc.core.JdbcTemplate;
|
15
|
import org.springframework.jdbc.core.PreparedStatementCreator;
|
16
|
import org.springframework.jdbc.core.PreparedStatementSetter;
|
17
|
import org.springframework.jdbc.core.RowMapper;
|
18
|
|
19
|
import javax.sql.DataSource;
|
20
|
import java.io.ByteArrayOutputStream;
|
21
|
import java.io.InputStream;
|
22
|
import java.sql.Connection;
|
23
|
import java.sql.PreparedStatement;
|
24
|
import java.sql.ResultSet;
|
25
|
import java.sql.SQLException;
|
26
|
import java.sql.Types;
|
27
|
import java.util.ArrayList;
|
28
|
import java.util.Date;
|
29
|
import java.util.List;
|
30
|
import java.util.UUID;
|
31
|
|
32
|
/**
|
33
|
* Created by antleb on 4/2/15.
|
34
|
*/
|
35
|
public class BudgetDAO extends AbstractDao<String, Budget>{
|
36
|
|
37
|
@Autowired
|
38
|
private DataSource dataSource;
|
39
|
|
40
|
private static final String GET_BUDGET = "select id, date, startdate, \"enddate\", amount_requested, amount_granted, remaining, currency, status, bank_name, bank_address, bank_code, bank_holder, bank_iban, \"user\", transfer_cost, other_cost, date_paid, organization, publisher, invoice from budget b where id=?";
|
41
|
private static final String GET_BUDGETS = "select id, date, startdate, \"enddate\", amount_requested, amount_granted, remaining, currency, status, bank_name, bank_address, bank_code, bank_holder, bank_iban, \"user\", transfer_cost, other_cost, date_paid, organization, publisher, invoice from budget b order by date";
|
42
|
private static final String UPDATE_BUDGET = "update budget set date=?, startdate=?, \"enddate\"=?, amount_requested=?, amount_granted=?, remaining=?, currency=?, status=?, \"user\"=?, bank_name=?, bank_address=?, bank_code=?, bank_holder=?, bank_iban=?, transfer_cost=?, other_cost=?, date_paid=?, organization=?, publisher=?, invoice=? where id=?";
|
43
|
private static final String INSERT_BUDGET = "insert into budget (date, startdate, \"enddate\", amount_requested, amount_granted, remaining, currency, status, \"user\", bank_name, bank_address, bank_code, bank_holder, bank_iban, transfer_cost, other_cost, date_paid, organization, publisher, invoice, id) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
44
|
|
45
|
private static final String GET_USER_BUDGETS = "select id, date, startdate, \"enddate\", amount_requested, amount_granted, remaining, currency, status, bank_name, bank_address, bank_code, bank_holder, bank_iban, \"user\", transfer_cost, other_cost, date_paid, organization, publisher, invoice from budget b where \"user\"=? order by date";
|
46
|
private static final String GET_ORGANIZATION_BUDGETS = "select id, date, startdate, \"enddate\", amount_requested, amount_granted, remaining, currency, status, bank_name, bank_address, bank_code, bank_holder, bank_iban, \"user\", transfer_cost, other_cost, date_paid, organization, publisher, invoice from budget b where array[organization]::text[] <@ ?::text[] order by date";
|
47
|
private static final String GET_PUBLISHER_BUDGETS = "select id, date, startdate, \"enddate\", amount_requested, amount_granted, remaining, currency, status, bank_name, bank_address, bank_code, bank_holder, bank_iban, \"user\", transfer_cost, other_cost, date_paid, organization, publisher, invoice from budget b where publisher=? order by date";
|
48
|
private static final String GET_ACCOUNTING_BUDGETS = "select id, date, startdate, \"enddate\", amount_requested, amount_granted, remaining, currency, status, bank_name, bank_address, bank_code, bank_holder, bank_iban, \"user\", transfer_cost, other_cost, date_paid, organization, publisher, invoice from budget b order by date";
|
49
|
private static final String APPROVE_BUDGET = "update budget set status = ?, amount_granted=?, remaining=? where id=?";
|
50
|
private static final String UPDATE_BUDGET_STATUS = "update budget set status = ? where id=?";
|
51
|
|
52
|
private static final String UPDATE_BUDGET_TERMS = "update budget set termsmimetype=?, termsfile=? where id=?";
|
53
|
private static final String INSERT_BUDGET_TERMS = "insert into budget (termsmimetype, termsfile, id) values (?,?,?)";
|
54
|
// private static final String GET_BUDGET_FILE = "select termsmimetype, termsfile from budget where id=?";
|
55
|
|
56
|
private RowMapper<Budget> budgetRowMapper = new RowMapper<Budget>() {
|
57
|
@Override
|
58
|
public Budget mapRow(ResultSet rs, int rowNum) throws SQLException {
|
59
|
BankAccount bankAccount = new BankAccount(rs.getString("bank_name"), rs.getString("bank_address"), rs.getString("bank_code"),
|
60
|
rs.getString("bank_holder"), rs.getString("bank_iban"));
|
61
|
|
62
|
|
63
|
Budget budget = new Budget(rs.getString("id"), rs.getTimestamp("date"), rs.getTimestamp("startdate"),
|
64
|
rs.getTimestamp("enddate"), rs.getFloat("amount_requested"), rs.getFloat("amount_granted"),
|
65
|
Currency.valueOf(rs.getString("currency")), rs.getFloat("remaining"), rs.getInt("status"),
|
66
|
rs.getString("user"), rs.getString("organization"), rs.getString("publisher"), bankAccount,
|
67
|
rs.getFloat("transfer_cost"), rs.getFloat("other_cost"), rs.getTimestamp("date_paid"), rs.getString("invoice"));
|
68
|
|
69
|
rs.getFloat("amount_requested");
|
70
|
if (rs.wasNull())
|
71
|
budget.setAmountRequested(null);
|
72
|
rs.getFloat("amount_granted");
|
73
|
if (rs.wasNull())
|
74
|
budget.setAmountGranted(null);
|
75
|
rs.getFloat("remaining");
|
76
|
if (rs.wasNull())
|
77
|
budget.setRemaining(null);
|
78
|
rs.getFloat("transfer_cost");
|
79
|
if (rs.wasNull())
|
80
|
budget.setTransfer_cost(null);
|
81
|
rs.getFloat("other_cost");
|
82
|
if (rs.wasNull())
|
83
|
budget.setOther_cost(null);
|
84
|
|
85
|
return budget;
|
86
|
}
|
87
|
};
|
88
|
|
89
|
public Budget saveBudget(Budget budget) {
|
90
|
persist(budget);
|
91
|
return budget;
|
92
|
}
|
93
|
|
94
|
public Budget getBudget(String budgetId) {
|
95
|
return getByKey(budgetId);
|
96
|
}
|
97
|
|
98
|
@SuppressWarnings("unchecked")
|
99
|
public List<Budget> getBudgets() {
|
100
|
return createEntityCriteria().addOrder(Order.asc("date")).list();
|
101
|
}
|
102
|
|
103
|
@SuppressWarnings("unchecked")
|
104
|
public List<Budget> getBudgetsForUser(String user_email) {
|
105
|
return createEntityCriteria().add(Restrictions.ilike("user_email", user_email))
|
106
|
.addOrder(Order.asc("date")).list();
|
107
|
}
|
108
|
|
109
|
public List<Budget> getBudgetsForOrganization(final List<String> organizationIds) {
|
110
|
List<Budget> rs = new ArrayList<>();
|
111
|
for(String orgID : organizationIds){
|
112
|
rs.add((Budget) createEntityCriteria().add(Restrictions.ilike("organization", orgID))
|
113
|
.addOrder(Order.asc("date")).list());
|
114
|
}
|
115
|
return rs;
|
116
|
}
|
117
|
|
118
|
@SuppressWarnings("unchecked")
|
119
|
public List<Budget> getBudgetsForPublisher(String publisherId) {
|
120
|
return createEntityCriteria().add(Restrictions.ilike("publisher", publisherId))
|
121
|
.addOrder(Order.asc("date")).list();
|
122
|
}
|
123
|
|
124
|
@SuppressWarnings("unchecked")
|
125
|
public List<Budget> getBudgetsForAccounting() {
|
126
|
return createEntityCriteria().addOrder(Order.asc("date")).list();
|
127
|
}
|
128
|
|
129
|
|
130
|
public void initiallyApproveBudget(String budgetId) {
|
131
|
new JdbcTemplate(dataSource).update(UPDATE_BUDGET_STATUS, new Object[]{Budget.Status.INITIALLY_APPROVED.getCode(), budgetId}, new int[]{Types.INTEGER, Types.VARCHAR});
|
132
|
}
|
133
|
|
134
|
public void approveBudget(String budgetId, float amountGranted) {
|
135
|
new JdbcTemplate(dataSource).update(APPROVE_BUDGET, new Object[]{Budget.Status.APPROVED.getCode(), amountGranted, amountGranted, budgetId}, new int[]{Types.INTEGER, Types.REAL, Types.REAL, Types.VARCHAR});
|
136
|
}
|
137
|
|
138
|
public void rejectBudget(String budgetId) {
|
139
|
new JdbcTemplate(dataSource).update(UPDATE_BUDGET_STATUS, new Object[]{Budget.Status.REJECTED.getCode(), budgetId}, new int[]{Types.INTEGER, Types.VARCHAR});
|
140
|
}
|
141
|
|
142
|
public void processingBudget(String budgetId) {
|
143
|
new JdbcTemplate(dataSource).update(UPDATE_BUDGET_STATUS, new Object[]{Budget.Status.ACCOUNTING_PROCESSING.getCode(), budgetId}, new int[]{Types.INTEGER, Types.VARCHAR});
|
144
|
}
|
145
|
|
146
|
public void accountingDeniedBudget(String budgetId) {
|
147
|
new JdbcTemplate(dataSource).update(UPDATE_BUDGET_STATUS, new Object[]{Budget.Status.ACCOUNTING_DENIED.getCode(), budgetId}, new int[]{Types.INTEGER, Types.VARCHAR});
|
148
|
}
|
149
|
|
150
|
public void accountingPaidBudget(String budgetId) {
|
151
|
new JdbcTemplate(dataSource).update(UPDATE_BUDGET_STATUS, new Object[]{Budget.Status.ACCOUNTING_PAID.getCode(), budgetId}, new int[]{Types.INTEGER, Types.VARCHAR});
|
152
|
}
|
153
|
|
154
|
public void accountingOnHoldBudget(String budgetId) {
|
155
|
new JdbcTemplate(dataSource).update(UPDATE_BUDGET_STATUS, new Object[]{Budget.Status.ACCOUNTING_ONHOLD.getCode(), budgetId}, new int[]{Types.INTEGER, Types.VARCHAR});
|
156
|
}
|
157
|
|
158
|
public void uploadTerms(final String id, final String contentType, InputStream inputStream) {
|
159
|
try {
|
160
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
161
|
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
162
|
|
163
|
IOUtils.copy(inputStream, baos);
|
164
|
IOUtils.closeQuietly(baos);
|
165
|
|
166
|
final PreparedStatementSetter pss = new PreparedStatementSetter() {
|
167
|
@Override
|
168
|
public void setValues(PreparedStatement ps) throws SQLException {
|
169
|
ps.setString(1, contentType);
|
170
|
ps.setBytes(2, baos.toByteArray());
|
171
|
ps.setString(3, id);
|
172
|
}
|
173
|
};
|
174
|
|
175
|
if (jdbcTemplate.update(UPDATE_BUDGET_TERMS, pss) == 0) {
|
176
|
jdbcTemplate.update(INSERT_BUDGET_TERMS, pss);
|
177
|
}
|
178
|
} catch (Exception e) {
|
179
|
e.printStackTrace();
|
180
|
}
|
181
|
}
|
182
|
|
183
|
public void saveComment(String budgetId, String personId, String comment, String template) {
|
184
|
String commentId = "portal::" + DigestUtils.md5Hex(comment + new Date());
|
185
|
|
186
|
new JdbcTemplate(dataSource).update("with comm as ( insert into comment (id, person, comment, date, template) values (?, ?, ?, ?, ?) returning id) insert into budget_comment (budget, comment) select ?, comm.id from comm",
|
187
|
new Object[]{commentId, personId, comment, new Date(), template, budgetId}, new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR});
|
188
|
}
|
189
|
|
190
|
public List<Comment> getBudgetComments(String budgetId) {
|
191
|
|
192
|
|
193
|
// try {
|
194
|
// return new JdbcTemplate(dataSource).query("select c.comment, c.date, c.person, c.template from comment c join budget_comment bc on bc.comment=c.id and bc.budget=? order by c.date asc", new String[]{budgetId}, new int[]{Types.VARCHAR}, new RowMapper<Comment>() {
|
195
|
// @Override
|
196
|
// public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
|
197
|
// return new Comment(new Person(rs.getString("person")), rs.getTimestamp("date"), rs.getString("comment"), rs.getString("template"));
|
198
|
// }
|
199
|
// });
|
200
|
// } catch (EmptyResultDataAccessException e) {
|
201
|
// return null;
|
202
|
// }
|
203
|
}
|
204
|
|
205
|
public void uploadBankReceipt(final String budgetId, final String contentType, InputStream inputStream) {
|
206
|
try {
|
207
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
208
|
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
209
|
|
210
|
IOUtils.copy(inputStream, baos);
|
211
|
IOUtils.closeQuietly(baos);
|
212
|
|
213
|
final PreparedStatementSetter pss = new PreparedStatementSetter() {
|
214
|
@Override
|
215
|
public void setValues(PreparedStatement ps) throws SQLException {
|
216
|
ps.setString(1, contentType);
|
217
|
ps.setBytes(2, baos.toByteArray());
|
218
|
ps.setString(3, budgetId);
|
219
|
}
|
220
|
};
|
221
|
|
222
|
jdbcTemplate.update("update budget set bank_receipt_contenttype=?, bank_receipt=? where id=?", pss);
|
223
|
|
224
|
} catch (Exception e) {
|
225
|
e.printStackTrace();
|
226
|
}
|
227
|
}
|
228
|
|
229
|
public void uploadInitialContract(final String budgetId, final String contentType, InputStream inputStream) {
|
230
|
|
231
|
try {
|
232
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
233
|
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
234
|
final String id = "portal::" + UUID.randomUUID().toString();
|
235
|
|
236
|
IOUtils.copy(inputStream, baos);
|
237
|
IOUtils.closeQuietly(baos);
|
238
|
|
239
|
final PreparedStatementSetter pss = new PreparedStatementSetter() {
|
240
|
@Override
|
241
|
public void setValues(PreparedStatement ps) throws SQLException {
|
242
|
ps.setString(1, id);
|
243
|
ps.setString(2, contentType);
|
244
|
ps.setBytes(3, baos.toByteArray());
|
245
|
ps.setString(4, budgetId);
|
246
|
}
|
247
|
};
|
248
|
|
249
|
jdbcTemplate.update("with con as (insert into budgetfile (id, contenttype, content) values (?,?,?) returning id) update budget b set initialcontractfile=con.id where b.id=?", pss);
|
250
|
|
251
|
} catch (Exception e) {
|
252
|
e.printStackTrace();
|
253
|
}
|
254
|
}
|
255
|
|
256
|
public void uploadBeneficiaryContract(final String budgetId, final String contentType, InputStream inputStream) {
|
257
|
try {
|
258
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
259
|
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
260
|
final String id = "portal::" + UUID.randomUUID().toString();
|
261
|
|
262
|
IOUtils.copy(inputStream, baos);
|
263
|
IOUtils.closeQuietly(baos);
|
264
|
|
265
|
final PreparedStatementSetter pss = new PreparedStatementSetter() {
|
266
|
@Override
|
267
|
public void setValues(PreparedStatement ps) throws SQLException {
|
268
|
ps.setString(1, id);
|
269
|
ps.setString(2, contentType);
|
270
|
ps.setBytes(3, baos.toByteArray());
|
271
|
ps.setString(4, budgetId);
|
272
|
}
|
273
|
};
|
274
|
|
275
|
jdbcTemplate.update("with con as (insert into budgetfile (id, contenttype, content) values (?,?,?) returning id) update budget b set beneficiarysignedcontract=con.id where b.id=?", pss);
|
276
|
|
277
|
} catch (Exception e) {
|
278
|
e.printStackTrace();
|
279
|
}
|
280
|
}
|
281
|
|
282
|
public void uploadSignedContract(final String budgetId, final String contentType, InputStream inputStream) {
|
283
|
try {
|
284
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
285
|
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
286
|
final String id = "portal::" + UUID.randomUUID().toString();
|
287
|
|
288
|
IOUtils.copy(inputStream, baos);
|
289
|
IOUtils.closeQuietly(baos);
|
290
|
|
291
|
final PreparedStatementSetter pss = new PreparedStatementSetter() {
|
292
|
@Override
|
293
|
public void setValues(PreparedStatement ps) throws SQLException {
|
294
|
ps.setString(1, id);
|
295
|
ps.setString(2, contentType);
|
296
|
ps.setBytes(3, baos.toByteArray());
|
297
|
ps.setString(4, budgetId);
|
298
|
}
|
299
|
};
|
300
|
|
301
|
jdbcTemplate.update("with con as (insert into budgetfile (id, contenttype, content) values (?,?,?) returning id) update budget b set signedcontract=con.id where b.id=?", pss);
|
302
|
|
303
|
} catch (Exception e) {
|
304
|
e.printStackTrace();
|
305
|
}
|
306
|
}
|
307
|
|
308
|
public int getRequestId() {
|
309
|
return new JdbcTemplate(dataSource).queryForObject("select nextval('budget_id_seq') as id", new RowMapper<Integer>() {
|
310
|
@Override
|
311
|
public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
|
312
|
return rs.getInt("id");
|
313
|
}
|
314
|
});
|
315
|
}
|
316
|
}
|