Skip to content

Commit

Permalink
Merge branch 'fix/componentsreport' of github.com:siemens/sw360 into …
Browse files Browse the repository at this point in the history
…fix/componentsreport
  • Loading branch information
keerthi-bl committed Jul 11, 2023
2 parents 0c70359 + 12b05d1 commit 0943ca3
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2849,26 +2849,26 @@ public Map<String, String> getReleasesName(String releases) {
return releaseNames;
}

public String getComponentReportInEmail(User user,boolean extendedByReleases) throws TException {
public String getComponentReportInEmail(User user, boolean extendedByReleases) throws TException {
try {
List<Component> componentlist = getRecentComponentsSummary(-1, user);
ComponentExporter exporter = getComponentExporterObject(componentlist,user, extendedByReleases);
return exporter.makeExcelExportForProject(componentlist, user);
}catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
List<Component> componentlist = getRecentComponentsSummary(-1, user);
ComponentExporter exporter = getComponentExporterObject(componentlist, user, extendedByReleases);
return exporter.makeExcelExportForProject(componentlist, user);
} catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}

private ComponentExporter getComponentExporterObject(List<Component> componentList ,User user,
private ComponentExporter getComponentExporterObject(List<Component> componentList, User user,
boolean extendedByRelease) throws SW360Exception {
ThriftClients thriftClients = new ThriftClients();
return new ComponentExporter(thriftClients.makeComponentClient(), componentList, user,extendedByRelease);
ThriftClients thriftClients = new ThriftClients();
return new ComponentExporter(thriftClients.makeComponentClient(), componentList, user, extendedByRelease);
}

public ByteBuffer downloadExcel(User user,boolean extendedByReleases,String token) throws SW360Exception {
public ByteBuffer downloadExcel(User user, boolean extendedByReleases, String token) throws SW360Exception {
try {
ThriftClients thriftClients = new ThriftClients();
ComponentExporter exporter = new ComponentExporter(thriftClients.makeComponentClient(), user,
ThriftClients thriftClients = new ThriftClients();
ComponentExporter exporter = new ComponentExporter(thriftClients.makeComponentClient(), user,
extendedByReleases);
InputStream stream = exporter.downloadExcelSheet(token);
return ByteBuffer.wrap(IOUtils.toByteArray(stream));
Expand All @@ -2878,14 +2878,14 @@ public ByteBuffer downloadExcel(User user,boolean extendedByReleases,String toke
return null;
}

public ByteBuffer getComponentReportDataStream(User user, boolean extendedByReleases) throws TException{
try {
List<Component> componentlist = getRecentComponentsSummary(-1, user);
public ByteBuffer getComponentReportDataStream(User user, boolean extendedByReleases) throws TException {
try {
List<Component> componentlist = getRecentComponentsSummary(-1, user);
ComponentExporter exporter = getComponentExporterObject(componentlist, user, extendedByReleases);
InputStream stream = exporter.makeExcelExport(componentlist);
return ByteBuffer.wrap(IOUtils.toByteArray(stream));
}catch (IOException e) {
} catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,16 @@ public void sendExportSpreadsheetSuccessMail(String url, String recepient) throw

@Override
public ByteBuffer downloadExcel(User user, boolean extendedByReleases, String token) throws TException {
return handler.downloadExcel(user,extendedByReleases,token);
return handler.downloadExcel(user, extendedByReleases, token);
}

@Override
public ByteBuffer getComponentReportDataStream(User user, boolean extendedByReleases) throws TException {
return handler.getComponentReportDataStream(user,extendedByReleases);
}
@Override
public ByteBuffer getComponentReportDataStream(User user, boolean extendedByReleases) throws TException {
return handler.getComponentReportDataStream(user, extendedByReleases);
}

@Override
public String getComponentReportInEmail(User user, boolean extendedByReleases) throws TException {
return handler.getComponentReportInEmail(user,extendedByReleases);
}
@Override
public String getComponentReportInEmail(User user, boolean extendedByReleases) throws TException {
return handler.getComponentReportInEmail(user, extendedByReleases);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@

@BasePathAwareController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
<<<<<<< HEAD
public class SW360ReportController implements RepresentationModelProcessor<RepositoryLinksResource>{
private static final String COMPONENTS = "components";

private static final String PROJECTS = "projects";

public static final String REPORTS_URL = "/reports";

=======
public class SW360ReportController implements RepresentationModelProcessor<RepositoryLinksResource> {
private static final String COMPONENTS = "components";

private static final String PROJECTS = "projects";

public static final String REPORTS_URL = "/reports";

>>>>>>> 12b05d1598dbdf1841c3a3d44256abba4195215a
private static String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

@NonNull
Expand All @@ -59,6 +69,7 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(SW360ReportController.class).slash("api/" + REPORTS_URL).withRel("reports"));
return resource;
}
<<<<<<< HEAD

private List<String> mimeTypeList = Arrays.asList("xls","xlsx");

Expand All @@ -79,18 +90,42 @@ public void getProjectReport(@RequestParam(value = "withlinkedreleases", require

case COMPONENTS :
getComponentsReports(withLinkedReleases,mailRequest, response, request, sw360User,module);
=======

private List<String> mimeTypeList = Arrays.asList("xls", "xlsx");

@RequestMapping(value = REPORTS_URL, method = RequestMethod.GET)
public void getProjectReport(
@RequestParam(value = "withlinkedreleases", required = false, defaultValue = "false") boolean withLinkedReleases,
@RequestParam(value = "mimetype", required = false, defaultValue = "xlsx") String mimeType,
@RequestParam(value = "mailrequest", required = false, defaultValue = "false") boolean mailRequest,
@RequestParam(value = "module", required = true) String module, HttpServletRequest request,
HttpServletResponse response) throws TException {

final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
try {
if (validateMimeType(mimeType)) {
switch (module) {
case PROJECTS:
getProjectReports(withLinkedReleases, mailRequest, response, request, sw360User, module);
break;

case COMPONENTS:
getComponentsReports(withLinkedReleases, mailRequest, response, request, sw360User, module);
>>>>>>> 12b05d1598dbdf1841c3a3d44256abba4195215a
break;
default:
break;
}
} else {
throw new TException("Error : Mimetype either should be : xls/xlsx");
}
}catch (Exception e) {
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

<<<<<<< HEAD
private void getProjectReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,HttpServletRequest request,
User sw360User, String module) throws TException{
try {
Expand Down Expand Up @@ -167,13 +202,94 @@ private void downloadExcelReport(boolean withLinkedReleases, HttpServletResponse
if(null==buffer) {
throw new TException("No data available for the user "+ user.getEmail());
}
=======
private void getProjectReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,
HttpServletRequest request, User sw360User, String module) throws TException {
try {
if (mailRequest) {
String base = getBaseUrl(request);

String projectPath = sw360ReportService.getUploadedProjectPath(sw360User, withLinkedReleases);

String backendURL = base + "api/reports/download?user=" + sw360User.getEmail() + "&module=projects"
+ "&extendedByReleases=" + withLinkedReleases + "&token=";
URL emailURL = new URL(backendURL + projectPath);

if (!CommonUtils.isNullEmptyOrWhitespace(projectPath)) {
sw360ReportService.sendExportSpreadsheetSuccessMail(emailURL.toString(), sw360User.getEmail());
}
JsonObject responseJson = new JsonObject();
responseJson.addProperty("response", "E-mail sent succesfully to the end user.");
responseJson.addProperty("url", emailURL.toString());
responseJson.toString();
response.getWriter().write(responseJson.toString());
} else {
downloadExcelReport(withLinkedReleases, response, sw360User, module);
}
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

private String getBaseUrl(HttpServletRequest request) {
StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String ctx = request.getContextPath();
return url.substring(0, url.length() - uri.length() + ctx.length()) + "/";
}

private void getComponentsReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,
HttpServletRequest request, User sw360User, String module) throws TException {
try {
if (mailRequest) {
String base = getBaseUrl(request);
String componentPath = sw360ReportService.getUploadedComponentPath(sw360User, withLinkedReleases);
String backendURL = base + "api/reports/download?user=" + sw360User.getEmail() + "&module=components"
+ "&extendedByReleases=" + withLinkedReleases + "&token=";
URL emailURL = new URL(backendURL + componentPath);

if (!CommonUtils.isNullEmptyOrWhitespace(componentPath)) {
sw360ReportService.sendComponentExportSpreadsheetSuccessMail(emailURL.toString(),
sw360User.getEmail());
}
JsonObject responseJson = new JsonObject();
responseJson.addProperty("response", "E-mail sent succesfully to the end user.");
responseJson.addProperty("url", emailURL.toString());
responseJson.toString();
response.getWriter().write(responseJson.toString());
} else {
downloadExcelReport(withLinkedReleases, response, sw360User, module);
}
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void downloadExcelReport(boolean withLinkedReleases, HttpServletResponse response, User user, String module)
throws TException, IOException {
try {
ByteBuffer buffer = null;
switch (module) {
case PROJECTS:
buffer = sw360ReportService.getProjectBuffer(user, withLinkedReleases);
break;
case COMPONENTS:
buffer = sw360ReportService.getComponentBuffer(user, withLinkedReleases);
break;
default:
break;
}
if (null == buffer) {
throw new TException("No data available for the user " + user.getEmail());
}
>>>>>>> 12b05d1598dbdf1841c3a3d44256abba4195215a
response.setContentType(CONTENT_TYPE);
String filename = String.format("projects-%s.xlsx", SW360Utils.getCreatedOn());
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
copyDataStreamToResponse(response, buffer);
}catch(Exception e) {
throw new TException(e.getMessage());
}
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void copyDataStreamToResponse(HttpServletResponse response, ByteBuffer buffer) throws IOException {
Expand All @@ -185,6 +301,7 @@ private boolean validateMimeType(String mimeType) {
}

@RequestMapping(value = REPORTS_URL + "/download", method = RequestMethod.GET)
<<<<<<< HEAD
public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws TException{
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
String module = request.getParameter("module");
Expand All @@ -205,6 +322,29 @@ public void downloadExcel(HttpServletRequest request,HttpServletResponse respons
default:
break;
}
=======
public void downloadExcel(HttpServletRequest request, HttpServletResponse response) throws TException {
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
String module = request.getParameter("module");
String token = request.getParameter("token");
String extendedByReleases = request.getParameter("extendedByReleases");
User user = restControllerHelper.getUserByEmail(sw360User.getEmail());
String fileConstant = "projects-%s.xlsx";
try {
ByteBuffer buffer = null;
switch (module) {
case PROJECTS:
buffer = sw360ReportService.getReportStreamFromURl(user, Boolean.valueOf(extendedByReleases), token);
break;
case COMPONENTS:
fileConstant = "components-%s.xlsx";
buffer = sw360ReportService.getComponentReportStreamFromURl(user, Boolean.valueOf(extendedByReleases),
token);
break;
default:
break;
}
>>>>>>> 12b05d1598dbdf1841c3a3d44256abba4195215a
String filename = String.format(fileConstant, SW360Utils.getCreatedOn());
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.eclipse.sw360.rest.resourceserver.report;

import java.nio.ByteBuffer;
import java.util.List;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
Expand All @@ -20,41 +19,41 @@
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SW360ReportService {
ThriftClients thriftClients = new ThriftClients();

ThriftClients thriftClients = new ThriftClients();
ProjectService.Iface projectclient = thriftClients.makeProjectClient();
ComponentService.Iface componentclient = thriftClients.makeComponentClient();

public ByteBuffer getProjectBuffer(User user, boolean extendedByReleases)
throws TException {
public ByteBuffer getProjectBuffer(User user, boolean extendedByReleases) throws TException {
return projectclient.getReportDataStream(user, extendedByReleases);
}

public String getUploadedProjectPath(User user, boolean extendedByReleases) throws TException{
public String getUploadedProjectPath(User user, boolean extendedByReleases) throws TException {
return projectclient.getReportInEmail(user, extendedByReleases);
}

public ByteBuffer getReportStreamFromURl(User user,boolean extendedByReleases, String token)
throws TException{
return projectclient.downloadExcel(user,extendedByReleases, token);
}
public void sendExportSpreadsheetSuccessMail(String emailURL, String email) throws TException{
projectclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}

public String getUploadedComponentPath(User sw360User, boolean withLinkedReleases) throws TException{
return componentclient.getComponentReportInEmail(sw360User, withLinkedReleases);
}

public ByteBuffer getComponentBuffer(User sw360User, boolean withLinkedReleases) throws TException{
return componentclient.getComponentReportDataStream(sw360User, withLinkedReleases);
}

public ByteBuffer getComponentReportStreamFromURl(User user,boolean extendedByReleases, String token)
throws TException{
return componentclient.downloadExcel(user,extendedByReleases, token);
}
public void sendComponentExportSpreadsheetSuccessMail(String emailURL, String email) throws TException{
componentclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}

public ByteBuffer getReportStreamFromURl(User user, boolean extendedByReleases, String token) throws TException {
return projectclient.downloadExcel(user, extendedByReleases, token);
}

public void sendExportSpreadsheetSuccessMail(String emailURL, String email) throws TException {
projectclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}

public String getUploadedComponentPath(User sw360User, boolean withLinkedReleases) throws TException {
return componentclient.getComponentReportInEmail(sw360User, withLinkedReleases);
}

public ByteBuffer getComponentBuffer(User sw360User, boolean withLinkedReleases) throws TException {
return componentclient.getComponentReportDataStream(sw360User, withLinkedReleases);
}

public ByteBuffer getComponentReportStreamFromURl(User user, boolean extendedByReleases, String token)
throws TException {
return componentclient.downloadExcel(user, extendedByReleases, token);
}

public void sendComponentExportSpreadsheetSuccessMail(String emailURL, String email) throws TException {
componentclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}
}
Loading

0 comments on commit 0943ca3

Please sign in to comment.