Skip to content

Commit

Permalink
feat(rest) : Rest end point for project ECC Export Spreadsheet
Browse files Browse the repository at this point in the history
Signed-off-by: Keerthi B L <keerthi.bl@siemens.com>
  • Loading branch information
keerthi-bl committed Nov 20, 2024
1 parent ffd83c6 commit 17e5f1f
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ public class SW360Constants {
public static final Boolean MAIL_REQUEST_FOR_PROJECT_REPORT;
public static final Boolean MAIL_REQUEST_FOR_COMPONENT_REPORT;

public static final String COMPONENTS = "components";
public static final String PROJECTS = "projects";
public static final String LICENSES = "licenses";
public static final String REPORTS_URL = "/reports";
public static final String PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO = "projectReleaseSpreadSheetWithEcc";
public static final String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

/**
* Hashmap containing the name field for each type.
* Used by the search service to fill the search results
Expand Down
15 changes: 15 additions & 0 deletions rest/resource-server/src/docs/asciidoc/projects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1182,3 +1182,18 @@ include::{snippets}/should_document_compare_dependency_network_with_default_rele

===== Example response
include::{snippets}/should_document_compare_dependency_network_with_default_releases_relationship/http-response.adoc[]

[[resources-projects-download-releases-ecc-report]]
==== Downloading project release with ecc information

A `GET` request help to download the project release with ecc information report.

===== Request parameter
include::{snippets}/should_document_get_project_release_with_ecc_spreadsheet/request-parameters.adoc[]

===== Example request
include::{snippets}/should_document_get_project_release_with_ecc_spreadsheet/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_get_project_release_with_ecc_spreadsheet/http-response.adoc[]

Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@
@SecurityRequirement(name = "tokenAuth")
@SecurityRequirement(name = "basic")
public class SW360ReportController implements RepresentationModelProcessor<RepositoryLinksResource> {
private static final String COMPONENTS = "components";
private static final String PROJECTS = "projects";
private static final String LICENSES = "licenses";
public static final String REPORTS_URL = "/reports";
private static final String LICENSE_INFO = "licenseInfo";
private static final String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
private static final String LICENSES_RESOURCE_BUNDLE = "licenseResourceBundle";
private static final String ZIP_CONTENT_TYPE = "application/zip";
private ByteBuffer defaultByteBufferVal = null;
Expand All @@ -64,7 +59,7 @@ public class SW360ReportController implements RepresentationModelProcessor<Repos

@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(SW360ReportController.class).slash("api/" + REPORTS_URL).withRel("reports"));
resource.add(linkTo(SW360ReportController.class).slash("api/" + SW360Constants.REPORTS_URL).withRel("reports"));
return resource;
}

Expand All @@ -75,15 +70,15 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {
description = "Generate the reports.",
tags = {"Reports"}
)
@GetMapping(value = REPORTS_URL)
@GetMapping(value = SW360Constants.REPORTS_URL)
public void getProjectReport(
@Parameter(description = "Projects with linked releases.")
@RequestParam(value = "withlinkedreleases", required = false, defaultValue = "false") boolean withLinkedReleases,
@Parameter(description = "Report download format.", schema = @Schema(allowableValues = {"xls", "xlsx"}))
@RequestParam(value = "mimetype", required = false, defaultValue = "xlsx") String mimeType,
@Parameter(description = "Project id.")
@RequestParam(value = "projectId", required = false) String projectId,
@Parameter(description = "Module name.", schema = @Schema(allowableValues = {PROJECTS, COMPONENTS, LICENSES, LICENSES_RESOURCE_BUNDLE}))
@Parameter(description = "Module name.", schema = @Schema(allowableValues = {SW360Constants.PROJECTS, SW360Constants.COMPONENTS, SW360Constants.LICENSES, LICENSES_RESOURCE_BUNDLE, SW360Constants.PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO}))
@RequestParam(value = "module", required = true) String module,
@Parameter(description = "Exclude release version from the license info file")
@RequestParam(value = "excludeReleaseVersion", required = false, defaultValue = "false") boolean excludeReleaseVersion,
Expand All @@ -95,15 +90,15 @@ public void getProjectReport(
try {
if (validateMimeType(mimeType)) {
switch (module) {
case PROJECTS:
getProjectReports(withLinkedReleases, SW360Constants.MAIL_REQUEST_FOR_PROJECT_REPORT, response,
case SW360Constants.PROJECTS:
getProjectReports(withLinkedReleases, response,
request, sw360User, module, projectId, excludeReleaseVersion);
break;
case COMPONENTS:
case SW360Constants.COMPONENTS:
getComponentsReports(withLinkedReleases, SW360Constants.MAIL_REQUEST_FOR_COMPONENT_REPORT, response,
request, sw360User, module, excludeReleaseVersion);
break;
case LICENSES:
case SW360Constants.LICENSES:
getLicensesReports(request, response, sw360User, module, excludeReleaseVersion);
break;
case LICENSE_INFO:
Expand All @@ -112,6 +107,9 @@ public void getProjectReport(
case LICENSES_RESOURCE_BUNDLE:
getLicenseResourceBundleReports(projectId, request, response, sw360User, module, excludeReleaseVersion);
break;
case SW360Constants.PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO:
getProjectReleaseWithEccSpreadSheet(response, sw360User, module, projectId, request, excludeReleaseVersion);
break;
default:
break;
}
Expand All @@ -123,10 +121,10 @@ public void getProjectReport(
}
}

private void getProjectReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,
private void getProjectReports(boolean withLinkedReleases, HttpServletResponse response,
HttpServletRequest request, User sw360User, String module, String projectId, boolean excludeReleaseVersion) throws TException {
try {
if (mailRequest) {
if (SW360Constants.MAIL_REQUEST_FOR_PROJECT_REPORT) {
sw360ReportService.getUploadedProjectPath(sw360User, withLinkedReleases,getBaseUrl(request), projectId);
JsonObject responseJson = new JsonObject();
responseJson.addProperty("response", "The downloaded report link will be send to the end user.");
Expand Down Expand Up @@ -180,13 +178,13 @@ private void downloadExcelReport(boolean withLinkedReleases, HttpServletRequest
try {
ByteBuffer buff = null;
switch (module) {
case PROJECTS:
case SW360Constants.PROJECTS:
buff = sw360ReportService.getProjectBuffer(user, withLinkedReleases, projectId);
break;
case COMPONENTS:
case SW360Constants.COMPONENTS:
buff = sw360ReportService.getComponentBuffer(user, withLinkedReleases);
break;
case LICENSES:
case SW360Constants.LICENSES:
buff = sw360ReportService.getLicenseBuffer();
break;
case LICENSES_RESOURCE_BUNDLE:
Expand All @@ -199,23 +197,26 @@ private void downloadExcelReport(boolean withLinkedReleases, HttpServletRequest
final String externalIds = request.getParameter("externalIds");
buff = sw360ReportService.getLicenseInfoBuffer(user, projectId, generatorClassName, variant, template, externalIds, excludeReleaseVersion);
break;
case SW360Constants.PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO:
buff = sw360ReportService.getProjectReleaseSpreadSheetWithEcc(user, projectId);
break;
default:
break;
}
if (null == buff) {
throw new TException("No data available for the user " + user.getEmail());
}
response.setContentType(CONTENT_TYPE);
response.setContentType(SW360Constants.CONTENT_TYPE);
String fileName;
if (module.equals(LICENSES)) {
if (module.equals(SW360Constants.LICENSES)) {
fileName = String.format("licenses-%s.xlsx", SW360Utils.getCreatedOn());
} else if (module.equals(LICENSES_RESOURCE_BUNDLE)) {
response.setContentType(ZIP_CONTENT_TYPE);
fileName = sw360ReportService.getSourceCodeBundleName(projectId, user);
} else if (module.equals(PROJECTS)) {
fileName = sw360ReportService.getDocumentName(user, projectId);
} else if ( module.equals(SW360Constants.PROJECTS) || module.equals(SW360Constants.PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO) ) {
fileName = sw360ReportService.getDocumentName(user, projectId, module);
} else {
fileName = sw360ReportService.getDocumentName(user, null);
fileName = sw360ReportService.getDocumentName(user, null, module);
}
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
copyDataStreamToResponse(response, buff);
Expand Down Expand Up @@ -249,15 +250,15 @@ private boolean validateMimeType(String mimeType) {
responses = {@ApiResponse(
responseCode = "200",
description = "Generated report.",
content = @Content(mediaType = CONTENT_TYPE,
content = @Content(mediaType = SW360Constants.CONTENT_TYPE,
schema = @Schema(type = "string", format = "binary"))
)}
)
@GetMapping(value = REPORTS_URL + "/download")
@GetMapping(value = SW360Constants.REPORTS_URL + "/download")
public void downloadExcel(
HttpServletRequest request,
HttpServletResponse response,
@Parameter(description = "Module name.", schema = @Schema(allowableValues = {PROJECTS, COMPONENTS, LICENSES}))
@Parameter(description = "Module name.", schema = @Schema(allowableValues = {SW360Constants.PROJECTS, SW360Constants.COMPONENTS, SW360Constants.LICENSES}))
@RequestParam(value = "module", required = true) String module,
@Parameter(description = "Token to download report.")
@RequestParam(value = "token", required = true) String token,
Expand All @@ -268,13 +269,13 @@ public void downloadExcel(
try {
ByteBuffer buffer = null;
switch (module) {
case PROJECTS:
case SW360Constants.PROJECTS:
buffer = sw360ReportService.getReportStreamFromURl(user, extendedByReleases, token);
break;
case COMPONENTS:
case SW360Constants.COMPONENTS:
buffer = sw360ReportService.getComponentReportStreamFromURl(user, extendedByReleases, token);
break;
case LICENSES:
case SW360Constants.LICENSES:
buffer = sw360ReportService.getLicenseReportStreamFromURl(token);
break;
default:
Expand All @@ -284,16 +285,16 @@ public void downloadExcel(
throw new TException("No data available for the user " + user.getEmail());
}
String fileName;
if(module.equals(LICENSES)) {
if(module.equals(SW360Constants.LICENSES)) {
fileName = String.format("licenses-%s.xlsx", SW360Utils.getCreatedOn());
} else if(module.equals(PROJECTS)) {
fileName = sw360ReportService.getDocumentName(user, request.getParameter("projectId"));
} else if(module.equals(SW360Constants.PROJECTS) || module.equals(SW360Constants.PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO)) {
fileName = sw360ReportService.getDocumentName(user, request.getParameter("projectId"), module);
} else if(module.equals(LICENSE_INFO)) {
fileName = sw360ReportService.getGenericLicInfoFileName(request, user);
} else {
fileName = sw360ReportService.getDocumentName(user, null);
fileName = sw360ReportService.getDocumentName(user, null, module);
}
response.setContentType(CONTENT_TYPE);
response.setContentType(SW360Constants.CONTENT_TYPE);
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
copyDataStreamToResponse(response, buffer);
} catch (Exception e) {
Expand All @@ -307,4 +308,13 @@ private String getBaseUrl(HttpServletRequest request) {
String ctx = request.getContextPath();
return url.substring(0, url.length() - uri.length() + ctx.length()) + "/";
}
}

private void getProjectReleaseWithEccSpreadSheet(HttpServletResponse response, User sw360User, String module,
String projectId, HttpServletRequest request, boolean excludeReleaseVersion) throws TException {
try {
downloadExcelReport(false, request, response, sw360User, module, projectId, excludeReleaseVersion, defaultByteBufferVal);
} catch (Exception e) {
throw new TException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@

import static org.eclipse.sw360.datahandler.common.WrappedException.wrapTException;

import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.common.SW360Constants;
import org.eclipse.sw360.datahandler.common.SW360Utils;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
import org.eclipse.sw360.datahandler.thrift.components.ComponentService;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.components.ReleaseClearingStatusData;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectService;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.exporter.ReleaseExporter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -104,7 +115,7 @@ public class SW360ReportService {
AttachmentService.Iface attachmentClient = thriftClients.makeAttachmentClient();

public ByteBuffer getProjectBuffer(User user, boolean extendedByReleases, String projectId) throws TException {
if (projectId != null && validateProject(projectId, user)) {
if (projectId != null && !validateProject(projectId, user)) {
throw new TException("No project record found for the project Id : " + projectId);
}
return projectclient.getReportDataStream(user, extendedByReleases, projectId);
Expand All @@ -123,12 +134,30 @@ private boolean validateProject(String projectId, User user) throws TException {
return validProject;
}

public String getDocumentName(User user, String projectId) throws TException {
if (projectId != null && !projectId.equalsIgnoreCase("null")) {
Project project = projectclient.getProjectById(projectId, user);
return String.format("project-%s-%s-%s.xlsx", project.getName(), project.getVersion(), SW360Utils.getCreatedOn());
public String getDocumentName(User user, String projectId, String module) throws TException {
String documentName = String.format("projects-%s.xlsx", SW360Utils.getCreatedOn());
switch (module) {
case SW360Constants.PROJECTS:
if (projectId != null && !projectId.equalsIgnoreCase("null")) {
Project project = projectclient.getProjectById(projectId, user);
documentName = String.format("project-%s-%s-%s.xlsx", project.getName(), project.getVersion(),
SW360Utils.getCreatedOn());
}
break;
case SW360Constants.LICENSES:
documentName = String.format("licenses-%s.xlsx", SW360Utils.getCreatedOn());
break;
case SW360Constants.PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO:
if (projectId != null && !projectId.equalsIgnoreCase("null")) {
Project project = projectclient.getProjectById(projectId, user);
documentName = String.format("releases-%s-%s-%s.xlsx", project.getName(), project.getVersion(),
SW360Utils.getCreatedOn());
}
break;
default:
break;
}
return String.format("projects-%s.xlsx", SW360Utils.getCreatedOn());
return documentName;
}

public void getUploadedProjectPath(User user, boolean withLinkedReleases, String base, String projectId)
Expand Down Expand Up @@ -438,4 +467,22 @@ private InputStream getStreamToServeAFile(AttachmentStreamConnector attachmentSt
return attachmentStreamConnector.getAttachmentBundleStream(new HashSet<>(attachments), sw360User, context);
}
}

public ByteBuffer getProjectReleaseSpreadSheetWithEcc(User user, String projectId) throws TException, IOException {
if (projectId == null || projectId.isEmpty() || !validateProject(projectId, user)) {
throw new TException("No project record found for the project Id : " + projectId);
}
ReleaseExporter exporter = null;
List<Release> releases = null;
try {
List<ReleaseClearingStatusData> releaseStringMap = projectclient
.getReleaseClearingStatusesWithAccessibility(projectId, user);
releases = releaseStringMap.stream().map(ReleaseClearingStatusData::getRelease)
.sorted(Comparator.comparing(SW360Utils::printFullname)).collect(Collectors.toList());
exporter = new ReleaseExporter(componentclient, releases, user, releaseStringMap);
} catch (Exception e) {
throw new TException(e.getMessage());
}
return ByteBuffer.wrap(IOUtils.toByteArray(exporter.makeExcelExport(releases)));
}
}
Loading

0 comments on commit 17e5f1f

Please sign in to comment.