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-claimsDemo.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
     *
129
     * @param dateFrom
130
     * @param dataTo
131
     * @param openaireId for project
132
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
133
     * @param offset returns claims after 'offset' position (null for no offset)
134
     * @return
135
     * @throws Exception
136
     */
137
    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 {
138
        ArrayList<Object> params = new ArrayList<>();
139
        String query = queryGenerator.generateFetchNumberOfClaimsByDateAndOpenaireId(dateFrom, dataTo, openaireId, limit, offset,keyword, orderField, descending,types, params);
140
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
141
        return countClaimsByResultSet(rs);
142
    }
143

    
144
    /**
145
     *
146
     * @param openaireId for project
147
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
148
     * @param offset returns claims after 'offset' position (null for no offset)
149
     * @return
150
     * @throws Exception
151
     */
152
    public List<Claim> fetchClaimsByProject(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
153

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

    
279
    /*
280
    public List<Claim> fetchClaimsByToken(String token, String email, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
281
        return fetchClaimsByToken(token, email, limit, offset, null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
282
    }
283

    
284
    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 {
285
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByProjectToken(token, email, limit, offset, keyword, orderField,descending,types));
286
        return fetchClaimsByResultSet(rs, addCurationInfo);
287
    }
288
    */
289

    
290

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

    
305
            if(addCurationInfo) {
306
                String curationDate =rs.getString(7);
307
                if(curationDate != null) {
308
                    date = format.parse(curationDate);
309
                    claim.setCurationDate(date);
310
                }
311
                claim.setCuratedBy(rs.getString(8));
312
            }
313
            claim.setApproved(rs.getBoolean(9));
314
            claim.setSource(buildEntity(rs,10,claim.getSourceType()));
315
            claim.setTarget(buildEntity(rs,22,claim.getTargetType()));
316

    
317
        }
318
        return claims;
319
    }
320
    public Integer countClaimsByResultSet(ResultSet rs) throws Exception {
321

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

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

    
422
        return result;
423

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

    
441
        return project;
442

    
443
    }
444
    private static Context buildContext(ResultSet rs, Integer index) throws SQLException {
445
        Context context = new Context();
446
        context.setOpenaireId(rs.getString(index));
447
        context.setTitle(rs.getString(index + 1));
448
        return context;
449

    
450
    }
451
    private static OpenaireEntity buildEntity(ResultSet rs, Integer index, String type) throws SQLException {
452
        OpenaireEntity openaireEntity = null;
453
        if(type == null){
454
            openaireEntity = null;
455
        }else if(type.equals(ClaimUtils.PUBLICATION)||type.equals(ClaimUtils.DATASET)||type.equals(ClaimUtils.SOFTWARE)||type.equals(ClaimUtils.OTHER)) {
456
            openaireEntity= buildResult(rs, index);
457
        }else if(type.equals(ClaimUtils.PROJECT)){
458
            openaireEntity = buildProject(rs,index);
459
        }else if(type.equals(ClaimUtils.CONTEXT)){
460
            openaireEntity = buildContext(rs,index);
461
        }
462
        return openaireEntity;
463
    }
464

    
465
    public SqlDAO getSqlDAO() {
466
        return sqlDAO;
467
    }
468

    
469
    public void setSqlDAO(SqlDAO sqlDAO) {
470
        this.sqlDAO = sqlDAO;
471
    }
472

    
473
    public QueryGenerator getQueryGenerator() {
474
        return queryGenerator;
475
    }
476

    
477
    public void setQueryGenerator(QueryGenerator queryGenerator) {
478
        this.queryGenerator = queryGenerator;
479
    }
480
}
(5-5/12)