Project

General

Profile

1
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
import java.util.UUID;
22

    
23
/**
24
 * Created by antleb on 3/21/15.
25
 */
26
@Transactional
27
public class InvoiceManagerImpl implements InvoiceManager {
28

    
29
	// TODO move to dao if needed
30
	@Autowired
31
	private DataSource dataSource;
32

    
33
	private static final String INSERT_INVOICE = "insert into invoice (number, alternativeid, date, source, id) values (?, ?, ?, ?, ?)";
34

    
35
	private static final String UPDATE_INVOICE = "update invoice set number=?, alternativeid=?, date=?, source=? where id = ?";
36

    
37
	private static final String GET_INVOICE = "select id, alternativeid, number, date, source from invoice where id = ?";
38

    
39
	private static final String GET_INVOICE_FILE = "select invoice, mimetype, file from file where invoice = ?";
40

    
41
	private static final String INSERT_INVOICE_FILE = "insert into file (mimetype, file, invoice) values (?, ?, ?)";
42

    
43
	private static final String UPDATE_INVOICE_FILE = "update file set mimetype=?, file=? where invoice=?";
44

    
45
	@Override
46
	public Invoice saveInvoice(Invoice invoice) {
47
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
48

    
49
		if (invoice.getId() == null) {
50
			invoice.setSource("portal");
51
			invoice.setId("portal::" + UUID.randomUUID().toString());
52
		}
53

    
54
		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

    
60
		return invoice;
61
	}
62

    
63
	@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

    
71
					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

    
77
					return invoice;
78
				}
79
			});
80
		} catch (EmptyResultDataAccessException e) {
81
			throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS);
82
		}
83
	}
84

    
85
	@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

    
99
			IOUtils.copy(invoice, baos);
100
			IOUtils.closeQuietly(baos);
101

    
102
			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

    
110
	@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

    
126
	public DataSource getDataSource() {
127
		return dataSource;
128
	}
129

    
130
	public void setDataSource(DataSource dataSource) {
131
		this.dataSource = dataSource;
132
	}
133
}
(12-12/29)