1
|
package eu.dnetlib.data.claimsDemo;
|
2
|
|
3
|
|
4
|
import eu.dnetlib.data.claims.migration.Claim;
|
5
|
import eu.dnetlib.data.claims.migration.Context;
|
6
|
import eu.dnetlib.data.claims.migration.Project;
|
7
|
import eu.dnetlib.data.claims.migration.Result;
|
8
|
|
9
|
import java.io.IOException;
|
10
|
import java.io.InputStream;
|
11
|
import java.text.SimpleDateFormat;
|
12
|
import java.util.Date;
|
13
|
|
14
|
|
15
|
public class QueryGenerator {
|
16
|
/**
|
17
|
* Call a stored procedure with the given name.
|
18
|
*
|
19
|
* @throws Exception
|
20
|
*/
|
21
|
public String callProcedure(String procedure) {
|
22
|
return "{call " + procedure + "()}";
|
23
|
}
|
24
|
/*
|
25
|
* private String title;
|
26
|
private String externalUrl;
|
27
|
private String openaireId;
|
28
|
private String doi;
|
29
|
//accessRights : set by user
|
30
|
private String accessRights;
|
31
|
//bestLicence : from search
|
32
|
private String bestLicense;
|
33
|
|
34
|
private String collectedFrom;
|
35
|
private String xml;
|
36
|
private String resultType;*/
|
37
|
private String insertResult(String openaire_id, String result_type,String title, String collected_from , String external_url, String doi,String access_rights , String best_license,boolean found, String xml) {
|
38
|
if(title!=null&&title.contains("'")){
|
39
|
title=title.replace("'","''");
|
40
|
}
|
41
|
String fields="openaire_id, found";
|
42
|
if(result_type!=null){
|
43
|
fields+=", result_type";
|
44
|
}
|
45
|
if(title!=null){
|
46
|
fields+=", title";
|
47
|
}
|
48
|
if(collected_from!=null){
|
49
|
fields+=", collected_from";
|
50
|
}
|
51
|
if(external_url!=null){
|
52
|
fields+=", external_url";
|
53
|
}
|
54
|
if(doi!=null){
|
55
|
fields+=", doi";
|
56
|
}
|
57
|
if(access_rights!=null){
|
58
|
fields+=", access_rights";
|
59
|
}
|
60
|
if(best_license!=null){
|
61
|
fields+=", best_license";
|
62
|
}
|
63
|
String values="'"+openaire_id+"','"+found+"'";
|
64
|
if(result_type!=null){
|
65
|
values+=",'"+result_type+"'";
|
66
|
}
|
67
|
if(title!=null){
|
68
|
values+=",'"+title+"'";
|
69
|
}
|
70
|
if(collected_from!=null){
|
71
|
values+=",'"+collected_from+"'";
|
72
|
}
|
73
|
if(external_url!=null){
|
74
|
values+=",'"+external_url+"'";
|
75
|
}
|
76
|
if(doi!=null){
|
77
|
values+=",'"+doi+"'";
|
78
|
}
|
79
|
if(access_rights!=null){
|
80
|
values+=",'"+access_rights+"'";
|
81
|
}
|
82
|
if(best_license!=null){
|
83
|
values+=",'"+best_license+"'";
|
84
|
}
|
85
|
return "INSERT INTO result("+fields+")\n" +
|
86
|
" Select "+values+
|
87
|
" where not exists (select openaire_id from result where openaire_id='"+openaire_id+"')\n" +
|
88
|
" RETURNING openaire_id";
|
89
|
}
|
90
|
private String insertProject(String openaire_id, String name,String acronym, String funder_id,String funder_name, boolean found) {
|
91
|
if(name!=null) {
|
92
|
name = name.replace("'", "''");
|
93
|
}
|
94
|
String fields="openaire_id";
|
95
|
if(name!=null){
|
96
|
fields+=",name";
|
97
|
}
|
98
|
if(acronym!=null){
|
99
|
fields+=",acronym";
|
100
|
}
|
101
|
if(funder_id!=null){
|
102
|
fields+=",funder_id";
|
103
|
}
|
104
|
if(funder_name!=null){
|
105
|
fields+=",funder_name";
|
106
|
}
|
107
|
fields+=",found";
|
108
|
String values="'"+openaire_id+"'";
|
109
|
if(name!=null){
|
110
|
values+=",'"+name+"'";
|
111
|
}
|
112
|
if(acronym!=null){
|
113
|
values+=",'"+acronym+"'";
|
114
|
}
|
115
|
if(funder_id!=null){
|
116
|
values+=",'"+funder_id+"'";
|
117
|
}
|
118
|
if(funder_name!=null){
|
119
|
values+=",'"+funder_name+"'";
|
120
|
}
|
121
|
values+=",'"+found+"'";
|
122
|
return "INSERT INTO project("+fields+")\n" +
|
123
|
" Select "+values +
|
124
|
" where not exists (select openaire_id from project where openaire_id='"+openaire_id+"')\n" +
|
125
|
" RETURNING openaire_id";
|
126
|
}
|
127
|
private String insertContext(String openaire_id, String name) {
|
128
|
String fields="openaire_id";
|
129
|
if(name!=null){
|
130
|
fields+=",name";
|
131
|
}
|
132
|
String values="'"+openaire_id+"'";
|
133
|
if(name!=null){
|
134
|
values+=",'"+name+"'";
|
135
|
}
|
136
|
return "INSERT INTO context("+fields+")\n" +
|
137
|
" Select "+values+
|
138
|
" where not exists (select openaire_id from context where openaire_id='"+openaire_id+"')\n" +
|
139
|
" RETURNING openaire_id";
|
140
|
}
|
141
|
private String insertClaim(Date date, String claimedBy,String source_type, String target_type ,String source_id , String target_id) {
|
142
|
|
143
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
144
|
String dateStr=null;
|
145
|
dateStr=(format.format(date));
|
146
|
|
147
|
return " INSERT INTO claim( claim_date,claimedBy,source_type,target_type, source_id, target_id)\n" +
|
148
|
" VALUES ( '"+dateStr+"','"+claimedBy+"','"+source_type+"', '"+target_type+"','"+source_id+"','"+target_id+"')\n" +
|
149
|
" RETURNING id, source_id, target_id\n";
|
150
|
}
|
151
|
public String insertClaimhasResult(Result targetResult, Result sourceResult, Claim claim) {
|
152
|
|
153
|
return " WITH ins1 AS (\n" +
|
154
|
insertResult(targetResult.getOpenaireId(),targetResult.getResultType(),targetResult.getTitle(),targetResult.getCollectedFrom(),targetResult.getExternal_url(),targetResult.getDoi(),targetResult.getAccessRights(), targetResult.getBestLicense(),targetResult.isFound(),targetResult.getXml())+
|
155
|
" ),\n ins2 AS (\n" +
|
156
|
insertResult(sourceResult.getOpenaireId(),sourceResult.getResultType(),sourceResult.getTitle(),sourceResult.getCollectedFrom(),sourceResult.getExternal_url(),sourceResult.getDoi(),sourceResult.getAccessRights(), sourceResult.getBestLicense(),sourceResult.isFound(),sourceResult.getXml())+
|
157
|
" ),\n ins3 AS (\n" +
|
158
|
insertClaim(claim.getDate(),claim.getUserMail(),claim.getSourceType(),claim.getTargetType(),claim.getSource().getOpenaireId(),claim.getTarget().getOpenaireId()) +
|
159
|
" ),\n ins4 AS (\n" +
|
160
|
" INSERT INTO has_source_result (claim_id, openaire_id, semantics)\n" +
|
161
|
" SELECT id, source_id,'resultResult_publicationDataset_isRelatedTo'\n" +
|
162
|
" FROM ins3) \n"+
|
163
|
" INSERT INTO has_target_result(claim_id, openaire_id)\n" +
|
164
|
" SELECT id, target_id\n" +
|
165
|
" FROM ins3;";
|
166
|
}
|
167
|
public String insertClaimhasProject(Result targetResult, Project project, Claim claim) {
|
168
|
|
169
|
return " WITH ins1 AS (\n" +
|
170
|
insertResult(targetResult.getOpenaireId(),targetResult.getResultType(),targetResult.getTitle(),targetResult.getCollectedFrom(),targetResult.getExternal_url(),targetResult.getDoi(),targetResult.getAccessRights(), targetResult.getBestLicense(),targetResult.isFound(), targetResult.getXml())+
|
171
|
" ),\n ins2 AS (\n" +
|
172
|
insertProject(project.getOpenaireId(), project.getName(), project.getAcronym(), project.getFunderId(), project.getFunderName(), project.isFound())+
|
173
|
" ),\n ins3 AS (\n" +
|
174
|
insertClaim(claim.getDate(),claim.getUserMail(),claim.getSourceType(),claim.getTargetType(),claim.getSource().getOpenaireId(),claim.getTarget().getOpenaireId()) +
|
175
|
" ),\n ins4 AS (\n" +
|
176
|
" INSERT INTO has_source_project (claim_id, openaire_id,semantics)\n" +
|
177
|
" SELECT id, source_id,'resultProject_outcome_isProducedBy'\n" +
|
178
|
" FROM ins3) \n"+
|
179
|
" INSERT INTO has_target_result(claim_id, openaire_id)\n" +
|
180
|
" SELECT id, target_id\n" +
|
181
|
" FROM ins3;";
|
182
|
}
|
183
|
public String insertClaimhasContext(Result targetResult, Context context, Claim claim) {
|
184
|
|
185
|
return " WITH ins1 AS (\n" +
|
186
|
insertResult(targetResult.getOpenaireId(),targetResult.getResultType(),targetResult.getTitle(),targetResult.getCollectedFrom(),targetResult.getExternal_url(),targetResult.getDoi(),targetResult.getAccessRights(), targetResult.getBestLicense(),targetResult.isFound(), targetResult.getXml())+
|
187
|
" ),\n ins2 AS (\n" +
|
188
|
insertContext(context.getOpenaireId(), context.getTitle())+
|
189
|
" ),\n ins3 AS (\n" +
|
190
|
insertClaim(claim.getDate(),claim.getUserMail(),claim.getSourceType(),claim.getTargetType(),claim.getSource().getOpenaireId(),claim.getTarget().getOpenaireId()) +
|
191
|
" ), ins4 AS (\n" +
|
192
|
" INSERT INTO has_source_context(claim_id, openaire_id)\n" +
|
193
|
" SELECT id, source_id\n" +
|
194
|
" FROM ins3) \n"+
|
195
|
" INSERT INTO has_target_result(claim_id, openaire_id)\n" +
|
196
|
" SELECT id, target_id\n" +
|
197
|
" FROM ins3;";
|
198
|
}
|
199
|
|
200
|
public String deleteClaimTables() {
|
201
|
return "drop table if exists has_source_context;\n" +
|
202
|
"drop table if exists has_source_project;\n" +
|
203
|
"drop table if exists has_source_result;\n" +
|
204
|
"drop table if exists has_target_project;\n" +
|
205
|
"drop table if exists has_target_result;\n" +
|
206
|
"drop table if exists claim;\n" +
|
207
|
"drop table if exists project;\n" +
|
208
|
"drop table if exists result;\n" +
|
209
|
"drop table if exists context;\n" +
|
210
|
"\n ";
|
211
|
|
212
|
}
|
213
|
public String createClaimTables() {
|
214
|
return "CREATE TABLE project (\n" +
|
215
|
" openaire_id varchar(60) primary key NOT NULL,\n" +
|
216
|
" name text,\n" +
|
217
|
" acronym varchar(60),\n" +
|
218
|
" funder_id varchar(60),\n" +
|
219
|
" funder_name varchar(60),\n" +
|
220
|
" found boolean\n" +
|
221
|
");\n" +
|
222
|
"\n" +
|
223
|
"CREATE TABLE result (\n" +
|
224
|
" openaire_id varchar(60) primary key NOT NULL,\n" +
|
225
|
"\t\tresult_type varchar(30) NOT NULL,\n" +
|
226
|
"\t\tdoi varchar(60),\n" +
|
227
|
"\t\ttitle text,\n" +
|
228
|
"\t\taccess_rights varchar(30),\n" +
|
229
|
"\t\tbest_license varchar(30),\t\t\n" +
|
230
|
"\t\texternal_url text,\n" +
|
231
|
"\t\tcollected_from varchar(60) ,\n" +
|
232
|
" found boolean,\n" +
|
233
|
"\t\txml text\n" +
|
234
|
");\n" +
|
235
|
"\n" +
|
236
|
"CREATE TABLE context (\n" +
|
237
|
" openaire_id varchar(60) primary key,\n" +
|
238
|
" name text\n" +
|
239
|
");\n" +
|
240
|
"CREATE TABLE claim (\n" +
|
241
|
" id int primary key NOT NULL DEFAULT nextval('claim_id'::regclass),\n" +
|
242
|
" claim_date date NOT NULL,\n" +
|
243
|
" claimedBy varchar(60) NOT NULL,\n" +
|
244
|
" source_type varchar(30) NOT NULL,\n" +
|
245
|
" target_type varchar(30) NOT NULL,\n" +
|
246
|
" source_id varchar(60) NOT NULL,\n" +
|
247
|
" target_id varchar(60) references result(openaire_id) NOT NULL,\n" +
|
248
|
" claim_status varchar(30)\n" +
|
249
|
");\n" +
|
250
|
"\n" +
|
251
|
"CREATE TABLE has_source_context(\n" +
|
252
|
"\tclaim_id int references claim(id) NOT NULL,\n" +
|
253
|
"\topenaire_id varchar(60) references context(openaire_id) NOT NULL,\n" +
|
254
|
"\tsemantics varchar(60)\n" +
|
255
|
");\n" +
|
256
|
"CREATE TABLE has_source_project(\n" +
|
257
|
"\tclaim_id int references claim(id) NOT NULL,\n" +
|
258
|
"\topenaire_id varchar(60) references project(openaire_id) NOT NULL,\n" +
|
259
|
"\tsemantics varchar(60)\n" +
|
260
|
");\n" +
|
261
|
"CREATE TABLE has_source_result(\n" +
|
262
|
"\tclaim_id int references claim(id) NOT NULL,\n" +
|
263
|
"\topenaire_id varchar(60) references result(openaire_id) NOT NULL,\n" +
|
264
|
"\tsemantics varchar(60)\n" +
|
265
|
");\n"+
|
266
|
"CREATE TABLE has_target_project(\n" +
|
267
|
"\tclaim_id int references claim(id) NOT NULL,\n" +
|
268
|
"\topenaire_id varchar(60) references project(openaire_id) NOT NULL,\n" +
|
269
|
"\tsemantics varchar(60)\n" +
|
270
|
");\n" +
|
271
|
"CREATE TABLE has_target_result(\n" +
|
272
|
"\tclaim_id int references claim(id) NOT NULL,\n" +
|
273
|
"\topenaire_id varchar(60) references result(openaire_id) NOT NULL,\n" +
|
274
|
"\tsemantics varchar(60)\n" +
|
275
|
");\n"
|
276
|
|
277
|
;
|
278
|
|
279
|
}
|
280
|
//TODO Argiro here you can set up your mostly used queries and call them from the SQLDAO class
|
281
|
// without having to change them in your test methods
|
282
|
public String selectClaims() {
|
283
|
return " Select * from claims where id = ?";
|
284
|
|
285
|
}
|
286
|
public String selectDMFClaims(Integer limit) {
|
287
|
return " Select * from claims_view where type='dmf2actions' and xml NOT ILIKE '%<oaf:concept%' limit "+limit;
|
288
|
|
289
|
}
|
290
|
public String selectDMFById(String id) {
|
291
|
return " Select * from claims_view where type='dmf2actions' and xml NOT ILIKE '%<oaf:concept%' and resultid='"+id+"' order by date desc limit 1";
|
292
|
}
|
293
|
public String selectConceptDMFClaims(Integer limit) {
|
294
|
return " Select * from claims_view where ( type='dmf2actions' or type='updates2actions' ) and xml LIKE '%<oaf:concept%' limit "+limit;
|
295
|
}
|
296
|
public String selectRelationClaims(Integer limit) {
|
297
|
return " Select * from claims_view where type='rels2actions' limit "+limit;
|
298
|
|
299
|
}
|
300
|
public String selectClaimById(String id) {
|
301
|
return " Select * from claims_view where id='"+id+"'";
|
302
|
|
303
|
}
|
304
|
/**
|
305
|
* Renames a database SCHEMA from @n to @newDatabase
|
306
|
*
|
307
|
* @param oldSchema
|
308
|
* @param newSchema
|
309
|
* @throws Exception
|
310
|
*/
|
311
|
public String renameSchema(String oldSchema, String newSchema) {
|
312
|
return "ALTER SCHEMA " + oldSchema + " RENAME TO " + newSchema + ";";
|
313
|
}
|
314
|
|
315
|
public String dropSchema(String oldSchema) {
|
316
|
return " DROP SCHEMA if exists " + oldSchema + " CASCADE ; ";
|
317
|
}
|
318
|
|
319
|
/**
|
320
|
* Renames a database from @oldDatabase to @newDatabase
|
321
|
*
|
322
|
* @param newDatabase
|
323
|
* @param oldDatabase oldDatabase
|
324
|
* @throws Exception
|
325
|
*/
|
326
|
public String renameDatabase(String oldDatabase, String newDatabase) {
|
327
|
return " drop database if exists " + oldDatabase + " ;" + " alter database " + newDatabase + " rename to " + oldDatabase + " ; ";
|
328
|
|
329
|
}
|
330
|
|
331
|
/**
|
332
|
* Creates a new schema from a given SQL file at @schemaPath
|
333
|
*
|
334
|
* @param schemaPath
|
335
|
* @throws Exception
|
336
|
*/
|
337
|
public String createSchema(String schemaPath) throws IOException {
|
338
|
|
339
|
//InputStream in = ClassLoader.getSystemResourceAsStream("eu.dnetlib.data.eu.dnetlib.data.claimsDemo/schema.sql");
|
340
|
InputStream in = ClassLoader.getSystemResourceAsStream(schemaPath);
|
341
|
|
342
|
|
343
|
byte[] b = new byte[in.available()];
|
344
|
in.read(b);
|
345
|
String schema = new String(b);
|
346
|
|
347
|
in.close();
|
348
|
return schema;
|
349
|
|
350
|
}
|
351
|
|
352
|
public String setSearchPathDB(String user, String schemaList) {
|
353
|
|
354
|
return "ALTER ROLE " + user + " SET search_path TO " + schemaList + ";";
|
355
|
}
|
356
|
|
357
|
|
358
|
public String createDB(String database) {
|
359
|
|
360
|
return " DROP DATABASE IF EXISTS " + database + ";" + " CREATE DATABASE " + database + " WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';";
|
361
|
|
362
|
}
|
363
|
|
364
|
|
365
|
}
|