Project

General

Profile

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