Revision 45623
Added by Panagiotis Kanakakis about 7 years ago
InvoiceManagerImpl.java | ||
---|---|---|
1 |
/* |
|
2 | 1 |
package eu.dnetlib.goldoa.service; |
3 | 2 |
|
4 | 3 |
import eu.dnetlib.goldoa.domain.Invoice; |
5 | 4 |
import eu.dnetlib.goldoa.domain.InvoiceFile; |
6 | 5 |
import eu.dnetlib.goldoa.domain.ManagerException; |
7 |
import org.apache.commons.io.IOUtils;
|
|
6 |
import eu.dnetlib.goldoa.service.dao.InvoiceDAO;
|
|
8 | 7 |
import org.springframework.beans.factory.annotation.Autowired; |
9 |
import org.springframework.dao.EmptyResultDataAccessException; |
|
10 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
11 |
import org.springframework.jdbc.core.PreparedStatementSetter; |
|
12 |
import org.springframework.jdbc.core.RowMapper; |
|
8 |
import org.springframework.stereotype.Service; |
|
13 | 9 |
import org.springframework.transaction.annotation.Transactional; |
14 | 10 |
|
15 |
import javax.sql.DataSource; |
|
16 |
import java.io.ByteArrayOutputStream; |
|
17 | 11 |
import java.io.InputStream; |
18 |
import java.sql.PreparedStatement; |
|
19 |
import java.sql.ResultSet; |
|
20 |
import java.sql.SQLException; |
|
21 |
import java.sql.Types; |
|
22 | 12 |
import java.util.UUID; |
23 | 13 |
|
24 |
*/ |
|
25 | 14 |
/** |
26 | 15 |
* Created by antleb on 3/21/15. |
27 |
*//*
|
|
16 |
*/ |
|
28 | 17 |
|
29 | 18 |
@Transactional |
19 |
@Service |
|
30 | 20 |
public class InvoiceManagerImpl implements InvoiceManager { |
31 |
|
|
32 |
// TODO move to dao if needed |
|
33 | 21 |
@Autowired |
34 |
private DataSource dataSource;
|
|
22 |
private InvoiceDAO invoiceDAO;
|
|
35 | 23 |
|
36 |
private static final String INSERT_INVOICE = "insert into invoice (number, alternativeid, date, source, id) values (?, ?, ?, ?, ?)"; |
|
37 |
|
|
38 |
private static final String UPDATE_INVOICE = "update invoice set number=?, alternativeid=?, date=?, source=? where id = ?"; |
|
39 |
|
|
40 |
private static final String GET_INVOICE = "select id, alternativeid, number, date, source from invoice where id = ?"; |
|
41 |
|
|
42 |
private static final String GET_INVOICE_FILE = "select invoice, mimetype, file from file where invoice = ?"; |
|
43 |
|
|
44 |
private static final String INSERT_INVOICE_FILE = "insert into file (mimetype, file, invoice) values (?, ?, ?)"; |
|
45 |
|
|
46 |
private static final String UPDATE_INVOICE_FILE = "update file set mimetype=?, file=? where invoice=?"; |
|
47 |
|
|
48 | 24 |
@Override |
49 | 25 |
public Invoice saveInvoice(Invoice invoice) { |
50 |
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); |
|
51 |
|
|
52 | 26 |
if (invoice.getId() == null) { |
53 | 27 |
invoice.setSource("portal"); |
54 | 28 |
invoice.setId("portal::" + UUID.randomUUID().toString()); |
55 | 29 |
} |
56 |
|
|
57 |
if (jdbcTemplate.update(UPDATE_INVOICE, new Object[]{invoice.getNumber(), invoice.getAlternativeId(), invoice.getDate(), invoice.getSource(), invoice.getId()}, |
|
58 |
new int[]{Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR}) == 0) { |
|
59 |
jdbcTemplate.update(INSERT_INVOICE, new Object[]{invoice.getNumber(), invoice.getAlternativeId(), invoice.getDate(), invoice.getSource(), invoice.getId()}, |
|
60 |
new int[]{Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR}); |
|
61 |
} |
|
62 |
|
|
63 |
return invoice; |
|
30 |
return invoiceDAO.saveInvoice(invoice); |
|
64 | 31 |
} |
65 | 32 |
|
66 | 33 |
@Override |
67 | 34 |
public Invoice getInvoice(String invoiceId) throws ManagerException { |
68 |
try { |
|
69 |
return new JdbcTemplate(dataSource).queryForObject(GET_INVOICE, new String[]{invoiceId}, new int[]{Types.VARCHAR}, new RowMapper<Invoice>() { |
|
70 |
@Override |
|
71 |
public Invoice mapRow(ResultSet rs, int rowNum) throws SQLException { |
|
72 |
Invoice invoice = new Invoice(); |
|
73 |
|
|
74 |
invoice.setId(rs.getString("id")); |
|
75 |
invoice.setAlternativeId(rs.getString("alternativeid")); |
|
76 |
invoice.setNumber(rs.getString("number")); |
|
77 |
invoice.setDate(rs.getTimestamp("date")); |
|
78 |
invoice.setSource(rs.getString("source")); |
|
79 |
|
|
80 |
return invoice; |
|
81 |
} |
|
82 |
}); |
|
83 |
} catch (EmptyResultDataAccessException e) { |
|
84 |
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS); |
|
85 |
} |
|
35 |
Invoice invoice = invoiceDAO.getByKey(invoiceId); |
|
36 |
if(invoice == null) |
|
37 |
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS); |
|
38 |
else |
|
39 |
return invoice; |
|
86 | 40 |
} |
87 | 41 |
|
88 | 42 |
@Override |
89 | 43 |
public void uploadInvoice(final String invoiceId, final String mimetype, InputStream invoice) throws ManagerException { |
90 | 44 |
try { |
91 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
92 |
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); |
|
93 |
final PreparedStatementSetter pss = new PreparedStatementSetter() { |
|
94 |
@Override |
|
95 |
public void setValues(PreparedStatement ps) throws SQLException { |
|
96 |
ps.setString(1, mimetype); |
|
97 |
ps.setBytes(2, baos.toByteArray()); |
|
98 |
ps.setString(3, invoiceId); |
|
99 |
} |
|
100 |
}; |
|
101 |
|
|
102 |
IOUtils.copy(invoice, baos); |
|
103 |
IOUtils.closeQuietly(baos); |
|
104 |
|
|
105 |
if (jdbcTemplate.update(UPDATE_INVOICE_FILE, pss) == 0) { |
|
106 |
jdbcTemplate.update(INSERT_INVOICE_FILE, pss); |
|
107 |
} |
|
45 |
invoiceDAO.uploadInvoice(invoiceId,mimetype,invoice); |
|
108 | 46 |
} catch (Exception e) { |
109 | 47 |
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN); |
110 | 48 |
} |
... | ... | |
112 | 50 |
|
113 | 51 |
@Override |
114 | 52 |
public InvoiceFile downloadInvoice(String invoiceId) throws ManagerException { |
115 |
try { |
|
116 |
return new JdbcTemplate(dataSource).queryForObject(GET_INVOICE_FILE, new String[]{invoiceId}, new int[]{Types.VARCHAR}, new RowMapper<InvoiceFile>() { |
|
117 |
@Override |
|
118 |
public InvoiceFile mapRow(ResultSet rs, int rowNum) throws SQLException { |
|
119 |
return new InvoiceFile(rs.getString("invoice"), rs.getString("mimetype"), rs.getBytes("file")); |
|
120 |
} |
|
121 |
}); |
|
122 |
} catch (EmptyResultDataAccessException e) { |
|
123 |
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS); |
|
124 |
} catch (Exception e) { |
|
125 |
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN); |
|
126 |
} |
|
127 |
} |
|
128 | 53 |
|
129 |
public DataSource getDataSource() { |
|
130 |
return dataSource; |
|
54 |
Invoice inv = invoiceDAO.getByKey(invoiceId); |
|
55 |
if(inv == null) |
|
56 |
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS); |
|
57 |
if(inv.getFile() == null) |
|
58 |
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN); |
|
59 |
return new InvoiceFile(inv,inv.getFile().getMimetype(),inv.getFile().getFile()); |
|
131 | 60 |
} |
132 | 61 |
|
133 |
public void setDataSource(DataSource dataSource) { |
|
134 |
this.dataSource = dataSource; |
|
135 |
} |
|
136 |
}*/ |
|
62 |
} |
Also available in: Unified diff