Project

General

Profile

« Previous | Next » 

Revision 47260

View differences:

InvoiceDAO.java
3 3
import eu.dnetlib.goldoa.domain.Budget;
4 4
import eu.dnetlib.goldoa.domain.File;
5 5
import eu.dnetlib.goldoa.domain.Invoice;
6
import eu.dnetlib.goldoa.service.RequestManagerImpl;
6 7
import org.apache.commons.io.IOUtils;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
7 10
import org.hibernate.criterion.Restrictions;
8 11
import org.springframework.stereotype.Repository;
9 12

  
10 13
import java.io.ByteArrayOutputStream;
11 14
import java.io.IOException;
12 15
import java.io.InputStream;
16
import java.math.BigInteger;
13 17
import java.util.List;
14 18

  
15 19
/**
......
18 22
@Repository
19 23
public class InvoiceDAO extends AbstractDao<String, Invoice>{
20 24

  
25
    private Log logger = LogFactory.getLog(InvoiceDAO.class);
21 26

  
22 27
    public Invoice saveInvoice(Invoice invoice) {
23
        persist(invoice);
28

  
29
        Invoice i = getByKey(invoice.getId());
30
        Invoice i_merged = null;
31

  
32
        if(i!=null) {
33
            logger.debug("Merge invoice with id " + i.getId());
34

  
35
            if(i.getFile()!=null)
36
                invoice.setFile(i.getFile());
37

  
38

  
39
            i_merged = (Invoice) getSession().merge(invoice);
40
        }else
41
            persist(invoice);
42

  
24 43
        return invoice;
25 44
    }
26 45

  
......
33 52
        return null;
34 53
    }
35 54

  
36
    public void uploadInvoice(String invoiceId, String mimetype, InputStream invoice) throws IOException {
55
    public Invoice uploadInvoice(String invoiceId, String mimetype, InputStream inputStream) throws IOException {
37 56
        Invoice inv = getByKey(invoiceId);
38 57

  
39 58
        ByteArrayOutputStream baos = null;
40 59
        baos = new ByteArrayOutputStream();
41
        IOUtils.copy(invoice, baos);
60
        IOUtils.copy(inputStream, baos);
42 61
        IOUtils.closeQuietly(baos);
43 62

  
63

  
44 64
        File file = new File();
65
        file.setId(getFileId());
45 66
        file.setMimetype(mimetype);
46 67
        file.setFile(baos.toByteArray());
47 68

  
48 69
        inv.setFile(file);
49
        persist(inv);
70
        return inv;
50 71
    }
72

  
73
    private BigInteger getFileId(){
74
        return (BigInteger) getSession().createSQLQuery("select nextval('file_id_seq') as id").list().get(0);
75
    }
51 76
}

Also available in: Unified diff