1 |
1 |
package eu.dnetlib.goldoa.service;
|
2 |
2 |
|
3 |
3 |
import eu.dnetlib.goldoa.domain.*;
|
|
4 |
import eu.dnetlib.goldoa.service.dao.RequestDAO;
|
4 |
5 |
import eu.dnetlib.goldoa.service.utils.EmailUtils;
|
5 |
6 |
import org.apache.commons.codec.digest.DigestUtils;
|
6 |
7 |
import org.springframework.beans.factory.annotation.Autowired;
|
... | ... | |
26 |
27 |
public class RequestManagerImpl implements RequestManager {
|
27 |
28 |
|
28 |
29 |
@Autowired
|
29 |
|
private DataSource dataSource;
|
30 |
|
|
|
30 |
private RequestDAO requestDAO;
|
31 |
31 |
@Autowired
|
32 |
32 |
private PersonManager personManager;
|
33 |
33 |
@Autowired
|
... | ... | |
45 |
45 |
@Autowired
|
46 |
46 |
private ExecutorService executorService;
|
47 |
47 |
|
48 |
|
private final String GET_FOR_PROJECT = "select id, \"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status from request where project=?";
|
49 |
|
|
50 |
|
private final String GET_FOR_USER = "select id, \"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status from request where \"user\"=?";
|
51 |
|
|
52 |
|
private final String GET_BY_ID = "select id, \"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status from request where id=?";
|
53 |
|
|
54 |
|
private final String UPDATE_REQUEST = "update request set \"user\"=?, date=?, researcher=?, project=?, publication=?, journal=?, publisher=?, budget=?, invoice = ?, projectparticipation=?, fundingrequested=?, status=? where id = ?";
|
55 |
|
|
56 |
|
private final String INSERT_REQUEST = "insert into request (\"user\", date, researcher, project, publication, journal, publisher, budget, invoice, projectparticipation, fundingrequested, status, id) values (?, ?, ?, ?, ? ,? ,? ,? ,?, ?, ?, ?, ?)";
|
57 |
|
|
58 |
|
private final String APPROVE_REQUEST = "update request set status = (status & ~" + Request.RequestStatus.REJECTED.getCode() + ") | " + Request.RequestStatus.APPROVED.getCode() + " where id=?";
|
59 |
|
|
60 |
|
private final String REJECT_REQUEST = "update request set status = (status & ~" + Request.RequestStatus.APPROVED.getCode() + ") | " + Request.RequestStatus.REJECTED.getCode() + " where id=?";
|
61 |
|
|
62 |
|
private final String INVOICE_UPLOADED = "update request set status = status | " + Request.RequestStatus.INVOICE_UPLOADED.getCode() + " where id=?";
|
63 |
|
|
64 |
48 |
@Override
|
65 |
49 |
public Request saveRequest(final Request request) {
|
66 |
|
String sql = UPDATE_REQUEST;
|
67 |
50 |
|
68 |
51 |
if (request.getId() == null) {
|
69 |
|
request.setId(new SimpleDateFormat("yyyyMMdd-").format(new Date()) + this.getRequestId());
|
70 |
|
|
71 |
|
sql = INSERT_REQUEST;
|
|
52 |
request.setId(new SimpleDateFormat("yyyyMMdd-").format(new Date()) + requestDAO.getRequestId());
|
72 |
53 |
}
|
73 |
54 |
|
74 |
55 |
if (request.getInvoice() != null)
|
75 |
|
request.addStatus(Request.RequestStatus.REJECTED);
|
|
56 |
request.addStatus(Request.RequestStatus.INVOICE_UPLOADED);
|
76 |
57 |
|
77 |
|
new JdbcTemplate(dataSource).update(sql, new Object[] {request.getUser(), request.getDate(), request.getResearcher(), request.getProject(),
|
78 |
|
request.getPublication(), request.getJournal(), request.getPublisher(), request.getBudget(), request.getInvoice(),
|
79 |
|
request.getProjectParticipation(), request.getFundingRequested(), request.getStatus(), request.getId()},
|
80 |
|
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});
|
|
58 |
requestDAO.saveRequest(request);
|
81 |
59 |
|
82 |
60 |
return request;
|
83 |
61 |
}
|
... | ... | |
85 |
63 |
@Override
|
86 |
64 |
public RequestInfo getById(String requestId) {
|
87 |
65 |
try {
|
88 |
|
return this.getRequestInfo(new JdbcTemplate(dataSource).queryForObject(GET_BY_ID, new String[]{requestId}, new int[]{
|
89 |
|
Types.VARCHAR}, requestRowMapper));
|
|
66 |
return this.getRequestInfo(requestDAO.getRequest(requestId));
|
90 |
67 |
} catch (PersonManagerException e) {
|
91 |
68 |
e.printStackTrace();
|
92 |
69 |
}
|
... | ... | |
96 |
73 |
|
97 |
74 |
@Override
|
98 |
75 |
public List<RequestInfo> getForUser(String personId, Date from, Date to, RequestSort requestSortBy, RequestSortOrder order, RequestFilter requestFilter, String term) {
|
99 |
|
List<Request> requests = new JdbcTemplate(dataSource).query(GET_FOR_USER, new String[]{personId}, new int[]{
|
100 |
|
Types.VARCHAR}, requestRowMapper);
|
|
76 |
List<Request> requests = requestDAO.getForUser(personId, from, to, requestSortBy, order, requestFilter, "", 0, 0);
|
101 |
77 |
List<RequestInfo> res = new ArrayList<RequestInfo>();
|
102 |
78 |
|
103 |
79 |
for (Request request:requests) {
|
... | ... | |
115 |
91 |
|
116 |
92 |
@Override
|
117 |
93 |
public List<Request> getForProject(String projectId) {
|
118 |
|
return new JdbcTemplate(dataSource).query(GET_FOR_PROJECT, new String[]{projectId}, new int[]{
|
119 |
|
Types.VARCHAR}, requestRowMapper);
|
|
94 |
return requestDAO.getForProject(projectId);
|
120 |
95 |
}
|
121 |
96 |
|
122 |
97 |
@Override
|
123 |
98 |
public List<RequestInfo> getRequests(Date from, Date to, RequestSort requestSortBy, RequestSortOrder order, RequestFilter requestFilter, String term) {
|
124 |
|
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";
|
125 |
|
|
126 |
|
// if (requestFilter != null) {
|
127 |
|
// switch (requestFilter) {
|
128 |
|
// case USER:
|
129 |
|
// query += " join person p on p.email = r.user and (lower(p.firstname) like (lower(?) or lower(p.lastname) like (?) ";
|
130 |
|
// break;
|
131 |
|
// case RESEARCHER:
|
132 |
|
// query += " join person p on p.email = r.researcher and (lower(p.firstname) like (lower(?) or lower(p.lastname) like (?) ";
|
133 |
|
// break;
|
134 |
|
// }
|
135 |
|
// }
|
136 |
|
|
137 |
|
List<Request> requests = new JdbcTemplate(dataSource).query(query, new String[] {}, new int[]{}, requestRowMapper);
|
|
99 |
List<Request> requests = requestDAO.getRequests(from, to, requestSortBy, order, requestFilter, term, 0, 0);
|
138 |
100 |
List<RequestInfo> res = new ArrayList<RequestInfo>();
|
139 |
101 |
|
140 |
102 |
for (Request request:requests) {
|
... | ... | |
150 |
112 |
return res;
|
151 |
113 |
}
|
152 |
114 |
|
153 |
|
private RequestInfo getRequestInfo(Request request) throws PersonManagerException {
|
154 |
|
RequestInfo req = new RequestInfo();
|
|
115 |
@Override
|
|
116 |
public Request submitRequest(Request request) throws PersonManagerException {
|
155 |
117 |
|
156 |
|
// TODO add missing fields
|
157 |
|
req.setId(request.getId());
|
158 |
|
req.setUser(personManager.getById(request.getUser()));
|
159 |
|
req.setDate(request.getDate());
|
160 |
|
req.setResearcher(personManager.getById(request.getResearcher()));
|
161 |
|
req.setProject(projectManager.getById(request.getProject()));
|
162 |
|
req.setPublication(publicationManager.getPublication(request.getPublication()));
|
163 |
|
req.setProjectParticipation(request.getProjectParticipation());
|
164 |
|
req.setFundingRequested(request.getFundingRequested());
|
165 |
|
req.setEligibility(eligibilityManager.validate(request));
|
166 |
|
req.setStatus(getStatus(request));
|
|
118 |
request.addStatus(Request.RequestStatus.SUBMITTED);
|
167 |
119 |
|
168 |
|
return req;
|
169 |
|
}
|
|
120 |
saveRequest(request);
|
170 |
121 |
|
171 |
|
private RequestInfo getShortRequestInfo(Request request) throws PersonManagerException {
|
172 |
|
RequestInfo req = new RequestInfo();
|
173 |
122 |
|
174 |
|
// TODO add missing fields
|
175 |
|
req.setId(request.getId());
|
176 |
|
req.setDate(request.getDate());
|
177 |
|
req.setPublication(publicationManager.getPublication(request.getPublication()));
|
178 |
|
req.setProjectParticipation(request.getProjectParticipation());
|
179 |
|
req.setFundingRequested(request.getFundingRequested());
|
180 |
|
|
181 |
|
req.setStatus(getStatus(request));
|
182 |
|
|
183 |
|
return req;
|
184 |
|
}
|
185 |
|
|
186 |
|
@Override
|
187 |
|
public Request submitRequest(Request request) throws PersonManagerException {
|
188 |
|
|
189 |
123 |
// TODO offline
|
190 |
124 |
RequestInfo requestInfo = this.getRequestInfo(request);
|
191 |
125 |
|
192 |
|
request.addStatus(Request.RequestStatus.SUBMITTED);
|
193 |
|
|
194 |
|
saveRequest(request);
|
195 |
|
|
196 |
126 |
try {
|
197 |
127 |
switch (requestInfo.getEligibility().getStatus()) {
|
198 |
128 |
case OK:
|
... | ... | |
219 |
149 |
|
220 |
150 |
@Override
|
221 |
151 |
public void approveRequest(String requestId, String comment) {
|
222 |
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
|
152 |
requestDAO.approveRequest(requestId, comment);
|
223 |
153 |
|
224 |
|
jdbcTemplate.update(APPROVE_REQUEST, new String[]{requestId}, new int[]{Types.VARCHAR});
|
225 |
|
|
226 |
|
if (comment != null) {
|
227 |
|
if (jdbcTemplate.update("update comment set comment=?, date=? where id = (select comment from request_comment where request=? )", new Object[] {comment, new Date(), requestId}, new int[] {Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR}) == 0) {
|
228 |
|
String commentId = "portal::" + DigestUtils.md5Hex(comment + new Date());
|
229 |
|
|
230 |
|
jdbcTemplate.update("with comment as ( insert into comment (id, comment, date) values (?, ?, ?) returning id) insert into request_comment (request, comment) select ?, comment.id",
|
231 |
|
new Object[] {commentId, comment, new Date(), requestId}, new int[] {Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR});
|
232 |
|
}
|
233 |
|
}
|
234 |
|
|
235 |
154 |
RequestInfo requestInfo = getById(requestId);
|
236 |
155 |
|
237 |
156 |
try {
|
... | ... | |
243 |
162 |
|
244 |
163 |
@Override
|
245 |
164 |
public void rejectRequest(String requestId, String comment) {
|
246 |
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
|
165 |
requestDAO.rejectRequest(requestId, comment);
|
247 |
166 |
|
248 |
|
jdbcTemplate.update(REJECT_REQUEST, new String[] {requestId}, new int[] {Types.VARCHAR});
|
249 |
|
|
250 |
167 |
RequestInfo requestInfo = getById(requestId);
|
251 |
168 |
|
252 |
|
if (comment != null) {
|
253 |
|
if (jdbcTemplate.update("update comment set comment=?, date=? where id = (select comment from request_comment where request=? )", new Object[] {comment, new Date(), requestId}, new int[] {Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR}) == 0) {
|
254 |
|
String commentId = "portal::" + DigestUtils.md5Hex(comment + new Date());
|
255 |
|
|
256 |
|
jdbcTemplate.update("with comment as ( insert into comment (id, comment, date) values (?, ?, ?) returning id) insert into request_comment (request, comment) select ?, comment.id",
|
257 |
|
new Object[] {commentId, comment, new Date(), requestId}, new int[] {Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR});
|
258 |
|
}
|
259 |
|
}
|
260 |
|
|
261 |
|
|
262 |
169 |
try {
|
263 |
170 |
emailUtils.sendRequesterRejectedEmail(requestInfo);
|
264 |
171 |
} catch (MessagingException e) {
|
... | ... | |
268 |
175 |
|
269 |
176 |
@Override
|
270 |
177 |
public void invoiceUploaded(String requestId) {
|
271 |
|
new JdbcTemplate(dataSource).update(INVOICE_UPLOADED, new String[] {requestId}, new int[] {Types.VARCHAR});
|
|
178 |
requestDAO.invoiceUploaded(requestId);
|
272 |
179 |
}
|
273 |
180 |
|
274 |
181 |
private Request.RequestStatus getStatus(Request request) {
|
... | ... | |
284 |
191 |
return Request.RequestStatus.INCOMPLETE;
|
285 |
192 |
}
|
286 |
193 |
|
287 |
|
private RowMapper<Request> requestRowMapper = new RowMapper<Request>() {
|
288 |
|
@Override
|
289 |
|
public Request mapRow(ResultSet rs, int rowNum) throws SQLException {
|
290 |
|
return new Request(
|
291 |
|
rs.getString("id"), rs.getString("user"), rs.getTimestamp("date"), rs.getString("researcher"),
|
292 |
|
rs.getString("project"), rs.getString("publication"), rs.getString("journal"), rs.getString("publisher"),
|
293 |
|
rs.getString("budget"), rs.getString("invoice"), rs.getFloat("projectparticipation"), rs.getFloat("fundingrequested"), rs.getInt("status"));
|
294 |
|
}
|
295 |
|
};
|
296 |
194 |
|
297 |
|
private int getRequestId() {
|
298 |
|
return new JdbcTemplate(dataSource).queryForObject("select nextval('request_id_seq') as id", new RowMapper<Integer>() {
|
299 |
|
@Override
|
300 |
|
public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
|
301 |
|
return rs.getInt("id");
|
302 |
|
}
|
303 |
|
});
|
304 |
|
}
|
305 |
195 |
|
306 |
|
public DataSource getDataSource() {
|
307 |
|
return dataSource;
|
308 |
|
}
|
|
196 |
private RequestInfo getRequestInfo(Request request) throws PersonManagerException {
|
|
197 |
RequestInfo req = new RequestInfo();
|
309 |
198 |
|
310 |
|
public void setDataSource(DataSource dataSource) {
|
311 |
|
this.dataSource = dataSource;
|
312 |
|
}
|
|
199 |
// TODO add missing fields
|
|
200 |
req.setId(request.getId());
|
|
201 |
req.setUser(personManager.getById(request.getUser()));
|
|
202 |
req.setDate(request.getDate());
|
|
203 |
req.setResearcher(personManager.getById(request.getResearcher()));
|
|
204 |
req.setProject(projectManager.getById(request.getProject()));
|
|
205 |
req.setPublication(publicationManager.getPublication(request.getPublication()));
|
|
206 |
req.setProjectParticipation(request.getProjectParticipation());
|
|
207 |
req.setFundingRequested(request.getFundingRequested());
|
|
208 |
req.setEligibility(eligibilityManager.validate(request));
|
|
209 |
req.setStatus(getStatus(request));
|
313 |
210 |
|
314 |
|
public PersonManager getPersonManager() {
|
315 |
|
return personManager;
|
|
211 |
return req;
|
316 |
212 |
}
|
317 |
213 |
|
318 |
|
public void setPersonManager(PersonManager personManager) {
|
319 |
|
this.personManager = personManager;
|
320 |
|
}
|
|
214 |
private RequestInfo getShortRequestInfo(Request request) throws PersonManagerException {
|
|
215 |
RequestInfo req = new RequestInfo();
|
321 |
216 |
|
322 |
|
public OrganizationManager getOrganizationManager() {
|
323 |
|
return organizationManager;
|
324 |
|
}
|
|
217 |
// TODO add missing fields
|
|
218 |
req.setId(request.getId());
|
|
219 |
req.setDate(request.getDate());
|
|
220 |
req.setPublication(publicationManager.getPublication(request.getPublication()));
|
|
221 |
req.setProjectParticipation(request.getProjectParticipation());
|
|
222 |
req.setFundingRequested(request.getFundingRequested());
|
325 |
223 |
|
326 |
|
public void setOrganizationManager(OrganizationManager organizationManager) {
|
327 |
|
this.organizationManager = organizationManager;
|
328 |
|
}
|
|
224 |
req.setStatus(getStatus(request));
|
329 |
225 |
|
330 |
|
public ProjectManager getProjectManager() {
|
331 |
|
return projectManager;
|
|
226 |
return req;
|
332 |
227 |
}
|
333 |
|
|
334 |
|
public void setProjectManager(ProjectManager projectManager) {
|
335 |
|
this.projectManager = projectManager;
|
336 |
|
}
|
337 |
|
|
338 |
|
public PublicationManager getPublicationManager() {
|
339 |
|
return publicationManager;
|
340 |
|
}
|
341 |
|
|
342 |
|
public void setPublicationManager(PublicationManager publicationManager) {
|
343 |
|
this.publicationManager = publicationManager;
|
344 |
|
}
|
345 |
|
|
346 |
|
public InvoiceManager getInvoiceManager() {
|
347 |
|
return invoiceManager;
|
348 |
|
}
|
349 |
|
|
350 |
|
public void setInvoiceManager(InvoiceManager invoiceManager) {
|
351 |
|
this.invoiceManager = invoiceManager;
|
352 |
|
}
|
353 |
|
|
354 |
|
public EligibilityManager getEligibilityManager() {
|
355 |
|
return eligibilityManager;
|
356 |
|
}
|
357 |
|
|
358 |
|
public void setEligibilityManager(EligibilityManager eligibilityManager) {
|
359 |
|
this.eligibilityManager = eligibilityManager;
|
360 |
|
}
|
361 |
|
|
362 |
|
public EmailUtils getEmailUtils() {
|
363 |
|
return emailUtils;
|
364 |
|
}
|
365 |
|
|
366 |
|
public void setEmailUtils(EmailUtils emailUtils) {
|
367 |
|
this.emailUtils = emailUtils;
|
368 |
|
}
|
369 |
228 |
}
|
refactored request manager to add a DAO