Project

General

Profile

« Previous | Next » 

Revision 48305

Added by Tsampikos Livisianos almost 7 years ago

move changes from dnet4

View differences:

modules/dnet-openaire-usage-stats-export/trunk/src/main/java/eu/dnetlib/usagestats/export/IrusStats.java
1
package eu.dnetlib.usagestats.export;
2

  
3
import java.io.*;
4
//import java.io.BufferedReader;
5
//import java.io.InputStreamReader;
6
import java.net.URL;
7
import java.net.URLConnection;
8
import java.sql.ResultSet;
9
import java.text.SimpleDateFormat;
10
import java.util.Date;
11
import java.util.Calendar;
12

  
13
import java.sql.Connection;
14
import java.sql.DriverManager;
15
import java.sql.PreparedStatement;
16
import java.sql.Statement;
17

  
18
import org.json.simple.JSONArray;
19
import org.json.simple.JSONObject;
20
import org.json.simple.parser.JSONParser;
21

  
22
import org.apache.log4j.Logger;
23

  
24
/**
25
 * Created by tsampikos on 28/3/2017.
26
 */
27
public class IrusStats {
28
     private final String dbUrl;
29
    private final String dbSchema;
30
    private final String dbUserName;
31
    private final String dbPassword;
32

  
33
    private Connection conn = null;
34
    private Statement stmt = null;
35

  
36
    private final Logger log = Logger.getLogger(this.getClass());
37

  
38
    public IrusStats(String dbUrl, String dbUsername, String dbPassword) throws Exception {
39
        this.dbUrl = dbUrl;
40
        this.dbSchema = "shadow";
41
        this.dbUserName = dbUsername;
42
        this.dbPassword = dbPassword;
43

  
44
        connectDB();
45
        createTables();
46
    }
47

  
48
    private void connectDB() throws Exception {
49
        try {
50
            Class.forName("org.postgresql.Driver");
51
            conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
52

  
53
            stmt = conn.createStatement();
54
            String sqlSetSearchPath = "SET search_path TO " + dbSchema + ";";
55
            stmt.executeUpdate(sqlSetSearchPath);
56

  
57
            log.info("Opened database successfully");
58

  
59
        } catch (Exception e) {
60
            log.error("Connect to db failed: " + e);
61
            throw new Exception("Failed to connect to db: " + e.toString(), e);
62
        }
63
    }
64

  
65
    private void createTables() throws Exception {
66
        try {
67

  
68
            stmt = conn.createStatement();
69
            String sqlCreateTableSushiLog = "CREATE TABLE IF NOT EXISTS sushilog(source TEXT, repository TEXT, rid TEXT, date TEXT, metric_type TEXT, count INT, PRIMARY KEY(source, repository, rid, date, metric_type));";
70
            stmt.executeUpdate(sqlCreateTableSushiLog);
71

  
72
            String sqlCopyPublicSushiLog="INSERT INTO sushilog SELECT * FROM public.sushilog;";
73
            stmt.executeUpdate(sqlCopyPublicSushiLog);
74

  
75
            String sqlcreateRuleSushiLog = "CREATE OR REPLACE RULE ignore_duplicate_inserts AS " +
76
                " ON INSERT TO sushilog " +
77
                " WHERE (EXISTS ( SELECT sushilog.source, sushilog.repository," +
78
                "sushilog.rid, sushilog.date " +
79
                "FROM sushilog " +
80
                "WHERE sushilog.source = new.source AND sushilog.repository = new.repository AND sushilog.rid = new.rid AND sushilog.date = new.date AND sushilog.metric_type = new.metric_type)) DO INSTEAD NOTHING;";
81
            stmt.executeUpdate(sqlcreateRuleSushiLog);
82

  
83
            stmt.close();
84
            conn.close();
85
            log.info("Sushi Tables Created");
86
        } catch (Exception e) {
87
            log.error("Failed to create tables: " + e);
88
            throw new Exception("Failed to create tables: " + e.toString(), e);
89
        }
90
    }
91

  
92
    public void irusStats() throws Exception {
93
        if (conn.isClosed())
94
            connectDB();
95

  
96
        stmt = conn.createStatement();
97
        conn.setAutoCommit(false);
98

  
99
        //String sql = "INSERT INTO sushi_result_downloads SELECT s.source, d.id AS repository, ro.id, s.date, s.count FROM sushilog s, datasource_oids d, result_oids ro WHERE s.repository=d.orid AND s.oai=ro.orid AND metric_type='ft_total'";
100
        //String sql = "SELECT s.source, d.id AS repository_id, ro.id as result_id, extract('year' from s.date::date) ||'/'|| LPAD(CAST(extract('month' from s.date::date) AS VARCHAR), 2, '0') as date, s.count INTO downloads_stats FROM sushilog s, datasource_oids d, result_oids ro WHERE s.repository=d.orid AND s.oai=ro.orid AND metric_type='ft_total'";
101
        //String sql = "INSERT INTO downloads_stats SELECT s.source, d.id AS repository_id, ro.id as result_id, extract('year' from s.date::date) ||'/'|| LPAD(CAST(extract('month' from s.date::date) AS VARCHAR), 2, '0') as date, s.count FROM sushilog s, datasource_oids d, result_oids ro WHERE s.repository=d.orid AND s.oai=ro.orid AND metric_type='ft_total';";
102
        String sql = "INSERT INTO downloads_stats SELECT s.source, d.id AS repository_id, ro.id as result_id, extract('year' from s.date::date) ||'/'|| LPAD(CAST(extract('month' from s.date::date) AS VARCHAR), 2, '0') as date, s.count, '0' FROM sushilog s, datasource_oids d, result_oids ro WHERE s.repository=d.orid AND s.rid=ro.orid AND metric_type='ft_total' AND s.source='IRUS-UK';";
103
        stmt.executeUpdate(sql);
104

  
105

  
106
        stmt.close();
107
        conn.commit();
108
        conn.close();
109
    }
110

  
111
    public void processIrusRRReport() throws Exception {
112
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM");
113
        String reportUrl = "http://www.irus.mimas.ac.uk/api/sushilite/v1_7/GetReport/?Report=RR1&Release=4&RequestorID=OpenAIRE&BeginDate=2016-01&EndDate=" + simpleDateFormat.format(new Date()) + "&RepositoryIdentifier=&ItemDataType=&NewJiscBand=&Granularity=Monthly&Callback=";
114
        String text = getJson(reportUrl, "", "");
115

  
116
        JSONParser parser = new JSONParser();
117
        JSONObject jsonObject = (JSONObject) parser.parse(text);
118
        jsonObject = (JSONObject) jsonObject.get("ReportResponse");
119
        jsonObject = (JSONObject) jsonObject.get("Report");
120
        jsonObject = (JSONObject) jsonObject.get("Report");
121
        jsonObject = (JSONObject) jsonObject.get("Customer");
122
        JSONArray jsonArray = (JSONArray) jsonObject.get("ReportItems");
123
        int i = 0;
124
        for (Object aJsonArray : jsonArray) {
125
            JSONObject jsonObjectRow = (JSONObject) aJsonArray;
126
            JSONArray itemIdentifier = (JSONArray) jsonObjectRow.get("ItemIdentifier");
127
            for (Object identifier : itemIdentifier) {
128
                JSONObject opendoar = (JSONObject) identifier;
129
                if(opendoar.get("Type").toString().equals("OpenDOAR")){
130
                    //System.out.println(i + ": " + opendoar.get("Value").toString());
131
                    log.info(i + ": " + opendoar.get("Value").toString());
132
                    i++;
133
                    processIrusIRReport(opendoar.get("Value").toString());
134
                    break;
135
                }
136
            }
137
            //break;
138
        }
139
    }
140

  
141
    private void processIrusIRReport(String opendoar) throws Exception {
142
        if (conn.isClosed())
143
            connectDB();
144

  
145
        conn.setAutoCommit(false);
146

  
147
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM");
148

  
149
        Calendar start = Calendar.getInstance();
150
        start.set(Calendar.YEAR, 2016);
151
        start.set(Calendar.MONTH, Calendar.JANUARY);
152
        //start.setTime(simpleDateFormat.parse("2016-01"));
153

  
154
        Calendar end = Calendar.getInstance();
155
        end.add(Calendar.DAY_OF_MONTH, -1);
156

  
157
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
158
        PreparedStatement st = conn.prepareStatement("SELECT max(date) FROM public.sushilog WHERE repository=?;");
159
        st.setString(1, "opendoar____::" + opendoar);
160
        ResultSet rs_date = st.executeQuery();
161
        while(rs_date.next()){
162
            if(rs_date.getString(1) != null && !rs_date.getString(1).equals("null") && !rs_date.getString(1).equals("")) {
163
                start.setTime(sdf.parse(rs_date.getString(1)));
164
            }
165
        }
166
        rs_date.close();
167

  
168
        PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO sushilog (source, repository, rid, date, metric_type, count) VALUES (?,?,?,?,?,?)");
169
        int batch_size = 0;
170

  
171
        while(start.before(end)){
172
            //log.info("date: " + simpleDateFormat.format(start.getTime()));
173
            String reportUrl = "http://irus.mimas.ac.uk/api/sushilite/v1_7/GetReport/?Report=IR1&Release=4&RequestorID=OpenAIRE&BeginDate=" + simpleDateFormat.format(start.getTime()) + "&EndDate=" + simpleDateFormat.format(start.getTime()) + "&RepositoryIdentifier=opendoar%3A" + opendoar + "&ItemIdentifier=&ItemDataType=&hasDOI=&Granularity=Monthly&Callback=";
174
            start.add(Calendar.MONTH, 1);
175

  
176
            String text = getJson(reportUrl, "", "");
177
            if(text == null){
178
                continue;
179
            }
180

  
181
            JSONParser parser = new JSONParser();
182
            JSONObject jsonObject = (JSONObject) parser.parse(text);
183
            jsonObject = (JSONObject) jsonObject.get("ReportResponse");
184
            jsonObject = (JSONObject) jsonObject.get("Report");
185
            jsonObject = (JSONObject) jsonObject.get("Report");
186
            jsonObject = (JSONObject) jsonObject.get("Customer");
187
            JSONArray jsonArray = (JSONArray) jsonObject.get("ReportItems");
188
            if(jsonArray == null){
189
                continue;
190
            }
191
            String oai = "";
192
            for (Object aJsonArray : jsonArray) {
193
                JSONObject jsonObjectRow = (JSONObject) aJsonArray;
194
                JSONArray itemIdentifier = (JSONArray) jsonObjectRow.get("ItemIdentifier");
195
                for (Object identifier : itemIdentifier) {
196
                    JSONObject oaiPmh = (JSONObject) identifier;
197
                    if(oaiPmh.get("Type").toString().equals("OAI")){
198
                        oai = oaiPmh .get("Value").toString();
199
                        //System.out.println("OAI: " + oai);
200
                        break;
201
                    }
202
                }
203

  
204
                JSONArray itemPerformance = (JSONArray) jsonObjectRow.get("ItemPerformance");
205
                String period;
206
                String type;
207
                String count;
208
                for (Object perf : itemPerformance) {
209
                    JSONObject performance = (JSONObject) perf;
210
                    JSONObject periodObj = (JSONObject) performance.get("Period");
211
                    period = periodObj.get("Begin").toString();
212
                    JSONObject instanceObj = (JSONObject) performance.get("Instance");
213
                    type = instanceObj.get("MetricType").toString();
214
                    count = instanceObj.get("Count").toString();
215
                    //System.out.println(oai + " : " + period + " : " + count);
216

  
217
                    preparedStatement.setString(1, "IRUS-UK");
218
                    preparedStatement.setString(2, "opendoar____::" + opendoar);
219
                    preparedStatement.setString(3, oai);
220
                    preparedStatement.setString(4, period);
221
                    preparedStatement.setString(5, type);
222
                    preparedStatement.setInt(6, Integer.parseInt(count));
223
                    preparedStatement.addBatch();
224
                    batch_size++;
225
                    if(batch_size == 10000){
226
                        preparedStatement.executeBatch();
227
                        conn.commit();
228
                        batch_size = 0;
229
                    }
230
                }
231
                //break;
232
            }
233
            //break;
234
        }
235

  
236
        preparedStatement.executeBatch();
237
        conn.commit();
238
        conn.close();
239
    }
240

  
241
    private String getJson(String url,String username, String password) throws Exception {
242
        //String cred=username+":"+password;
243
        //String encoded = new sun.misc.BASE64Encoder().encode (cred.getBytes());
244
        try {
245
            URL website = new URL(url);
246
            URLConnection connection = website.openConnection();
247
            //connection.setRequestProperty ("Authorization", "Basic "+encoded);
248
            StringBuilder response;
249
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
250
                response = new StringBuilder();
251
                String inputLine;
252
                while ((inputLine = in.readLine()) != null) {
253
                    response.append(inputLine);
254
                    response.append("\n");
255
                }
256
            }
257
            return response.toString();
258
        }catch (Exception e){
259
            log.error("Failed to get URL: " + e);
260
            //System.out.println("Failed to get URL: " + e);
261
            return null;
262
            //throw new Exception("Failed to get URL: " + e.toString(), e);
263
        }
264
    }
265

  
266
    /*
267
    private void flushString(String data, String destination) throws Exception {
268
        FSDataOutputStream fin;
269
        try {
270
            FileSystem fs = FileSystem.get(new Configuration());
271
            fin = fs.create(new Path(destination), true);
272
            fin.write(data.getBytes());
273
            fin.close();
274
        } catch (Exception e) {
275
            log.error("Failed  to write exported data to a file : ", e);
276
            throw new Exception("Failed  to write exported data to a file : " + e.toString(), e);
277
        }
278
    }
279
    */
280
}
modules/dnet-openaire-usage-stats-export/trunk/src/main/java/eu/dnetlib/usagestats/export/SarcStats.java
1
package eu.dnetlib.usagestats.export;
2

  
3
import java.io.*;
4
//import java.io.BufferedReader;
5
//import java.io.InputStreamReader;
6
import java.net.URL;
7
import java.net.URLConnection;
8
import java.sql.ResultSet;
9
import java.text.SimpleDateFormat;
10
import java.util.Date;
11
import java.util.Calendar;
12

  
13
import java.sql.Connection;
14
import java.sql.DriverManager;
15
import java.sql.PreparedStatement;
16
import java.sql.Statement;
17

  
18
import org.json.simple.JSONArray;
19
import org.json.simple.JSONObject;
20
import org.json.simple.parser.JSONParser;
21

  
22
import org.apache.log4j.Logger;
23
/**
24
 * Created by tsampikos on 27/6/2017.
25
 */
26

  
27
public class SarcStats {
28

  
29
    private final String dbUrl;
30
    private final String dbSchema;
31
    private final String dbUserName;
32
    private final String dbPassword;
33

  
34
    private Connection conn = null;
35
    private Statement stmt = null;
36

  
37
    private final Logger log = Logger.getLogger(this.getClass());
38

  
39
    public SarcStats(String dbUrl, String dbUsername, String dbPassword) throws Exception {
40
        this.dbUrl = dbUrl;
41
        this.dbSchema = "shadow";
42
        this.dbUserName = dbUsername;
43
        this.dbPassword = dbPassword;
44

  
45
        connectDB();
46
        createTables();
47
    }
48

  
49
    private void connectDB() throws Exception {
50
        try {
51
            Class.forName("org.postgresql.Driver");
52
            conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
53

  
54
            stmt = conn.createStatement();
55
            String sqlSetSearchPath = "SET search_path TO " + dbSchema + ";";
56
            stmt.executeUpdate(sqlSetSearchPath);
57

  
58
            //log.info("Opened database successfully")
59

  
60
        } catch (Exception e) {
61
            //log.error("Connect to db failed: " + e);
62
            throw new Exception("Failed to connect to db: " + e.toString(), e);
63
        }
64
    }
65

  
66
    private void createTables() throws Exception {
67
        try {
68

  
69
            stmt = conn.createStatement();
70
            String sqlCreateTableSushiLog = "CREATE TABLE IF NOT EXISTS sushilog(source TEXT, repository TEXT, rid TEXT, date TEXT, metric_type TEXT, count INT, PRIMARY KEY(source, repository, rid, date, metric_type));";
71
            stmt.executeUpdate(sqlCreateTableSushiLog);
72

  
73
            //String sqlCopyPublicSushiLog="INSERT INTO sushilog SELECT * FROM public.sushilog;";
74
            //stmt.executeUpdate(sqlCopyPublicSushiLog);
75

  
76
            String sqlcreateRuleSushiLog = "CREATE OR REPLACE RULE ignore_duplicate_inserts AS " +
77
                    " ON INSERT TO sushilog " +
78
                    " WHERE (EXISTS ( SELECT sushilog.source, sushilog.repository," +
79
                    "sushilog.rid, sushilog.date " +
80
                    "FROM sushilog " +
81
                    "WHERE sushilog.source = new.source AND sushilog.repository = new.repository AND sushilog.rid = new.rid AND sushilog.date = new.date AND sushilog.metric_type = new.metric_type)) DO INSTEAD NOTHING;";
82
            stmt.executeUpdate(sqlcreateRuleSushiLog);
83

  
84
            stmt.close();
85
            conn.close();
86
            log.info("Sushi Tables Created");
87
        } catch (Exception e) {
88
            log.error("Failed to create tables: " + e);
89
            throw new Exception("Failed to create tables: " + e.toString(), e);
90
        }
91
    }
92

  
93

  
94
    public void processSarc() throws Exception {
95
        processARReport("https://revistas.rcaap.pt/motricidade/sushiLite/v1_7/", "1646-107X");
96
        processARReport("http://revistas.rcaap.pt/antropologicas/sushiLite/v1_7/", "0873-819X");
97
        processARReport("https://revistas.rcaap.pt/interaccoes/sushiLite/v1_7/", "1646-2335");
98
        processARReport("http://revistas.rcaap.pt/cct/sushiLite/v1_7/", "2182-3030");
99
        processARReport("http://actapediatrica.spp.pt/sushiLite/v1_7/", "0873-9781");
100
        //processARReport("http://revistas.rcaap.pt/boletimspm/sushiLite/v1_7/");
101
        processARReport("https://revistas.rcaap.pt/sociologiapp/sushiLite/v1_7/", "0873-6529");
102
        processARReport("http://revistas.rcaap.pt/finisterra/sushiLite/v1_7/", "0430-5027");
103
        processARReport("http://revistas.rcaap.pt/sisyphus/sushiLite/v1_7/", "2182-8474");
104
        processARReport("https://revistas.rcaap.pt/anestesiologia/sushiLite/v1_7/", "0871-6099");
105
        processARReport("https://revistas.rcaap.pt/rpe/sushiLite/v1_7/", "0871-9187");
106
        processARReport("https://revistas.rcaap.pt/psilogos/sushiLite/v1_7/", "1646-091X");
107
        processARReport("https://revistas.rcaap.pt/juridica/sushiLite/v1_7/", "2183-5799");
108
        processARReport("http://revistas.rcaap.pt/ecr/sushiLite/v1_7/", "1647-2098");
109
        processARReport("https://revistas.rcaap.pt/nascercrescer/sushiLite/v1_7/", "0872-0754");
110
        processARReport("http://revistas.rcaap.pt/cea/sushiLite/v1_7/", "1645-3794");
111
        processARReport("http://revistas.rcaap.pt/proelium/sushiLite/v1_7/", "1645-8826");
112
        processARReport("http://revistas.rcaap.pt/millenium/sushiLite/v1_7/", "0873-3015");
113

  
114
        //processARReport("http://www.bad.pt/publicacoes/index.php/cadernos/sushiLite/v1_7/");
115
    }
116

  
117
    public void sarcStats() throws Exception {
118
        if (conn.isClosed())
119
            connectDB();
120

  
121
        stmt = conn.createStatement();
122
        conn.setAutoCommit(false);
123

  
124
        //String sql = "SELECT s.source, d.id AS repository_id, ro.id as result_id, extract('year' from s.date::date) ||'/'|| LPAD(CAST(extract('month' from s.date::date) AS VARCHAR), 2, '0') as date, s.count, '0' INTO downloads_stats FROM sushilog s, datasource_oids d, result_oids ro WHERE s.repository=d.orid AND s.rid=ro.orid AND metric_type='ft_total'";
125
        String sql = "INSERT INTO downloads_stats SELECT s.source, d.id AS repository_id, ro.id as result_id, extract('year' from s.date::date) ||'/'|| LPAD(CAST(extract('month' from s.date::date) AS VARCHAR), 2, '0') as date, s.count, '0' FROM sushilog s, datasource_oids d, datasource_results dr, result_pids ro WHERE d.orid LIKE '%' || s.repository || '%' AND dr.id=d.id AND dr.result=ro.id AND s.rid=ro.pid AND ro.type='doi' AND metric_type='ft_total' AND s.source='SARC-OJS';";
126
        stmt.executeUpdate(sql);
127

  
128

  
129
        stmt.close();
130
        conn.commit();
131
        conn.close();
132
    }
133

  
134
    public void processARReport(String url, String issn) throws Exception {
135
        log.info("Processing SARC! issn: " + issn + " with url: " + url);
136
        if (conn.isClosed())
137
            connectDB();
138

  
139
        conn.setAutoCommit(false);
140

  
141
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM");
142

  
143
        Calendar start = Calendar.getInstance();
144
        start.set(Calendar.YEAR, 2016);
145
        start.set(Calendar.MONTH, Calendar.JANUARY);
146
        //start.setTime(simpleDateFormat.parse("2016-01"));
147

  
148
        Calendar end = Calendar.getInstance();
149
        end.add(Calendar.DAY_OF_MONTH, -1);
150

  
151
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
152
        PreparedStatement st = conn.prepareStatement("SELECT max(date) FROM public.sushilog WHERE repository=?;");
153
        st.setString(1, issn);
154
        ResultSet rs_date = st.executeQuery();
155
        while(rs_date.next()){
156
            if(rs_date.getString(1) != null && !rs_date.getString(1).equals("null") && !rs_date.getString(1).equals("")) {
157
                start.setTime(sdf.parse(rs_date.getString(1)));
158
            }
159
        }
160
        rs_date.close();
161

  
162
        PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO sushilog (source, repository, rid, date, metric_type, count) VALUES (?,?,?,?,?,?)");
163
        int batch_size = 0;
164

  
165
        while(start.before(end)){
166
            //String reportUrl = "http://irus.mimas.ac.uk/api/sushilite/v1_7/GetReport/?Report=IR1&Release=4&RequestorID=OpenAIRE&BeginDate=" + simpleDateFormat.format(start.getTime()) + "&EndDate=" + simpleDateFormat.format(start.getTime()) + "&RepositoryIdentifier=opendoar%3A" + opendoar + "&ItemIdentifier=&ItemDataType=&hasDOI=&Granularity=Monthly&Callback=";
167
            String reportUrl = url + "GetReport/?Report=AR1&Format=json&BeginDate=" + simpleDateFormat.format(start.getTime()) + "&EndDate=" + simpleDateFormat.format(start.getTime());
168
            //System.out.println(reportUrl);
169
            start.add(Calendar.MONTH, 1);
170

  
171
            String text = getJson(reportUrl, "", "");
172
            if(text == null){
173
                continue;
174
            }
175

  
176
            /*
177
            PrintWriter wr = new PrintWriter(new FileWriter("logs/" + simpleDateFormat.format(start.getTime()) + ".json"));
178
            wr.print(text);
179
            wr.close();
180
            */
181

  
182
            JSONParser parser = new JSONParser();
183
            JSONObject jsonObject = (JSONObject) parser.parse(text);
184
            jsonObject = (JSONObject) jsonObject.get("sc:ReportResponse");
185
            jsonObject = (JSONObject) jsonObject.get("sc:Report");
186
            if(jsonObject == null){
187
                continue;
188
            }
189
            jsonObject = (JSONObject) jsonObject.get("c:Report");
190
            jsonObject = (JSONObject) jsonObject.get("c:Customer");
191
            Object obj = jsonObject.get("c:ReportItems");
192
            JSONArray jsonArray = new JSONArray();
193
            if(obj instanceof JSONObject){
194
                jsonArray.add(obj);
195
            }
196
            else{
197
                jsonArray = (JSONArray) obj;
198
                //jsonArray = (JSONArray) jsonObject.get("c:ReportItems");
199
            }
200
            if(jsonArray == null){
201
                continue;
202
            }
203

  
204
            String rid = "";
205
            for (Object aJsonArray : jsonArray) {
206
                JSONObject jsonObjectRow = (JSONObject) aJsonArray;
207
                JSONArray itemIdentifier = new JSONArray();
208
                obj = jsonObjectRow.get("c:ItemIdentifier");
209
                if(obj instanceof JSONObject){
210
                    itemIdentifier.add(obj);
211
                }
212
                else{
213
                    //JSONArray itemIdentifier = (JSONArray) jsonObjectRow.get("c:ItemIdentifier");
214
                    itemIdentifier = (JSONArray) obj;
215
                }
216
                for (Object identifier : itemIdentifier) {
217
                    JSONObject doi = (JSONObject) identifier;
218
                    if(doi.get("c:Type").toString().equals("DOI")){
219
                        rid = doi.get("c:Value").toString();
220
                        //System.out.println("DOI: " + rid);
221
                        break;
222
                    }
223
                }
224
                if(rid.isEmpty()){
225
                    continue;
226
                }
227

  
228
                JSONObject itemPerformance = (JSONObject) jsonObjectRow.get("c:ItemPerformance");
229
                //for (Object perf : itemPerformance) {
230
                JSONObject performance = (JSONObject) itemPerformance;
231
                JSONObject periodObj = (JSONObject) performance.get("c:Period");
232
                String period = periodObj.get("c:Begin").toString();
233
                JSONObject instanceObj = (JSONObject) performance.get("c:Instance");
234
                String type = instanceObj.get("c:MetricType").toString();
235
                String count = instanceObj.get("c:Count").toString();
236
                //System.out.println(rid + " : " + period + " : " + count);
237

  
238
                preparedStatement.setString(1, "SARC-OJS");
239
                preparedStatement.setString(2, issn);
240
                //preparedStatement.setString(2, url);
241
                preparedStatement.setString(3, rid);
242
                preparedStatement.setString(4, period);
243
                preparedStatement.setString(5, type);
244
                preparedStatement.setInt(6, Integer.parseInt(count));
245
                preparedStatement.addBatch();
246
                batch_size++;
247
                if(batch_size == 10000){
248
                    preparedStatement.executeBatch();
249
                    conn.commit();
250
                    batch_size = 0;
251
                }
252
                //}
253

  
254
                //break;
255
            }
256
            //break;
257
        }
258

  
259
        preparedStatement.executeBatch();
260
        conn.commit();
261
        conn.close();
262
    }
263

  
264
    private String getJson(String url,String username, String password) throws Exception {
265
        //String cred=username+":"+password;
266
        //String encoded = new sun.misc.BASE64Encoder().encode (cred.getBytes());
267
        try {
268
            URL website = new URL(url);
269
            URLConnection connection = website.openConnection();
270
            //connection.setRequestProperty ("Authorization", "Basic "+encoded);
271
            StringBuilder response;
272
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
273
                response = new StringBuilder();
274
                String inputLine;
275
                while ((inputLine = in.readLine()) != null) {
276
                    response.append(inputLine);
277
                    response.append("\n");
278
                }
279
            }
280
            return response.toString();
281
        }catch (Exception e){
282
            log.error("Failed to get URL: " + e);
283
            //System.out.println("Failed to get URL: " + e);
284
            return null;
285
            //throw new Exception("Failed to get URL: " + e.toString(), e);
286
        }
287
    }
288

  
289
    /*
290
    private void flushString(String data, String destination) throws Exception {
291
        FSDataOutputStream fin;
292
        try {
293
            FileSystem fs = FileSystem.get(new Configuration());
294
            fin = fs.create(new Path(destination), true);
295
            fin.write(data.getBytes());
296
            fin.close();
297
        } catch (Exception e) {
298
            log.error("Failed  to write exported data to a file : ", e);
299
            throw new Exception("Failed  to write exported data to a file : " + e.toString(), e);
300
        }
301
    }
302
    */
303
}

Also available in: Unified diff