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_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.COUNTER_Title_Report; 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 java.sql.SQLException; 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 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 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 COUNTER_Platform_Report buildReportPR(String customerID, String repositoryIdentifier, 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 = "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(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); return reportPr; } @Override public COUNTER_Platform_Report buildReportPR_P1(String customerID, String repositoryIdentifier, 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 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 = "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 repositoryIdentifier, 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)); 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("")) { //if (dataType.equalsIgnoreCase("") || dataType.equalsIgnoreCase("article")) { usageStatsRepository.executeItemIR(reportItems, repoid, 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("")) { usageStatsRepository.executeBatchItemsIR(reportItems, repoid, 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 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_Item_Report reportResponse = new COUNTER_Item_Report(dateTime.format(formatter), customerID, "IR", "Item 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(); log.info((beginDate + " " + endDate)); try { return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR(customerID, repositoryIdentifier, beginDate, endDate, metricType, dataType, granularity)) + "
"; } catch (JsonProcessingException 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(); log.info((beginDate + " " + endDate)); try { return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportIR(customerID, repositoryIdentifier, itemIdentifier, beginDate, endDate, metricType, dataType, granularity)) + "
"; } catch (Exception ex) { java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } return null; } }