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.SqlDAO;
8
import org.apache.log4j.Logger;
9

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

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

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

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

    
31
    }
32

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

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

    
77
    /**
78
     *
79
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
80
     * @param offset returns claims after 'offset' position (null for no offset)
81
     * @param keyword  filters in specific fields per type (result: {title,doi},project: {name.acronym. funder_name, funder_acronym}, context: {name})
82
     * @param orderField
83
     * @param descending
84
     * @return
85
     * @throws Exception
86
     */
87
    public List<Claim> fetchAllClaims( Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception {
88
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaims(limit, offset,keyword,orderField,descending,types));
89
        return fetchClaimsByResultSet(rs, addCurationInfo);
90
    }
91

    
92
    /**
93
     *
94
     * @param dateFrom
95
     * @param dataTo
96
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
97
     * @param offset returns claims after 'offset' position (null for no offset)
98
     * @return
99
     * @throws Exception
100
     */
101
    public List<Claim> fetchClaimsByDate(String dateFrom, String dataTo, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
102
         return fetchClaimsByDate(dateFrom, dataTo, limit, offset, null, "claim.claim_date",true, new ArrayList<String>(),addCurationInfo);
103
    }
104
    /**
105
     *
106
     * @param dateFrom
107
     * @param dataTo
108
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
109
     * @param offset returns claims after 'offset' position (null for no offset)
110
     * @return
111
     * @throws Exception
112
     */
113
    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 {
114
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByDate(dateFrom, dataTo, limit, offset,keyword, orderField, descending,types));
115
        return fetchClaimsByResultSet(rs, addCurationInfo);
116
    }
117

    
118
    /**
119
     *
120
     * @param openaireId for project
121
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
122
     * @param offset returns claims after 'offset' position (null for no offset)
123
     * @return
124
     * @throws Exception
125
     */
126
    public List<Claim> fetchClaimsByProject(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
127

    
128
        return fetchClaimsByProject(openaireId,limit, offset, null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
129
    }
130
    /**
131
     *
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
     * @param orderField
136
     * @param descending
137
     * @return
138
     * @throws Exception
139
     */
140
    public List<Claim> fetchClaimsByProject(String openaireId, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception {
141
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByProject(openaireId, limit, offset, keyword, orderField,descending,types));
142
        return fetchClaimsByResultSet(rs, addCurationInfo);
143
    }
144
    /**
145
     *
146
     * @param openaireId of Funder
147
     * @param limit
148
     * @param offset
149
     * @return
150
     * @throws Exception
151
     */
152
    public List<Claim> fetchClaimsByFunder(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
153
         return fetchClaimsByFunder(openaireId,limit,offset,null,"claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
154
    }
155
    /**
156
     *
157
     * @param openaireId of Funder
158
     * @param limit
159
     * @param offset
160
     * @param orderField
161
     * @param descending
162
     * @return
163
     * @throws Exception
164
     */
165
    public List<Claim> fetchClaimsByFunder(String openaireId, Integer limit, Integer offset,String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception {
166
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByFunder(openaireId, limit, offset,keyword, orderField,descending,types));
167
        return fetchClaimsByResultSet(rs, addCurationInfo);
168
    }
169
    /**
170
     *
171
     * @param openaireId for result
172
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
173
     * @param offset returns claims after 'offset' position (null for no offset)
174
     * @return
175
     * @throws Exception
176
     */
177
    public List<Claim> fetchClaimsByResult(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
178
         return fetchClaimsByResult(openaireId,limit,offset,null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
179
    }
180
    /**
181
     *
182
     * @param openaireId for result
183
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
184
     * @param offset returns claims after 'offset' position (null for no offset)
185
     * @param orderField
186
     * @param descending
187
     * @return
188
     * @throws Exception
189
     */
190
    public List<Claim> fetchClaimsByResult(String openaireId, Integer limit, Integer offset,String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception {
191
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByResult(openaireId, limit, offset,keyword, orderField, descending,types));
192
        return fetchClaimsByResultSet(rs, addCurationInfo);
193
    }
194
    /**
195
     *
196
     * @param openaireId
197
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
198
     * @param offset returns claims after 'offset' position (null for no offset)
199
     * @return
200
     * @throws Exception
201
     */
202
    public List<Claim> fetchClaimsByContext(String openaireId, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
203
         return fetchClaimsByContext(openaireId,limit,offset,null,"claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
204
    }
205
    /**
206
     *
207
     * @param openaireId
208
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
209
     * @param offset returns claims after 'offset' position (null for no offset)
210
     * @param orderField
211
     * @param descending
212
     * @return
213
     * @throws Exception
214
     */
215
    public List<Claim> fetchClaimsByContext(String openaireId, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception {
216
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByContext(openaireId, limit, offset,keyword,orderField, descending,types));
217
        return fetchClaimsByResultSet(rs, addCurationInfo);
218
    }
219
    public Claim fetchClaimById(String claimId, boolean addCurationInfo) throws Exception {
220
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateSelectClaimQuery(claimId));
221
        if(rs.next()) {
222
            String sourceType =rs.getString(2);
223
            String sourceId =rs.getString(3);
224
            String targetType =rs.getString(4);
225
            String targetId =rs.getString(5);
226
           rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByClaimId(claimId,sourceType,targetType));
227
        }else{
228
            log.info("No claim with id : "+ claimId+"\n");
229
        }
230
        Claim claim = null;
231
        List<Claim> claims=fetchClaimsByResultSet(rs, addCurationInfo);
232
        if(claims.size()==1){
233
            claim=claims.get(0);
234
        }
235
        return claim;
236
    }
237

    
238
    /**
239
     *
240
     * @param token for project token
241
     * @param email for check if this email belongs to one project manager of the project specified by token
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
     * @return
245
     * @throws Exception
246
     */
247
    public List<Claim> fetchClaimsByToken(String token, String email, Integer limit, Integer offset, boolean addCurationInfo) throws Exception {
248
        return fetchClaimsByToken(token, email, limit, offset, null, "claim.claim_date",true, new ArrayList<String>(), addCurationInfo);
249
    }
250
    /**
251
     *
252
     * @param token for project token
253
     * @param email for check if this email belongs to one project manager of the project specified by token
254
     * @param limit returns at most 'limit' claims  (use -1 or null for no limit)
255
     * @param offset returns claims after 'offset' position (null for no offset)
256
     * @param orderField
257
     * @param descending
258
     * @return
259
     * @throws Exception
260
     */
261
    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 {
262
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchClaimsByToken(token, email, limit, offset, keyword, orderField,descending,types));
263
        return fetchClaimsByResultSet(rs, addCurationInfo);
264
    }
265

    
266
    public List<Claim> fetchClaimsByResultSet(ResultSet rs, boolean addCurationInfo) throws Exception {
267
        List<Claim> claims= new ArrayList<Claim>();
268
        while(rs.next()) {
269
            Claim claim= new Claim();
270
            claims.add(claim);
271
            claim.setId(rs.getString(1));
272
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
273
            Date date = format.parse(rs.getString(2));
274
            claim.setDate(date);
275
            claim.setUserMail(rs.getString(3));
276
            claim.setSourceType(rs.getString(4));
277
            claim.setTargetType(rs.getString(5));
278
            claim.setSemantics(rs.getString(6));
279

    
280
            if(addCurationInfo) {
281
                date = format.parse(rs.getString(7));
282
                claim.setCurationDate(date);
283
                claim.setCuratedBy(rs.getString(8));
284
            }
285
            claim.setApproved(rs.getBoolean(9));
286
            claim.setSource(buildEntity(rs,10,claim.getSourceType()));
287
            claim.setTarget(buildEntity(rs,20,claim.getTargetType()));
288

    
289
        }
290
        return claims;
291
    }
292
    public Integer countClaimsByResultSet(ResultSet rs) throws Exception {
293

    
294
        while(rs.next()) {
295
            return rs.getInt(1);
296
        }
297
        return null;
298
    }
299
    public List<Project> getProjectsByResultSet(ResultSet rs) throws Exception {
300
        List<Project> projects= new ArrayList<Project>();
301
        while(rs.next()) {
302
            Project project=(Project)(buildEntity(rs, 1, ClaimUtils.PROJECT));
303
             projects.add(project);
304
        }
305
        return projects;
306
    }
307
    public List<Context> getContextsByResultSet(ResultSet rs) throws Exception {
308
        List<Context> contexts= new ArrayList<Context>();
309
        while(rs.next()) {
310
            Context context=(Context)(buildEntity(rs, 1, ClaimUtils.CONTEXT));
311
            contexts.add(context);
312
        }
313
        return contexts;
314
    }
315
    public Integer countAllClaims(String keyword, List<String> types) throws Exception {
316
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountAllClaims(keyword,types));
317
        return countClaimsByResultSet(rs);
318
    }
319
    public Integer countClaimsByUser(String user,String keyword, List<String> types)throws Exception {
320
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountByUser(user,keyword,types));
321
        return countClaimsByResultSet(rs);
322
    }
323
    public Integer countClaimsByProject(String projectId,String keyword, List<String> types)throws Exception {
324
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountByProject(projectId,keyword,types));
325
        return countClaimsByResultSet(rs);
326
    }
327
    public Integer countClaimsByFunder(String funderId,String keyword, List<String> types)throws Exception {
328
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountByFunder(funderId,keyword,types));
329
        return countClaimsByResultSet(rs);
330
    }
331
    public Integer countClaimsByResult(String resultId,String keyword, List<String> types)throws Exception {
332
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountByResult(resultId,keyword,types));
333
        return countClaimsByResultSet(rs);
334
    }
335
    public Integer countClaimsByContext(String contextId,String keyword, List<String> types)throws Exception {
336
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountByContext(contextId,keyword,types));
337
        return countClaimsByResultSet(rs);
338
    }
339
    public Integer countClaimsByDate(String dateFrom,String dateTo,String keyword, List<String> types)throws Exception {
340
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountByDate(dateFrom,dateTo,keyword,types));
341
        return countClaimsByResultSet(rs);
342
    }
343

    
344
    public List<Project> getAllclaimedProjects()throws Exception {
345
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchAllProjectsQuery());
346
        return getProjectsByResultSet(rs);
347
    }
348
    public List<Context> getAllclaimedContexts()throws Exception {
349
        ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateFetchAllContextsQuery());
350
        return getContextsByResultSet(rs);
351
    }
352
    /**
353
     *
354
     * @param rs
355
     * @param index
356
     * @return
357
     * @throws SQLException
358
     */
359
    private static Result buildResult(ResultSet rs, Integer index) throws SQLException {
360
        Result result = new Result();
361
        result.setOpenaireId(rs.getString(index));
362
        result.setTitle(rs.getString(index + 1));
363
        result.setResultType(rs.getString(index + 2));
364
        result.setDoi(rs.getString(index + 3));
365
        result.setOrcidworkid(rs.getString(index + 4));
366
        result.setAccessRights(rs.getString(index + 5));
367
        result.setEmbargoEndDate(rs.getString(index + 6));
368
        result.setBestLicense(rs.getString(index + 7));
369
        result.setExternalUrl(rs.getString(index + 8));
370
        result.setCollectedFrom(rs.getString(index + 9));
371
        return result;
372

    
373
    }
374
    private static Project buildProject(ResultSet rs, Integer index) throws SQLException {
375
        Project project = new Project();
376
        project.setOpenaireId(rs.getString(index));
377
        project.setName(rs.getString(index + 1));
378
        project.setAcronym(rs.getString(index + 2));
379
        project.setFunderId(rs.getString(index + 3));
380
        project.setFunderName(rs.getString(index + 4));
381
        project.setFunderShortName(rs.getString(index + 5));
382
        return project;
383

    
384
    }
385
    private static Context buildContext(ResultSet rs, Integer index) throws SQLException {
386
        Context context = new Context();
387
        context.setOpenaireId(rs.getString(index));
388
        context.setTitle(rs.getString(index + 1));
389
        return context;
390

    
391
    }
392
    private static OpenaireEntity buildEntity(ResultSet rs, Integer index, String type) throws SQLException {
393
        OpenaireEntity openaireEntity = null;
394
        if(type == null){
395
            openaireEntity = null;
396
        }else if(type.equals(ClaimUtils.PUBLICATION)||type.equals(ClaimUtils.DATASET)) {
397
            openaireEntity= buildResult(rs, index);
398
        }else if(type.equals(ClaimUtils.PROJECT)){
399
            openaireEntity = buildProject(rs,index);
400
        }else if(type.equals(ClaimUtils.CONTEXT)){
401
            openaireEntity = buildContext(rs,index);
402
        }
403
        return openaireEntity;
404
    }
405

    
406
    public SqlDAO getSqlDAO() {
407
        return sqlDAO;
408
    }
409

    
410
    public void setSqlDAO(SqlDAO sqlDAO) {
411
        this.sqlDAO = sqlDAO;
412
    }
413

    
414
    public QueryGenerator getQueryGenerator() {
415
        return queryGenerator;
416
    }
417

    
418
    public void setQueryGenerator(QueryGenerator queryGenerator) {
419
        this.queryGenerator = queryGenerator;
420
    }
421
}
(7-7/12)