Project

General

Profile

1
package eu.dnetlib.goldoa.service.dao;
2

    
3
import eu.dnetlib.goldoa.domain.*;
4
import org.apache.commons.codec.digest.DigestUtils;
5
import org.apache.commons.io.IOUtils;
6
import org.hibernate.criterion.Order;
7
import org.hibernate.criterion.Restrictions;
8
import org.springframework.stereotype.Repository;
9

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

    
19
//import eu.dnetlib.goldoa.domain.BankAccount;
20

    
21
/*
22
 * Created by antleb on 4/2/15.
23
*/
24
@Repository
25
public class BudgetDAO extends AbstractDao<String, Budget>{
26

    
27
	private static final String UPDATE_BUDGET_TERMS = "update budget set termsmimetype=?, termsfile=? where id=?";
28
	private static final String INSERT_BUDGET_TERMS = "insert into budget (termsmimetype, termsfile, id) values (?,?,?)";
29
//	private static final String GET_BUDGET_FILE = "select termsmimetype, termsfile from budget where id=?";
30

    
31
	public Budget saveBudget(Budget budget) {
32
		persist(budget);
33
		return budget;
34
	}
35

    
36
	public Budget getBudget(String budgetId) {
37
		return getByKey(budgetId);
38
	}
39

    
40
	@SuppressWarnings("unchecked")
41
	public List<Budget> getBudgets() {
42
		return createEntityCriteria().addOrder(Order.asc("date")).list();
43
	}
44

    
45
	@SuppressWarnings("unchecked")
46
	public List<Budget> getBudgetsForUser(String email) {
47
		return createEntityCriteria().add(Restrictions.eq("user.email",email))
48
				.addOrder(Order.asc("date")).list();
49
	}
50

    
51
	@SuppressWarnings("unchecked")
52
	public List<Budget> getBudgetsForOrganization(final List<String> organizationIds) {
53
		List<Budget> rs = new ArrayList<>();
54
		for(String orgID : organizationIds){
55
			rs.addAll( createEntityCriteria().createAlias("organizations","o")
56
					.add(Restrictions.eq("o.id",orgID))
57
					.addOrder(Order.asc("date")).list());
58
		}
59
		return rs;
60
	}
61

    
62
	@SuppressWarnings("unchecked")
63
	public List<Budget> getBudgetsForPublisher(String publisherEmail) {
64
		return createEntityCriteria().add(Restrictions.eq("publisher", publisherEmail))
65
				.addOrder(Order.asc("date")).list();
66
	}
67

    
68
	@SuppressWarnings("unchecked")
69
	public List<Budget> getBudgetsForAccounting() {
70
		return createEntityCriteria().addOrder(Order.asc("date")).list();
71
	}
72

    
73

    
74
	public void initiallyApproveBudget(String budgetId) {
75
        Budget b = getBudget(budgetId);
76
        b.setStatus(Budget.Status.INITIALLY_APPROVED);
77
        saveBudget(b);
78
	}
79

    
80
	public void approveBudget(String budgetId, float amountGranted) {
81
        Budget b = getBudget(budgetId);
82
        b.setStatus(Budget.Status.APPROVED);
83
        saveBudget(b);
84
	}
85

    
86
	public void rejectBudget(String budgetId) {
87
        Budget b = getBudget(budgetId);
88
        b.setStatus(Budget.Status.REJECTED);
89
        saveBudget(b);
90
	}
91

    
92
	public void processingBudget(String budgetId) {
93
        Budget b = getBudget(budgetId);
94
        b.setStatus(Budget.Status.ACCOUNTING_PROCESSING);
95
        saveBudget(b);
96
	}
97

    
98
	public void accountingDeniedBudget(String budgetId) {
99
        Budget b = getBudget(budgetId);
100
        b.setStatus(Budget.Status.ACCOUNTING_DENIED);
101
        saveBudget(b);
102
	}
103

    
104
	public void accountingPaidBudget(String budgetId) {
105
        Budget b = getBudget(budgetId);
106
        b.setStatus(Budget.Status.ACCOUNTING_PAID);
107
        saveBudget(b);
108
	}
109

    
110
	public void accountingOnHoldBudget(String budgetId) {
111
        Budget b = getBudget(budgetId);
112
        b.setStatus(Budget.Status.ACCOUNTING_ONHOLD);
113
        saveBudget(b);
114
	}
115

    
116
	public void uploadTerms(final String id, final String contentType, InputStream inputStream) {
117
		/*try {
118
			final ByteArrayOutputStream baos = new ByteArrayOutputStream();
119

    
120
			IOUtils.copy(inputStream, baos);
121
			IOUtils.closeQuietly(baos);
122

    
123
			final PreparedStatementSetter pss = new PreparedStatementSetter() {
124
				@Override
125
				public void setValues(PreparedStatement ps) throws SQLException {
126
					ps.setString(1, contentType);
127
					ps.setBytes(2, baos.toByteArray());
128
					ps.setString(3, id);
129
				}
130
			};
131

    
132
			if (jdbcTemplate.update(UPDATE_BUDGET_TERMS, pss) == 0) {
133
				jdbcTemplate.update(INSERT_BUDGET_TERMS, pss);
134
			}
135

    
136
		} catch (Exception e) {
137
			e.printStackTrace();
138
		}*/
139
	}
140

    
141
	public void saveComment(String budgetId, User user, String comment, String template) {
142
        String commentId = "portal::" + DigestUtils.md5Hex(comment + new Date());
143
        Comment c = new Comment();
144
        c.setId(commentId);
145
        c.setPerson(user);
146
        c.setComment(comment);
147

    
148
        CommentTemplate ct = new CommentTemplate();
149
		//TODO fix comment template
150
        c.setTemplate(null);
151

    
152
        Budget b = getBudget(budgetId);
153
        b.getComments().add(c);
154
        persist(b);
155
    }
156

    
157
	public List<Comment> getBudgetComments(String budgetId) {
158
		return getBudget(budgetId).getComments();
159
	}
160

    
161
	public void uploadBankReceipt(final String budgetId, final String contentType, InputStream inputStream) {
162
        Budget b;
163
        ByteArrayOutputStream baos = null;
164
        try {
165
            baos = new ByteArrayOutputStream();
166
            IOUtils.copy(inputStream, baos);
167
            IOUtils.closeQuietly(baos);
168
        } catch (IOException e) {
169
            e.printStackTrace();
170
        }
171

    
172
        b = getBudget(budgetId);
173
        b.setBankTransferReceipt(new BankTransferReceipt(contentType,baos.toByteArray()));
174
        saveBudget(b);
175
	}
176

    
177
	public void uploadInitialContract(final String budgetId, final String contentType, InputStream inputStream) {
178
        final String id = "portal::" + UUID.randomUUID().toString();
179
        ByteArrayOutputStream baos = null;
180
        try {
181
            baos = new ByteArrayOutputStream();
182

    
183
            IOUtils.copy(inputStream, baos);
184
            IOUtils.closeQuietly(baos);
185
        } catch (IOException e) {
186
            e.printStackTrace();
187
        }
188

    
189
		Budgetfile bf = new Budgetfile();
190
		bf.setId(id);
191
		bf.setContenttype(contentType);
192
		bf.setContent(baos.toByteArray());
193

    
194
        Budget b = getBudget(budgetId);
195
        b.setInitialcontractfile(bf);
196
        saveBudget(b);
197
	}
198

    
199
	public void uploadBeneficiaryContract(final String budgetId, final String contentType, InputStream inputStream) {
200
		try {
201
			final ByteArrayOutputStream baos = new ByteArrayOutputStream();
202
			final String id = "portal::" + UUID.randomUUID().toString();
203
			IOUtils.copy(inputStream, baos);
204
			IOUtils.closeQuietly(baos);
205

    
206
			BudgetContract bc = new BudgetContract();
207
			bc.setId(id);
208
			bc.setContent(baos.toByteArray());
209
			bc.setContentType(contentType);
210
			Budget b = getBudget(budgetId);
211
			b.setBeneficiarysignedcontract(bc);
212
			saveBudget(b);
213
		} catch (Exception e) {
214
			e.printStackTrace();
215
		}
216
	}
217

    
218
	public void uploadSignedContract(final String budgetId, final String contentType, InputStream inputStream) {
219
		try {
220
			final ByteArrayOutputStream baos = new ByteArrayOutputStream();
221
			final String id = "portal::" + UUID.randomUUID().toString();
222
			IOUtils.copy(inputStream, baos);
223
			IOUtils.closeQuietly(baos);
224

    
225
			BudgetContract bc = new BudgetContract();
226
			bc.setId(id);
227
			bc.setContent(baos.toByteArray());
228
			bc.setContentType(contentType);
229
			Budget b = getBudget(budgetId);
230
			b.setSignedcontract(bc);
231
			saveBudget(b);
232
		} catch (Exception e) {
233
			e.printStackTrace();
234
		}
235
	}
236

    
237
	public BigInteger getRequestId() {
238
		return (BigInteger) getSession().createSQLQuery("select nextval('budget_id_seq') as id").list().get(0);
239
	}
240

    
241
    public Organization getOrganizationForBudget(String budgetID) {
242
		return (Organization) createEntityCriteria().add(Restrictions.eq("budget.id", budgetID)).list().get(0);
243
    }
244
}
(2-2/12)