Project

General

Profile

1
package eu.dnetlib.data.claims.utils;
2

    
3
import eu.dnetlib.data.claims.entity.Project;
4
import eu.dnetlib.data.claims.entity.Result;
5
import org.apache.log4j.Logger;
6
import org.json.simple.JSONArray;
7
import org.json.simple.JSONObject;
8
import sun.net.www.protocol.http.HttpURLConnection;
9

    
10
import java.io.*;
11
import java.net.URL;
12
import java.util.List;
13
import java.util.Map;
14

    
15
/**
16
 * Created by kiatrop on 21/3/2017.
17
 */
18
public class IndexUtils {
19

    
20
    private static final Logger logger = Logger.getLogger(IndexUtils.class);
21

    
22
    public static JSONObject createJSON(Result result, List<Project> projects, List<String> concepts) throws Exception {
23
        //id ?????? result.getOpenaireId();
24
        //source ????
25
        result.getTitle();
26
        //Description ????
27
        result.getAccessRights();
28
        result.getEmbargoEndDate();
29
        result.getAuthors();
30
        result.getExternalUrl();
31
        //dcSource ????
32
        result.getDoi();
33
        //subjects ++++
34
        //Concepts
35
        //publication date
36
        //publisher
37
        //language
38
        //category
39
        //isConcept????
40

    
41
        return null;
42
    }
43

    
44
    public static JSONObject createJSON(String id, String source, String title, String description, String access_mode,
45
                                        String embargoEndDate, Map<String,String> authors, String url, String dcSource,
46
                                        String doi, List<String> subjects, List<String> concepts,
47
                                        String publicationDate, String publisher, String language, String category,
48
                                        boolean isConcept, List<Project> project4Indexes, List<String> concepts4index) throws Exception {
49

    
50
        if (source.equals("openaire")) {
51
            throw new IllegalArgumentException("cannot create JSON for openaire");
52
        }
53
        //logger.info("create JSON for doi " + doi);
54
        //logger.info("projects " + project4Indexes);
55

    
56
        //if the orcid id exists
57
        String orcid = null;
58
        String hostedby = null;
59
        String hostedbyName = null;
60
        String collectedFrom = null;
61
        String collectedFromName = null;
62
        String recordIdentifier = id;
63
        String newDoi = doi;
64
        hostedby = "openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18";
65

    
66
        if (source.equals("doi") && !isConcept) {
67
            hostedbyName = "Unknown Repository";
68
            collectedFrom = "openaire____::crossref";
69
            collectedFromName = "Crossref";
70
            newDoi = id;
71

    
72
        } else if (source.equals("datacite")) {
73
            hostedbyName = "Unknown Repository";
74
            collectedFrom = "openaire____::datacite";
75
            collectedFromName = "Datacite";
76

    
77
        } else if (source.equals("orcid")) {
78
            orcid = id;
79
            hostedbyName = "Unknown Repository";
80
            collectedFrom = "openaire____::orcid";
81
            collectedFromName = "ORCID";
82

    
83
        } else if (source.equals("driver")) {
84
            hostedbyName = "Unknown Repository";
85
            collectedFrom = "openaire____::driver";
86
            collectedFromName = "Digital Repository Infrastructure Vision for European Research";
87
        }
88

    
89
        JSONObject claimJSON = new JSONObject();
90
//        String prefix = getPrefix(source);
91
        claimJSON.put("originalId", id);
92
        claimJSON.put("title", title);
93

    
94
        JSONArray authorsJSONArray = new JSONArray();
95
        for (Map.Entry<String,String> authorEntry: authors.entrySet()) {
96
            authorsJSONArray.add(authorEntry.getValue());
97
        }
98

    
99
        claimJSON.put("authors", authorsJSONArray);
100

    
101
        claimJSON.put("publisher", publisher);
102
        claimJSON.put("description", description);
103
        claimJSON.put("language", language);
104

    
105
        JSONArray pidsJSONArray = new JSONArray();
106
        JSONObject pidJSON = null;
107
        if (newDoi != null) {
108
            pidJSON = new JSONObject();
109
            pidJSON.put("type", "doi");
110
            pidJSON.put("value", newDoi);
111
            pidsJSONArray.add(pidJSON);
112
        }
113
        if (orcid != null) {
114
            pidJSON = new JSONObject();
115
            pidJSON.put("type", "orcidworkid");
116
            pidJSON.put("value", orcid);
117
            pidsJSONArray.add(pidJSON);
118
        }
119
        claimJSON.put("pids", pidsJSONArray);
120

    
121
        claimJSON.put("licenseCode", access_mode);
122
        if(source.equals("crossref")) {
123
            claimJSON.put("resourceType", "0001");
124
        } else if (source.equals("datacite")) {
125
            claimJSON.put("resourceType", "0021");
126
        } else {
127
            claimJSON.put("resourceType", "0001");
128
        }
129

    
130
        claimJSON.put("url", url);
131
        claimJSON.put("collectedFromId", collectedFrom);
132
        claimJSON.put("hostedById", hostedby);
133

    
134
        JSONArray linksToProjectsJSONArray = new JSONArray();
135
        for (Project project:project4Indexes) {
136
            linksToProjectsJSONArray.add(createProjectPath(project)) ;
137
        }
138
        claimJSON.put("linksToProjects",linksToProjectsJSONArray);
139

    
140
        JSONArray contextsJSONArray = new JSONArray();
141
        if(!concepts4index.isEmpty()) {
142
            for (String context: concepts4index) {
143
                contextsJSONArray.add(context); //TODO check
144
            }
145
        }
146
        claimJSON.put("contexts", contextsJSONArray);
147

    
148
        return claimJSON;
149
    }
150

    
151
    public static void feedIndex(String id, String source, String title,
152
                                 String description, String access_mode, String embargoEndDate,
153
                                 Map<String, String> authors, String url, String dcSource, String doi,
154
                                 List<Project> projects, List<String> subjects, List<String> concepts,
155
                                 String publicationDate, String publisher, String language, String category,
156
                                 boolean isConcept, List<Project> project4Indexes, List<String> concepts4index,
157
                                 String directClaimAPIUrl) throws Exception {
158

    
159
        logger.info("number of projects " + project4Indexes);
160
        if (project4Indexes!= null) {
161
            logger.info("number of projects " + project4Indexes.size());
162
        }
163

    
164
        URL obj = new URL(directClaimAPIUrl);
165
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
166

    
167
        JSONObject claimJson = createJSON(id, source, title, description, access_mode, embargoEndDate,
168
                authors, url, dcSource, doi, subjects, concepts, publicationDate,
169
                publisher, language, category, isConcept, project4Indexes, concepts4index);
170
        //add request header
171
        con.setRequestMethod("POST");
172
        //con.setRequestProperty("User-Agent", USER_AGENT);
173
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
174
        con.setRequestProperty("Content-Type", "application/json");
175

    
176
        logger.debug("Feeding index with " + claimJson.toJSONString());
177
        String urlParameters = claimJson.toJSONString();
178

    
179
        // Send post request
180
        con.setDoOutput(true);
181
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
182
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
183
        writer.write(urlParameters);
184
        writer.close();
185
        wr.close();
186

    
187
        int responseCode = con.getResponseCode();
188
        logger.debug("\nSending 'POST' request to URL : " + url);
189
        logger.debug("Post parameters : " + urlParameters);
190
        logger.debug("Response Code : " + responseCode);
191

    
192
        BufferedReader in = new BufferedReader(
193
                new InputStreamReader(con.getInputStream()));
194
        String inputLine;
195
        StringBuffer response = new StringBuffer();
196

    
197
        while ((inputLine = in.readLine()) != null) {
198
            response.append(inputLine);
199
        }
200
        in.close();
201

    
202
        //print result
203
        logger.debug(response.toString());
204
    }
205

    
206
    private static String createProjectPath(Project project) {
207
        if (project.getFunderShortName().equals("EC")) {
208
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty()) {
209
                return "info:eu-repo/grantAgreement/EC/" + project.getFundingStreamLevel0() + "/"
210
                        + project.getCode() + "/EU//" + project.getAcronym();
211
            }
212

    
213
            return "info:eu-repo/grantAgreement/EC/"+project.getFundingStreamLevel0()+"/"
214
                    +project.getCode()+"/EU//"+project.getName();
215
        }
216

    
217
        if (project.getFunderShortName().equals("WT")) {
218
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty()) {
219
                return "info:eu-repo/grantAgreement/WT//" + project.getCode() + "//" + project.getAcronym();
220
            }
221
            return "info:eu-repo/grantAgreement/WT//"+project.getCode()+project.getName();
222
        }
223

    
224
        if (project.getFunderShortName().equals("ARC")) {
225
            logger.debug("name: '" + project.getName()+"'");
226
            logger.debug("name: '" + project.getAcronym()+"'");
227
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty()) {
228
                return "info:eu-repo/grantAgreement/ARC/" + project.getFundingStreamLevel0() + "/"
229
                        + project.getCode() + "/AU//" + project.getAcronym();
230
            }
231
            return "info:eu-repo/grantAgreement/ARC/"+project.getFundingStreamLevel0()+"/"
232
                    +project.getCode()+"/AU//"+project.getName();
233
        }
234

    
235
        if (project.getFunderShortName().equals("NHMRC")) {
236
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty()) {
237
                return "info:eu-repo/grantAgreement/NHMRC/" + project.getFundingStreamLevel0() + "/"
238
                        + project.getCode() + "/AU//" + project.getAcronym();
239
            }
240
            return "info:eu-repo/grantAgreement/NHMRC/"+project.getFundingStreamLevel0()+"/"
241
                    +project.getCode()+"/AU//"+project.getName();
242
        }
243

    
244
        if (project.getFunderShortName().equals("FCT")) {
245
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty()) {
246
                return "info:eu-repo/grantAgreement/FCT/" + project.getFundingStreamLevel0() + "/"
247
                        + project.getCode() + "/PT//" + project.getAcronym();
248
            }
249
            return "info:eu-repo/grantAgreement/FCT/"+project.getFundingStreamLevel0()+"/"
250
                    +project.getCode()+"/PT//"+project.getName();
251
        }
252

    
253
        return "";
254
    }
255

    
256
    private static  String getPrefix(String source) {
257
        logger.debug("SOURCE " + source);
258
        if (source == null || source.equals("openaire")) {
259
            return "openaire____";
260
        } else if (source.equals("doi")) {
261
            return "crossref____";
262
        } if (source.equals("orcid")) {
263
            return "orcid_______";
264
        } else if (source.equals("datacite")) {
265
            return "datacite____";
266
        } else if (source.equals("driver")) {
267
            return "driver______";
268
        } else {
269
            return null;
270
        }
271
    }
272
/*
273
    public static void main(String[] args) throws IOException {
274

    
275
        String url = "http://beta.services.openaire.eu:8280/is/mvc/api/publications/feedObject";
276
        URL obj = new URL(url);
277
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
278

    
279
        //add reuqest header
280
        con.setRequestMethod("POST");
281
        //con.setRequestProperty("User-Agent", USER_AGENT);
282
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
283
        con.setRequestProperty("Content-Type", "application/json");
284

    
285

    
286
        /*String urlParameters = "{\n" +
287
                "\"collectedFromId\": \"openaire____::crossref\",\n" +
288
                "\"authors\": [\"George Papastefanatos\", \"Panos Vassiliadis\", \"Alkis Simitsis\", \"Yannis Vassiliou\"],\n" +
289
                "\"title\": \"HECATAEUS: Regulating schema evolution\",\n" +
290
                "\"hostedById\": \"openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18\",\n" +
291
                "\"licenseCode\": \"OPEN\",\n" +
292
                "\"pids\": [{\n" +
293
                "\"value\": \"10.1109\\/icde.2010.5447778\",\n" +
294
                "\"type\": \"doi\"\n" +
295
                "}],\n" +
296
                "\"description\": null,\n" +
297
                "\"originalId\": \"crossref____::817ee94bc4a0515adadb1fdaa78927f6\",\n" +
298
                "\"contexts\": [],\n" +
299
                "\"language\": null,\n" +
300
                "\"linksToProjects\": [\"info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus\"],\n" +
301
                "\"url\": \"http:\\/\\/dx.doi.org\\/10.1109\\/icde.2010.5447778\",\n" +
302
                "\"resourceType\": \"0001\",\n" +
303
                "\"publisher\": \"Institute of Electrical & Electronics Engineers (IEEE)\"\n" +
304
                "}";
305

    
306
        String urlParameters = "{\n" +
307
                "\"collectedFromId\": \"openaire____::crossref\",\n" +
308
                "\"authors\": [\"George Papastefanatos\", \"Panos Vassiliadis\", \"Alkis Simitsis\", \"Yannis Vassiliou\"],\n" +
309
                " \"title\": \"â\\\\ dark bottomless Abyss, that lies under our feet, had yawned openâ\\\\\",\n" +
310
                "\"hostedById\": \"openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18\",\n" +
311
                "\"licenseCode\": \"CLOSED\",\n" +
312
                "\"pids\": [{\n" +
313
                "\"value\": \"10.1007\\/s13740_012_0006_9\",\n" +
314
                "\"type\": \"doi\"\n" +
315
                "}],\n" +
316
                "\"description\": null,\n" +
317
                "\"originalId\": \"crossref____::test\",\n" +
318
                "\"contexts\": [\"egi::classification::natsc::infos::dmin\"], " +
319
                "\"language\": null, " +
320
                "\"linksToProjects\": [\"info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus\"], " +
321
                "\"url\": \"http:\\/\\/dx.doi.org\\/10.1007\\/s13740_012_0006_9\", " +
322
                "\"resourceType\": \"0001\", " +
323
                "\"publisher\": \"Springer Science + Business Media\" " +
324
                "}";
325
*/
326

    
327
/*        String urlParameters = "{ " +
328
                "\"collectedFromId\": \"openaire____::crossref\", " +
329
                "\"authors\": [\"Agapi Dalkou\"], " +
330
                "\"title\": \"Η ανάπτυξη ερμηνευτικών στρατηγικών στο μάθημα της νεοελληνικής λογοτεχνίας στη δευτεροβάθμια εκπαίδευση σε περιβάλλον συνεργατικής μάθησης\", " +
331
                "\"hostedById\": \"openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18\", " +
332
                "\"licenseCode\": \"CLOSED\", " +
333
                "\"pids\": [{ " +
334
                "\"value\": \"10.12681\\/eadd\\/29202\", " +
335
                "\"type\": \"doi\" " +
336
                "}], " +
337
                "\"description\": null, " +
338
                "\"originalId\": \"crossref____::cf7d85a13ac43f92431b102723d13d5a\", " +
339
                "\"contexts\": [\"egi\", \"egi::country::gr\"], " +
340
                "\"language\": null, " +
341
                "\"linksToProjects\": [\"info:eu-repo\\/grantAgreement\\/EC\\/FP7\\/246686\\/EU\\/\\/OPENAIRE\", \"info:eu-repo\\/grantAgreement\\/EC\\/FP7\\/288611\\/EU\\/\\/ECODRIVER\"], " +
342
                "\"url\": \"http:\\/\\/dx.doi.org\\/10.12681\\/eadd\\/29202\", " +
343
                "\"resourceType\": \"0001\", " +
344
                "\"publisher\": \"National Documentation Centre\"" +
345
                "}";
346

    
347
        // Send post request
348
        con.setDoOutput(true);
349
        /*DataOutputStream wr = new DataOutputStream(con.getOutputStream());
350
        wr.writeBytes(urlParameters);
351
        wr.flush();
352
        wr.close();
353
        */
354

    
355
/*        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
356
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
357
        writer.write(urlParameters);
358
        writer.close();
359
        wr.close();
360

    
361

    
362
        int responseCode = con.getResponseCode();
363
        System.out.println(" Sending 'POST' request to URL : " + url);
364
        System.out.println("Post parameters : " + urlParameters);
365
        System.out.println("Response Code : " + responseCode);
366

    
367
        BufferedReader in = new BufferedReader(
368
                new InputStreamReader(con.getInputStream()));
369
        String inputLine;
370
        StringBuffer response = new StringBuffer();
371

    
372
        while ((inputLine = in.readLine()) != null) {
373
            response.append(inputLine);
374
        }
375
        in.close();
376

    
377
        //print result
378
        System.out.println(response.toString());
379
    }
380
*/
381

    
382
}
(6-6/9)