From 73fe7e68feafa2025081e89eec5797af15b6f819 Mon Sep 17 00:00:00 2001 From: Smruti Prakash Sahoo Date: Wed, 18 May 2022 12:25:24 +0530 Subject: [PATCH] feat(export): Enable mailing for exported spreadsheet for components Signed-off-by: Smruti Prakash Sahoo --- .../db/ComponentDatabaseHandler.java | 6 ++ .../db/ProjectDatabaseHandler.java | 2 +- .../src/main/resources/sw360.properties | 3 +- .../sw360/components/ComponentHandler.java | 5 + .../sw360/portal/common/PortalConstants.java | 2 + .../portlets/components/ComponentPortlet.java | 99 ++++++++++++++++++- .../resources/html/components/view.jsp | 42 +++++--- .../sw360/exporter/ComponentExporter.java | 5 + .../src/main/thrift/components.thrift | 5 + 9 files changed, 151 insertions(+), 18 deletions(-) diff --git a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java index f12137c5b8..6d6b623a05 100644 --- a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java +++ b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java @@ -2502,4 +2502,10 @@ private void checkSuperAttachmentExists(Release release) { }); } } + + public void sendExportSpreadsheetSuccessMail(String url, String recepient) throws TException { + mailUtil.sendMail(recepient, MailConstants.SUBJECT_SPREADSHEET_EXPORT_SUCCESS, + MailConstants.TEXT_SPREADSHEET_EXPORT_SUCCESS, SW360Constants.NOTIFICATION_CLASS_COMPONENT, "", false, + "component", url); + } } diff --git a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java index 3a108b0d36..19b0e14792 100644 --- a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java +++ b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java @@ -1556,7 +1556,7 @@ private void flattenlinkedReleaseOfRelease(Map rele public void sendExportSpreadsheetSuccessMail(String url, String recepient) throws TException { mailUtil.sendMail(recepient, MailConstants.SUBJECT_SPREADSHEET_EXPORT_SUCCESS, MailConstants.TEXT_SPREADSHEET_EXPORT_SUCCESS, SW360Constants.NOTIFICATION_CLASS_PROJECT, "", false, - url); + "project", url); } private Map createProjectCSRow(String relation, Project prj, diff --git a/backend/src-common/src/main/resources/sw360.properties b/backend/src-common/src/main/resources/sw360.properties index b85d7fdc42..b6c079f5eb 100644 --- a/backend/src-common/src/main/resources/sw360.properties +++ b/backend/src-common/src/main/resources/sw360.properties @@ -56,7 +56,7 @@ subjectForRejectedClearingRequest= Your clearing request <%s> has been rejected subjectForUpdatedProjectWithClearingRequest= Project <%s> with clearing request <%s> updated subjectForSuccessfulExport = Spreadsheet Export Successful -textForSuccessfulExport = The project spreadsheet export successfully completed. Please find the download link(%s) here. +textForSuccessfulExport = The %s spreadsheet export successfully completed. Please find the download link(%s) here. textForNewModerationRequest= a new moderation request has been added to your SW360-account.\n\n textForUpdateModerationRequest= \ one of the moderation requests previously added to your \ @@ -83,3 +83,4 @@ enable.sw360.change.log=false sw360changelog.output.path=sw360changelog/sw360changelog auto.set.ecc.status=false send.project.spreadsheet.export.to.mail.enabled=false +send.component.spreadsheet.export.to.mail.enabled=false \ No newline at end of file diff --git a/backend/src/src-components/src/main/java/org/eclipse/sw360/components/ComponentHandler.java b/backend/src/src-components/src/main/java/org/eclipse/sw360/components/ComponentHandler.java index 684d1e29ca..875bb962ca 100644 --- a/backend/src/src-components/src/main/java/org/eclipse/sw360/components/ComponentHandler.java +++ b/backend/src/src-components/src/main/java/org/eclipse/sw360/components/ComponentHandler.java @@ -643,4 +643,9 @@ public Map> getRecentComponentsSummaryWithPagina PaginationData pageData) throws TException { return handler.getRecentComponentsSummaryWithPagination(user, pageData); } + + @Override + public void sendExportSpreadsheetSuccessMail(String url, String recepient) throws TException { + handler.sendExportSpreadsheetSuccessMail(url, recepient); + } } diff --git a/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/common/PortalConstants.java b/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/common/PortalConstants.java index 1f12fd8a5a..342e656b70 100644 --- a/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/common/PortalConstants.java +++ b/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/common/PortalConstants.java @@ -46,6 +46,7 @@ public class PortalConstants { public static final String CLEARING_REPORT_TEMPLATE_FORMAT; public static final String DISABLE_CLEARING_REQUEST_FOR_PROJECT_WITH_GROUPS; public static final Boolean SEND_PROJECT_SPREADSHEET_EXPORT_TO_MAIL_ENABLED; + public static final Boolean SEND_COMPONENT_SPREADSHEET_EXPORT_TO_MAIL_ENABLED; public static final String LOAD_OPEN_MODERATION_REQUEST = "loadOpenModerationRequest"; public static final String LOAD_CLOSED_MODERATION_REQUEST = "loadClosedModerationRequest"; @@ -699,6 +700,7 @@ public class PortalConstants { System.getProperty("RunComponentVisibilityRestrictionTest", props.getProperty("component.visibility.restriction.enabled", "false"))); DISABLE_CLEARING_REQUEST_FOR_PROJECT_WITH_GROUPS = props.getProperty("org.eclipse.sw360.disable.clearing.request.for.project.group", ""); SEND_PROJECT_SPREADSHEET_EXPORT_TO_MAIL_ENABLED = Boolean.parseBoolean(props.getProperty("send.project.spreadsheet.export.to.mail.enabled", "false")); + SEND_COMPONENT_SPREADSHEET_EXPORT_TO_MAIL_ENABLED = Boolean.parseBoolean(props.getProperty("send.component.spreadsheet.export.to.mail.enabled", "false")); } private PortalConstants() { diff --git a/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/portlets/components/ComponentPortlet.java b/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/portlets/components/ComponentPortlet.java index b5b6e27cae..1a4896c0bf 100644 --- a/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/portlets/components/ComponentPortlet.java +++ b/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/portlets/components/ComponentPortlet.java @@ -219,6 +219,10 @@ public void serveResource(ResourceRequest request, ResourceResponse response) th updateVulnerabilityVerification(request, response); } else if (PortalConstants.EXPORT_TO_EXCEL.equals(action)) { exportExcel(request, response); + } else if (PortalConstants.DOWNLOAD_EXCEL.equals(action)) { + downloadExcel(request, response); + } else if (PortalConstants.EMAIL_EXPORTED_EXCEL.equals(action)) { + exportExcelWithEmail(request, response); } else if (PortalConstants.RELEASE_LINK_TO_PROJECT.equals(action)) { linkReleaseToProject(request, response); } else if (PortalConstants.LOAD_SPDX_LICENSE_INFO.equals(action)) { @@ -242,6 +246,59 @@ public void serveResource(ResourceRequest request, ResourceResponse response) th } } + private void exportExcelWithEmail(ResourceRequest request, ResourceResponse response) { + final User user = UserCacheHolder.getUserFromRequest(request); + final String componentId = request.getParameter(Component._Fields.ID.toString()); + ResourceBundle resourceBundle = ResourceBundleUtil.getBundle("content.Language", request.getLocale(), getClass()); + String token = null; + + try { + setSessionMessage(request, LanguageUtil.get(resourceBundle, + "excel.report.generation.has.started.we.will.send.you.an.email.with.download.link.once.completed")); + boolean extendedByReleases = Boolean.valueOf(request.getParameter(PortalConstants.EXTENDED_EXCEL_EXPORT)); + ComponentService.Iface client = thriftClients.makeComponentClient(); + int total = client.getTotalComponentsCount(user); + PaginationData pageData = new PaginationData(); + pageData.setAscending(true); + Map> pageDtToProjects; + Set projects = new HashSet<>(); + int displayStart = 0; + int rowsPerPage = 500; + while (0 < total) { + pageData.setDisplayStart(displayStart); + pageData.setRowsPerPage(rowsPerPage); + displayStart = displayStart + rowsPerPage; + pageDtToProjects = getFilteredComponentList(request, pageData); + projects.addAll(pageDtToProjects.entrySet().iterator().next().getValue()); + total = total - rowsPerPage; + } + List listOfComponent = new ArrayList(projects); + ComponentExporter exporter = new ComponentExporter(thriftClients.makeComponentClient(), listOfComponent, + user, extendedByReleases); + + token = exporter.makeExcelExportForProject(listOfComponent, user); + + String portletId = (String) request.getAttribute(WebKeys.PORTLET_ID); + ThemeDisplay tD = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); + long plid = tD.getPlid(); + + LiferayPortletURL componentUrl = PortletURLFactoryUtil.create(request, portletId, plid, + PortletRequest.RESOURCE_PHASE); + componentUrl.setParameter("action", PortalConstants.DOWNLOAD_EXCEL); + componentUrl.setParameter("token", token); + componentUrl.setParameter(PortalConstants.EXTENDED_EXCEL_EXPORT, String.valueOf(extendedByReleases)); + + if(!CommonUtils.isNullEmptyOrWhitespace(token)) { + client.sendExportSpreadsheetSuccessMail(componentUrl.toString(), user.getEmail()); + } + } catch (IOException | TException | PortletException e) { + log.error("An error occurred while generating the Excel export", e); + response.setProperty(ResourceResponse.HTTP_STATUS_CODE, + Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); + } + + } + private void evaluateCLIAttachments(ResourceRequest request, ResourceResponse response) throws IOException { User user = UserCacheHolder.getUserFromRequest(request); String releaseId = request.getParameter(RELEASE_ID); @@ -466,13 +523,47 @@ private void exportExcel(ResourceRequest request, ResourceResponse response) { try { boolean extendedByReleases = Boolean.valueOf(request.getParameter(PortalConstants.EXTENDED_EXCEL_EXPORT)); - List components = getFilteredComponentList(request); - ComponentExporter exporter = new ComponentExporter(thriftClients.makeComponentClient(), components, user, + ComponentService.Iface client = thriftClients.makeComponentClient(); + int total = client.getTotalComponentsCount(user); + PaginationData pageData = new PaginationData(); + pageData.setAscending(true); + Map> pageDtToProjects; + Set projects = new HashSet<>(); + int displayStart = 0; + int rowsPerPage = 500; + while (0 < total) { + pageData.setDisplayStart(displayStart); + pageData.setRowsPerPage(rowsPerPage); + displayStart = displayStart + rowsPerPage; + pageDtToProjects = getFilteredComponentList(request, pageData); + projects.addAll(pageDtToProjects.entrySet().iterator().next().getValue()); + total = total - rowsPerPage; + } + List listOfComponent = new ArrayList(projects); + ComponentExporter exporter = new ComponentExporter(thriftClients.makeComponentClient(), listOfComponent, user, + extendedByReleases); + String filename = String.format("components-%s.xlsx", SW360Utils.getCreatedOn()); + PortletResponseUtil.sendFile(request, response, filename, exporter.makeExcelExport(listOfComponent), + CONTENT_TYPE_OPENXML_SPREADSHEET); + } catch (IOException | TException e) { + log.error("An error occurred while generating the Excel export", e); + response.setProperty(ResourceResponse.HTTP_STATUS_CODE, + Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); + } + } + + private void downloadExcel(ResourceRequest request, ResourceResponse response) { + final User user = UserCacheHolder.getUserFromRequest(request); + final String token = request.getParameter("token"); + + try { + boolean extendedByReleases = Boolean.valueOf(request.getParameter(PortalConstants.EXTENDED_EXCEL_EXPORT)); + ComponentExporter exporter = new ComponentExporter(thriftClients.makeComponentClient(), user, extendedByReleases); String filename = String.format("components-%s.xlsx", SW360Utils.getCreatedOn()); - PortletResponseUtil.sendFile(request, response, filename, exporter.makeExcelExport(components), + PortletResponseUtil.sendFile(request, response, filename, exporter.downloadExcelSheet(token), CONTENT_TYPE_OPENXML_SPREADSHEET); - } catch (IOException | SW360Exception e) { + } catch (IOException | TException e) { log.error("An error occurred while generating the Excel export", e); response.setProperty(ResourceResponse.HTTP_STATUS_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); diff --git a/frontend/sw360-portlet/src/main/resources/META-INF/resources/html/components/view.jsp b/frontend/sw360-portlet/src/main/resources/META-INF/resources/html/components/view.jsp index dd63106bad..5719a478a5 100644 --- a/frontend/sw360-portlet/src/main/resources/META-INF/resources/html/components/view.jsp +++ b/frontend/sw360-portlet/src/main/resources/META-INF/resources/html/components/view.jsp @@ -66,6 +66,11 @@ + + + + +