Project

General

Profile

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

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

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

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

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

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

    
33
    }
34

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

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

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

    
101
    /**
102
     *
103
     * @param dateFrom
104
     * @param dataTo
105
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
106
     * @param offset returns claims after 'offset' position (null for no offset)
107
     * @return
108
     * @throws Exception
109
     */
110
    public List<Claim> fetchClaimsByDate(String dateFrom, String dataTo, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
111
         return fetchClaimsByDate(dateFrom, dataTo, limit, offset, null, "claim.claim_date",true, new ArrayList<String>(),addCurationInfo);
112
    }
113
    /**
114
     *
115
     * @param dateFrom
116
     * @param dataTo
117
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
118
     * @param offset returns claims after 'offset' position (null for no offset)
119
     * @return
120
     * @throws Exception
121
     */
122
    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 {
123
        ArrayList<Object> params = new ArrayList<>();
124
        String query = queryGenerator.generateFetchClaimsByDate(dateFrom, dataTo, limit, offset,keyword, orderField, descending,types, params);
125
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
126
        return fetchClaimsByResultSet(rs, addCurationInfo);
127
    }
128
    /**
129
     *
130
     * @param dateFrom
131
     * @param dataTo
132
     * @param openaireId for project
133
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
134
     * @param offset returns claims after 'offset' position (null for no offset)
135
     * @return
136
     * @throws Exception
137
     */
138
    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 {
139
        ArrayList<Object> params = new ArrayList<>();
140
        String query = queryGenerator.generateFetchNumberOfClaimsByDateAndOpenaireId(dateFrom, dataTo, openaireId, limit, offset,keyword, orderField, descending,types, params);
141
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
142
        return countClaimsByResultSet(rs);
143
    }
144

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

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

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

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

    
291

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

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

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

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

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

    
422
    }
423
    private static Project buildProject(ResultSet rs, Integer index) throws SQLException {
424
        Project project = new Project();
425
        project.setOpenaireId(rs.getString(index));
426
        project.setName(rs.getString(index + 1));
427
        project.setAcronym(rs.getString(index + 2));
428
        project.setFunderId(rs.getString(index + 3));
429
        project.setFunderName(rs.getString(index + 4));
430
        return project;
431

    
432
    }
433
    private static Context buildContext(ResultSet rs, Integer index) throws SQLException {
434
        Context context = new Context();
435
        context.setOpenaireId(rs.getString(index));
436
        context.setTitle(rs.getString(index + 1));
437
        return context;
438

    
439
    }
440
    private static OpenaireEntity buildEntity(ResultSet rs, Integer index, String type) throws SQLException {
441
        OpenaireEntity openaireEntity = null;
442
        if(type == null){
443
            openaireEntity = null;
444
        }else if(type.equals(ClaimUtils.PUBLICATION)||type.equals(ClaimUtils.DATASET)||type.equals(ClaimUtils.SOFTWARE)||type.equals(ClaimUtils.OTHER)) {
445
            openaireEntity= buildResult(rs, index);
446
        }else if(type.equals(ClaimUtils.PROJECT)){
447
            openaireEntity = buildProject(rs,index);
448
        }else if(type.equals(ClaimUtils.CONTEXT)){
449
            openaireEntity = buildContext(rs,index);
450
        }
451
        return openaireEntity;
452
    }
453

    
454
    public SqlDAO getSqlDAO() {
455
        return sqlDAO;
456
    }
457

    
458
    public void setSqlDAO(SqlDAO sqlDAO) {
459
        this.sqlDAO = sqlDAO;
460
    }
461

    
462
    public QueryGenerator getQueryGenerator() {
463
        return queryGenerator;
464
    }
465

    
466
    public void setQueryGenerator(QueryGenerator queryGenerator) {
467
        this.queryGenerator = queryGenerator;
468
    }
469
}
(7-7/15)