openaire-usage-stats-api-r5/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java

387 lines
22 KiB
Java
Raw Normal View History

2020-12-30 14:56:37 +01:00
package eu.dnetlib.usagestats.services;
2021-01-05 13:22:12 +01:00
import com.fasterxml.jackson.core.JsonProcessingException;
2020-12-30 14:56:37 +01:00
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.usagestats.repositories.UsageStatsRepository;
2021-01-05 13:22:12 +01:00
import eu.dnetlib.usagestats.sushilite.domain.Alert;
import eu.dnetlib.usagestats.sushilite.domain.Filter;
import eu.dnetlib.usagestats.sushilite.domain.InstitutionID;
2020-12-30 14:56:37 +01:00
import eu.dnetlib.usagestats.sushilite.domain.ReportItem;
import eu.dnetlib.usagestats.sushilite.domain.ReportException;
2021-01-05 13:22:12 +01:00
import eu.dnetlib.usagestats.sushilite.domain.ReportPR;
2020-12-30 14:56:37 +01:00
import eu.dnetlib.usagestats.sushilite.domain.ReportResponse;
import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper;
2021-01-05 13:22:12 +01:00
import eu.dnetlib.usagestats.sushilite.domain.ReportStatus;
import eu.dnetlib.usagestats.sushilite.domain.ReportSupported;
2020-12-30 14:56:37 +01:00
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
2021-01-05 13:22:12 +01:00
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
2020-12-30 14:56:37 +01:00
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
2021-01-05 13:22:12 +01:00
import java.util.logging.Level;
2020-12-30 14:56:37 +01:00
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;
}
2021-01-05 13:22:12 +01:00
2020-12-30 14:56:37 +01:00
@Override
2021-01-05 13:22:12 +01:00
public ReportResponseWrapper buildReport(String reportName, String release,
String requestorId, String beginDate,
2020-12-30 14:56:37 +01:00
String endDate, String repositoryIdentifier, String itemIdentifier,
2021-01-05 13:22:12 +01:00
String itemDataType, String hasDoi, String granularity,
String callback) {
2020-12-30 14:56:37 +01:00
List<ReportItem> reportItems = new ArrayList<>();
List<ReportException> 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
2021-01-05 13:22:12 +01:00
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) {
2020-12-30 14:56:37 +01:00
ObjectMapper objectMapper = new ObjectMapper();
try {
if (pretty.equalsIgnoreCase("pretty")) {
2021-01-05 13:22:12 +01:00
return "<pre>" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReport(reportName, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/") + "</pre>";
2020-12-30 14:56:37 +01:00
}
2021-01-05 13:22:12 +01:00
return objectMapper.writeValueAsString(buildReport(reportName, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/");
2020-12-30 14:56:37 +01:00
} catch (Exception e) {
e.printStackTrace();
return null;
}
2021-01-05 13:22:12 +01:00
//return report.getReport(reportP, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback, pretty);
2020-12-30 14:56:37 +01:00
}
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;
}
2021-01-05 13:22:12 +01:00
@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 "<pre>" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportStatus()) + "</pre>";
} 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 "<pre>" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportSupported()) + "</pre>";
} 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<ReportException> 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<Filter> 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<InstitutionID> institutionIdD = new ArrayList();
institutionIdD.add(new InstitutionID("Openaire", repositoryIdentifier));
List<ReportItem> 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 "<pre>" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR(repositoryIdentifier, beginDate, endDate)) + "</pre>";
} catch (JsonProcessingException ex) {
java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
2020-12-30 14:56:37 +01:00
}