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.codec.digest.DigestUtils;
7
import org.apache.commons.io.IOUtils;
8
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;
13
import org.springframework.transaction.annotation.Transactional;
14

    
15
import javax.sql.DataSource;
16
import java.io.ByteArrayInputStream;
17
import java.io.ByteArrayOutputStream;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.sql.PreparedStatement;
21
import java.sql.ResultSet;
22
import java.sql.SQLException;
23
import java.sql.Types;
24
import java.util.UUID;
25

    
26
/**
27
 * Created by antleb on 3/21/15.
28
 */
29
@Transactional
30
public class InvoiceManagerImpl implements InvoiceManager {
31

    
32
    // TODO move to dao if needed
33
    @Autowired
34
    private DataSource dataSource;
35

    
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
    @Override
49
    public Invoice saveInvoice(Invoice invoice) {
50
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
51

    
52
        if (invoice.getId() == null) {
53
            invoice.setSource("portal");
54
            invoice.setId("portal::" + UUID.randomUUID().toString());
55
        }
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;
64
    }
65

    
66
    @Override
67
    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
        }
86
    }
87

    
88
    @Override
89
    public void uploadInvoice(final String invoiceId, final String mimetype, InputStream invoice) throws ManagerException {
90
        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
            }
108
        } catch (Exception e) {
109
            throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
110
        }
111
    }
112

    
113
    @Override
114
    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

    
129
    public DataSource getDataSource() {
130
        return dataSource;
131
    }
132

    
133
    public void setDataSource(DataSource dataSource) {
134
        this.dataSource = dataSource;
135
    }
136
}
(12-12/29)