Skip to content

Commit

Permalink
Merge pull request #2218 from Siemens-Healthineers/fix/incorrect-erro…
Browse files Browse the repository at this point in the history
…r-displayed-during-comp-creation-2215

fix(importCDX): Update failed component creation error message

Reviewed by: anupam.ghosh@siemens.com
Tested  by: anupam.ghosh@siemens.com
  • Loading branch information
ag4ums authored and keerthi-bl committed Jan 11, 2024
2 parents deedeef + b5f6cb4 commit 229b090
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ErrorMessages {
public static final String PROJECT_NAMING_ERROR = "Name of project cannot contain only space characters.";
public static final String CLOSED_UPDATE_NOT_ALLOWED = "User cannot edit a closed project";
public static final String COMPONENT_NOT_ADDED = "Component could not be added.";
public static final String COMPONENT_DUPLICATE = "A component with the same name already exists.";
public static final String COMPONENT_DUPLICATE = "A component with the same name or VCS already exists.";
public static final String COMPONENT_NAMING_ERROR = "Name and Categories of component cannot contain only space characters.";
public static final String RELEASE_NOT_ADDED = "Release could not be added.";
public static final String RELEASE_DUPLICATE = "A release with the same name and version already exists.";
Expand Down
5 changes: 5 additions & 0 deletions rest/resource-server/src/docs/asciidoc/api-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,9 @@ include::clearingRequests.adoc[]
include::obligations.adoc[]
include::moderationRequests.adoc[]
include::fossology.adoc[]
<<<<<<< HEAD
include::schedule.adoc[]
=======
include::ecc.adoc[]
>>>>>>> cf35e0782 (feat(rest) : Rest Api for ECC page)
26 changes: 26 additions & 0 deletions rest/resource-server/src/docs/asciidoc/ecc.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright Siemens AG, 2017-2018. Part of the SW360 Portal Project.
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

[[resources-ecc]]
=== Ecc

The Ecc resource is used to list ecc.


[[resources-ecc-list]]
==== Listing ecc details

A `GET` request will list all of the service's ecc.

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

===== Example response
include::{snippets}/should_document_get_ecc/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public PagedModel emptyPageResource(Class resourceClass, PaginationResult pagina
return PagedModel.of(list, pageMetadata, new ArrayList<>());
}

private PagedModel.PageMetadata createPageMetadata(PaginationResult paginationResult) {
public PagedModel.PageMetadata createPageMetadata(PaginationResult paginationResult) {
PaginationOptions paginationOptions = paginationResult.getPaginationOptions();
return new PagedModel.PageMetadata(
paginationOptions.getPageSize(),
Expand All @@ -232,7 +232,7 @@ private PagedModel.PageMetadata createPageMetadata(PaginationResult paginationRe
paginationResult.getTotalPageCount());
}

private List<Link> getPaginationLinks(PaginationResult paginationResult, String baseUrl) {
public List<Link> getPaginationLinks(PaginationResult paginationResult, String baseUrl) {
PaginationOptions paginationOptions = paginationResult.getPaginationOptions();
List<Link> paginationLinks = new ArrayList<>();

Expand All @@ -252,7 +252,7 @@ private String createPaginationLink(String baseUrl, int page, int pageSize) {
return baseUrl + "?" + PAGINATION_PARAM_PAGE + "=" + page + "&" + PAGINATION_PARAM_PAGE_ENTRIES + "=" + pageSize;
}

private String getAPIBaseUrl() throws URISyntaxException {
public String getAPIBaseUrl() throws URISyntaxException {
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().build().toUri();
return new URI(uri.getScheme(),
uri.getAuthority(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright Siemens AG, 2017-2019.
* Copyright Bosch Software Innovations GmbH, 2017-2018.
* Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.rest.resourceserver.ecc;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;

import java.net.URISyntaxException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.common.SW360Constants;
import org.eclipse.sw360.datahandler.resourcelists.PaginationParameterException;
import org.eclipse.sw360.datahandler.resourcelists.PaginationResult;
import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
import org.jose4j.json.internal.json_simple.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedModel;
import org.springframework.hateoas.server.RepresentationModelProcessor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@BasePathAwareController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@SecurityRequirement(name = "tokenAuth")
public class SW360EccController implements RepresentationModelProcessor<RepositoryLinksResource> {

public static final String ECC_URL = "/ecc";

@NonNull
private final SW360EccService sw360EccService;

@NonNull
private final RestControllerHelper restControllerHelper;

@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(SW360EccController.class).slash("api/ecc").withRel("ecc"));
return resource;
}

@GetMapping(value = ECC_URL)
public ResponseEntity<CollectionModel<?>> getEccDetails(HttpServletRequest request, Pageable pageable)
throws TException, URISyntaxException {
User user = restControllerHelper.getSw360UserFromAuthentication();
JSONArray eccData = null;
try {
eccData = sw360EccService.getECCPageData(user, pageable);
} catch (Exception e) {
throw new TException(e.getMessage());
}
PaginationResult<?> paginationResult = new PaginationResult<>(eccData);
CollectionModel<?> resources = sw360EccService.getPaginatedEccData(eccData,paginationResult);
return new ResponseEntity<>(resources, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* Copyright Siemens AG, 2017-2019.
* Copyright Bosch Software Innovations GmbH, 2017-2018.
* Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.rest.resourceserver.ecc;

import static com.google.common.base.Strings.nullToEmpty;
import static java.lang.Math.min;
import static org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString;

import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.common.SW360Utils;
import org.eclipse.sw360.datahandler.resourcelists.PaginationResult;
import org.eclipse.sw360.datahandler.thrift.PaginationData;
import org.eclipse.sw360.datahandler.thrift.components.ComponentService;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
import org.jose4j.json.internal.json_simple.JSONArray;
import org.jose4j.json.internal.json_simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedModel;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SW360EccService {
@Value("${sw360.thrift-server-url:http://localhost:8080}")
private String thriftServerUrl;

@NonNull
private final RestControllerHelper restControllerHelper;

// ECC release view datatables, index of columns
private static final String RELEASE_DT_ROW_ECC_STATUS = "status";
private static final String RELEASE_DT_ROW_NAME = "name";
private static final String RELEASE_DT_ROW_VERSION = "version";
private static final String RELEASE_DT_ROW_GROUP = "group";
private static final String RELEASE_DT_ROW_ASSESSOR_CONTACT_PERSON = "assessor_contact_person";
private static final String RELEASE_DT_ROW_ASSESSOR_DEPARTMENT = "assessor_dept";
private static final String RELEASE_DT_ROW_ASSESSMENT_DATE = "assessment_date";

public JSONArray getECCPageData(User user,Pageable pageable) throws TException {
JSONArray jsonReleases = new JSONArray();
try {
PaginationData pageData = getPaginationData(pageable);
Map<PaginationData, List<Release>> releaseList = getReleaseList(user, pageData);
jsonReleases = getReleaseData(releaseList.values().iterator().next(), pageable);
} catch (Exception e) {
throw new TException(e.getMessage());
}
return jsonReleases;
}

private Map<PaginationData, List<Release>> getReleaseList(User user, PaginationData pageData) throws TException {
ComponentService.Iface client = getThriftComponentClient();
Map<PaginationData, List<Release>> releaseSummary = Maps.newHashMap();
try {
releaseSummary = client.getAccessibleReleasesWithPagination(user, pageData);
} catch (TException e) {
throw new TException(e.getMessage());
}
return releaseSummary;
}

private JSONArray getReleaseData(List<Release> releaseList, Pageable pageable) {
List<Release> sortedReleases = sortReleaseList(releaseList, pageable);
long count = getReleaseDataCount(pageable, releaseList.size());
final int start = 0;
JSONArray releaseData = new JSONArray();
for (int i = start; i < count; i++) {
JSONObject jsonObject = new JSONObject();
Release release = sortedReleases.get(i);
jsonObject.put("id", release.getId());
jsonObject.put("DT_RowId", release.getId());
jsonObject.put("status", nullToEmptyString(release.getEccInformation().getEccStatus()));
jsonObject.put("name", SW360Utils.printName(release));
jsonObject.put("version", nullToEmpty(release.getVersion()));
jsonObject.put("group", nullToEmptyString(release.getCreatorDepartment()));
jsonObject.put("assessor_contact_person",
nullToEmptyString(release.getEccInformation().getAssessorContactPerson()));
jsonObject.put("assessor_dept", nullToEmptyString(release.getEccInformation().getAssessorDepartment()));
jsonObject.put("assessment_date", nullToEmptyString(release.getEccInformation().getAssessmentDate()));
releaseData.add(jsonObject);
}
return releaseData;
}

private static long getReleaseDataCount(Pageable pageable, int maxSize) {
if (pageable.getPageSize() == -1) {
return maxSize;
} else {
return min(pageable.getPageNumber() + pageable.getPageSize(), maxSize);
}
}

private List<Release> sortReleaseList(List<Release> releaseList, Pageable pageable) {
boolean isAsc = pageable.getSort().iterator().next().isAscending();

switch (pageable.getSort().iterator().next().getProperty()) {
case RELEASE_DT_ROW_ECC_STATUS:
Collections.sort(releaseList, compareByECCStatus(isAsc));
break;
case RELEASE_DT_ROW_NAME:
Collections.sort(releaseList, compareByName(isAsc));
break;
case RELEASE_DT_ROW_VERSION:
Collections.sort(releaseList, compareByVersion(isAsc));
break;
case RELEASE_DT_ROW_GROUP:
Collections.sort(releaseList, compareByCreatorGroup(isAsc));
break;
case RELEASE_DT_ROW_ASSESSOR_CONTACT_PERSON:
Collections.sort(releaseList, compareByAssessorContactPerson(isAsc));
break;
case RELEASE_DT_ROW_ASSESSOR_DEPARTMENT:
Collections.sort(releaseList, compareByAssessorDept(isAsc));
break;
case RELEASE_DT_ROW_ASSESSMENT_DATE:
Collections.sort(releaseList, compareByAssessmentDate(isAsc));
break;
default:
break;
}
return releaseList;
}

private static Comparator<Release> compareByECCStatus(boolean isAscending) {
Comparator<Release> comparator = Comparator
.comparing(r -> CommonUtils.nullToEmptyString(r.getEccInformation().getEccStatus()));
return isAscending ? comparator : comparator.reversed();
}

private static Comparator<Release> compareByName(boolean isAscending) {
Comparator<Release> comparator = Comparator.comparing(r -> CommonUtils.nullToEmptyString(r.getName()));
return isAscending ? comparator : comparator.reversed();
}

private static Comparator<Release> compareByVersion(boolean isAscending) {
Comparator<Release> comparator = Comparator.comparing(r -> CommonUtils.nullToEmptyString(r.getVersion()));
return isAscending ? comparator : comparator.reversed();
}

private static Comparator<Release> compareByCreatorGroup(boolean isAscending) {
Comparator<Release> comparator = Comparator
.comparing(r -> CommonUtils.nullToEmptyString(r.getCreatorDepartment()));
return isAscending ? comparator : comparator.reversed();
}

private static Comparator<Release> compareByAssessorContactPerson(boolean isAscending) {
Comparator<Release> comparator = Comparator
.comparing(r -> CommonUtils.nullToEmptyString(r.getEccInformation().getAssessorContactPerson()));
return isAscending ? comparator : comparator.reversed();
}

private static Comparator<Release> compareByAssessorDept(boolean isAscending) {
Comparator<Release> comparator = Comparator
.comparing(r -> CommonUtils.nullToEmptyString(r.getEccInformation().getAssessorDepartment()));
return isAscending ? comparator : comparator.reversed();
}

private static Comparator<Release> compareByAssessmentDate(boolean isAscending) {
Comparator<Release> comparator = Comparator
.comparing(r -> CommonUtils.nullToEmptyString(r.getEccInformation().getAssessmentDate()));
return isAscending ? comparator : comparator.reversed();
}

private ComponentService.Iface getThriftComponentClient() throws TTransportException {
THttpClient thriftClient = new THttpClient(thriftServerUrl + "/components/thrift");
TProtocol protocol = new TCompactProtocol(thriftClient);
return new ComponentService.Client(protocol);
}

private PaginationData getPaginationData(Pageable pageable) {
return new PaginationData().setDisplayStart((int) pageable.getOffset()).setRowsPerPage(pageable.getPageSize())
.setSortColumnNumber(0);
}

public CollectionModel<?> getPaginatedEccData(JSONArray eccData, PaginationResult<?> paginationResult) throws URISyntaxException{
PagedModel.PageMetadata pageMetadata = restControllerHelper.createPageMetadata(paginationResult);
List<Link> pagingLinks = restControllerHelper.getPaginationLinks(paginationResult, restControllerHelper.getAPIBaseUrl());
return PagedModel.of(eccData, pageMetadata, pagingLinks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ public void should_document_index() throws Exception {
linkWithRel("sw360:obligations").description("The <<resources-obligations,Obligation resource>>"),
linkWithRel("sw360:moderationRequests").description("The <<resources-moderationRequest,ModerationRequest resource>>"),
linkWithRel("sw360:fossology").description("The <<resources-fossology,Fossology resource>>"),
<<<<<<< HEAD
linkWithRel("sw360:schedule").description("The <<resources-schedule,Schedule resource>>"),
=======
linkWithRel("sw360:ecc").description("The <<resources-ecc,Ecc resource>>"),
>>>>>>> cf35e0782 (feat(rest) : Rest Api for ECC page)
linkWithRel("curies").description("The Curies for documentation"),
linkWithRel("profile").description("The profiles of the REST resources")
),
Expand Down
Loading

0 comments on commit 229b090

Please sign in to comment.