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 eu.dnetlib.goldoa.service.dao.InvoiceDAO;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.stereotype.Service;
|
11
|
import org.springframework.transaction.annotation.Transactional;
|
12
|
|
13
|
import java.io.InputStream;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
/**
|
17
|
* Created by antleb on 3/21/15.
|
18
|
*/
|
19
|
|
20
|
@Transactional
|
21
|
@Service("invoiceManager")
|
22
|
public class InvoiceManagerImpl implements InvoiceManager {
|
23
|
@Autowired
|
24
|
private InvoiceDAO invoiceDAO;
|
25
|
|
26
|
private Log logger = LogFactory.getLog(InvoiceManagerImpl.class);
|
27
|
@Override
|
28
|
|
29
|
public Invoice saveInvoice(Invoice invoice) {
|
30
|
if (invoice.getId() == null) {
|
31
|
invoice.setSource("portal");
|
32
|
invoice.setId("portal::" + UUID.randomUUID().toString());
|
33
|
}
|
34
|
return invoiceDAO.saveInvoice(invoice);
|
35
|
}
|
36
|
|
37
|
@Override
|
38
|
public Invoice getInvoice(String invoiceId) throws ManagerException {
|
39
|
Invoice invoice = invoiceDAO.getByKey(invoiceId);
|
40
|
if(invoice == null)
|
41
|
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS);
|
42
|
else
|
43
|
return invoice;
|
44
|
}
|
45
|
|
46
|
@Override
|
47
|
public Invoice uploadInvoice(final String invoiceId, final String mimetype, InputStream invoice) throws ManagerException {
|
48
|
try {
|
49
|
return invoiceDAO.uploadInvoice(invoiceId,mimetype,invoice);
|
50
|
} catch (Exception e) {
|
51
|
logger.debug("uploadInvoice",e);
|
52
|
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
|
53
|
}
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
public InvoiceFile downloadInvoice(String invoiceId) throws ManagerException {
|
58
|
|
59
|
Invoice inv = invoiceDAO.getByKey(invoiceId);
|
60
|
if(inv == null)
|
61
|
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS);
|
62
|
if(inv.getFile() == null)
|
63
|
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
|
64
|
return new InvoiceFile(inv,inv.getFile().getMimetype(),inv.getFile().getFile());
|
65
|
}
|
66
|
|
67
|
}
|