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
     *
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 List<Claim> fetchClaimsByProject(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception, SQLStoreException {
138

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

    
264
    /*
265
    public List<Claim> fetchClaimsByToken(String token, String email, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
266
        return fetchClaimsByToken(token, email, limit, offset, null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
267
    }
268

    
269
    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 {
270
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByProjectToken(token, email, limit, offset, keyword, orderField,descending,types));
271
        return fetchClaimsByResultSet(rs, addCurationInfo);
272
    }
273
    */
274

    
275

    
276
    public List<Claim> fetchClaimsByResultSet(ResultSet rs, boolean addCurationInfo) throws Exception {
277
        List<Claim> claims= new ArrayList<Claim>();
278
        while(rs.next()) {
279
            Claim claim= new Claim();
280
            claims.add(claim);
281
            claim.setId(rs.getString(1));
282
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
283
            Date date = format.parse(rs.getString(2));
284
            claim.setDate(date);
285
            claim.setUserMail(rs.getString(3));
286
            claim.setSourceType(rs.getString(4));
287
            claim.setTargetType(rs.getString(5));
288
            claim.setSemantics(rs.getString(6));
289

    
290
            if(addCurationInfo) {
291
                String curationDate =rs.getString(7);
292
                if(curationDate != null) {
293
                    date = format.parse(curationDate);
294
                    claim.setCurationDate(date);
295
                }
296
                claim.setCuratedBy(rs.getString(8));
297
            }
298
            claim.setApproved(rs.getBoolean(9));
299
            claim.setSource(buildEntity(rs,10,claim.getSourceType()));
300
            claim.setTarget(buildEntity(rs,20,claim.getTargetType()));
301

    
302
        }
303
        return claims;
304
    }
305
    public Integer countClaimsByResultSet(ResultSet rs) throws Exception {
306

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

    
377
    public List<Project> getAllclaimedProjects(boolean addCurationInfo) throws Exception, SQLStoreException {
378
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchAllProjectsQuery());
379
        return getProjectsByResultSet(rs,addCurationInfo);
380
    }
381
    public List<Context> getAllclaimedContexts( boolean addCurationInfo) throws Exception, SQLStoreException {
382
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchAllContextsQuery());
383
        return getContextsByResultSet(rs,addCurationInfo);
384
    }
385
    /**
386
     *
387
     * @param rs
388
     * @param index
389
     * @return
390
     * @throws SQLException
391
     */
392
    private static Result buildResult(ResultSet rs, Integer index) throws SQLException {
393
        Result result = new Result();
394
        result.setOpenaireId(rs.getString(index));
395
        result.setTitle(rs.getString(index + 1));
396
        result.setResultType(rs.getString(index + 2));
397
        result.setDoi(rs.getString(index + 3));
398
        result.setOrcidworkid(rs.getString(index + 4));
399
        result.setAccessRights(rs.getString(index + 5));
400
        result.setEmbargoEndDate(rs.getString(index + 6));
401
        result.setBestLicense(rs.getString(index + 7));
402
        result.setExternalUrl(rs.getString(index + 8));
403
        result.setCollectedFrom(rs.getString(index + 9));
404
        return result;
405

    
406
    }
407
    private static Project buildProject(ResultSet rs, Integer index) throws SQLException {
408
        Project project = new Project();
409
        project.setOpenaireId(rs.getString(index));
410
        project.setName(rs.getString(index + 1));
411
        project.setAcronym(rs.getString(index + 2));
412
        project.setFunderId(rs.getString(index + 3));
413
        project.setFunderName(rs.getString(index + 4));
414
        return project;
415

    
416
    }
417
    private static Context buildContext(ResultSet rs, Integer index) throws SQLException {
418
        Context context = new Context();
419
        context.setOpenaireId(rs.getString(index));
420
        context.setTitle(rs.getString(index + 1));
421
        return context;
422

    
423
    }
424
    private static OpenaireEntity buildEntity(ResultSet rs, Integer index, String type) throws SQLException {
425
        OpenaireEntity openaireEntity = null;
426
        if(type == null){
427
            openaireEntity = null;
428
        }else if(type.equals(ClaimUtils.PUBLICATION)||type.equals(ClaimUtils.DATASET)) {
429
            openaireEntity= buildResult(rs, index);
430
        }else if(type.equals(ClaimUtils.PROJECT)){
431
            openaireEntity = buildProject(rs,index);
432
        }else if(type.equals(ClaimUtils.CONTEXT)){
433
            openaireEntity = buildContext(rs,index);
434
        }
435
        return openaireEntity;
436
    }
437

    
438
    public SqlDAO getSqlDAO() {
439
        return sqlDAO;
440
    }
441

    
442
    public void setSqlDAO(SqlDAO sqlDAO) {
443
        this.sqlDAO = sqlDAO;
444
    }
445

    
446
    public QueryGenerator getQueryGenerator() {
447
        return queryGenerator;
448
    }
449

    
450
    public void setQueryGenerator(QueryGenerator queryGenerator) {
451
        this.queryGenerator = queryGenerator;
452
    }
453
}
(7-7/12)