Project

General

Profile

1 35215 antonis.le
package eu.dnetlib.goldoa.service;
2
3 35663 antonis.le
import eu.dnetlib.goldoa.domain.*;
4 35686 antonis.le
import eu.dnetlib.goldoa.service.utils.EmailUtils;
5 35478 antonis.le
import org.springframework.beans.factory.annotation.Autowired;
6 35215 antonis.le
import org.springframework.jdbc.core.JdbcTemplate;
7
import org.springframework.jdbc.core.RowMapper;
8
import org.springframework.transaction.annotation.Transactional;
9
10 35686 antonis.le
import javax.mail.MessagingException;
11 35215 antonis.le
import javax.sql.DataSource;
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.sql.Types;
15 35639 antonis.le
import java.text.SimpleDateFormat;
16 35667 antonis.le
import java.util.ArrayList;
17 35639 antonis.le
import java.util.Date;
18 35215 antonis.le
import java.util.List;
19
20
/**
21
 * Created by antleb on 3/10/15.
22
 */
23
@Transactional
24
public class RequestManagerImpl implements RequestManager {
25
26 35478 antonis.le
    @Autowired
27 35215 antonis.le
	private DataSource dataSource;
28
29 35670 antonis.le
    @Autowired
30
    private PersonManager personManager;
31
    @Autowired
32
    private OrganizationManager organizationManager;
33
    @Autowired
34
    private ProjectManager projectManager;
35
    @Autowired
36
    private PublicationManager publicationManager;
37
    @Autowired
38
    private InvoiceManager invoiceManager;
39
    @Autowired
40
    private EligibilityManager eligibilityManager;
41 35686 antonis.le
    @Autowired
42
    private EmailUtils emailUtils;
43 35670 antonis.le
44 35643 stefania.m
	private final String GET_FOR_PROJECT = "select id, \"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status from request where project=?";
45 35215 antonis.le
46 35687 stefania.m
    private final String GET_FOR_USER = "select id, \"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status from request where \"user\"=?";
47 35215 antonis.le
48 35639 antonis.le
	private final String GET_BY_ID = "select id, \"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status from request where id=?";
49 35215 antonis.le
50 35639 antonis.le
	private final String UPDATE_REQUEST = "update request set \"user\"=?, date=?, researcher=?, project=?, publication=?, journal=?, publisher=?, budget=?, invoice = ?, projectparticipation=?, fundingrequested=?, status=? where id = ?";
51 35215 antonis.le
52 35639 antonis.le
	private final String INSERT_REQUEST = "insert into request (\"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status, id) values (?, ?, ?, ?, ? ,? ,? ,? ,?, ?, ?, ?, ?)";
53
54 35766 antonis.le
    private final String APPROVE_REQUEST = "update request set status = (status & ~" + Request.REJECTED + ") | " + Request.APPROVED + " where id=?";
55 35693 antonis.le
56 35766 antonis.le
    private final String REJECT_REQUEST = "update request set status = (status & ~" + Request.APPROVED + ") | " + Request.REJECTED + " where id=?";
57 35759 antonis.le
58 35766 antonis.le
    private final String INVOICE_UPLOADED = "update request set status = status | " + Request.INVOICE_UPLOADED + " where id=?";
59 35759 antonis.le
60 35215 antonis.le
	@Override
61
	public Request saveRequest(final Request request) {
62
		String sql = UPDATE_REQUEST;
63
64
		if (request.getId() == null) {
65 35652 antonis.le
			request.setId(new SimpleDateFormat("yyyyMMdd-").format(new Date()) + this.getRequestId());
66 35215 antonis.le
67
			sql = INSERT_REQUEST;
68
		}
69
70 35686 antonis.le
        if (request.getInvoice() != null)
71
            request.addStatus(Request.INVOICE_UPLOADED);
72
73 35666 antonis.le
        new JdbcTemplate(dataSource).update(sql, new Object[] {request.getUser(), request.getDate(), request.getResearcher(), request.getProject(),
74
                request.getPublication(), request.getJournal(), request.getPublisher(), request.getBudget(), request.getInvoice(),
75
                request.getProjectParticipation(), request.getFundingRequested(), request.getStatus(), request.getId()},
76
                new int[]{Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.REAL, Types.REAL, Types.INTEGER, Types.VARCHAR});
77 35215 antonis.le
78
		return request;
79
	}
80
81
	@Override
82 35694 antonis.le
	public RequestInfo getById(String requestId) {
83
        try {
84
            return this.getRequestInfo(new JdbcTemplate(dataSource).queryForObject(GET_BY_ID, new String[]{requestId}, new int[]{
85
                    Types.VARCHAR}, requestRowMapper));
86
        } catch (PersonManagerException e) {
87
            e.printStackTrace();
88
        }
89
90
        return null;
91 35639 antonis.le
	}
92 35215 antonis.le
93 35639 antonis.le
    @Override
94 35749 antonis.le
    public List<RequestInfo> getForUser(String personId, Date from, Date to, RequestSort requestSortBy, RequestSortOrder order, RequestFilter requestFilter, String term) {
95
        List<Request> requests = new JdbcTemplate(dataSource).query(GET_FOR_USER, new String[]{personId}, new int[]{
96 35639 antonis.le
                Types.VARCHAR}, requestRowMapper);
97 35684 antonis.le
        List<RequestInfo> res = new ArrayList<RequestInfo>();
98
99
        for (Request request:requests) {
100
            try {
101
                RequestInfo req = getRequestInfo(request);
102
103
                res.add(req);
104
            } catch (Exception e) {
105
                e.printStackTrace();
106
            }
107
        }
108
109
        return res;
110 35639 antonis.le
    }
111 35215 antonis.le
112
	@Override
113
	public List<Request> getForProject(String projectId) {
114
		return new JdbcTemplate(dataSource).query(GET_FOR_PROJECT, new String[]{projectId}, new int[]{
115 35639 antonis.le
				Types.VARCHAR}, requestRowMapper);
116
	}
117 35215 antonis.le
118 35662 antonis.le
    @Override
119 35663 antonis.le
    public List<RequestInfo> getRequests(Date from, Date to, RequestSort requestSortBy, RequestSortOrder order, RequestFilter requestFilter, String term) {
120 35666 antonis.le
        String query = "select r.id, r.\"user\", r.date, r.researcher, r.project, r.publication, r.journal, r.publisher, r.budget, r.invoice, r.projectparticipation, r.fundingrequested, r.status from request r";
121
122 35667 antonis.le
//        if (requestFilter != null) {
123
//            switch (requestFilter) {
124
//                case USER:
125
//                    query += " join person p on p.email = r.user and (lower(p.firstname) like (lower(?) or lower(p.lastname) like (?) ";
126
//                    break;
127
//                case RESEARCHER:
128
//                    query += " join person p on p.email = r.researcher and (lower(p.firstname) like (lower(?) or lower(p.lastname) like (?) ";
129
//                    break;
130
//            }
131
//        }
132
133
        List<Request> requests = new JdbcTemplate(dataSource).query(query, new String[] {}, new int[]{}, requestRowMapper);
134
        List<RequestInfo> res = new ArrayList<RequestInfo>();
135
136
        for (Request request:requests) {
137
            try {
138 35684 antonis.le
                RequestInfo req = getRequestInfo(request);
139 35667 antonis.le
140
                res.add(req);
141
            } catch (Exception e) {
142
                e.printStackTrace();
143 35666 antonis.le
            }
144
        }
145
146 35667 antonis.le
        return res;
147 35662 antonis.le
    }
148
149 35684 antonis.le
    private RequestInfo getRequestInfo(Request request) throws PersonManagerException {
150
        RequestInfo req = new RequestInfo();
151
152
        // TODO add missing fields
153 35693 antonis.le
        req.setId(request.getId());
154 35749 antonis.le
        req.setUser(personManager.getById(request.getUser()));
155 35684 antonis.le
        req.setDate(request.getDate());
156 35749 antonis.le
        req.setResearcher(personManager.getById(request.getResearcher()));
157 35684 antonis.le
        req.setProject(projectManager.getById(request.getProject()));
158
        req.setPublication(publicationManager.getPublication(request.getPublication()));
159
        req.setProjectParticipation(request.getProjectParticipation());
160
        req.setFundingRequested(request.getFundingRequested());
161
        req.setEligibility(eligibilityManager.validate(request));
162 35694 antonis.le
        req.setStatusCode(request.getStatus());
163 35684 antonis.le
164
        req.setStatus(getStatus(request));
165
        return req;
166
    }
167
168
    @Override
169 35693 antonis.le
    public Request submitRequest(Request request) throws PersonManagerException {
170 35749 antonis.le
171
        // TODO offline
172 35686 antonis.le
        RequestInfo requestInfo = this.getRequestInfo(request);
173 35684 antonis.le
174 35686 antonis.le
        request.addStatus(Request.SUBMITTED);
175
176 35684 antonis.le
        saveRequest(request);
177
178 35693 antonis.le
        try {
179
            switch (requestInfo.getEligibility().getStatus()) {
180
                case OK:
181
                    emailUtils.sendRequesterOKEmail(requestInfo);
182 35686 antonis.le
183 35693 antonis.le
                    for (Person person : personManager.getModerators())
184
                        emailUtils.sendModeratorOKEmail(requestInfo, person);
185
                    break;
186
                case IFFY:
187
                    emailUtils.sendRequesterIFFYEmail(requestInfo);
188 35686 antonis.le
189 35693 antonis.le
                    for (Person person : personManager.getModerators())
190
                        emailUtils.sendModeratorIFFYEmail(requestInfo, person);
191
                    break;
192
                case NONO:
193
                    break;
194
            }
195
        } catch (MessagingException e) {
196
            e.printStackTrace();
197 35684 antonis.le
        }
198
199 35686 antonis.le
        return request;
200 35684 antonis.le
    }
201
202 35693 antonis.le
    @Override
203 35759 antonis.le
    public void approveRequest(String requestId) {
204
        new JdbcTemplate(dataSource).update(APPROVE_REQUEST, new String[] {requestId}, new int[] {Types.VARCHAR});
205 35766 antonis.le
206
        RequestInfo requestInfo = getById(requestId);
207
208
        try {
209
            emailUtils.sendRequesterApprovedEmail(requestInfo);
210
        } catch (MessagingException e) {
211
            e.printStackTrace();
212
        }
213 35693 antonis.le
    }
214
215
    @Override
216 35759 antonis.le
    public void rejectRequest(String requestId) {
217
        new JdbcTemplate(dataSource).update(REJECT_REQUEST, new String[] {requestId}, new int[] {Types.VARCHAR});
218 35766 antonis.le
219
        RequestInfo requestInfo = getById(requestId);
220
221
        try {
222
            emailUtils.sendRequesterRejectedEmail(requestInfo);
223
        } catch (MessagingException e) {
224
            e.printStackTrace();
225
        }
226 35693 antonis.le
    }
227
228 35759 antonis.le
    @Override
229
    public void invoiceUploaded(String requestId) {
230
        new JdbcTemplate(dataSource).update(INVOICE_UPLOADED, new String[] {requestId}, new int[] {Types.VARCHAR});
231
    }
232
233 35667 antonis.le
    private String getStatus(Request request) {
234 35772 antonis.le
        if (request.getStatus(Request.SUBMITTED)) {
235
            if (request.getStatus(Request.APPROVED))
236
                return  "Approved";
237
            else if (request.getStatus(Request.REJECTED))
238
                return "Rejected";
239
            else
240
                return "Submitted";
241
        }
242 35766 antonis.le
        else
243
            return "Incomplete";
244 35667 antonis.le
    }
245
246 35639 antonis.le
    private RowMapper<Request> requestRowMapper = new RowMapper<Request>() {
247
        @Override
248
        public Request mapRow(ResultSet rs, int rowNum) throws SQLException {
249
            return new Request(
250
                    rs.getString("id"), rs.getString("user"), rs.getTimestamp("date"), rs.getString("researcher"),
251
                    rs.getString("project"), rs.getString("publication"), rs.getString("journal"), rs.getString("publisher"),
252
                    rs.getString("budget"), rs.getString("invoice"), rs.getFloat("projectparticipation"), rs.getFloat("fundingrequested"), rs.getInt("status"));
253
        }
254
    };
255 35215 antonis.le
256 35639 antonis.le
    private int getRequestId() {
257 35652 antonis.le
        return new JdbcTemplate(dataSource).queryForObject("select nextval('request_id_seq') as id", new RowMapper<Integer>() {
258 35639 antonis.le
            @Override
259
            public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
260
                return rs.getInt("id");
261
            }
262
        });
263
    }
264 35226 stefania.m
265
    public DataSource getDataSource() {
266
        return dataSource;
267
    }
268
269
    public void setDataSource(DataSource dataSource) {
270
        this.dataSource = dataSource;
271
    }
272 35670 antonis.le
273
    public PersonManager getPersonManager() {
274
        return personManager;
275
    }
276
277
    public void setPersonManager(PersonManager personManager) {
278
        this.personManager = personManager;
279
    }
280
281
    public OrganizationManager getOrganizationManager() {
282
        return organizationManager;
283
    }
284
285
    public void setOrganizationManager(OrganizationManager organizationManager) {
286
        this.organizationManager = organizationManager;
287
    }
288
289
    public ProjectManager getProjectManager() {
290
        return projectManager;
291
    }
292
293
    public void setProjectManager(ProjectManager projectManager) {
294
        this.projectManager = projectManager;
295
    }
296
297
    public PublicationManager getPublicationManager() {
298
        return publicationManager;
299
    }
300
301
    public void setPublicationManager(PublicationManager publicationManager) {
302
        this.publicationManager = publicationManager;
303
    }
304
305
    public InvoiceManager getInvoiceManager() {
306
        return invoiceManager;
307
    }
308
309
    public void setInvoiceManager(InvoiceManager invoiceManager) {
310
        this.invoiceManager = invoiceManager;
311
    }
312
313
    public EligibilityManager getEligibilityManager() {
314
        return eligibilityManager;
315
    }
316
317
    public void setEligibilityManager(EligibilityManager eligibilityManager) {
318
        this.eligibilityManager = eligibilityManager;
319
    }
320 35686 antonis.le
321
    public EmailUtils getEmailUtils() {
322
        return emailUtils;
323
    }
324
325
    public void setEmailUtils(EmailUtils emailUtils) {
326
        this.emailUtils = emailUtils;
327
    }
328 35215 antonis.le
}