Project

General

Profile

1
package eu.dnetlib.data.claims.handler;
2

    
3
import eu.dnetlib.data.claims.utils.JsonldBuilder;
4
import eu.dnetlib.data.claims.entity.*;
5
import eu.dnetlib.data.claims.utils.ClaimUtils;
6
import eu.dnetlib.data.claims.utils.QueryGenerator;
7
import eu.dnetlib.data.claims.sql.SQLStoreException;
8
import eu.dnetlib.data.claims.sql.SqlDAO;
9
import org.apache.log4j.Logger;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.text.DateFormat;
14
import java.text.SimpleDateFormat;
15
import java.util.ArrayList;
16
import java.util.Date;
17
import java.util.List;
18

    
19
/**
20
 * Created by argirok on 9/3/2016.
21
 */
22
public class FetchClaimHandler {
23
    private Logger log = Logger.getLogger(this.getClass());
24

    
25
    SqlDAO sqlDAO = null;
26
    QueryGenerator queryGenerator = null;
27

    
28
    public FetchClaimHandler(){
29
//        ApplicationContext context = new ClassPathXmlApplicationContext("../claims/migration/springContext-claims.xml");
30
//        sqlDAO = context.getBean(SqlDAO.class);
31

    
32
    }
33

    
34
    public String claims2JSON(List<Claim> claims) throws Exception {
35
        return JsonldBuilder.toJsonld(claims);
36
    }
37
    public String claim2JSON(Claim claim) throws Exception {
38
        return JsonldBuilder.toJsonld(claim);
39
    }
40

    
41
    /**
42
     *
43
     * @param user - user e-mail stored in claims DB
44
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
45
     * @param offset returns claims after 'offset' position (null for no offset)
46
     * @return
47
     * @throws Exception
48
     */
49
    public List<Claim> fetchClaimsByUser(String user, Integer limit, Integer offset, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
50
        return fetchClaimsByUser(user,limit,offset,null,"claim.claim_date",true, types, addCurationInfo);
51
    }
52
    /**
53
     *
54
     * @param user - user e-mail stored in claims DB
55
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
56
     * @param offset returns claims after 'offset' position (null for no offset)
57
     * @param keyword filters in specific fields per type (result: {title,doi},project: {name.acronym. funder_name, funder_acronym}, context: {name})
58
     * @param orderField
59
     * @param descending
60
     * @return
61
     * @throws Exception
62
     */
63
    public List<Claim> fetchClaimsByUser(String user, Integer limit, Integer offset,String keyword,String orderField,boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
64
        log.info("Fetching claims for user"+user);
65
        ArrayList<Object> params = new ArrayList<>();
66
        String query = queryGenerator.generateFetchClaimsByUser(user, limit, offset,keyword,orderField,descending, types, params);
67
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
68
        return fetchClaimsByResultSet(rs, addCurationInfo);
69
    }
70
    /**
71
     *
72
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
73
     * @param offset returns claims after 'offset' position (null for no offset)
74
     * @return
75
     * @throws Exception
76
     */
77
    public List<Claim> fetchAllClaims( Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
78
        log.info("Fetching all claims");
79
         return fetchAllClaims(limit,offset,null,"claim.claim_date",true,new ArrayList<String>(), addCurationInfo);
80
    }
81

    
82
    /**
83
     *
84
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
85
     * @param offset returns claims after 'offset' position (null for no offset)
86
     * @param keyword  filters in specific fields per type (result: {title,doi},project: {name.acronym. funder_name, funder_acronym}, context: {name})
87
     * @param orderField
88
     * @param descending
89
     * @return
90
     * @throws Exception
91
     */
92
    public List<Claim> fetchAllClaims( Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
93
        log.info("Fetching all claims");
94
        ArrayList<Object> params = new ArrayList<>();
95
        String query = queryGenerator.generateFetchClaims(limit, offset,keyword,orderField,descending,types, params);
96
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
97
        return fetchClaimsByResultSet(rs, addCurationInfo);
98
    }
99

    
100
    /**
101
     *
102
     * @param dateFrom
103
     * @param dataTo
104
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
105
     * @param offset returns claims after 'offset' position (null for no offset)
106
     * @return
107
     * @throws Exception
108
     */
109
    public List<Claim> fetchClaimsByDate(String dateFrom, String dataTo, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
110
         return fetchClaimsByDate(dateFrom, dataTo, limit, offset, null, "claim.claim_date",true, new ArrayList<String>(),addCurationInfo);
111
    }
112
    /**
113
     *
114
     * @param dateFrom
115
     * @param dataTo
116
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
117
     * @param offset returns claims after 'offset' position (null for no offset)
118
     * @return
119
     * @throws Exception
120
     */
121
    public List<Claim> fetchClaimsByDate(String dateFrom, String dataTo, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
122
        ArrayList<Object> params = new ArrayList<>();
123
        String query = queryGenerator.generateFetchClaimsByDate(dateFrom, dataTo, limit, offset,keyword, orderField, descending,types, params);
124
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
125
        return fetchClaimsByResultSet(rs, addCurationInfo);
126
    }
127

    
128
    public List<Claim> fetchClaimsByDateForDashboards(String dateFrom, String dataTo, Integer limit, Integer offset, boolean addCurationInfo, ArrayList<String> dashboards) throws Exception, SQLStoreException {
129
        ArrayList<Object> params = new ArrayList<>();
130
        String query = queryGenerator.generateFetchClaimsByDateForDashboards(dateFrom, dataTo, limit, offset,null, "claim.claim_date", true,new ArrayList<String>(), params, dashboards);
131
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
132
        return fetchClaimsByResultSet(rs, addCurationInfo);
133
    }
134
    /**
135
     *
136
     * @param dateFrom
137
     * @param dataTo
138
     * @param openaireId for project
139
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
140
     * @param offset returns claims after 'offset' position (null for no offset)
141
     * @return
142
     * @throws Exception
143
     */
144
    public int fetchNumberOfClaimsByDateAndOpenaireId(String dateFrom, String dataTo, String openaireId, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
145
        ArrayList<Object> params = new ArrayList<>();
146
        String query = queryGenerator.generateFetchNumberOfClaimsByDateAndOpenaireId(dateFrom, dataTo, openaireId, limit, offset,keyword, orderField, descending,types, params);
147
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
148
        return countClaimsByResultSet(rs);
149
    }
150

    
151
    /**
152
     *
153
     * @param openaireId for project
154
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
155
     * @param offset returns claims after 'offset' position (null for no offset)
156
     * @return
157
     * @throws Exception
158
     */
159
    public List<Claim> fetchClaimsByProject(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
160

    
161
        return fetchClaimsByProject(openaireId,limit, offset, null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
162
    }
163
    /**
164
     *
165
     * @param openaireId for project
166
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
167
     * @param offset returns claims after 'offset' position (null for no offset)
168
     * @param orderField
169
     * @param descending
170
     * @return
171
     * @throws Exception
172
     */
173
    public List<Claim> fetchClaimsByProject(String openaireId, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
174
        log.info("Fetching claims by project ID:"+openaireId);
175
        ArrayList<Object> params = new ArrayList<>();
176
        String query = queryGenerator.generateFetchClaimsByProject(openaireId, limit, offset, keyword, orderField,descending,types, params);
177
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
178
        return fetchClaimsByResultSet(rs, addCurationInfo);
179
    }
180
    /**
181
     *
182
     * @param openaireId of Funder
183
     * @param limit
184
     * @param offset
185
     * @return
186
     * @throws Exception
187
     */
188
    public List<Claim> fetchClaimsByFunder(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
189
         return fetchClaimsByFunder(openaireId,limit,offset,null,"claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
190
    }
191
    /**
192
     *
193
     * @param openaireId of Funder
194
     * @param limit
195
     * @param offset
196
     * @param orderField
197
     * @param descending
198
     * @return
199
     * @throws Exception
200
     */
201
    public List<Claim> fetchClaimsByFunder(String openaireId, Integer limit, Integer offset,String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
202
        ArrayList<Object> params = new ArrayList<>();
203
        String query = queryGenerator.generateFetchClaimsByFunder(openaireId, limit, offset,keyword, orderField,descending,types, params);
204
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
205
        return fetchClaimsByResultSet(rs, addCurationInfo);
206
    }
207
    /**
208
     *
209
     * @param openaireId for result
210
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
211
     * @param offset returns claims after 'offset' position (null for no offset)
212
     * @return
213
     * @throws Exception
214
     */
215
    public List<Claim> fetchClaimsByResult(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
216
         return fetchClaimsByResult(openaireId,limit,offset,null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
217
    }
218
    /**
219
     *
220
     * @param openaireId for result
221
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
222
     * @param offset returns claims after 'offset' position (null for no offset)
223
     * @param orderField
224
     * @param descending
225
     * @return
226
     * @throws Exception
227
     */
228
    public List<Claim> fetchClaimsByResult(String openaireId, Integer limit, Integer offset,String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
229
        log.info("Fetching claims by result ID:"+openaireId);
230
        ArrayList<Object> params = new ArrayList<>();
231
        String query = queryGenerator.generateFetchClaimsByResult(openaireId, limit, offset,keyword, orderField, descending,types, params);
232
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
233
        return fetchClaimsByResultSet(rs, addCurationInfo);
234
    }
235
    /**
236
     *
237
     * @param openaireId
238
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
239
     * @param offset returns claims after 'offset' position (null for no offset)
240
     * @return
241
     * @throws Exception
242
     */
243
    public List<Claim> fetchClaimsByContext(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
244
         return fetchClaimsByContext(openaireId,limit,offset,null,"claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
245
    }
246
    /**
247
     *
248
     * @param openaireId
249
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
250
     * @param offset returns claims after 'offset' position (null for no offset)
251
     * @param orderField
252
     * @param descending
253
     * @return
254
     * @throws Exception
255
     */
256
    public List<Claim> fetchClaimsByContext(String openaireId, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
257
        log.info("Fetching claims by context ID:"+openaireId);
258
        ArrayList<Object> params = new ArrayList<>();
259
        String query = queryGenerator.generateFetchClaimsByContext(openaireId, limit, offset,keyword,orderField, descending,types, params);
260
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
261
        return fetchClaimsByResultSet(rs, addCurationInfo);
262
    }
263
    public Claim fetchClaimById(String claimId, boolean addCurationInfo) throws Exception, SQLStoreException {
264
        ArrayList<Object> params = new ArrayList<>();
265
        String query = queryGenerator.generateSelectClaimQuery(claimId, params);
266
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
267
        if(rs.next()) {
268
            String sourceType =rs.getString(2);
269
//            String sourceId =rs.getString(3);
270
            String targetType =rs.getString(4);
271
//            String targetId =rs.getString(5);
272
            ArrayList<Object> params2 = new ArrayList<>();
273
            String query2 = queryGenerator.generateFetchClaimsByClaimId(claimId,sourceType,targetType, params2);
274
           rs = sqlDAO.executePreparedQuery(query2, params2);
275
        }else{
276
            log.info("No claim with id : "+ claimId+"\n");
277
        }
278
        Claim claim = null;
279
        List<Claim> claims=fetchClaimsByResultSet(rs, addCurationInfo);
280
        if(claims.size()==1){
281
            claim=claims.get(0);
282
        }
283
        return claim;
284
    }
285

    
286
    /*
287
    public List<Claim> fetchClaimsByToken(String token, String email, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
288
        return fetchClaimsByToken(token, email, limit, offset, null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
289
    }
290

    
291
    public List<Claim> fetchClaimsByToken(String token, String email, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception {
292
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByProjectToken(token, email, limit, offset, keyword, orderField,descending,types));
293
        return fetchClaimsByResultSet(rs, addCurationInfo);
294
    }
295
    */
296

    
297

    
298
    public List<Claim> fetchClaimsByResultSet(ResultSet rs, boolean addCurationInfo) throws Exception {
299
        List<Claim> claims= new ArrayList<Claim>();
300
        while(rs.next()) {
301
            Claim claim= new Claim();
302
            claims.add(claim);
303
            claim.setId(rs.getString(1));
304
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
305
            Date date = format.parse(rs.getString(2));
306
            claim.setDate(date);
307
            claim.setUserMail(rs.getString(3));
308
            claim.setSourceType(rs.getString(4));
309
            claim.setTargetType(rs.getString(5));
310
            claim.setSemantics(rs.getString(6));
311

    
312
            if(addCurationInfo) {
313
                String curationDate =rs.getString(7);
314
                if(curationDate != null) {
315
                    date = format.parse(curationDate);
316
                    claim.setCurationDate(date);
317
                }
318
                claim.setCuratedBy(rs.getString(8));
319
            }
320
            claim.setApproved(rs.getBoolean(9));
321
            claim.setSource(buildEntity(rs,10,claim.getSourceType()));
322
            claim.setTarget(buildEntity(rs,22,claim.getTargetType()));
323

    
324
        }
325
        return claims;
326
    }
327
    public Integer countClaimsByResultSet(ResultSet rs) throws Exception {
328

    
329
        while(rs.next()) {
330
            return rs.getInt(1);
331
        }
332
        return null;
333
    }
334
    public List<Project> getProjectsByResultSet(ResultSet rs, boolean addCurationInfo) throws Exception {
335
        List<Project> projects= new ArrayList<Project>();
336
        while(rs.next()) {
337
            Project project=(Project)(buildEntity(rs, 1, ClaimUtils.PROJECT));
338
             projects.add(project);
339
        }
340
        return projects;
341
    }
342
    public List<Context> getContextsByResultSet(ResultSet rs, boolean addCurationInfo) throws Exception {
343
        List<Context> contexts= new ArrayList<Context>();
344
        while(rs.next()) {
345
            Context context=(Context)(buildEntity(rs, 1, ClaimUtils.CONTEXT));
346
            contexts.add(context);
347
        }
348
        return contexts;
349
    }
350
    public Integer countAllClaims(String keyword, List<String> types) throws Exception, SQLStoreException {
351
        ArrayList<Object> params = new ArrayList<>();
352
        String query = queryGenerator.generateCountAllClaims(keyword,types, params);
353
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
354
        return countClaimsByResultSet(rs);
355
    }
356
    public Integer countClaimsByUser(String user,String keyword, List<String> types) throws Exception, SQLStoreException {
357
        ArrayList<Object> params = new ArrayList<>();
358
        String query = queryGenerator.generateCountByUser(user,keyword,types, params);
359
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
360
        return countClaimsByResultSet(rs);
361
    }
362
    public Integer countClaimsByProject(String projectId,String keyword, List<String> types) throws Exception, SQLStoreException {
363
        ArrayList<Object> params = new ArrayList<>();
364
        String query = queryGenerator.generateCountByProject(projectId,keyword,types, params);
365
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
366
        return countClaimsByResultSet(rs);
367
    }
368
    /*
369
    public Integer countClaimsByProjectToken(String projectToken,String email,String keyword, List<String> types)throws Exception {
370
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountByProjectToken(projectToken,email,keyword,types));
371
        return countClaimsByResultSet(rs);
372
    }
373
    */
374
    public Integer countClaimsByFunder(String funderId,String keyword, List<String> types) throws Exception, SQLStoreException {
375
        ArrayList<Object> params = new ArrayList<>();
376
        String query = queryGenerator.generateCountByFunder(funderId,keyword,types, params);
377
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
378
        return countClaimsByResultSet(rs);
379
    }
380
    public Integer countClaimsByResult(String resultId,String keyword, List<String> types) throws Exception, SQLStoreException {
381
        ArrayList<Object> params = new ArrayList<>();
382
        String query = queryGenerator.generateCountByResult(resultId,keyword,types, params);
383
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
384
        return countClaimsByResultSet(rs);
385
    }
386
    public Integer countClaimsByContext(String contextId,String keyword, List<String> types) throws Exception, SQLStoreException {
387
        ArrayList<Object> params = new ArrayList<>();
388
        String query = queryGenerator.generateCountByContext(contextId,keyword,types, params);
389
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
390
        return countClaimsByResultSet(rs);
391
    }
392
    public Integer countClaimsByDate(String dateFrom,String dateTo,String keyword, List<String> types) throws Exception, SQLStoreException {
393
        ArrayList<Object> params = new ArrayList<>();
394
        String query = queryGenerator.generateCountByDate(dateFrom,dateTo,keyword,types, params);
395
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
396
        return countClaimsByResultSet(rs);
397
    }
398

    
399
    public List<Project> getAllclaimedProjects(boolean addCurationInfo) throws Exception, SQLStoreException {
400
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchAllProjectsQuery());
401
        return getProjectsByResultSet(rs,addCurationInfo);
402
    }
403
    public List<Context> getAllclaimedContexts( boolean addCurationInfo) throws Exception, SQLStoreException {
404
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchAllContextsQuery());
405
        return getContextsByResultSet(rs,addCurationInfo);
406
    }
407
    /**
408
     *
409
     * @param rs
410
     * @param index
411
     * @return
412
     * @throws SQLException
413
     */
414
    private static Result buildResult(ResultSet rs, Integer index) throws SQLException {
415
        Result result = new Result();
416
        result.setOpenaireId(rs.getString(index));
417
        result.setTitle(rs.getString(index + 1));
418
        result.setResultType(rs.getString(index + 2));
419
        result.setDoi(rs.getString(index + 3));
420
        result.setOrcidworkid(rs.getString(index + 4));
421
        result.setAccessRights(rs.getString(index + 5));
422
        result.setEmbargoEndDate(rs.getString(index + 6));
423
        result.setBestLicense(rs.getString(index + 7));
424
        result.setExternalUrl(rs.getString(index + 8));
425
        result.setCollectedFrom(rs.getString(index + 9));
426
        result.setRecordPath(rs.getString(index + 10));
427
        result.setRecordFormat(rs.getString(index + 11));
428

    
429
        return result;
430

    
431
    }
432
    private static Project buildProject(ResultSet rs, Integer index) throws SQLException {
433
        Project project = new Project();
434
        project.setOpenaireId(rs.getString(index));
435
        project.setName(rs.getString(index + 1));
436
        project.setAcronym(rs.getString(index + 2));
437
        project.setFunderId(rs.getString(index + 3));
438
        project.setFunderName(rs.getString(index + 4));
439
        project.setFunderShortName(rs.getString(index + 5));
440
        String contactP = rs.getString(index + 7);
441
        project.setContactEmails(new ArrayList<String>());
442
        if(contactP != null) {
443
            for (String s : contactP.split(",")) {
444
                project.getContactEmails().add(s);
445
            }
446
        }
447

    
448
        return project;
449

    
450
    }
451
    private static Context buildContext(ResultSet rs, Integer index) throws SQLException {
452
        Context context = new Context();
453
        context.setOpenaireId(rs.getString(index));
454
        context.setTitle(rs.getString(index + 1));
455
        return context;
456

    
457
    }
458
    private static OpenaireEntity buildEntity(ResultSet rs, Integer index, String type) throws SQLException {
459
        OpenaireEntity openaireEntity = null;
460
        if(type == null){
461
            openaireEntity = null;
462
        }else if(type.equals(ClaimUtils.PUBLICATION)||type.equals(ClaimUtils.DATASET)||type.equals(ClaimUtils.SOFTWARE)||type.equals(ClaimUtils.OTHER)) {
463
            openaireEntity= buildResult(rs, index);
464
        }else if(type.equals(ClaimUtils.PROJECT)){
465
            openaireEntity = buildProject(rs,index);
466
        }else if(type.equals(ClaimUtils.CONTEXT)){
467
            openaireEntity = buildContext(rs,index);
468
        }
469
        return openaireEntity;
470
    }
471

    
472
    public SqlDAO getSqlDAO() {
473
        return sqlDAO;
474
    }
475

    
476
    public void setSqlDAO(SqlDAO sqlDAO) {
477
        this.sqlDAO = sqlDAO;
478
    }
479

    
480
    public QueryGenerator getQueryGenerator() {
481
        return queryGenerator;
482
    }
483

    
484
    public void setQueryGenerator(QueryGenerator queryGenerator) {
485
        this.queryGenerator = queryGenerator;
486
    }
487
}
(5-5/12)