From e667fed660fa4249abce0bdd88ffd28b84a5abb0 Mon Sep 17 00:00:00 2001 From: dimitrispie Date: Mon, 24 Oct 2022 12:06:15 +0300 Subject: [PATCH] Changes to include a sample report for IR --- .../SushiLiteControllerSample.java | 228 ++ .../UsageStatsRepositorySample.java | 3151 +++++++++++++++++ .../services/SushiLiteServiceImplSample.java | 1309 +++++++ .../services/SushiLiteServiceSample.java | 40 + 4 files changed, 4728 insertions(+) create mode 100755 src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteControllerSample.java create mode 100755 src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepositorySample.java create mode 100755 src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImplSample.java create mode 100755 src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceSample.java diff --git a/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteControllerSample.java b/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteControllerSample.java new file mode 100755 index 0000000..9357d94 --- /dev/null +++ b/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteControllerSample.java @@ -0,0 +1,228 @@ +package eu.dnetlib.usagestats.controllers; + +import com.fasterxml.jackson.databind.ObjectMapper; +import eu.dnetlib.usagestats.services.SushiLiteService; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Error_Model; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.util.FileCopyUtils; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by D.Pierrakos + */ +@RestController +class SushiLiteController { + + private final Logger log = Logger.getLogger(this.getClass()); + + private final SushiLiteService sushiLiteService; + @Value("${download.folder}") + private String download_folder; + + public SushiLiteController(SushiLiteService sushiLiteService) { + this.sushiLiteService = sushiLiteService; + } + + @RequestMapping(value = "/sushilite/r5/status", method = RequestMethod.GET) + public String getReportStatus() { + log.info("COUNTER Report status request "); + return sushiLiteService.displayReportStatus(); + } + + @RequestMapping(value = "/sushilite/r5/reports", method = RequestMethod.GET) + public String getReportSupported() { + log.info("COUNTER Supported Reports request "); + return sushiLiteService.displayReportsSupported(); + } + + @RequestMapping(value = "/sushilite/r5/members", method = RequestMethod.GET) + public String getMembers() { + log.info("COUNTER Members request "); + return sushiLiteService.displayConsortiumMemberList(); + } + + @RequestMapping(value = "/sushilite/r5/reports/pr_p1", method = RequestMethod.GET) + public String getReportPR_P1( + @RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier, + @RequestParam(value = "RequestorID", defaultValue = "anonymous") String requestorId, + @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, + @RequestParam(value = "EndDate", defaultValue = "") String endDate) { + log.info("COUNTER PR_P1 Report request for repository " + repositoryIdentifier); + return sushiLiteService.displayReportPR_P1(requestorId, repositoryIdentifier, beginDate, endDate); + } + + @RequestMapping(value = "/sushilite/r5/reports/ir", method = RequestMethod.GET) + public ResponseEntity getReportΙR( + @RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier, + @RequestParam(value = "ItemIdentifier", defaultValue = "") String itemIdentifier, + @RequestParam(value = "RequestorID", defaultValue = "anonymous") String requestorId, + @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, + @RequestParam(value = "EndDate", defaultValue = "") String endDate, + @RequestParam(value = "MetricType", defaultValue = "") List metricType, + @RequestParam(value = "DataType", defaultValue = "") String dataType, + @RequestParam(value = "Granularity", defaultValue = "Monthly") String granularity) { + log.info("COUNTER ΙR Report request for repository " + repositoryIdentifier + " and for item " + itemIdentifier); + class BuildReportWithTimeout implements Callable { + + @Override + public ResponseEntity call() throws Exception { + String report = sushiLiteService.displayReportIR(requestorId, repositoryIdentifier, itemIdentifier, beginDate, endDate, metricType, dataType, granularity); + if (report.indexOf(".zip") < 0) { + return new ResponseEntity<>(report, HttpStatus.OK); + } else { + ObjectMapper objectMapper = new ObjectMapper(); + //String compressedOutput = "
 {\"Report\":\"IR\", \"Description\":\"Compressed Report Due to large number of records\", \"URL To Download Report from: \":\"" + report + "\"} 
"; + SUSHI_Error_Model errorModel = new SUSHI_Error_Model("100000", "Notice", "Compressed IR report due to a large number of records", + null, report); + String compressedOutput = "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorModel) + "
"; + return new ResponseEntity<>(compressedOutput, HttpStatus.OK); + } + } + } + final ExecutorService executor = Executors.newSingleThreadExecutor(); + final Future future = executor.submit(new BuildReportWithTimeout()); + + try { + return future.get(2, TimeUnit.MINUTES); + } catch (InterruptedException ie) { + String compressedOutput = "
 {\"Report\":\"IR\", \"Description\":\"Large Report Timeout. Please contact usagecounts@openaire.eu for requests" + "\"} 
"; + return new ResponseEntity<>(compressedOutput, HttpStatus.OK); + } catch (ExecutionException ee) { + /* Handle the error. Or ignore it. */ + } catch (TimeoutException te) { + String compressedOutput = "
 {\"Report\":\"IR\", \"Description\":\"Large Report Timeout. Please contact usagecounts@openaire.eu for requests" + "\"} 
"; + return new ResponseEntity<>(compressedOutput, HttpStatus.OK); + + } + if (!executor.isTerminated()) { + executor.shutdownNow(); + } + + return null; +// String report = sushiLiteService.displayReportIR(requestorId, repositoryIdentifier, itemIdentifier, beginDate, endDate, metricType, dataType, granularity); +// +// if (report.indexOf(".zip") < 0) { +// return new ResponseEntity<>(report, HttpStatus.OK); +// } else { +// +// String compressedOutput = "
 {\"Report\":\"IR\", \"Description\":\"Compressed Report Due to large number of records\", \"URL To Download Report from: \":\"" + report + "\"} 
"; +// return new ResponseEntity<>(compressedOutput, HttpStatus.OK); +// } + } + + @RequestMapping(value = "/sushilite/r5/reports/dsr", method = RequestMethod.GET) + public ResponseEntity getReportDSR( + @RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier, + @RequestParam(value = "DatasetIdentifier", defaultValue = "") String itemIdentifier, + @RequestParam(value = "RequestorID", defaultValue = "anonymous") String requestorId, + @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, + @RequestParam(value = "EndDate", defaultValue = "") String endDate, + @RequestParam(value = "MetricType", defaultValue = "") List metricType, + @RequestParam(value = "Granularity", defaultValue = "Monthly") String granularity) throws Exception { + log.info("COUNTER DSR Report request for repository " + repositoryIdentifier + " and for item " + itemIdentifier); + + class BuildReportWithTimeout implements Callable { + + @Override + public ResponseEntity call() throws Exception { + String report = sushiLiteService.displayReportDSR(requestorId, repositoryIdentifier, itemIdentifier, beginDate, endDate, metricType, granularity); + if (report.indexOf(".zip") < 0) { + return new ResponseEntity<>(report, HttpStatus.OK); + } else { + + String compressedOutput = "
 {\"Report\":\"DSR\", \"Description\":\"Compressed Report Due to large number of records\", \"URL To Download Report from: \":\"" + report + "\"} 
"; + return new ResponseEntity<>(compressedOutput, HttpStatus.OK); + } + } + } + final ExecutorService executor = Executors.newSingleThreadExecutor(); + final Future future = executor.submit(new BuildReportWithTimeout()); + + try { + return future.get(2, TimeUnit.MINUTES); + } catch (InterruptedException ie) { + String compressedOutput = "
 {\"Report\":\"DSR\", \"Description\":\"Large Report Timeout. Please contact usagecounts@openaire.eu for requests" + "\"} 
"; + return new ResponseEntity<>(compressedOutput, HttpStatus.OK); + } catch (ExecutionException ee) { + /* Handle the error. Or ignore it. */ + } catch (TimeoutException te) { + String compressedOutput = "
 {\"Report\":\"DSR\", \"Description\":\"Large Report Timeout. Please contact usagecounts@openaire.eu for requests" + "\"} 
"; + return new ResponseEntity<>(compressedOutput, HttpStatus.OK); + + } + if (!executor.isTerminated()) { + executor.shutdownNow(); + } + + return null; +// String report = sushiLiteService.displayReportDSR(requestorId, repositoryIdentifier, itemIdentifier, beginDate, endDate, metricType, granularity); +// if (report.indexOf(".zip") < 0) { +// return new ResponseEntity<>(report, HttpStatus.OK); +// } else { +// +// String compressedOutput = "
 {\"Report\":\"DSR\", \"Description\":\"Compressed Report Due to large number of records\", \"URL To Download Report from: \":\"" + report + "\"} 
"; +// return new ResponseEntity<>(compressedOutput, HttpStatus.OK); +// } + } + + @RequestMapping(value = "/download/{file_name}", method = RequestMethod.GET) + public void downloadFile(HttpServletResponse response, @PathVariable("file_name") String filetoDownload) throws IOException { + File file = new File(download_folder + "/" + filetoDownload); + log.info("File downloaded at " + file.getAbsolutePath()); + String mimeType = "application/octet-stream"; + + response.setContentType(mimeType); + + /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ + response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); + response.setContentLength((int) file.length()); + InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); + FileCopyUtils.copy(inputStream, response.getOutputStream()); + } + + @RequestMapping(value = "/sushilite/r5/reports/pr", method = RequestMethod.GET) + public ResponseEntity getReportPR(@RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier, + @RequestParam(value = "RequestorID", defaultValue = "anonymous") String requestorId, + @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, + @RequestParam(value = "EndDate", defaultValue = "") String endDate, + @RequestParam(value = "MetricType", defaultValue = "") String metricType, + @RequestParam(value = "DataType", defaultValue = "") String dataType, + @RequestParam(value = "Granularity", defaultValue = "Monthly") String granularity) throws InterruptedException, Exception { + + String report = sushiLiteService.displayReportPR(requestorId, repositoryIdentifier, beginDate, endDate, metricType, dataType, granularity); + + if (report.indexOf(".zip") < 0) { + return new ResponseEntity<>(report, HttpStatus.OK); + } else { + + String compreessedOutput = "
 {\"Report\":\"PR\", \"Description\":\"Compressed Report Due to large number of records\", \"URL To Download Report from: \":\"" + report + "\"} 
"; + + return new ResponseEntity<>(compreessedOutput, HttpStatus.OK); + } + } +} diff --git a/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepositorySample.java b/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepositorySample.java new file mode 100755 index 0000000..f55ad9b --- /dev/null +++ b/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepositorySample.java @@ -0,0 +1,3151 @@ +package eu.dnetlib.usagestats.repositories; + +import org.apache.log4j.Logger; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import com.fasterxml.jackson.databind.ObjectMapper; +import eu.dnetlib.usagestats.portal.CountryRepositories; +import eu.dnetlib.usagestats.portal.CountryUsageStats; +import eu.dnetlib.usagestats.portal.CountryUsageStatsAll; + +import eu.dnetlib.usagestats.portal.MonthlyStats; +import eu.dnetlib.usagestats.portal.MonthlyUsageStats; +import eu.dnetlib.usagestats.portal.RepositoryStats; +import eu.dnetlib.usagestats.portal.TotalStats; +import eu.dnetlib.usagestats.portal.TotalStatsReposViewsDownloads; +import eu.dnetlib.usagestats.portal.UsageStats; +import eu.dnetlib.usagestats.portal.YearlyStats; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Dataset_Identifiers; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Dataset_Performance; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Dataset_Usage; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Item_Identifiers; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Item_Performance; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Item_Usage; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Platform_Usage; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Consortium_Member_List; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Org_Identifiers; + +import org.apache.commons.dbutils.DbUtils; + +import javax.sql.DataSource; + +import java.security.MessageDigest; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import org.springframework.beans.factory.annotation.Value; + +/** + * Created by D.Pierrakos + */ +@Repository +public class UsageStatsRepository { + + private final DataSource usageStatsDB; + + private final HashOperations jedis; + + private final Logger log = Logger.getLogger(this.getClass()); + @Value("${prod.statsdb}") + private String statsDB; + @Value("${prod.usagestatsImpalaDB}") + private String usagestatsImpalaDB; + + public UsageStatsRepository(DataSource usageStatsDB, + RedisTemplate redisTemplate) { + this.usageStatsDB = usageStatsDB; + this.jedis = redisTemplate.opsForHash(); + } + + private static String MD5(String string) throws java.security.NoSuchAlgorithmException { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(string.getBytes()); + + byte byteData[] = md.digest(); + StringBuilder sb = new StringBuilder(); + for (byte aByteData : byteData) { + sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1)); + } + + return sb.toString(); + } + + private static String toJson(Object o) throws com.fasterxml.jackson.core.JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.writeValueAsString(o); + } + + private static UsageStats fromJson(String string) throws java.io.IOException { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(string, UsageStats.class); + } + + /* + private static List reportItemsFromJson(String string) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(string, objectMapper.getTypeFactory().constructCollectionType(List.class, COUNTER_Platform_Usage.class)); + } + */ + public List executeMontlyUsageStats(String query) { + List montlhyList = new ArrayList(); + + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + try { + connection = usageStatsDB.getConnection(); + log.info(connection.toString()); + st = connection.prepareStatement(query); + log.info(st.toString()); + rs = st.executeQuery(); + while (rs.next()) { + MonthlyUsageStats monthlyUsageStats = new MonthlyUsageStats(); + monthlyUsageStats.addDate(rs.getString(1)); + monthlyUsageStats.addDownloads(rs.getString(2)); + monthlyUsageStats.addViews(rs.getString(3)); + montlhyList.add(monthlyUsageStats); + } + + } catch (Exception e) { + System.out.println(e); + } + + try { + jedis.put("test", "result", toJson(montlhyList)); + jedis.put("test", "persistent", "false"); + jedis.put("test", "fetchMode", "3"); + } catch (Exception e) { + System.out.println(e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + + return montlhyList; + } + + public TotalStatsReposViewsDownloads executeTotalStatsReposViewsDownloads( + String query) { + TotalStatsReposViewsDownloads totalStatsReposViewsDownlads = new TotalStatsReposViewsDownloads(); + + String total_repos = " "; + String views = " "; + String downloads = " "; + String redis_key = ""; + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + try { + connection = usageStatsDB.getConnection(); + log.info(connection.toString()); + st = connection.prepareStatement(query); + log.info(st.toString()); + rs = st.executeQuery(); + redis_key = MD5(st.toString()); + while (rs.next()) { + totalStatsReposViewsDownlads.addRepositories(rs.getString(1)); + totalStatsReposViewsDownlads.addViews(rs.getString(2)); + totalStatsReposViewsDownlads.addDownloads(rs.getString(3)); + } + + } catch (Exception e) { + System.out.println(e); + } + + try { + jedis.put(redis_key, "result", toJson(totalStatsReposViewsDownlads)); + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "fetchMode", "3"); + } catch (Exception e) { + System.out.println(e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + + return totalStatsReposViewsDownlads; + } + + public CountryUsageStatsAll executeCountryUsageStats(String query) { + CountryUsageStatsAll countryListAll = new CountryUsageStatsAll(); + + List countryList = new ArrayList(); + + String date = " "; + String total_repos = " "; + String views = " "; + String downloads = " "; + String redis_key = "redis_key"; + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + int total_views = 0; + int total_downloads = 0; + + try { + connection = usageStatsDB.getConnection(); + log.info(connection.toString()); + st = connection.prepareStatement(query); + log.info(st.toString()); + rs = st.executeQuery(); + redis_key = MD5(st.toString()); + while (rs.next()) { + CountryUsageStats countryUsageStats = new CountryUsageStats(); + countryUsageStats.addCountry(rs.getString(1)); + countryUsageStats.addTotalRepos(rs.getString(2)); + countryUsageStats.addViews(rs.getString(3)); + countryUsageStats.addDownloads(rs.getString(4)); + total_views += Integer.parseInt(rs.getString(3)); + total_downloads += Integer.parseInt(rs.getString(4)); + + countryList.add(countryUsageStats); + } + countryListAll.addViewsAll(Integer.toString(total_views)); + countryListAll.addDownloadsAll(Integer.toString(total_downloads)); + + countryListAll.addCountryUsageStats(countryList); + + } catch (Exception e) { + log.info(e); + System.out.println(e); + } + + try { + jedis.put(redis_key, "result", toJson(countryListAll)); + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "fetchMode", "3"); + } catch (Exception e) { + System.out.println(e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return countryListAll; + } + + public CountryUsageStats executeCountryUsageStats(String query, + String country) { + CountryUsageStats countryUsageStats = new CountryUsageStats(); + String total_repos = " "; + String views = " "; + String downloads = " "; + String redis_key = ""; + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + int total_views = 0; + int total_downloads = 0; + + try { + connection = usageStatsDB.getConnection(); + st = connection.prepareStatement(query); + + redis_key = MD5(st.toString()); + //st.setString(1, country); + log.info(st.toString()); + rs = st.executeQuery(); + + while (rs.next()) { + countryUsageStats.addCountry(country); + countryUsageStats.addTotalRepos(rs.getString(1)); + countryUsageStats.addViews(rs.getString(2)); + countryUsageStats.addDownloads(rs.getString(3)); + + } + + } catch (Exception e) { + log.info(e); + System.out.println(e); + } + + try { + jedis.put(redis_key, "result", toJson(countryUsageStats)); + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "fetchMode", "3"); + } catch (Exception e) { + System.out.println(e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return countryUsageStats; + } + + public List executeCountryRepositories(String query) { + + List countryReposList = new ArrayList(); + + String country = " "; + String repository = " "; + String redis_key = ""; + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + try { + connection = usageStatsDB.getConnection(); + log.info(connection.toString()); + st = connection.prepareStatement(query); + log.info(st.toString()); + rs = st.executeQuery(); + redis_key = MD5(st.toString()); + while (rs.next()) { + CountryRepositories countryRepository = new CountryRepositories(); + countryRepository.addCountry(rs.getString(1)); + countryRepository.addRepository(rs.getString(2)); + countryReposList.add(countryRepository); + } + + } catch (Exception e) { + System.out.println(e); + } + + try { + jedis.put(redis_key, "result", toJson(countryReposList)); + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "fetchMode", "3"); + } catch (Exception e) { + System.out.println(e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + + return countryReposList; + } + + public List executeMontlyUsageStatsForRepo(String query, + String datasourceId) { + List montlhyList = new ArrayList(); + + String redis_key = ""; + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + try { + connection = usageStatsDB.getConnection(); + st = connection.prepareStatement(query); + redis_key = MD5(st.toString()); + st.setString(1, datasourceId); + log.info(connection.toString()); + rs = st.executeQuery(); + while (rs.next()) { + MonthlyUsageStats monthlyUsageStats = new MonthlyUsageStats(); + monthlyUsageStats.addDate(rs.getString(1)); + monthlyUsageStats.addDownloads(rs.getString(2)); + monthlyUsageStats.addViews(rs.getString(3)); + montlhyList.add(monthlyUsageStats); + } + + } catch (Exception e) { + System.out.println(e); + } + + try { + jedis.put(redis_key, "result", toJson(montlhyList)); + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "fetchMode", "3"); + } catch (Exception e) { + System.out.println(e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + + return montlhyList; + } + + public UsageStats executeUsageStats(String query, List values, + String type) { + + UsageStats usageStats = new UsageStats(); + int total_views = 0; + int total_downloads = 0; + int page_views = 0; + int openaire_downloads = 0; + int openaire_views = 0; + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + try { + connection = usageStatsDB.getConnection(); + st = connection.prepareStatement(query); + int i = 1; + for (String s : values) { + st.setString(i, s); + i++; + } + + String redis_key = MD5(st.toString()); + + String redis_result = jedis.get(redis_key, "result"); + if (redis_result != null) { + return fromJson(redis_result); + } + + rs = st.executeQuery(); + if (type.equals("result")) { + while (rs.next()) { + if (rs.getString(1).equals("views") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) { + usageStats.addViews(new RepositoryStats(rs.getString(3), rs.getString(2), rs.getString(4), rs.getString(5))); + total_views += Integer.parseInt(rs.getString(4)); + openaire_views += Integer.parseInt(rs.getString(5)); + } else if (rs.getString(1).equals("downloads") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) { + usageStats.addDownloads(new RepositoryStats(rs.getString(3), rs.getString(2), rs.getString(4), "0")); + total_downloads += Integer.parseInt(rs.getString(4)); + openaire_downloads += Integer.parseInt(rs.getString(5)); + } else if (rs.getString(1).equals("pageviews") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) { + page_views = Integer.parseInt(rs.getString(4)); + } + } + usageStats.setTotal_views(Integer.toString(total_views)); + usageStats.setTotal_downloads(Integer.toString(total_downloads)); + usageStats.setPageViews(Integer.toString(page_views)); + usageStats.setTotal_openaire_views(Integer.toString(openaire_views)); + usageStats.setTotal_openaire_downloads(Integer.toString(openaire_downloads)); + } else if (type.equals("project") || type.equals("datasource")) { + while (rs.next()) { + if (rs.getString(1).equals("views") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) { + total_views += Integer.parseInt(rs.getString(2)); + openaire_views += Integer.parseInt(rs.getString(3)); + } else if (rs.getString(1).equals("downloads") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) { + total_downloads += Integer.parseInt(rs.getString(2)); + openaire_downloads += Integer.parseInt(rs.getString(3)); + } else if (rs.getString(1).equals("pageviews") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) { + page_views = Integer.parseInt(rs.getString(2)); + } + /* + else if (rs.getString(1).equals("openaire") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) { + openaire = Integer.parseInt(rs.getString(2)); + } + */ + + } + usageStats.setTotal_views(Integer.toString(total_views)); + usageStats.setTotal_downloads(Integer.toString(total_downloads)); + usageStats.setPageViews(Integer.toString(page_views)); + usageStats.setTotal_openaire_views(Integer.toString(openaire_views)); + usageStats.setTotal_openaire_downloads(Integer.toString(openaire_downloads)); + } + + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + //jedis.put(redis_key, "result", toString(usageStats)); + jedis.put(redis_key, "result", toJson(usageStats)); + jedis.put(redis_key, "fetchMode", "3"); + + } catch (Exception e) { + log.error("Cannot execute query2 : ", e); + + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return usageStats; + } + + public TotalStats executeTotalStats() { + TotalStats totalStats = null; + try { + String redis_result = jedis.get("total_stats", "result"); + if (redis_result != null) { + totalStats = fromJsonTotalStats(redis_result); + } else { + return updateTotalStats(); + } + } catch (Exception e) { + log.error("Cannot execute totalStats : ", e); + } + return totalStats; + } + + public TotalStats updateTotalStats() { + TotalStats totalStats = new TotalStats(); + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + HashMap> monthlyStatsMap = new HashMap<>(); + + try { + connection = usageStatsDB.getConnection(); + //st = connection.prepareStatement("SELECT count(distinct d.repository_id) AS repository, count(distinct d.result_id) AS items, sum(d.count) AS downloads, sum(v.count) AS views from public.downloads_stats d FULL OUTER JOIN public.views_stats v ON d.source=v.source AND d.repository_id=v.repository_id AND d.result_id=v.result_id AND d.date=v.date;"); + st = connection.prepareStatement("SELECT ndv(distinct repository_id) AS repository, ndv(distinct result_id) AS items, sum(downloads) AS downloads, sum(views) AS views FROM " + usagestatsImpalaDB + ".usage_stats;"); + rs = st.executeQuery(); + rs.next(); + totalStats.setRepositories(rs.getInt(1)); + totalStats.setItems(rs.getInt(2)); + totalStats.setDownloads(rs.getInt(3)); + totalStats.setViews(rs.getInt(4)); + rs.close(); + st.close(); + + //st = connection.prepareStatement("select coalesce(d.date,v.date) as month, count(distinct d.repository_id) as repository, count(distinct d.result_id) as items, sum(d.count) as downloads, sum(v.count) as views from public.downloads_stats d FULL OUTER JOIN public.views_stats v ON d.source=v.source AND d.repository_id=v.repository_id AND d.result_id=v.result_id AND d.date=v.date group by month order by month;"); + st = connection.prepareStatement("SELECT `date`, ndv(distinct repository_id) AS repository, ndv(distinct result_id) AS items, sum(downloads) AS downloads, sum(views) AS views FROM " + usagestatsImpalaDB + ".usage_stats GROUP BY `date` ORDER BY `date`;"); + rs = st.executeQuery(); + while (rs.next()) { + int year = Integer.parseInt(rs.getString(1).substring(0, 4)); + int month = Integer.parseInt(rs.getString(1).substring(5)); + MonthlyStats monthlyStats = new MonthlyStats(); + monthlyStats.setMonth(month); + monthlyStats.setRepositories(rs.getInt(2)); + monthlyStats.setItems(rs.getInt(3)); + monthlyStats.setDownloads(rs.getInt(4)); + monthlyStats.setViews(rs.getInt(5)); + + if (monthlyStatsMap.get(year) != null) { + monthlyStatsMap.get(year).add(monthlyStats); + } else { + List newList = new ArrayList<>(); + newList.add(monthlyStats); + monthlyStatsMap.put(year, newList); + + } + } + rs.close(); + st.close(); + + //st = connection.prepareStatement("SELECT COALESCE(SUBSTRING(d.date FROM 1 FOR 4), SUBSTRING(v.date FROM 1 FOR 4)) AS year, COUNT(DISTINCT d.repository_id) AS repository, COUNT(DISTINCT d.result_id) AS items, SUM(d.count) AS downloads, SUM(v.count) AS views FROM public.downloads_stats d FULL OUTER JOIN public.views_stats v ON d.source=v.source AND d.repository_id=v.repository_id AND d.result_id=v.result_id AND d.date=v.date GROUP BY year ORDER BY year;"); + st = connection.prepareStatement("SELECT SUBSTR(`date`,1,4) AS year, ndv(DISTINCT repository_id) AS repository, \n" + + "ndv(DISTINCT result_id) AS items, SUM(downloads) AS downloads, SUM(views) AS views \n" + + "FROM " + usagestatsImpalaDB + ".usage_stats GROUP BY year ORDER BY year;"); + rs = st.executeQuery(); + List yearlyStatsList = new ArrayList<>(); + while (rs.next()) { + YearlyStats yearlyStats = new YearlyStats(); + yearlyStats.setYear(rs.getInt(1)); + yearlyStats.setRepositories(rs.getInt(2)); + yearlyStats.setItems(rs.getInt(3)); + yearlyStats.setDownloads(rs.getInt(4)); + yearlyStats.setViews(rs.getInt(5)); + yearlyStats.setMonthlyStats(monthlyStatsMap.get(rs.getInt(1))); + yearlyStatsList.add(yearlyStats); + } + totalStats.setYearlyStats(yearlyStatsList); + jedis.put("total_stats", "result", toJson(totalStats)); + jedis.put("total_stats", "persistent", "false"); + + } catch (Exception e) { + log.error("Cannot execute totalStats : ", e); + + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return totalStats; + } + + private static TotalStats fromJsonTotalStats(String string) throws java.io.IOException { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(string, TotalStats.class); + } + + public String executeRepoId(String repositoryIdentifier, String report) { + PreparedStatement st = null; + Connection connection = null; + ResultSet rs = null; + log.info("database " + statsDB); + try { + connection = usageStatsDB.getConnection(); + String[] split = repositoryIdentifier.split(":"); + String openaire_id = "-1"; + switch (split[0].toLowerCase()) { + case "openaire": + if (!report.equals("jr1")) { + st = connection.prepareStatement("select id from " + statsDB + ".datasource where id=?"); + st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", "")); + } else { + st = connection.prepareStatement("select id from " + statsDB + ".datasource where id=? AND (type='Journal' OR type='Journal Aggregator/Publisher')"); + st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", "")); + } + + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + + case "opendoar": + if (!report.equals("jr1")) { + st = connection.prepareStatement("select id from " + statsDB + ".datasource_oids where oid=?"); + st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", "")); + } else { + st = connection.prepareStatement("select distinct d.id from " + statsDB + ".datasource d, " + statsDB + ".datasource_oids di where di.oid=? and d.id=di.id and (type='Journal' OR type='Journal Aggregator/Publisher')"); + st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", "")); + } + + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + case "issn": + st = connection.prepareStatement("select distinct d.id from " + statsDB + ".datasource d, " + statsDB + ".datasource_oids di, " + statsDB + ".datasource_results dr where d.id=dr.id and di.oid like ? and d.id=di.id and (type='Journal' OR type='Journal Aggregator/Publisher')"); + st.setString(1, "%" + repositoryIdentifier.replaceFirst(split[0] + ":", "") + "%"); + + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + default: + return "-1"; + } + } catch (Exception e) { + log.error("Repository id failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return "-1"; + } + + public String getInstitutionID(String repositoryIdentifier) { + PreparedStatement st = null; + Connection connection = null; + ResultSet rs = null; + log.info("database " + statsDB); + try { + connection = usageStatsDB.getConnection(); + String[] split = repositoryIdentifier.split(":"); + String openaire_id = "-1"; + switch (split[0].toLowerCase()) { + case "openaire": + st = connection.prepareStatement("select id from " + statsDB + ".datasource where id=?"); + st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", "")); + + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + + case "opendoar": + st = connection.prepareStatement("select id from " + statsDB + ".datasource_oids where oid=?"); + st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", "")); + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + case "issn": + st = connection.prepareStatement("select distinct d.id from " + statsDB + ".datasource d, " + statsDB + ".datasource_oids di, " + statsDB + ".datasource_results dr where d.id=dr.id and di.oid like ? and d.id=di.id and (type='Journal' OR type='Journal Aggregator/Publisher')"); + st.setString(1, "%" + repositoryIdentifier.replaceFirst(split[0] + ":", "") + "%"); + + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + default: + return "-1"; + } + } catch (Exception e) { + log.error("Repository id failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return "-1"; + } + + public void executeItemIR(List reportItems, String repositoryIdentifier, String itemIdentifier, Date beginDate, + Date endDate, List metricType, String itemDataType, String granularity) { + + String[] split = itemIdentifier.split(":"); + switch (split[0].toLowerCase()) { + case "oid": + executeOid(reportItems, repositoryIdentifier, itemIdentifier.replaceFirst(split[0] + ":", ""), beginDate, endDate, metricType, itemDataType, granularity); + break; + case "doi": + executeDoi(reportItems, repositoryIdentifier, itemIdentifier.replaceFirst(split[0] + ":", ""), beginDate, endDate, metricType, itemDataType, granularity); + break; +// case "openaire": +// executeOpenaire(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity); +// break; + default: + } + } + + public void executeItemDSR(List reportItems, String repositoryIdentifier, String itemIdentifier, Date beginDate, + Date endDate, List metricType, String granularity) { + + String[] split = itemIdentifier.split(":"); + switch (split[0].toLowerCase()) { + case "oid": + executeDSROid(reportItems, repositoryIdentifier, itemIdentifier.replaceFirst(split[0] + ":", ""), beginDate, endDate, metricType, granularity); + break; + case "doi": + executeDSRDoi(reportItems, repositoryIdentifier, itemIdentifier.replaceFirst(split[0] + ":", ""), beginDate, endDate, metricType, granularity); + break; +// case "openaire": +// executeOpenaire(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity); +// break; + default: + } + } + + private void executeOid(List reportItems, String repositoryIdentifier, + String oid, Date beginDate, Date endDate, List metricType, String itemDataType, String granularity) { + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + try { + connection = usageStatsDB.getConnection(); + //st = connection.prepareStatement("SELECT DISTINCT roid.id FROM public.result_oids roid, public.downloads_stats s WHERE s.result_id=roid.id AND roid.orid=? UNION SELECT DISTINCT roid.id FROM public.result_oids roid, public.views_stats s WHERE s.result_id=roid.id AND roid.orid=?"); + st = connection.prepareStatement("SELECT DISTINCT roid.id FROM " + statsDB + ".result_oids roid, " + usagestatsImpalaDB + ".usage_stats us WHERE us.result_id=roid.id AND roid.oid=?"); + st.setString(1, oid); + //st.setString(2, oid); + + rs = st.executeQuery(); + + while (rs.next()) { + if (repositoryIdentifier != null) { + executeBatchItemsIR(reportItems, repositoryIdentifier, rs.getString(1), beginDate, endDate, metricType, itemDataType, granularity); + } else { + executeItemsAllRepoIR(reportItems, rs.getString(1), beginDate, endDate, metricType, itemDataType, granularity); + } + } + connection.close(); + } catch (Exception e) { + log.error("Oid to OpenAIRE id failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + private void executeDoi(List reportItems, String repositoryIdentifier, + String doi, Date beginDate, Date endDate, List metricType, String itemDataType, String granularity) { + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + + try { + connection = usageStatsDB.getConnection(); + //st = connection.prepareStatement("SELECT DISTINCT poid.id FROM public.result_pids poid, public.downloads_stats s WHERE s.result_id=poid.id AND poid.type='doi' AND poid.pid=? UNION SELECT DISTINCT poid.id FROM public.result_pids poid, public.views_stats s WHERE s.result_id=poid.id AND poid.type='doi' AND poid.pid=?"); + st = connection.prepareStatement("SELECT DISTINCT poid.id FROM " + statsDB + ".result_pids poid, " + usagestatsImpalaDB + ".usage_stats us WHERE us.result_id=poid.id AND poid.type='Digital Object Identifier' AND poid.pid=?"); + st.setString(1, doi); + //st.setString(2, doi); + + rs = st.executeQuery(); + + while (rs.next()) { + if (repositoryIdentifier != null) { + executeBatchItemsIR(reportItems, repositoryIdentifier, rs.getString(1), beginDate, endDate, metricType, itemDataType, granularity); + } else { + executeItemsAllRepoIR(reportItems, rs.getString(1), beginDate, endDate, metricType, itemDataType, granularity); + } + } + } catch (Exception e) { + log.error("Doi to OpenAIRE id failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + private void executeDSROid(List reportItems, String repositoryIdentifier, + String oid, Date beginDate, Date endDate, List metricType, String granularity) { + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + try { + connection = usageStatsDB.getConnection(); + //st = connection.prepareStatement("SELECT DISTINCT roid.id FROM public.result_oids roid, public.downloads_stats s WHERE s.result_id=roid.id AND roid.orid=? UNION SELECT DISTINCT roid.id FROM public.result_oids roid, public.views_stats s WHERE s.result_id=roid.id AND roid.orid=?"); + st = connection.prepareStatement("SELECT DISTINCT roid.id FROM " + statsDB + ".result_oids roid, " + usagestatsImpalaDB + ".usage_stats us, " + + statsDB + ".result_classifications rc " + + "WHERE us.result_id=roid.id AND rc.id=us.result_id AND rc.type='Dataset' AND roid.oid=?"); + st.setString(1, oid); + //st.setString(2, oid); + + rs = st.executeQuery(); + + while (rs.next()) { + if (repositoryIdentifier != null) { + if (checkIfDatacite(repositoryIdentifier)) { + executeBatchItemsDSRDatacite(reportItems, repositoryIdentifier, rs.getString(1), beginDate, endDate, metricType, granularity); + } else { + executeBatchItemsDSR(reportItems, repositoryIdentifier, rs.getString(1), beginDate, endDate, metricType, granularity); + } + } else { +// if (checkIfDatacite(repositoryIdentifier)) { +// executeItemsAllRepoDSRDatacite(reportItems, rs.getString(1), beginDate, endDate, metricType, granularity); +// } else { + executeItemsAllRepoDSR(reportItems, rs.getString(1), beginDate, endDate, metricType, granularity); +// } + } + } + } catch (Exception e) { + log.error("Oid to OpenAIRE id failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + private void executeDSRDoi(List reportItems, String repositoryIdentifier, + String doi, Date beginDate, Date endDate, List metricType, String granularity) { + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + + try { + connection = usageStatsDB.getConnection(); + //st = connection.prepareStatement("SELECT DISTINCT poid.id FROM public.result_pids poid, public.downloads_stats s WHERE s.result_id=poid.id AND poid.type='doi' AND poid.pid=? UNION SELECT DISTINCT poid.id FROM public.result_pids poid, public.views_stats s WHERE s.result_id=poid.id AND poid.type='doi' AND poid.pid=?"); + st = connection.prepareStatement("SELECT DISTINCT poid.id FROM " + statsDB + ".result_pids poid, " + usagestatsImpalaDB + ".usage_stats us, " + + statsDB + ".result_classifications rc " + + "WHERE us.result_id=poid.id AND poid.type='Digital Object Identifier' AND rc.id=us.result_id AND rc.type='Dataset' AND poid.pid=?"); + st.setString(1, doi); + //st.setString(2, doi); + + rs = st.executeQuery(); + + while (rs.next()) { + if (repositoryIdentifier != null) { + if (checkIfDatacite(repositoryIdentifier)) { + executeBatchItemsDSRDatacite(reportItems, repositoryIdentifier, rs.getString(1), beginDate, endDate, metricType, granularity); + } else { + executeBatchItemsDSR(reportItems, repositoryIdentifier, rs.getString(1), beginDate, endDate, metricType, granularity); + } + } else { +// if (checkIfDatacite(repositoryIdentifier)) { +// executeItemsAllRepoDSRDatacite(reportItems, rs.getString(1), beginDate, endDate, metricType, granularity); +// } else { + executeItemsAllRepoDSR(reportItems, rs.getString(1), beginDate, endDate, metricType, granularity); +// } + } + } + } catch (Exception e) { + log.error("Doi to OpenAIRE id failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + public void executeBatchItemsPR(List reportItems, + String repositoryIdentifier, Date beginDate, + Date endDate, String metricType, String dataType, String granularity) { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); + String beginDateStr = postgresFormat.format(beginDate); + String endDateStr = postgresFormat.format(endDate); + + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + String platform = getInstitutionName(repositoryIdentifier); + log.info("Platform " + platform); + log.info("Data Type " + dataType); + + try { + connection = usageStatsDB.getConnection(); + if (granularity.equalsIgnoreCase("totals")) { + if (dataType.equals("") || dataType.equals("All")) { + st = connection.prepareStatement("SELECT rc.type, sum(us.total_item_requests) as total_item_requests, " + +"sum(us.total_item_investigations) as total_item_investigations, " + +"sum(us.unique_item_requests) as unique_item_requests, " + +"sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, " + + statsDB + ".result_classifications rc WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id GROUP BY rc.type order by rc.type ASC;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + COUNTER_Platform_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + while (rs.next()) { + reportItem = new COUNTER_Platform_Usage(platform, rs.getString(1), "Regular"); + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(4) : null, metricTypeValue[3] == true ? rs.getString(5) : null)); + reportItems.add(reportItem); + } + + } else { + st = connection.prepareStatement("SELECT rc.type, sum(us.total_item_requests) as total_item_requests, " + +"sum(us.total_item_investigations) as total_item_investigations, " + +"sum(us.unique_item_requests) as unique_item_requests, " + +"sum(us.unique_item_investigations) as unique_item_investigations " + +"FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, " + + statsDB + ".result_classifications rc WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.type=? AND rc.id=us.result_id GROUP BY rc.type order by rc.type ASC;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, dataType); + + rs = st.executeQuery(); + COUNTER_Platform_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Platform_Usage(platform, rs.getString(1), "Regular"); + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(4) : null, metricTypeValue[3] == true ? rs.getString(5) : null)); + reportItems.add(reportItem); + } + + } + } else if (granularity.equalsIgnoreCase("monthly")) { + if (dataType.equals("") || dataType.equals("All")) { + st = connection.prepareStatement("SELECT rc.type, us.`date`, sum(us.total_item_requests) as total_item_requests, " + +"sum(us.total_item_investigations) as total_item_investigations, " + +"sum(us.unique_item_requests) as unique_item_requests, " + +"sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, " + + statsDB + ".result_classifications rc WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id GROUP BY rc.type, us.`date` order by rc.type, us.`date` ASC;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Platform_Usage reportItem = null; + + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); + //iterate on data types + if (!datatype.equals(rs.getString(1))) { + if (reportItem != null) { + reportItems.add(reportItem); + } + reportItem = new COUNTER_Platform_Usage(platform, rs.getString(1), "Regular"); + datatype = rs.getString(1); + } + if (reportItem != null) { + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(5) : null, metricTypeValue[3] == true ? rs.getString(6) : null)); + + } + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + //add last report item + reportItems.add(reportItem); + } else { + st = connection.prepareStatement("SELECT rc.type, us.`date`, sum(us.total_item_requests) as total_item_requests, " + +"sum(us.total_item_investigations) as total_item_investigations, " + +"sum(us.unique_item_requests) as unique_item_requests, " + +"sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, " + + statsDB + ".result_classifications rc WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.type=? AND rc.id=us.result_id GROUP BY rc.type, us.`date` order by rc.type, us.`date` ASC;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, dataType); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Platform_Usage reportItem = null; + + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); + //iterate on data types + if (!datatype.equals(rs.getString(1))) { + if (reportItem != null) { + reportItems.add(reportItem); + } + reportItem = new COUNTER_Platform_Usage(platform, rs.getString(1), "Regular"); + datatype = rs.getString(1); + } + if (reportItem != null) { + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(5) : null, metricTypeValue[3] == true ? rs.getString(6) : null)); + } + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + //add last report item + reportItems.add(reportItem); + } + + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + public void executeBatchItemsPR_P1(List reportItems, + String repositoryIdentifier, Date beginDate, + Date endDate) { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); + String beginDateStr = postgresFormat.format(beginDate); + String endDateStr = postgresFormat.format(endDate); + String platform = getInstitutionName(repositoryIdentifier); + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + + try { + connection = usageStatsDB.getConnection(); + + st = connection.prepareStatement("SELECT us.`date`, sum(us.total_item_requests) as total_item_requests, " + +"sum(us.total_item_investigations) as total_item_investigations, " + +"sum(us.unique_item_requests) as unique_item_requests, " + +"sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us " + + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? GROUP BY us.`date` order by us.`date` ASC;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Platform_Usage reportItem = null; + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + reportItem = new COUNTER_Platform_Usage(platform, null, "Regular"); + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(1))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); + //iterate on data types + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(1))), report_dateFormat.format(endC.getTime()), + rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5))); + endC.setTime(postgresFormat.parse(rs.getString(1))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + if (reportItem.getItemPerformances().size() > 0) { + reportItems.add(reportItem); + } + + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + public void executeBatchItemsIR(List reportItems, + String repositoryIdentifier, String itemIdentifier, Date beginDate, + Date endDate, List metricType, String dataType, String granularity) throws Exception { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); + String beginDateStr = postgresFormat.format(beginDate); + String endDateStr = postgresFormat.format(endDate); + + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + String platform = getInstitutionName(repositoryIdentifier); + + if (itemIdentifier.equals("")) { + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { + if (dataType.equals("")) { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + + "openaire_prod_stats.result rs " + + "WHERE us.`date`>=? AND us.`date`<=? " + + "AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? " + + "GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY repo,type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + COUNTER_Item_Usage reportItem = null; + + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Item_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), null, "Regular"); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + + } else { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + + statsDB + ".result rs WHERE us.`date`>=? AND us.`date`<=? " + + "AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? and rc.type=? " + + "GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY repo,type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, dataType); + + rs = st.executeQuery(); + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Item_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), null, "Regular"); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + } + } else if (granularity.equalsIgnoreCase("monthly")) { + + if (dataType.equals("")) { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`,total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, us.`date` `date`,rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + + statsDB + ".result rs WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? " + + "GROUP BY us.`date`,rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp1, tpd " + + "WHERE tpd.id=resultid GROUP BY repo,`date`, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations"); + + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { + datatype = rs.getString(1); + } + + reportItem = new COUNTER_Item_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), null, "Regular"); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } else { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`,total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, us.`date` `date`,rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + + statsDB + ".result rs WHERE us.`date`>=? AND us.`date`<=? " + + "AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? AND rc.type=? " + + "GROUP BY us.`date`,rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid group by repo,`date`, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, dataType); + rs = st.executeQuery(); + + String result = ""; + String lastDate = ""; + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + List identifiers = null; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { +// if (reportItem != null) { +// reportItems.add(reportItem); +// } +// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs.getString(1); + } + + reportItem = new COUNTER_Item_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), null, "Regular"); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + // } +// +// //} + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } else { + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { + if (dataType.equals("")) { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + + statsDB + ".result rs WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? " + + "AND us.result_id=? GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd WHERE tpd.id=resultid " + + "GROUP BY repo,type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + + rs = st.executeQuery(); + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Item_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), null, "Regular"); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + } else { + st = connection.prepareStatement("WITH tpd as (select distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item, yop, group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, sum(us.unique_item_requests) as unique_item_requests, " + + "sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(select distinct id, type FROM " + statsDB + ".result_classifications) rc, " + + statsDB + ".result rs WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id " + + "AND us.repository_id=? AND us.result_id=? AND rc.type=? GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid group by repo, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + st.setString(5, dataType); + + rs = st.executeQuery(); + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Items_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Items_Ivestigations")) { + metricTypeValue[1] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Item_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), null, "Regular"); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + } + } else if (granularity.equalsIgnoreCase("monthly")) { + + if (dataType.equals("")) { + st = connection.prepareStatement("WITH tpd as (select distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`,total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, uniquedownloads,uniqueviews " + + "FROM (SELECT us.repository_id repo, us.`date` `date`,rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(select distinct id, type FROM " + statsDB + ".result_classifications) rc, " + + statsDB + ".result rs WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? AND us.result_id=? " + + "GROUP BY us.`date`,rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid group by repo,`date`, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { + datatype = rs.getString(1); + } + reportItem = new COUNTER_Item_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), null, "Regular"); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } else { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`,total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, us.`date` `date`,rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(select distinct id, type FROM " + statsDB + ".result_classifications) rc, " + statsDB + ".result rs " + + "WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id " + + "AND us.repository_id=? AND us.result_id=? AND rc.type=? " + + "GROUP BY us.`date`,rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid group by repo,`date`, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + st.setString(5, dataType); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Item_Usage reportItem = null; + + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + List identifiers = null; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { +// if (reportItem != null) { +// reportItems.add(reportItem); +// } +// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs.getString(1); + } + + reportItem = new COUNTER_Item_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), null, "Regular"); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + } + + public void executeItemsAllRepoIR(List reportItems, + String itemIdentifier, Date beginDate, + Date endDate, List metricType, String dataType, String granularity) throws Exception { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); + String beginDateStr = postgresFormat.format(beginDate); + String endDateStr = postgresFormat.format(endDate); + + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { + if (dataType.equals("")) { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, name, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo,rc.type type, ds.name as name, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, (SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + statsDB + ".result rs, " + + statsDB + ".datasource ds WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.result_id=? " + + "AND us.repository_id=ds.id GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id, ds.name) tmp, tpd " + + "WHERE tpd.id=resultid group by repo,name, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations"); + + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, itemIdentifier); + + rs = st.executeQuery(); + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Item_Usage(rs.getString(5), "OpenAIRE", rs.getString(8), rs.getString(1), Integer.toString(rs.getInt(6)), null, "Regular"); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + } + + } else { + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, name, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo,rc.type type, ds.name as name, sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, (SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + statsDB + ".result rs, " + + statsDB + ".datasource ds WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.result_id=? AND rc.type=? " + + "AND us.repository_id=ds.id GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id, ds.name) tmp, tpd " + + "WHERE tpd.id=resultid group by repo,name, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, itemIdentifier); + st.setString(4, dataType); + + rs = st.executeQuery(); + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + //String platform = getInstitutionName(rs.getString(8)); + reportItem = new COUNTER_Item_Usage(rs.getString(5), "OpenAIRE", rs.getString(8), rs.getString(1), Integer.toString(rs.getInt(6)), null, "Regular"); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + } + + } + } else if (granularity.equalsIgnoreCase("monthly")) { + + if (dataType.equals("")) { + + st = connection.prepareStatement("WITH tpd as (select distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`,total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, name, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, us.`date` `date`,rc.type type, ds.name as name, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, (SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + statsDB + ".result rs, " + + statsDB + ".datasource ds WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.result_id=? AND us.repository_id=ds.id " + + "GROUP BY us.`date`,rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id, ds.name) tmp, tpd " + + "WHERE tpd.id=resultid group by repo,`date`,name, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, itemIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { + datatype = rs.getString(1); + } + reportItem = new COUNTER_Item_Usage(rs.getString(6), "OpenAIRE", rs.getString(9), rs.getString(1), Integer.toString(rs.getInt(7)), null, "Regular"); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(10) : null, metricTypeValue[3] == true ? rs.getString(11) : null)); + reportItems.add(reportItem); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } else { + st = connection.prepareStatement("WITH tpd as (select distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`,total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, name, unique_item_requests,unique_item_investigations " + + "FROM (SELECT us.repository_id repo, us.`date` `date`,rc.type type, ds.name as name, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, (SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + statsDB + ".result rs, " + + statsDB + ".datasource ds WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.result_id=? AND us.repository_id=ds.id AND rc.type=? " + + "GROUP BY us.`date`,rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id, ds.name) tmp, tpd " + + "WHERE tpd.id=resultid group by repo,`date`,name, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, itemIdentifier); + st.setString(4, dataType); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Item_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Item_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Item_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Item_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Item_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { +// if (reportItem != null) { +// reportItems.add(reportItem); +// } +// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs.getString(1); + } + reportItem = new COUNTER_Item_Usage(rs.getString(6), "OpenAIRE", rs.getString(9), rs.getString(1), Integer.toString(rs.getInt(7)), null, "Regular"); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Item_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(10) : null, metricTypeValue[3] == true ? rs.getString(11) : null)); + reportItems.add(reportItem); + + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + public void executeBatchItemsDSR(List reportItems, + String repositoryIdentifier, String itemIdentifier, Date beginDate, + Date endDate, List metricType, String granularity) throws Exception { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); + String beginDateStr = postgresFormat.format(beginDate); + String endDateStr = postgresFormat.format(endDate); + + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + String platform = getInstitutionName(repositoryIdentifier); + + if (itemIdentifier.equals("")) { + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { + +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews, " +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, rs.year, dp.access_method ORDER by rc.type ASC;"); + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations, 'Regular' as access_method " + + "FROM (SELECT us.repository_id repo, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, openaire_prod_stats.result rs " + + "WHERE us.`date`>='? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? " + + "AND rc.type='Dataset' GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY repo,type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + while (rs.next()) { + reportItem = new COUNTER_Dataset_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), rs.getString(10)); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + //} + } else if (granularity.equalsIgnoreCase("monthly")) { +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, us.`date`, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews, " +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, us.`date`, rs.year, dp.access_method ORDER by rc.type, us.`date` ASC;"); + + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations, 'Regular' as access_method " + + "FROM (SELECT us.repository_id repo, us.`date` as `date`, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, openaire_prod_stats.result rs " + + "WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? " + + "AND rc.type='Dataset' GROUP BY us.`date`, rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY repo,`date`, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations ORDER BY `date` ASC;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + rs = st.executeQuery(); + + String result = ""; + String lastDate = ""; + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + List identifiers = null; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { +// if (reportItem != null) { +// reportItems.add(reportItem); +// } +// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs.getString(1); + } + + reportItem = new COUNTER_Dataset_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), rs.getString(11)); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } else { + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews," +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND us.result_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, rs.year, dp.access_method ORDER by rc.type ASC;"); + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations, 'Regular' as access_method " + + "FROM (SELECT us.repository_id repo, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, openaire_prod_stats.result rs " + + "WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? AND us.result_id=? " + + "AND rc.type='Dataset' GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY repo,type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations;"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + + rs = st.executeQuery(); + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Dataset_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), rs.getString(10)); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + } else if (granularity.equalsIgnoreCase("monthly")) { + +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, us.`date`, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews, " +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND us.result_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, us.`date`, rs.year, dp.access_method ORDER by rc.type, us.`date` ASC;"); + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations, 'Regular' as access_method " + + "FROM (SELECT us.repository_id repo, us.`date` as `date`, rc.type type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, openaire_prod_stats.result rs " + + "WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.repository_id=? AND us.result_id=? " + + "AND rc.type='Dataset' GROUP BY us.`date`, rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id, us.`date`) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY repo,`date`, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations ORDER BY `date` ASC;"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Dataset_Usage reportItem = null; + + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { + datatype = rs.getString(1); + } + + reportItem = new COUNTER_Dataset_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), rs.getString(11)); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + } + + public void executeItemsAllRepoDSR(List reportItems, + String itemIdentifier, Date beginDate, + Date endDate, List metricType, String granularity) throws Exception { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); + String beginDateStr = postgresFormat.format(beginDate); + String endDateStr = postgresFormat.format(endDate); + + Connection connection = null; + PreparedStatement st = null; + PreparedStatement st1 = null; + ResultSet rs = null; + ResultSet rs1 = null; + + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { + + st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " + + "SELECT rc.type, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, ds.name, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations, " + + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " + + statsDB + ".result_oids ro, tpd, " + statsDB + ".datasource ds, openaire_prod_datacite_usage_stats.datasetsperformance_nonarray_view dp " + + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=ds.id AND us.result_id=? AND rc.id=us.result_id " + + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " + + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, rs.year, ds.name,dp.access_method ORDER by rc.type ASC;"); + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, total_item_requests, total_item_investigations, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid,name, unique_item_requests,unique_item_investigations, 'Regular' as access_method " + + "FROM (SELECT us.repository_id repo, rc.type type, ds.name, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, " + statsDB + ".result rs, " + + statsDB + ".datasource ds WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.result_id=? " + + "AND rc.type='Dataset' AND ds.id=us.repository_id GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id, ds.name) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY name, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations "); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, itemIdentifier); +// + "UNION " + st1 = connection.prepareStatement("SELECT 'Dataset' as type, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " + + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " + + "rp.id as resultid,dp.ds_title as item, cast(dp.yop as Int) as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, dp.platform, " + + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " + + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, " + + "dp.access_method access_method FROM openaire_prod_datacite_usage_stats.datasetsperformance_nonarray_view dp, " + + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " + + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type AND rp.id=? " + + "AND ds.name=dp.platform GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id "); + + st1.setString(1, report_dateFormat.format(beginDate)); + st1.setString(2, report_dateFormat.format(endDate)); + st1.setString(3, itemIdentifier); + + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + rs = st.executeQuery(); + while (rs.next()) { + // String platform = getInstitutionName(rs.getString(8)); + reportItem = new COUNTER_Dataset_Usage(rs.getString(5), "OpenAIRE", rs.getString(8), rs.getString(1), Integer.toString(rs.getInt(6)), rs.getString(11)); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + } + rs1 = st1.executeQuery(); + while (rs1.next()) { + // String platform = getInstitutionName(rs.getString(8)); + reportItem = new COUNTER_Dataset_Usage(rs1.getString(5), "OpenAIRE", rs1.getString(8), rs1.getString(1), Integer.toString(rs1.getInt(6)), rs1.getString(11)); + String[] identifiersAll = rs1.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs1.getString(2) : null, metricTypeValue[1] == true ? rs1.getString(3) : null, metricTypeValue[2] == true ? rs1.getString(9) : null, metricTypeValue[3] == true ? rs1.getString(10) : null)); + reportItems.add(reportItem); + } + } else if (granularity.equalsIgnoreCase("monthly")) { + +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, us.`date`, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, ds.name, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews, " +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, " + statsDB + ".datasource ds, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=ds.id AND us.result_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, us.`date`, rs.year, ds.name, dp.access_method ORDER by rc.type, us.`date` ASC;"); + st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " + + "SELECT type, `date`, total_item_requests, total_item_investigations, resultid, item, yop, group_concat(distinct tpd.type_id,'#!#') as oid, unique_item_requests,unique_item_investigations, name, 'Regular' as access_method " + + "FROM (SELECT us.repository_id repo, us.`date` as `date`, rc.type type,ds.name, sum(us.total_item_requests) as total_item_requests, sum(us.total_item_investigations) as total_item_investigations, " + + "us.result_id as resultid, rs.title as item, rs.year as yop, " + + "sum(us.unique_item_requests)) as unique_item_requests, sum(us.unique_item_investigations) as unique_item_investigations " + + "FROM " + usagestatsImpalaDB + ".counter_r5_stats_with_metrics us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, openaire_prod_stats.result rs, " + + statsDB + ".datasource ds WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND ds.id=us.repository_id AND us.result_id=? " + + "AND rc.type='Dataset' GROUP BY ds.name, us.repository_id, rc.type, us.`date`, rs.title, us.result_id, rs.title, rs.year, us.`date`) tmp, tpd " + + "WHERE tpd.id=resultid GROUP BY name,`date`, type,resultid,item,yop,total_item_requests,total_item_investigations,unique_item_requests,unique_item_investigations ORDER BY `date` ASC;"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, itemIdentifier); + + st1 = connection.prepareStatement("SELECT 'Dataset' as type, dp.period_from, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " + + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " + + "rp.id as resultid,dp.ds_title as item, dp.yop as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, " + + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " + + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, dp.platform, " + + "dp.access_method access_method FROM openaire_prod_datacite_usage_stats.datasetsperformance_nonarray_view dp, " + + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " + + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type and rp.id=? and ds.name=dp.platform " + + "GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id, dp.period_from"); + st1.setString(1, report_dateFormat.format(beginDate)); + st1.setString(2, report_dateFormat.format(endDate)); + st1.setString(3, itemIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(postgresFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { +// if (reportItem != null) { +// reportItems.add(reportItem); +// } +// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs.getString(1); + } + reportItem = new COUNTER_Dataset_Usage(rs.getString(6), "OpenAIRE", rs.getString(11), rs.getString(1), Integer.toString(rs.getInt(7)), rs.getString(12)); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(postgresFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + rs1 = st1.executeQuery(); + while (rs1.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(report_dateFormat.parse(rs1.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs1.getString(1))) { +// if (reportItem != null) { +// reportItems.add(reportItem); +// } +// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs1.getString(1); + } + reportItem = new COUNTER_Dataset_Usage(rs1.getString(6), "OpenAIRE", rs1.getString(11), rs1.getString(1), Integer.toString(rs1.getInt(7)), rs1.getString(12)); + String[] identifiersAll = rs1.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(report_dateFormat.parse(rs1.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs1.getString(3) : null, metricTypeValue[1] == true ? rs1.getString(4) : null, metricTypeValue[2] == true ? rs1.getString(9) : null, metricTypeValue[3] == true ? rs1.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(report_dateFormat.parse(rs1.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = report_dateFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + + public void executeBatchItemsDSRDatacite(List reportItems, + String repositoryIdentifier, String itemIdentifier, Date beginDate, + Date endDate, List metricType, String granularity) throws Exception { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + //SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); + String beginDateStr = report_dateFormat.format(beginDate); + String endDateStr = report_dateFormat.format(endDate); + + Connection connection = null; + PreparedStatement st = null; + ResultSet rs = null; + String platform = getInstitutionName(repositoryIdentifier); + + if (itemIdentifier.equals("")) { + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { + +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews, " +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, rs.year, dp.access_method ORDER by rc.type ASC;"); + st = connection.prepareStatement("SELECT 'Dataset' as type, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " + + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " + + "rp.id as resultid,dp.ds_title as item, dp.yop as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, " + + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " + + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, " + + "dp.access_method access_method, dp.platform FROM openaire_prod_datacite_usage_stats.datasetsperformance_nonarray_view dp, " + + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " + + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type and ds.id=? and ds.name=dp.platform " + + "GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + while (rs.next()) { + reportItem = new COUNTER_Dataset_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), rs.getString(10)); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + //} + } else if (granularity.equalsIgnoreCase("monthly")) { +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, us.`date`, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews, " +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, us.`date`, rs.year, dp.access_method ORDER by rc.type, us.`date` ASC;"); + + st = connection.prepareStatement("SELECT 'Dataset' as type, dp.period_from, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " + + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " + + "rp.id as resultid,dp.ds_title as item, dp.yop as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, " + + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " + + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, " + + "dp.access_method access_method, dp.platform FROM openaire_prod_datacite_usage_stats.datasetsperformance_nonarray_view dp, " + + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " + + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type and ds.id=? and ds.name=dp.platform " + + "GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id, dp.period_from"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + rs = st.executeQuery(); + + String result = ""; + String lastDate = ""; + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(report_dateFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + List identifiers = null; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(report_dateFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { +// if (reportItem != null) { +// reportItems.add(reportItem); +// } +// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs.getString(1); + } + + reportItem = new COUNTER_Dataset_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), rs.getString(11)); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(report_dateFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(report_dateFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = report_dateFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } else { + try { + connection = usageStatsDB.getConnection(); + + if (granularity.equalsIgnoreCase("totals")) { +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews," +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND us.result_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, rs.year, dp.access_method ORDER by rc.type ASC;"); + st = connection.prepareStatement("SELECT 'Dataset' as type, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " + + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " + + "rp.id as resultid,dp.ds_title as item, dp.yop as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, " + + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " + + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, " + + "dp.access_method access_method, dp.platform FROM openaire_prod_datacite_usage_stats.datasetsperformance_nonarray_view dp, " + + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " + + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type and ds.id=? AND rp.id=? and ds.name=dp.platform " + + "GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + + rs = st.executeQuery(); + COUNTER_Dataset_Usage reportItem = null; + boolean[] metricTypeValue = {false, false, false, false}; + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + while (rs.next()) { + reportItem = new COUNTER_Dataset_Usage(rs.getString(5), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(6)), rs.getString(10)); + String[] identifiersAll = rs.getString(7).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), + metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(8) : null, metricTypeValue[3] == true ? rs.getString(9) : null)); + reportItems.add(reportItem); + } + } else if (granularity.equalsIgnoreCase("monthly")) { + +// st = connection.prepareStatement("WITH tpd as (select id, concat(type,'#-#',pid) type_id from " + statsDB + ".result_pids) " +// + "SELECT rc.type, us.`date`, sum(us.downloads) as downloads, sum(us.views) as views, " +// + "us.result_id as resultid, rs.title as item, rs.year as yop, group_concat(distinct tpd.type_id,'#!#') as oid, " +// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews, " +// + "case when us.source='Datacite' then dp.access_method else 'regular' end as access_method " +// + "FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc, " + statsDB + ".result rs, " +// + statsDB + ".result_oids ro, tpd, datasetsusagestats_20210312a.datasetsperformance dp " +// + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND us.result_id=? AND rc.id=us.result_id " +// + "AND us.result_id=rs.id AND ro.id=us.result_id AND tpd.id=us.result_id AND rc.type='Dataset' AND dp.ds_type=ro.oid " +// + "GROUP BY us.source, ro.id, rc.type, rs.title, us.result_id, us.`date`, rs.year, dp.access_method ORDER by rc.type, us.`date` ASC;"); + st = connection.prepareStatement("SELECT 'Dataset' as type, dp.period_from, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " + + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " + + "rp.id as resultid,dp.ds_title as item, dp.yop as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, " + + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " + + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, " + + "dp.access_method access_method, dp.platform FROM openaire_prod_datacite_usage_stats.datasetsperformance_nonarray_view dp, " + + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " + + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type and ds.id=? AND rp.id=? and ds.name=dp.platform " + + "GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id, dp.period_from"); + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + st.setString(4, itemIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + COUNTER_Dataset_Usage reportItem = null; + + boolean[] metricTypeValue = {false, false, false, false}; + + if (metricType.contains("Total_Dataset_Requests")) { + metricTypeValue[0] = true; + } + if (metricType.contains("Total_Dataset_Investigations")) { + metricTypeValue[1] = true; + } + if (metricType.contains("Unique_Dataset_Requests")) { + metricTypeValue[2] = true; + } + if (metricType.contains("Unique_Dataset_Investigations")) { + metricTypeValue[3] = true; + } + + int ft_total = 0; + int abstr = 0; + + Calendar endCal = Calendar.getInstance(); + endCal.setTime(report_dateFormat.parse(endDateStr)); + endCal.add(Calendar.MONTH, 1); + Date endDateForZeros = endCal.getTime(); + + lastDate = beginDateStr; + String datatype = ""; + + while (rs.next()) { + Calendar endC = Calendar.getInstance(); + endC.setTime(report_dateFormat.parse(rs.getString(2))); + endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +// //iterate on data types + if (!datatype.equals(rs.getString(1))) { + datatype = rs.getString(1); + } + + reportItem = new COUNTER_Dataset_Usage(rs.getString(6), "OpenAIRE", platform, rs.getString(1), Integer.toString(rs.getInt(7)), rs.getString(11)); + String[] identifiersAll = rs.getString(8).split("#!#"); + for (int i = 0; i < identifiersAll.length; i++) { + String[] typeIdentifierArray = identifiersAll[i].split("#-#"); + reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); + } + reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(report_dateFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), + metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); + reportItems.add(reportItem); + + endC.setTime(report_dateFormat.parse(rs.getString(2))); + endC.add(Calendar.MONTH, 1); + lastDate = report_dateFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); + } + } + /* + jedis.put(redis_key, "persistent", "false"); + jedis.put(redis_key, "query", st.toString()); + jedis.put(redis_key, "result", toJson(reportItems)); + jedis.put(redis_key, "fetchMode", "3"); + */ + } catch (Exception e) { + log.error("Batch Item Report failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + } + } + +// public void executeItemsAllRepoDSRDatacite(List reportItems, +// String itemIdentifier, Date beginDate, +// Date endDate, List metricType, String granularity) throws Exception { +// SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); +// SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); +// String beginDateStr = postgresFormat.format(beginDate); +// String endDateStr = postgresFormat.format(endDate); +// +// Connection connection = null; +// PreparedStatement st = null; +// ResultSet rs = null; +// +// try { +// connection = usageStatsDB.getConnection(); +// +// if (granularity.equalsIgnoreCase("totals")) { +// +//// st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " +//// + "SELECT type, downloads, views, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, uniquedownloads,uniqueviews, 'Regular' as access_method " +//// + "FROM (SELECT us.repository_id repo, rc.type type, sum(us.downloads) as downloads, sum(us.views) as views, " +//// + "us.result_id as resultid, rs.title as item, rs.year as yop, " +//// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews " +//// + "FROM " + usagestatsImpalaDB + ".usage_stats us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, openaire_prod_stats.result rs " +//// + "WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.result_id=? " +//// + "AND rc.type='Dataset' GROUP BY rc.type, rs.title, us.result_id, rs.title, rs.year, us.repository_id) tmp, tpd " +//// + "WHERE tpd.id=resultid GROUP BY repo, type,resultid,item,yop,downloads,views,uniquedownloads,uniqueviews"); +// st = connection.prepareStatement("SELECT 'Dataset' as type, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " +// + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " +// + "rp.id as resultid,dp.ds_title as item, dp.yop as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, " +// + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " +// + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, " +// + "dp.access_method access_method, dp.platform FROM datasetsusagestats_20210312a.datasetsperformance dp, " +// + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " +// + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type AND rp.id=? and ds.name=dp.platform " +// + "GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id"); +// st.setString(1, beginDateStr); +// st.setString(2, endDateStr); +// st.setString(3, itemIdentifier); +// +// rs = st.executeQuery(); +// COUNTER_Dataset_Usage reportItem = null; +// boolean[] metricTypeValue = {false, false, false, false}; +// +// if (metricType.contains("Total_Dataset_Requests")) { +// metricTypeValue[0] = true; +// } +// if (metricType.contains("Total_Dataset_Investigations")) { +// metricTypeValue[1] = true; +// } +// if (metricType.contains("Unique_Dataset_Requests")) { +// metricTypeValue[2] = true; +// } +// if (metricType.contains("Unique_Dataset_Investigations")) { +// metricTypeValue[3] = true; +// } +// +// while (rs.next()) { +// String platform = getInstitutionName(rs.getString(8)); +// reportItem = new COUNTER_Dataset_Usage(rs.getString(5), "OpenAIRE", rs.getString(8), rs.getString(1), Integer.toString(rs.getInt(6)), rs.getString(11)); +// String[] identifiersAll = rs.getString(7).split("#!#"); +// for (int i = 0; i < identifiersAll.length; i++) { +// String[] typeIdentifierArray = identifiersAll[i].split("#-#"); +// reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); +// } +// +// reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), +// metricTypeValue[0] == true ? rs.getString(2) : null, metricTypeValue[1] == true ? rs.getString(3) : null, metricTypeValue[2] == true ? rs.getString(9) : null, metricTypeValue[3] == true ? rs.getString(10) : null)); +// reportItems.add(reportItem); +// } +// +// //} +// } else if (granularity.equalsIgnoreCase("monthly")) { +// +//// st = connection.prepareStatement("WITH tpd as (SELECT distinct id, concat(type,'#-#',pid) type_id FROM " + statsDB + ".result_pids) " +//// + "SELECT type, `date`, downloads, views, resultid, item,yop,group_concat(distinct tpd.type_id,'#!#') as oid, uniquedownloads,uniqueviews, 'Regular' as access_method " +//// + "FROM (SELECT us.repository_id repo, us.`date` as `date`, rc.type type, sum(us.downloads) as downloads, sum(us.views) as views, " +//// + "us.result_id as resultid, rs.title as item, rs.year as yop, " +//// + "count(case when us.downloads >0 then 1 else null end) as uniquedownloads, count(case when us.views >0 then 1 else null end) as uniqueviews " +//// + "FROM " + usagestatsImpalaDB + ".usage_stats us,(SELECT distinct id, type FROM " + statsDB + ".result_classifications) rc, openaire_prod_stats.result rs " +//// + "WHERE us.`date`>=? AND us.`date`<=? AND rc.id=us.result_id AND us.result_id=rs.id AND us.result_id=? " +//// + "AND rc.type='Dataset' GROUP BY rc.type, us.`date`, rs.title, us.result_id, rs.title, rs.year, us.`date`) tmp, tpd " +//// + "WHERE tpd.id=resultid GROUP BY repo,`date`, type,resultid,item,yop,downloads,views,uniquedownloads,uniqueviews ORDER BY `date` ASC;"); +// st = connection.prepareStatement("SELECT 'Dataset' as type, dp.period_from, sum(case when dp.metric_type='total-dataset-requests' then count else 0 end) as downloads, " +// + "sum(case when dp.metric_type='total-dataset-investigations' then count else 0 end) as views, " +// + "rp.id as resultid,dp.ds_title as item, dp.yop as yop, concat('Digital Object Identifier#-#',dp.ds_type) as oid, " +// + "sum(case when dp.metric_type='unique-dataset-requests' then count else 0 end) as uniquedownloads, " +// + "sum(case when dp.metric_type='unique-dataset-investigations' then count else 0 end) as uniqueviews, " +// + "dp.access_method access_method, dp.platform FROM datasetsusagestats_20210312a.datasetsperformance dp, " +// + statsDB + ".result_pids rp, " + statsDB + ".datasource ds " +// + "WHERE dp.period_from>=? AND dp.period_end<=? and rp.pid=ds_type and rp.id=? and ds.name=dp.platform " +// + "GROUP BY dp.ds_title, dp.yop, dp.platform, dp.access_method, dp.ds_type,rp.id, dp.period_from"); +// st.setString(1, beginDateStr); +// st.setString(2, endDateStr); +// st.setString(3, itemIdentifier); +// +// rs = st.executeQuery(); +// String result = ""; +// String lastDate = ""; +// COUNTER_Dataset_Usage reportItem = null; +// boolean[] metricTypeValue = {false, false, false, false}; +// +// if (metricType.contains("Total_Dataset_Requests")) { +// metricTypeValue[0] = true; +// } +// if (metricType.contains("Total_Dataset_Investigations")) { +// metricTypeValue[1] = true; +// } +// if (metricType.contains("Unique_Dataset_Requests")) { +// metricTypeValue[2] = true; +// } +// if (metricType.contains("Unique_Dataset_Investigations")) { +// metricTypeValue[3] = true; +// } +// +// int ft_total = 0; +// int abstr = 0; +// +// Calendar endCal = Calendar.getInstance(); +// endCal.setTime(postgresFormat.parse(endDateStr)); +// endCal.add(Calendar.MONTH, 1); +// Date endDateForZeros = endCal.getTime(); +// +// lastDate = beginDateStr; +// String datatype = ""; +// +// while (rs.next()) { +// Calendar endC = Calendar.getInstance(); +// endC.setTime(postgresFormat.parse(rs.getString(2))); +// endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); +//// //iterate on data types +// if (!datatype.equals(rs.getString(1))) { +//// if (reportItem != null) { +//// reportItems.add(reportItem); +//// } +//// reportItem = new COUNTER_Platform_Usage("", "OpenAIRE", rs.getString(1), "Regular", ""); +// datatype = rs.getString(1); +// } +// reportItem = new COUNTER_Dataset_Usage(rs.getString(6), "OpenAIRE", rs.getString(9), rs.getString(1), Integer.toString(rs.getInt(7)), rs.getString(12)); +// String[] identifiersAll = rs.getString(8).split("#!#"); +// for (int i = 0; i < identifiersAll.length; i++) { +// String[] typeIdentifierArray = identifiersAll[i].split("#-#"); +// reportItem.addIdentifier(new COUNTER_Dataset_Identifiers(typeIdentifierArray[0], typeIdentifierArray[1])); +// } +// +// reportItem.addPerformance(new COUNTER_Dataset_Performance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), +// metricTypeValue[0] == true ? rs.getString(3) : null, metricTypeValue[1] == true ? rs.getString(4) : null, metricTypeValue[2] == true ? rs.getString(10) : null, metricTypeValue[3] == true ? rs.getString(11) : null)); +// reportItems.add(reportItem); +// +// endC.setTime(postgresFormat.parse(rs.getString(2))); +// endC.add(Calendar.MONTH, 1); +// lastDate = postgresFormat.format(endC.getTime()); +// //if (reportItem != null) { +// //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); +// } +// } +// /* +// jedis.put(redis_key, "persistent", "false"); +// jedis.put(redis_key, "query", st.toString()); +// jedis.put(redis_key, "result", toJson(reportItems)); +// jedis.put(redis_key, "fetchMode", "3"); +// */ +// } catch (Exception e) { +// log.error("Batch Item Report failed: ", e); +// } finally { +// DbUtils.closeQuietly(rs); +// DbUtils.closeQuietly(st); +// DbUtils.closeQuietly(connection); +// } +// } + + private void fillWithZeros(Date from, Date to, COUNTER_Platform_Usage reportItem) { + SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar fromCalendar = Calendar.getInstance(); + fromCalendar.setTime(from); + + Calendar toCalendar = Calendar.getInstance(); + toCalendar.setTime(to); + while (from.before(to)) { + Calendar temp_c = Calendar.getInstance(); + temp_c.setTime(from); + temp_c.set(Calendar.DAY_OF_MONTH, temp_c.getActualMaximum(Calendar.DAY_OF_MONTH)); + Date temp_endDate = temp_c.getTime(); + + reportItem.addPerformance(new COUNTER_Item_Performance(report_dateFormat.format(from), report_dateFormat.format(temp_endDate), "0", "0", "0", "0")); + fromCalendar.add(Calendar.MONTH, 1); + from = fromCalendar.getTime(); + } + } + + public String getInstitutionName(String repositoryIdentifier) { + PreparedStatement st = null; + Connection connection = null; + ResultSet rs = null; + String institutionName = ""; + try { + connection = usageStatsDB.getConnection(); + st = connection.prepareStatement("select name from " + statsDB + ".datasource where id=?"); + st.setString(1, repositoryIdentifier); + + rs = st.executeQuery(); + while (rs.next()) { + institutionName = rs.getString(1); + } + return institutionName; + } catch (Exception e) { + log.error("Repository name failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + + return "-1"; + } + + public boolean checkServiceConnection() { + Connection connection = null; + try { + connection = usageStatsDB.getConnection(); + if (connection != null) { + return true; + } + } catch (Exception e) { + log.info(e); + } finally { + DbUtils.closeQuietly(connection); + } + return false; + } + + public ArrayList buildMembersList() { + PreparedStatement st = null; + Connection connection = null; + ResultSet rs = null; + ArrayList membersList = new ArrayList(); + + try { + connection = usageStatsDB.getConnection(); + st = connection.prepareStatement("select distinct us.repository_id, d.name, do.oid from " + statsDB + ".datasource d, " + + statsDB + ".datasource_oids do, " + usagestatsImpalaDB + ".usage_stats us " + + "where us.repository_id=d.id and us.repository_id=do.id and d.id=do.id and do.oid not like '%piwik%' order by do.oid asc"); + rs = st.executeQuery(); + while (rs.next()) { + ArrayList identifiers = new ArrayList(); + identifiers.add(new SUSHI_Org_Identifiers("OpenAIRE", rs.getString(1))); + identifiers.add(new SUSHI_Org_Identifiers("OpenDOAR", rs.getString(3))); + + membersList.add(new SUSHI_Consortium_Member_List(null, null, rs.getString(2), null, identifiers)); + } + + } catch (Exception e) { + log.error("No member found " + e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return membersList; + + } + + public boolean checkIfDatacite(String repositoryIdentifer) { + PreparedStatement st = null; + Connection connection = null; + ResultSet rs = null; + boolean dataciteMember = false; + + try { + connection = usageStatsDB.getConnection(); + st = connection.prepareStatement("select distinct source from " + usagestatsImpalaDB + ".usage_stats where repository_id=?;"); + st.setString(1, repositoryIdentifer); + + rs = st.executeQuery(); + + while (rs.next()) { + if (rs.getString(1).equals("Datacite")) { + dataciteMember = true; + } + } + + } catch (Exception e) { + log.error("No member found " + e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return dataciteMember; + } +} diff --git a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImplSample.java b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImplSample.java new file mode 100755 index 0000000..3482c4e --- /dev/null +++ b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImplSample.java @@ -0,0 +1,1309 @@ +package eu.dnetlib.usagestats.services; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import eu.dnetlib.usagestats.repositories.UsageStatsRepository; +import eu.dnetlib.usagestats.sushilite.domain.Alert; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Dataset_Report; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Dataset_Usage; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Item_Report; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Item_Usage; +import eu.dnetlib.usagestats.sushilite.domain.Filter; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Org_Identifiers; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Platform_Usage; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Error_Model; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Platform_Report; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Service_Status; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Report_List; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import org.springframework.stereotype.Service; + +import java.text.SimpleDateFormat; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Value; + +@Service +public class SushiLiteServiceImpl implements SushiLiteService { + + private final UsageStatsRepository usageStatsRepository; + + private final Logger log = Logger.getLogger(this.getClass()); + + @Value("${compression.max_number_of_records}") + private int compression_max_number_of_records; + + @Value("${download.folder}") + private String download_folder; + + @Value("${sushi-lite.server}") + private String sushi_lite_server; + + boolean reportForCompression = false; + + public SushiLiteServiceImpl(UsageStatsRepository usageStatsRepository) { + this.usageStatsRepository = usageStatsRepository; + } + +// @Override +// public ReportResponseWrapper buildReport(String reportName, String release, +// String requestorId, String beginDate, +// String endDate, String repositoryIdentifier, String itemIdentifier, +// String itemDataType, String hasDoi, String granularity, +// String callback) { +// +// List reportItems = new ArrayList<>(); +// List reportExceptions = new ArrayList<>(); +// +// if (!granularity.equalsIgnoreCase("totals") && !granularity.equalsIgnoreCase("monthly")) { +// reportExceptions.add(new SUSHI_Error_Model("3062", "Warning", "Invalid ReportAttribute Value", "usagecounts.openaire.eu", "Granularity: \'" + granularity + "\' unknown. Defaulting to Monthly")); +// granularity = "Monthly"; +// } +// +// Date beginDateParsed; +// if (!beginDate.equals("")) { +// beginDateParsed = tryParse(beginDate); +// if (beginDateParsed != null && (granularity.toLowerCase().equals("monthly") || beginDate.length() == 7)) { +// Calendar temp = Calendar.getInstance(); +// temp.setTime(beginDateParsed); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); +// beginDateParsed = temp.getTime(); +// } +// } else { +// Calendar temp = Calendar.getInstance(); +// temp.add(Calendar.MONTH, -1); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); +// beginDateParsed = temp.getTime(); +// reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "Unspecified Date Arguments", "usagecounts.openaire.eu", "Begin Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed))); +// } +// +// Date endDateParsed; +// if (!endDate.equals("")) { +// endDateParsed = tryParse(endDate); +// if (endDateParsed != null && (granularity.toLowerCase().equals("monthly") || endDate.length() == 7)) { +// Calendar temp = Calendar.getInstance(); +// temp.setTime(endDateParsed); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); +// endDateParsed = temp.getTime(); +// } +// } else { +// Calendar temp = Calendar.getInstance(); +// temp.add(Calendar.MONTH, -1); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); +// endDateParsed = temp.getTime(); +// reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "Unspecified Date Arguments", "usagecounts.openaire.eu", "End Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed))); +// } +// //log.error("dates: " + beginDateParsed.toString() + " - " + endDateParsed.toString()); +// +// if (beginDateParsed == null) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "Begin Date: " + beginDate + " is not a valid date")); +// } +// if (endDateParsed == null) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "End Date: " + endDate + " is not a valid date")); +// } +// if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "BeginDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed) + "\' is greater than EndDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed) + "\'")); +// } +// +// String repoid = ""; +// if (!repositoryIdentifier.equals("")) { +// repoid = usageStatsRepository.executeRepoId(repositoryIdentifier, reportName.toLowerCase()); +// if (repoid.equals("-1")) { +// reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "RepositoryIdentifier: " + repositoryIdentifier + " is not valid")); +// } +// } +// String itemid = ""; +// if (!itemIdentifier.equals("")) { +// String[] split = itemIdentifier.split(":"); +// switch (split[0].toLowerCase()) { +// case "oid": +// itemid = itemIdentifier; +// break; +// case "doi": +// itemid = itemIdentifier; +// break; +// case "openaire": +// itemid = itemIdentifier; +// break; +// default: +// reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "ItemIdentifier: " + itemIdentifier + " is not valid")); +// itemid = "-1"; +// } +// } +// if (itemid.equals("") && repoid.equals("") && !reportName.equalsIgnoreCase("rr1") && !reportName.equalsIgnoreCase("jr1")) { +// reportExceptions.add(new SUSHI_Error_Model("3070", "Error", "Required Filter Missing", "usagecounts.openaire.eu", "ItemIdentifier or RepositoryIdentifier must be supplied")); +// } +// if (reportName.equalsIgnoreCase("ar1")) { +// if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// if (!itemid.equals("")) { +// if (itemDataType.equalsIgnoreCase("") || itemDataType.equalsIgnoreCase("article")) { +// usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Article", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else if (!repoid.equals("")) { +// usageStatsRepository.executeBatchItems(reportItems, repoid, "Article", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } +// } +// } else if (reportName.equalsIgnoreCase("br1")) { +// if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// if (!itemid.equals("")) { +// if (itemDataType.equalsIgnoreCase("") || itemDataType.equalsIgnoreCase("book")) { +// usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else if (!repoid.equals("")) { +// usageStatsRepository.executeBatchItems(reportItems, repoid, "Book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } +// } +// } else if (reportName.equalsIgnoreCase("br2")) { +// if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// if (!itemid.equals("")) { +// if (itemDataType.equalsIgnoreCase("") || itemDataType.equalsIgnoreCase("part of book or chapter of book")) { +// usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Part of book or chapter of book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else if (!repoid.equals("")) { +// usageStatsRepository.executeBatchItems(reportItems, repoid, "Part of book or chapter of book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } +// } +// } else if (reportName.equalsIgnoreCase("ir1")) { +// if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// if (!itemid.equals("")) { +// usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, itemDataType, beginDateParsed, endDateParsed, granularity); +// } else if (!repoid.equals("")) { +// usageStatsRepository.executeBatchItems(reportItems, repoid, itemDataType, beginDateParsed, endDateParsed, granularity); +// } +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } +// } else if (reportName.equalsIgnoreCase("rr1")) { +// if (!repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// usageStatsRepository.executeRepo(reportItems, repoid, itemDataType, beginDateParsed, endDateParsed, granularity); +// } +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else if (reportName.equalsIgnoreCase("jr1")) { +// if (!repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// usageStatsRepository.executeJournal(reportItems, repoid, itemDataType, beginDateParsed, endDateParsed, granularity); +// } +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else if (reportName.equals("")) { +// reportExceptions.add(new SUSHI_Error_Model("3050", "Error", "usagecounts.openaire.eu", "Report argument is missing", "You must supply a Report argument")); +// } else { +// reportExceptions.add(new SUSHI_Error_Model("3000", "Error", "usagecounts.openaire.eu", "Report " + reportName + " not supported", "Supported reports: AR1, IR1, RR1, BR1, BR2")); +// } +// +// ReportResponse reportResponse = new ReportResponse(reportName, release, requestorId, beginDate, endDate, +// repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback, reportItems, reportExceptions); +// +// return new ReportResponseWrapper(reportResponse); +// } +// @Override +// public String displayReport(String reportName, String release, +// String requestorId, String beginDate, String endDate, +// String repositoryIdentifier, String itemIdentifier, +// String itemDataType, String hasDoi, String granularity, +// String callback, String pretty) { +// ObjectMapper objectMapper = new ObjectMapper(); +// try { +// if (pretty.equalsIgnoreCase("pretty")) { +// return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReport(reportName, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/") + "
"; +// } +// return objectMapper.writeValueAsString(buildReport(reportName, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/"); +// } catch (Exception e) { +// e.printStackTrace(); +// return null; +// } +// //return report.getReport(reportP, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback, pretty); +// } + private Date tryParse(String dateString) { + try { + if (dateString.length() == 7) { + return new SimpleDateFormat("yyyy-MM").parse(dateString); + } else if (dateString.length() == 10) { + return new SimpleDateFormat("yyyy-MM-dd").parse(dateString); + } + } catch (Exception e) { + log.error("ParseError: ", e); + } + return null; + } + + @Override + public SUSHI_Service_Status buildReportStatus() { + + ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); + Alert alert = new Alert(dateTime.format(formatter), "No alert"); + ArrayList alertsList = new ArrayList(); + alertsList.add(alert); + try { + if (usageStatsRepository.checkServiceConnection() == true) { + + SUSHI_Service_Status reportStatus = new SUSHI_Service_Status("COUNTER R5 Usage Reports by OpenAIRE UsageCounts Service", true, "https://provide.openaire.eu", "UsageCounts Service Info: https://usagecounts.openaire.eu", alertsList); + return (reportStatus); + } else { + return null; + } + } catch (Exception e) { + System.out.println(e); + } + return null; + } + + @Override + public String displayReportStatus() { + ObjectMapper objectMapper = new ObjectMapper(); + try { + if (buildReportStatus() != null) { + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportStatus()) + "
"; + } else { + ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); + SUSHI_Error_Model errorModel = new SUSHI_Error_Model("1000", "Fatal", "Service Not Available", "usagecounts.openaire.eu", "Request was for: " + dateTime.format(formatter)); + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorModel) + "
"; + } + } catch (JsonProcessingException ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); + } + return null; + } + + @Override + public ArrayList buildReportSupported() { + ArrayList reportSupportedList = new ArrayList(); + SUSHI_Report_List r1 = new SUSHI_Report_List("Status Report", "Status Report", "5", "Current status of the reporting service supported by this API", "/status"); + SUSHI_Report_List r2 = new SUSHI_Report_List("Members Report", "Members Report", "5", "List of UsageCounts members", "/members"); + SUSHI_Report_List r3 = new SUSHI_Report_List("List Of Reports Report", "", "5", "List of reports supported by the API", "/reports/"); + SUSHI_Report_List r4 = new SUSHI_Report_List("Platform Master Report PR", "PR", "5", "A customizable report summarizing activity across a provider’s platforms that allows the user to apply filters and select other configuration options for the report. ", "/PR"); + SUSHI_Report_List r5 = new SUSHI_Report_List("Platform Usage Report", "PR_P1", "5", "Standard View of the Package Master Report that presents usage for the overall Platform broken down by Metric_Type.", "/PR_1"); + SUSHI_Report_List r6 = new SUSHI_Report_List("Platform Item Report", "IR", "5", "COUNTER Item Master Report", "/IR"); + SUSHI_Report_List r7 = new SUSHI_Report_List("Datasets Report", "DSR", "5", "COUNTER Datasets Report", "/DSR"); + + reportSupportedList.add(r1); + reportSupportedList.add(r2); + reportSupportedList.add(r3); + reportSupportedList.add(r4); + reportSupportedList.add(r5); + reportSupportedList.add(r6); + reportSupportedList.add(r7); + + return reportSupportedList; + } + + @Override + public String displayReportsSupported() { + ObjectMapper objectMapper = new ObjectMapper(); + try { + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportSupported()) + "
"; + } catch (JsonProcessingException ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); + } + return null; + + } + + @Override + public COUNTER_Platform_Report buildReportPR(String customerID, String repoID, String beginDate, + String endDate, String metricType, String dataType, String granularity) { + List reportExceptions = new ArrayList<>(); + ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); + //Display Report Created Day + + String repositoryIdentifier = usageStatsRepository.getInstitutionID(repoID); + if (!repositoryIdentifier.equals("")) { + if (repositoryIdentifier.equals("-1")) { + reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "RepositoryIdentifier: " + repoID + " is not valid")); + } + } + + Date beginDateParsed; + if (!beginDate.equals("")) { + beginDateParsed = tryParse(beginDate); + if (beginDateParsed != null && (granularity.toLowerCase().equals("monthly") || beginDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(beginDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + } else if (beginDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "Begin Date: " + beginDate + " is not a valid date")); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "usagecounts.openaire.eu", "Unspecified Date Arguments", "Begin Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed))); + } + + Date endDateParsed; + if (!endDate.equals("")) { + endDateParsed = tryParse(endDate); + if (endDateParsed != null && (granularity.toLowerCase().equals("monthly") || endDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(endDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + } else if (endDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "End Date: " + endDate + " is not a valid date")); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "usagecounts.openaire.eu", "Unspecified Date Arguments", "End Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed))); + } + //log.error("dates: " + beginDateParsed.toString() + " - " + endDateParsed.toString()); + + if (beginDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "Begin Date: " + beginDate + " is not a valid date")); + } + if (endDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "End Date: " + endDate + " is not a valid date")); + } + if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "BeginDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed) + "\' is greater than EndDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed) + "\'")); + } + + List reportFilters = new ArrayList(); + reportFilters.add(new Filter("BeginDate", beginDate)); + reportFilters.add(new Filter("EndDate", endDate)); + String reportID = "PR"; + String reportName = "Platform Master Report"; + //String institutionName = "Insititution Name " + repositoryIdentifier; + String institutionName = usageStatsRepository.getInstitutionName(repositoryIdentifier); + List institutionIdD = new ArrayList(); + institutionIdD.add(new SUSHI_Org_Identifiers("Openaire", repositoryIdentifier)); + + List reportItems = new ArrayList(); + if (reportExceptions.size() == 0) { + reportExceptions = null; + usageStatsRepository.executeBatchItemsPR(reportItems, repositoryIdentifier, beginDateParsed, endDateParsed, metricType, dataType, granularity); + } + + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + +// if (reportExceptions.size() == 0) { +// reportExceptions = null; +// } + COUNTER_Platform_Report reportPr = new COUNTER_Platform_Report(dateTime.format(formatter), customerID, reportID, reportName, institutionName, institutionIdD, reportExceptions, reportFilters, reportItems); + + log.info("Total report items " + reportItems.size()); + + if (reportItems.size() > compression_max_number_of_records) { + log.info("Compression due to " + reportItems.size()); + reportForCompression = true; + } + return reportPr; + } + + @Override + public COUNTER_Platform_Report buildReportPR_P1(String customerID, String repoID, + String beginDate, + String endDate) { + String granularity = "monthly"; + + List reportExceptions = new ArrayList<>(); + ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); + //Display Report Created Day + + String repositoryIdentifier = usageStatsRepository.getInstitutionID(repoID); + if (!repositoryIdentifier.equals("")) { + if (repositoryIdentifier.equals("-1")) { + reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "RepositoryIdentifier: " + repoID + " is not valid")); + } + } + Date beginDateParsed; + if (!beginDate.equals("")) { + beginDateParsed = tryParse(beginDate); + if (beginDateParsed != null && (granularity.toLowerCase().equals("monthly") || beginDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(beginDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "usagecounts.openaire.eu", "Unspecified Date Arguments", "Begin Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed))); + } + + Date endDateParsed; + if (!endDate.equals("")) { + endDateParsed = tryParse(endDate); + if (endDateParsed != null && (granularity.toLowerCase().equals("monthly") || endDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(endDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "usagecounts.openaire.eu", "Unspecified Date Arguments", "End Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed))); + } + //log.error("dates: " + beginDateParsed.toString() + " - " + endDateParsed.toString()); + + if (beginDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "Begin Date: " + beginDate + " is not a valid date")); + } + if (endDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "End Date: " + endDate + " is not a valid date")); + } + if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "BeginDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed) + "\' is greater than EndDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed) + "\'")); + } + + List reportFilters = new ArrayList(); + reportFilters.add(new Filter("BeginDate", beginDate)); + reportFilters.add(new Filter("EndDate", endDate)); + String reportID = "PR"; + String reportName = "Platform Master Report"; + //String institutionName = "Insititution Name " + repositoryIdentifier; + String institutionName = usageStatsRepository.getInstitutionName(repositoryIdentifier); + List institutionIdD = new ArrayList(); + institutionIdD.add(new SUSHI_Org_Identifiers("Openaire", repositoryIdentifier)); + + List reportItems = new ArrayList(); + usageStatsRepository.executeBatchItemsPR_P1(reportItems, repositoryIdentifier, beginDateParsed, endDateParsed); + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + + if (reportExceptions.size() == 0) { + reportExceptions = null; + } + + COUNTER_Platform_Report reportPr = new COUNTER_Platform_Report(dateTime.format(formatter), customerID, reportID, reportName, institutionName, institutionIdD, reportExceptions, reportFilters, reportItems); + return reportPr; + } + + @Override + public COUNTER_Item_Report buildReportIR(String customerID, String repoID, String itemIdentifier, String beginDate, + String endDate, List metricType, String dataType, String granularity) throws Exception { + + ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + List reportItems = new ArrayList<>(); + List reportExceptions = new ArrayList<>(); + List reportFilters = new ArrayList(); + List orgIdentifiers = new ArrayList<>(); + reportFilters.add(new Filter("BeginDate", beginDate)); + reportFilters.add(new Filter("EndDate", endDate)); + + String repositoryIdentifier = ""; + + if (!repoID.equals("")) { + repositoryIdentifier = usageStatsRepository.getInstitutionID(repoID); + if (repositoryIdentifier.equals("-1")) { + reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "RepositoryIdentifier: " + repoID + " is not valid")); + } + } + + if(repositoryIdentifier.equals("")) + orgIdentifiers=null; + else + orgIdentifiers.add(new SUSHI_Org_Identifiers("Openaire", repositoryIdentifier)); + + if (!granularity.equalsIgnoreCase("totals") && !granularity.equalsIgnoreCase("monthly")) { + reportExceptions.add(new SUSHI_Error_Model("3062", "Warning", "Invalid ReportAttribute Value", "usagecounts.openaire.eu", "Granularity: \'" + granularity + "\' unknown. Defaulting to Monthly")); + granularity = "Monthly"; + } + + Date beginDateParsed; + if (!beginDate.equals("")) { + beginDateParsed = tryParse(beginDate); + if (beginDateParsed != null && (granularity.toLowerCase().equals("monthly") || beginDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(beginDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + } else if (beginDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "Begin Date: " + beginDate + " is not a valid date")); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "Unspecified Date Arguments", "usagecounts.openaire.eu", "Begin Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed))); + } + + Date endDateParsed; + if (!endDate.equals("")) { + endDateParsed = tryParse(endDate); + if (endDateParsed != null && (granularity.toLowerCase().equals("monthly") || endDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(endDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + } else if (endDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "End Date: " + endDate + " is not a valid date")); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "Unspecified Date Arguments", "usagecounts.openaire.eu", "End Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed))); + } + + + // Calucalte time difference + // in milliseconds + beginDateParsed = tryParse(beginDate); + Calendar temp = Calendar.getInstance(); + temp.setTime(beginDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + + + endDateParsed = tryParse(endDate); + Calendar temp1 = Calendar.getInstance(); + temp1.setTime(endDateParsed); + temp1.set(Calendar.DAY_OF_MONTH, temp1.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp1.getTime(); + + long difference_In_Time + = endDateParsed.getTime() - beginDateParsed.getTime(); + long difference_In_Years = (difference_In_Time/ (1000 * 60 * 60 * 24)); + + if(difference_In_Years>365) + reportExceptions.add(new SUSHI_Error_Model("4000", "Notice", "Requested Period for more than a year not allowed", "usagecounts.openaire.eu", "Contact usagecounts@openaire.eu for longer period")); +// if (beginDateParsed == null) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "Begin Date: " + beginDate + " is not a valid date")); +// } +// if (endDateParsed == null) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "End Date: " + endDate + " is not a valid date")); +// } + if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "BeginDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed) + "\' is greater than EndDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed) + "\'")); + } + + String institutionName=null; + if(repositoryIdentifier!="-1") + institutionName = usageStatsRepository.getInstitutionName(repositoryIdentifier); + if(repoID.equals("")) + institutionName=null; + + //if (!repositoryIdentifier.equals("")) { + // repoid = usageStatsRepository.getInstitutionID(repositoryIdentifier); +// if (repositoryIdentifier.equals("-1")) { +// reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "RepositoryIdentifier: " + repoID + " is not valid")); +// } + //} + String itemid = ""; + if (!itemIdentifier.equals("")) { + String[] split = itemIdentifier.split(":"); + switch (split[0].toLowerCase()) { + case "oid": + itemid = itemIdentifier; + break; + case "doi": + itemid = itemIdentifier; + break; +// case "openaire": +// itemid = itemIdentifier; +// break; + default: + reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "ItemIdentifier: " + itemIdentifier + " is not valid")); + itemid = "-1"; + } + } + if (itemid.equals("") && repoID.equals("")) { + reportExceptions.add(new SUSHI_Error_Model("3070", "Error", "Required Filter Missing", "usagecounts.openaire.eu", "ItemIdentifier or RepositoryIdentifier must be supplied")); + } + //if (reportName.equalsIgnoreCase("ar1")) { + if (!itemid.equals("-1") && !repositoryIdentifier.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { + if (!itemid.equals("") && !repositoryIdentifier.equals("")) { + //if (dataType.equalsIgnoreCase("") || dataType.equalsIgnoreCase("article")) { + if (reportExceptions.size() == 0) { + //reportExceptions = null; + usageStatsRepository.executeItemIR(reportItems, repositoryIdentifier, itemIdentifier, beginDateParsed, endDateParsed, metricType, dataType, granularity); + } + + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + // } else { + //reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + //} + } else if (!repoID.equals("")) { + if (reportExceptions.size() == 0) { + //reportExceptions = null; + usageStatsRepository.executeBatchItemsIR(reportItems, repositoryIdentifier, itemIdentifier, beginDateParsed, endDateParsed, metricType, dataType, granularity); + } + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + } + } + if (repoID.equals("")) { + if (reportExceptions.isEmpty()) { + //reportExceptions = null; + usageStatsRepository.executeItemIR(reportItems, null, itemIdentifier, beginDateParsed, endDateParsed, metricType, dataType, granularity); + } + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + } + + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + //} else if (reportName.equals("")) { + // reportExceptions.add(new SUSHI_Error_Model("3050", "Error", "usagecounts.openaire.eu", "Report argument is missing", "You must supply a Report argument")); + //} else { + // reportExceptions.add(new SUSHI_Error_Model("3000", "Error", "usagecounts.openaire.eu", "Report " + reportName + " not supported", "Supported reports: AR1, IR1, RR1, BR1, BR2")); + //} + + if (reportExceptions.isEmpty()) { + reportExceptions = null; + } + + COUNTER_Item_Report reportResponse = new COUNTER_Item_Report(dateTime.format(formatter), customerID, "IR", "Item Report", institutionName, orgIdentifiers, + reportExceptions, reportFilters, reportItems); + log.info("Total report items " + reportItems.size()); + + if (reportItems.size() > compression_max_number_of_records) { + log.info("Compression due to " + reportItems.size()); + reportForCompression = true; + } + return reportResponse; + } + + @Override + public COUNTER_Dataset_Report buildReportDSR(String customerID, String repositoryIdentifier, String itemIdentifier, String beginDate, + String endDate, List metricType,String granularity) throws Exception { + + ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + List reportItems = new ArrayList<>(); + List reportExceptions = new ArrayList<>(); + List reportFilters = new ArrayList(); + List orgIdentifiers = new ArrayList<>(); + reportFilters.add(new Filter("BeginDate", beginDate)); + reportFilters.add(new Filter("EndDate", endDate)); + + if (!repositoryIdentifier.equals("")) { + repositoryIdentifier = usageStatsRepository.getInstitutionID(repositoryIdentifier); + } + + orgIdentifiers.add(new SUSHI_Org_Identifiers("Openaire", repositoryIdentifier)); + + if (!granularity.equalsIgnoreCase("totals") && !granularity.equalsIgnoreCase("monthly")) { + reportExceptions.add(new SUSHI_Error_Model("3062", "Warning", "Invalid ReportAttribute Value", "usagecounts.openaire.eu", "Granularity: \'" + granularity + "\' unknown. Defaulting to Monthly")); + granularity = "Monthly"; + } + + Date beginDateParsed; + if (!beginDate.equals("")) { + beginDateParsed = tryParse(beginDate); + if (beginDateParsed != null && (granularity.toLowerCase().equals("monthly") || beginDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(beginDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); + beginDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "Unspecified Date Arguments", "usagecounts.openaire.eu", "Begin Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed))); + } + + Date endDateParsed; + if (!endDate.equals("")) { + endDateParsed = tryParse(endDate); + if (endDateParsed != null && (granularity.toLowerCase().equals("monthly") || endDate.length() == 7)) { + Calendar temp = Calendar.getInstance(); + temp.setTime(endDateParsed); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + } + } else { + Calendar temp = Calendar.getInstance(); + temp.add(Calendar.MONTH, -1); + temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); + endDateParsed = temp.getTime(); + reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "Unspecified Date Arguments", "usagecounts.openaire.eu", "End Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed))); + } + + if (beginDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "Begin Date: " + beginDate + " is not a valid date")); + } + if (endDateParsed == null) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "End Date: " + endDate + " is not a valid date")); + } + if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { + reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "Invalid Date Arguments", "usagecounts.openaire.eu", "BeginDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed) + "\' is greater than EndDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed) + "\'")); + } + + String repoid = repositoryIdentifier; + String institutionName = usageStatsRepository.getInstitutionName(repositoryIdentifier); + + //if (!repositoryIdentifier.equals("")) { + // repoid = usageStatsRepository.getInstitutionID(repositoryIdentifier); + if (repoid.equals("-1")) { + reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "RepositoryIdentifier: " + repositoryIdentifier + " is not valid")); + } + //} + String itemid = ""; + if (!itemIdentifier.equals("")) { + String[] split = itemIdentifier.split(":"); + switch (split[0].toLowerCase()) { + case "oid": + itemid = itemIdentifier; + break; + case "doi": + itemid = itemIdentifier; + break; +// case "openaire": +// itemid = itemIdentifier; +// break; + default: + reportExceptions.add(new SUSHI_Error_Model("3060", "Error", "Invalid Filter Value", "usagecounts.openaire.eu", "ItemIdentifier: " + itemIdentifier + " is not valid")); + itemid = "-1"; + } + } + if (itemid.equals("") && repoid.equals("")) { + reportExceptions.add(new SUSHI_Error_Model("3070", "Error", "Required Filter Missing", "usagecounts.openaire.eu", "ItemIdentifier or RepositoryIdentifier must be supplied")); + } + //if (reportName.equalsIgnoreCase("ar1")) { + if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { + if (!itemid.equals("") && !repoid.equals("")) { + //if (dataType.equalsIgnoreCase("") || dataType.equalsIgnoreCase("article")) { + usageStatsRepository.executeItemDSR(reportItems, repoid, itemIdentifier, beginDateParsed, endDateParsed, metricType, granularity); + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + // } else { + //reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + //} + } else if (!repoid.equals("")) { + if(usageStatsRepository.checkIfDatacite(repoid)) + usageStatsRepository.executeBatchItemsDSRDatacite(reportItems, repoid, itemIdentifier, beginDateParsed, endDateParsed, metricType, granularity); + else + usageStatsRepository.executeBatchItemsDSR(reportItems, repoid, itemIdentifier, beginDateParsed, endDateParsed, metricType, granularity); + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + } + } + if (repoid.equals("")) { + usageStatsRepository.executeItemDSR(reportItems, null, itemIdentifier, beginDateParsed, endDateParsed, metricType, granularity); + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + } + +// } +// //} else if (reportName.equalsIgnoreCase("br1")) { +// if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// if (!itemid.equals("")) { +// if (dataType.equalsIgnoreCase("") || dataType.equalsIgnoreCase("book")) { +// usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else if (!repoid.equals("")) { +// usageStatsRepository.executeBatchItems(reportItems, repoid, "Book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } +// } +// //} +// if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// if (!itemid.equals("")) { +// if (dataType.equalsIgnoreCase("") || dataType.equalsIgnoreCase("part of book or chapter of book")) { +// usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Part of book or chapter of book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } else if (!repoid.equals("")) { +// usageStatsRepository.executeBatchItems(reportItems, repoid, "Part of book or chapter of book", beginDateParsed, endDateParsed, granularity); +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } +// } +// //else if (reportName.equalsIgnoreCase("ir1")) { +// if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// if (!itemid.equals("")) { +// usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, dataType, beginDateParsed, endDateParsed, granularity); +// } else if (!repoid.equals("")) { +// usageStatsRepository.executeBatchItems(reportItems, repoid, dataType, beginDateParsed, endDateParsed, granularity); +// } +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// } +// //} else if (reportName.equalsIgnoreCase("rr1")) { +// if (!repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// usageStatsRepository.executeRepo(reportItems, repoid, dataType, beginDateParsed, endDateParsed, granularity); +// } +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// //} else if (reportName.equalsIgnoreCase("jr1")) { +// if (!repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) { +// usageStatsRepository.executeJournal(reportItems, repoid, dataType, beginDateParsed, endDateParsed, granularity); +// } + if (reportItems.isEmpty()) { + reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); + } + //} else if (reportName.equals("")) { + // reportExceptions.add(new SUSHI_Error_Model("3050", "Error", "usagecounts.openaire.eu", "Report argument is missing", "You must supply a Report argument")); + //} else { + // reportExceptions.add(new SUSHI_Error_Model("3000", "Error", "usagecounts.openaire.eu", "Report " + reportName + " not supported", "Supported reports: AR1, IR1, RR1, BR1, BR2")); + //} + + if (reportExceptions.size() == 0) { + reportExceptions = null; + } + COUNTER_Dataset_Report reportResponse = new COUNTER_Dataset_Report(dateTime.format(formatter), customerID, "DSR", "Dataset Report", institutionName, orgIdentifiers, + reportExceptions, reportFilters, reportItems); + + return reportResponse; + } +// @Override +// public COUNTER_Title_Report buildReportIR(String customerID, String repositoryIdentifier, String itemIdentifier, String beginDate, +// String endDate, String metricType, String dataType, String granularity) { +// List reportExceptions = new ArrayList<>(); +// ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone +// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); +// //Display Report Created Day +// +// repositoryIdentifier = usageStatsRepository.getInstitutionID(repositoryIdentifier); +// +// Date beginDateParsed; +// if (!beginDate.equals("")) { +// beginDateParsed = tryParse(beginDate); +// if (beginDateParsed != null && (granularity.toLowerCase().equals("monthly") || beginDate.length() == 7)) { +// Calendar temp = Calendar.getInstance(); +// temp.setTime(beginDateParsed); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); +// beginDateParsed = temp.getTime(); +// } +// } else { +// Calendar temp = Calendar.getInstance(); +// temp.add(Calendar.MONTH, -1); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH)); +// beginDateParsed = temp.getTime(); +// reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "usagecounts.openaire.eu", "Unspecified Date Arguments", "Begin Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed))); +// } +// +// Date endDateParsed; +// if (!endDate.equals("")) { +// endDateParsed = tryParse(endDate); +// if (endDateParsed != null && (granularity.toLowerCase().equals("monthly") || endDate.length() == 7)) { +// Calendar temp = Calendar.getInstance(); +// temp.setTime(endDateParsed); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); +// endDateParsed = temp.getTime(); +// } +// } else { +// Calendar temp = Calendar.getInstance(); +// temp.add(Calendar.MONTH, -1); +// temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH)); +// endDateParsed = temp.getTime(); +// reportExceptions.add(new SUSHI_Error_Model("3021", "Warning", "usagecounts.openaire.eu", "Unspecified Date Arguments", "End Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed))); +// } +// //log.error("dates: " + beginDateParsed.toString() + " - " + endDateParsed.toString()); +// +// if (beginDateParsed == null) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "Begin Date: " + beginDate + " is not a valid date")); +// } +// if (endDateParsed == null) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "End Date: " + endDate + " is not a valid date")); +// } +// if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { +// reportExceptions.add(new SUSHI_Error_Model("3020", "Error", "usagecounts.openaire.eu", "Invalid Date Arguments", "BeginDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed) + "\' is greater than EndDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed) + "\'")); +// } +// +// List reportFilters = new ArrayList(); +// reportFilters.add(new Filter("BeginDate", beginDate)); +// reportFilters.add(new Filter("EndDate", endDate)); +// String reportID = "ΤR"; +// String reportName = "Title Master Report"; +// String institutionName = usageStatsRepository.getInstitutionName(repositoryIdentifier); +// List institutionIdD = new ArrayList(); +// institutionIdD.add(new SUSHI_Org_Identifiers("Openaire", repositoryIdentifier)); +// +// List reportItems = new ArrayList(); +// usageStatsRepository.executeBatchItemsTR(reportItems, repositoryIdentifier, itemIdentifier,beginDateParsed, endDateParsed, metricType, dataType, granularity); +// +// if (reportItems.isEmpty()) { +// reportExceptions.add(new SUSHI_Error_Model("3030", "Error", "usagecounts.openaire.eu", "No Usage Available for Requested Dates", "Service did not find any data")); +// } +// +// if (reportExceptions.size() == 0) { +// reportExceptions = null; +// } +// COUNTER_Title_Report reportTR = new COUNTER_Title_Report(dateTime.format(formatter), customerID, reportID, reportName, institutionName, institutionIdD, reportExceptions, reportFilters, reportItems); +// return reportTR; +// } + + @Override + public String displayReportPR(String customerID, String repositoryIdentifier, String beginDate, + String endDate, String metricType, String dataType, String granularity) { + ObjectMapper objectMapper = new ObjectMapper(); + COUNTER_Platform_Report report = buildReportPR(customerID, repositoryIdentifier, beginDate, endDate, metricType, dataType, granularity); + + try { + if (reportForCompression == false) { + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(report) + "
"; + } else { + log.info((beginDate + " " + endDate)); + try { + java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis()); + //System.out.println("String start " + timestamp1); + log.info("Start building report..." + timestamp1); + //String outputname = "PR" + "_" + repositoryIdentifier.replace("_", "").replace(":", "") +"_"+ beginDate.replace("-", "") + "_" + endDate.replace("-", ""); + String outputname = "PR" + "_" + repositoryIdentifier.substring(repositoryIdentifier.indexOf(":") + 1) + "_" + beginDate.replace("-", "") + "_" + endDate.replace("-", ""); + String directory = new File(download_folder).getAbsolutePath(); + //writer.writeValue(new File(directory + "/" + outputname + ".json"), tmpReport); + BufferedWriter writer = new BufferedWriter(new FileWriter(directory + "/" + outputname + ".json")); + writer.write(objectMapper.writer().writeValueAsString(report)); + writer.close(); + + FileOutputStream fos = new FileOutputStream(directory + "/" + outputname + ".zip"); + ZipOutputStream zipOut = new ZipOutputStream(fos); + File fileToZip = new File(directory + "/" + outputname + ".json"); + FileInputStream fis = new FileInputStream(fileToZip); + ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); + zipOut.putNextEntry(zipEntry); + byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + zipOut.close(); + fis.close(); + fos.close(); + fileToZip.delete(); + + java.sql.Timestamp timestamp2 = new java.sql.Timestamp(System.currentTimeMillis()); + //System.out.println("String end " + timestamp2); + log.info("Report created..." + timestamp2); + + reportForCompression = false; + return new String(sushi_lite_server + "/download/" + outputname + ".zip"); + } catch (JsonProcessingException ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); + } + } + } catch (Exception ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); + } + + return null; + } + + @Override + public String displayReportPR_P1(String customerID, String repositoryIdentifier, + String beginDate, + String endDate + ) { + ObjectMapper objectMapper = new ObjectMapper(); + log.info((beginDate + " " + endDate)); + try { + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR_P1(customerID, repositoryIdentifier, beginDate, endDate)) + "
"; + + } catch (JsonProcessingException ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class + .getName()).log(Level.SEVERE, null, ex); + } + return null; + + } + + @Override + public String displayReportIR(String customerID, String repositoryIdentifier, + String itemIdentifier, String beginDate, + String endDate, List metricType, + String dataType, String granularity + ) { + + ObjectMapper objectMapper = new ObjectMapper(); + try { + COUNTER_Item_Report report = buildReportIR(customerID, repositoryIdentifier, itemIdentifier, beginDate, endDate, metricType, dataType, granularity); + if (reportForCompression == false) { + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(report) + "
"; + } else { + log.info((beginDate + " " + endDate)); + + try { + java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis()); + //System.out.println("String start " + timestamp1); + log.info("Start building report..." + timestamp1); + String outputname = "IR" + "_" + repositoryIdentifier.substring(repositoryIdentifier.indexOf(":") + 1) + "_" + beginDate.replace("-", "") + "_" + endDate.replace("-", ""); + String directory = new File(download_folder).getAbsolutePath(); + BufferedWriter writer = new BufferedWriter(new FileWriter(directory + "/" + outputname + ".json")); + writer.write(objectMapper.writer().writeValueAsString(report)); + writer.close(); + + FileOutputStream fos = new FileOutputStream(directory + "/" + outputname + ".zip"); + ZipOutputStream zipOut = new ZipOutputStream(fos); + File fileToZip = new File(directory + "/" + outputname + ".json"); + FileInputStream fis = new FileInputStream(fileToZip); + ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); + zipOut.putNextEntry(zipEntry); + byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + zipOut.close(); + fis.close(); + fos.close(); + fileToZip.delete(); + + java.sql.Timestamp timestamp2 = new java.sql.Timestamp(System.currentTimeMillis()); + //System.out.println("String end " + timestamp2); + log.info("Report created..." + timestamp2); + reportForCompression = false; + return new String(sushi_lite_server + "/download/" + outputname + ".zip"); + + } catch (Exception ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class + .getName()).log(Level.SEVERE, null, ex); + + } + + } + } catch (Exception ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class + .getName()).log(Level.SEVERE, null, ex); + } + return null; + } + + @Override + public String displayReportDSR(String customerID, String repositoryIdentifier, + String itemIdentifier, String beginDate, + String endDate, List metricType,String granularity) { + + ObjectMapper objectMapper = new ObjectMapper(); + try { + COUNTER_Dataset_Report report = buildReportDSR(customerID, repositoryIdentifier, itemIdentifier, beginDate, endDate, metricType, granularity); + if (reportForCompression == false) { + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(report) + "
"; + } else { + log.info((beginDate + " " + endDate)); + + try { + java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis()); + //System.out.println("String start " + timestamp1); + log.info("Start building report..." + timestamp1); + String outputname = "DSR" + "_" + repositoryIdentifier.substring(repositoryIdentifier.indexOf(":") + 1) + "_" + beginDate.replace("-", "") + "_" + endDate.replace("-", ""); + String directory = new File(download_folder).getAbsolutePath(); + BufferedWriter writer = new BufferedWriter(new FileWriter(directory + "/" + outputname + ".json")); + writer.write(objectMapper.writer().writeValueAsString(report)); + writer.close(); + + FileOutputStream fos = new FileOutputStream(directory + "/" + outputname + ".zip"); + ZipOutputStream zipOut = new ZipOutputStream(fos); + File fileToZip = new File(directory + "/" + outputname + ".json"); + FileInputStream fis = new FileInputStream(fileToZip); + ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); + zipOut.putNextEntry(zipEntry); + byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + zipOut.close(); + fis.close(); + fos.close(); + fileToZip.delete(); + + java.sql.Timestamp timestamp2 = new java.sql.Timestamp(System.currentTimeMillis()); + //System.out.println("String end " + timestamp2); + log.info("Report created..." + timestamp2); + reportForCompression = false; + return new String(sushi_lite_server + "/download/" + outputname + ".zip"); + + } catch (Exception ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class + .getName()).log(Level.SEVERE, null, ex); + + } + + } + } catch (Exception ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class + .getName()).log(Level.SEVERE, null, ex); + } + return null; + } + @Override + public ArrayList buildMembersList() { + ArrayList consortiumMembers = usageStatsRepository.buildMembersList(); + return consortiumMembers; + } + + @Override + public String displayConsortiumMemberList() { + ObjectMapper objectMapper = new ObjectMapper(); + try { + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildMembersList()) + "
"; + + } catch (Exception ex) { + java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class + .getName()).log(Level.SEVERE, null, ex); + } + return null; + + } +} + +class JSONUtil { + + public static String escape(String input) { + StringBuilder output = new StringBuilder(); + + for (int i = 0; i < input.length(); i++) { + char ch = input.charAt(i); + int chx = (int) ch; + + // let's not put any nulls in our strings + assert (chx != 0); + + if (ch == '\n') { + output.append("\\n"); + } else if (ch == '\t') { + output.append("\\t"); + } else if (ch == '\r') { + output.append("\\r"); + } else if (ch == '\\') { + output.append("\\\\"); + } else if (ch == '"') { + output.append("\\\""); + } else if (ch == '\b') { + output.append("\\b"); + } else if (ch == '\f') { + output.append("\\f"); + } else if (chx >= 0x10000) { + assert false : "Java stores as u16, so it should never give us a character that's bigger than 2 bytes. It literally can't."; + } else if (chx > 127) { + output.append(String.format("\\u%04x", chx)); + } else { + output.append(ch); + } + } + + return output.toString(); + } + + public static String unescape(String input) { + StringBuilder builder = new StringBuilder(); + + int i = 0; + while (i < input.length()) { + char delimiter = input.charAt(i); + i++; // consume letter or backslash + + if (delimiter == '\\' && i < input.length()) { + + // consume first after backslash + char ch = input.charAt(i); + i++; + + if (ch == '\\' || ch == '/' || ch == '"' || ch == '\'') { + builder.append(ch); + } else if (ch == 'n') { + builder.append('\n'); + } else if (ch == 'r') { + builder.append('\r'); + } else if (ch == 't') { + builder.append('\t'); + } else if (ch == 'b') { + builder.append('\b'); + } else if (ch == 'f') { + builder.append('\f'); + } else if (ch == 'u') { + + StringBuilder hex = new StringBuilder(); + + // expect 4 digits + if (i + 4 > input.length()) { + throw new RuntimeException("Not enough unicode digits! "); + } + for (char x : input.substring(i, i + 4).toCharArray()) { + if (!Character.isLetterOrDigit(x)) { + throw new RuntimeException("Bad character in unicode escape."); + } + hex.append(Character.toLowerCase(x)); + } + i += 4; // consume those four digits. + + int code = Integer.parseInt(hex.toString(), 16); + builder.append((char) code); + } else { + throw new RuntimeException("Illegal escape sequence: \\" + ch); + } + } else { // it's not a backslash, or it's the last character. + builder.append(delimiter); + } + } + return builder.toString(); + } +} diff --git a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceSample.java b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceSample.java new file mode 100755 index 0000000..98eb6b2 --- /dev/null +++ b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceSample.java @@ -0,0 +1,40 @@ +package eu.dnetlib.usagestats.services; + +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Dataset_Report; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Item_Report; +import eu.dnetlib.usagestats.sushilite.domain.COUNTER_Platform_Report; +import eu.dnetlib.usagestats.sushilite.domain.SUSHI_Service_Status; +import java.util.ArrayList; +import java.util.List; + +public interface SushiLiteService { + +// ReportResponseWrapper buildReport(String reportName, String release, String requestorId, String beginDate, +// String endDate, String repositoryIdentifier, String itemIdentifier, +// String itemDataType, String hasDoi, String granularity, String callback); +// +// String displayReport(String reportName, String release, String requestorId, String beginDate, +// String endDate, String repositoryIdentifier, String itemIdentifier, +// String itemDataType, String hasDoi, String granularity, String callback, String pretty); + + SUSHI_Service_Status buildReportStatus(); + String displayReportStatus(); + + ArrayList buildReportSupported(); + String displayReportsSupported(); + + ArrayList buildMembersList(); + String displayConsortiumMemberList(); + + COUNTER_Platform_Report buildReportPR(String customerID, String repositoryIdentifier, String beginDate,String endDate, String metricType, String dataType,String granularity); + String displayReportPR(String customerID, String repositoryIdentifier, String beginDate,String endDate, String metricType, String dataType,String granularity); + + COUNTER_Platform_Report buildReportPR_P1(String customerID, String repositoryIdentifier, String beginDate,String endDate); + String displayReportPR_P1(String customerID, String repositoryIdentifier, String beginDate, String endDate); + + COUNTER_Item_Report buildReportIR(String customerID, String repositoryIdentifier, String itemIdentifier, String beginDate,String endDate, List metricType, String dataType,String granularity) throws Exception; + String displayReportIR(String customerID, String repositoryIdentifier, String itemIdentifier, String beginDate,String endDate, List metricType, String dataType,String granularity); + + COUNTER_Dataset_Report buildReportDSR(String customerID, String repositoryIdentifier, String itemIdentifier, String beginDate,String endDate, List metricType, String granularity) throws Exception; + String displayReportDSR(String customerID, String repositoryIdentifier, String itemIdentifier, String beginDate,String endDate, List metricType, String granularity); +}