Project

General

Profile

« Previous | Next » 

Revision 39076

reformatted code

View differences:

InvoiceManagerImpl.java
3 3
import eu.dnetlib.goldoa.domain.Invoice;
4 4
import eu.dnetlib.goldoa.domain.InvoiceFile;
5 5
import eu.dnetlib.goldoa.domain.ManagerException;
6
import org.apache.commons.codec.digest.DigestUtils;
7 6
import org.apache.commons.io.IOUtils;
8 7
import org.springframework.beans.factory.annotation.Autowired;
9 8
import org.springframework.dao.EmptyResultDataAccessException;
......
13 12
import org.springframework.transaction.annotation.Transactional;
14 13

  
15 14
import javax.sql.DataSource;
16
import java.io.ByteArrayInputStream;
17 15
import java.io.ByteArrayOutputStream;
18
import java.io.IOException;
19 16
import java.io.InputStream;
20 17
import java.sql.PreparedStatement;
21 18
import java.sql.ResultSet;
......
29 26
@Transactional
30 27
public class InvoiceManagerImpl implements InvoiceManager {
31 28

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

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

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

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

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

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

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

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

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

  
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
        }
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
		}
62 59

  
63
        return invoice;
64
    }
60
		return invoice;
61
	}
65 62

  
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();
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();
73 70

  
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"));
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"));
79 76

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

  
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
            };
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
			};
101 98

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

  
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
    }
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
	}
112 109

  
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
    }
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
	}
128 125

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

  
133
    public void setDataSource(DataSource dataSource) {
134
        this.dataSource = dataSource;
135
    }
130
	public void setDataSource(DataSource dataSource) {
131
		this.dataSource = dataSource;
132
	}
136 133
}

Also available in: Unified diff