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.Filter; import eu.dnetlib.usagestats.sushilite.domain.InstitutionID; import eu.dnetlib.usagestats.sushilite.domain.ReportItem; import eu.dnetlib.usagestats.sushilite.domain.ReportException; import eu.dnetlib.usagestats.sushilite.domain.ReportPR; import eu.dnetlib.usagestats.sushilite.domain.ReportResponse; import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper; import eu.dnetlib.usagestats.sushilite.domain.ReportStatus; import eu.dnetlib.usagestats.sushilite.domain.ReportSupported; 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 org.apache.log4j.Logger; @Service public class SushiLiteServiceImpl implements SushiLiteService { private final UsageStatsRepository usageStatsRepository; private final Logger log = Logger.getLogger(this.getClass()); 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 ReportException("3062", "Warning", "Invalid ReportAttribute Value", "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 ReportException("3021", "Warning", "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 ReportException("3021", "Warning", "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 ReportException("3020", "Error", "Invalid Date Arguments", "Begin Date: " + beginDate + " is not a valid date")); } if (endDateParsed == null) { reportExceptions.add(new ReportException("3020", "Error", "Invalid Date Arguments", "End Date: " + endDate + " is not a valid date")); } if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { reportExceptions.add(new ReportException("3020", "Error", "Invalid Date Arguments", "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 ReportException("3060", "Error", "Invalid Filter Value", "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 ReportException("3060", "Error", "Invalid Filter Value", "ItemIdentifier: " + itemIdentifier + " is not valid")); itemid = "-1"; } } if (itemid.equals("") && repoid.equals("") && !reportName.equalsIgnoreCase("rr1") && !reportName.equalsIgnoreCase("jr1")) { reportExceptions.add(new ReportException("3070", "Error", "Required Filter Missing", "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 ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data")); } } else { reportExceptions.add(new ReportException("3030", "Error", "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 ReportException("3030", "Error", "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 ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data")); } } else { reportExceptions.add(new ReportException("3030", "Error", "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 ReportException("3030", "Error", "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 ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data")); } } else { reportExceptions.add(new ReportException("3030", "Error", "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 ReportException("3030", "Error", "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 ReportException("3030", "Error", "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 ReportException("3030", "Error", "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 ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data")); } } else if (reportName.equals("")) { reportExceptions.add(new ReportException("3050", "Error", "Report argument is missing", "You must supply a Report argument")); } else { reportExceptions.add(new ReportException("3000", "Error", "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 ReportStatus 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 alert1 = new Alert("2020-12-28T13:32:44.632Z", "string"); Alert alert2 = new Alert(dateTime.format(formatter), "string"); ArrayList alertsList = new ArrayList(); alertsList.add(alert1); alertsList.add(alert2); ReportStatus reportStatus = new ReportStatus("desctiption", true, "registry_url", "note", alertsList); return (reportStatus); } @Override public String displayReportStatus() { ObjectMapper objectMapper = new ObjectMapper(); try { return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportStatus()) + "
"; } 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(); ReportSupported r1 = new ReportSupported("Report_Name", "Report_ID", "Release", "Report_Description", "Path"); ReportSupported r2 = new ReportSupported("Report_Name2", "Report_ID2", "Release2", "Report_Description2", "Path2"); reportSupportedList.add(r1); reportSupportedList.add(r2); 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 ReportPR buildReportPR(String repositoryIdentifier, String beginDate, String endDate) { 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 granularity = "totals"; 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 ReportException("3021", "Warning", "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 ReportException("3021", "Warning", "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 ReportException("3020", "Error", "Invalid Date Arguments", "Begin Date: " + beginDate + " is not a valid date")); } if (endDateParsed == null) { reportExceptions.add(new ReportException("3020", "Error", "Invalid Date Arguments", "End Date: " + endDate + " is not a valid date")); } if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) { reportExceptions.add(new ReportException("3020", "Error", "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 = "Platform"; String reportName = "Plaform Total Views & Downloads"; String insitutionName = "Insititution Name " + repositoryIdentifier; List institutionIdD = new ArrayList(); institutionIdD.add(new InstitutionID("Openaire", repositoryIdentifier)); List reportItems = new ArrayList(); usageStatsRepository.executeBatchItemsPR(reportItems, repositoryIdentifier, beginDateParsed, endDateParsed, granularity); if (reportItems.isEmpty()) { reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data")); } ReportPR reportPr = new ReportPR(dateTime.format(formatter), repositoryIdentifier, reportID, reportName, insitutionName, institutionIdD, reportExceptions, reportFilters, reportItems); return reportPr; } @Override public String displayReportPR(String repositoryIdentifier, String beginDate, String endDate) { ObjectMapper objectMapper = new ObjectMapper(); log.info((beginDate + " " + endDate)); try { return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR(repositoryIdentifier, beginDate, endDate)) + "
"; } catch (JsonProcessingException ex) { java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } return null; } }