Skip to content

Commit 738123f

Browse files
committed
fix(rest): sync CSV import with old code
Sync the service code for importing User CSV with old code. Signed-off-by: Gaurav Mishra <[email protected]>
1 parent b4834c2 commit 738123f

File tree

4 files changed

+287
-178
lines changed

4 files changed

+287
-178
lines changed

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/importexport/ImportExportController.java

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,45 @@
1111

1212
package org.eclipse.sw360.rest.resourceserver.importexport;
1313

14-
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
15-
16-
import java.io.IOException;
17-
14+
import io.swagger.v3.oas.annotations.Operation;
15+
import io.swagger.v3.oas.annotations.Parameter;
16+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
17+
import io.swagger.v3.oas.annotations.media.Content;
18+
import io.swagger.v3.oas.annotations.media.Schema;
19+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
20+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
21+
import jakarta.servlet.ServletException;
22+
import jakarta.servlet.http.HttpServletRequest;
23+
import jakarta.servlet.http.HttpServletResponse;
24+
import lombok.NonNull;
25+
import lombok.RequiredArgsConstructor;
1826
import org.apache.thrift.TException;
1927
import org.apache.thrift.transport.TTransportException;
28+
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
29+
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
2030
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
2131
import org.eclipse.sw360.datahandler.thrift.users.User;
2232
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
2333
import org.slf4j.Logger;
2434
import org.slf4j.LoggerFactory;
25-
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
2635
import org.springframework.beans.factory.annotation.Autowired;
2736
import org.springframework.data.rest.webmvc.BasePathAwareController;
2837
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
2938
import org.springframework.hateoas.server.RepresentationModelProcessor;
39+
import org.springframework.http.HttpStatus;
40+
import org.springframework.http.MediaType;
3041
import org.springframework.http.ResponseEntity;
3142
import org.springframework.security.access.prepost.PreAuthorize;
3243
import org.springframework.web.bind.annotation.GetMapping;
3344
import org.springframework.web.bind.annotation.RequestMapping;
3445
import org.springframework.web.bind.annotation.RequestMethod;
35-
import org.springframework.web.bind.annotation.RestController;
36-
37-
import io.swagger.v3.oas.annotations.Operation;
38-
import io.swagger.v3.oas.annotations.Parameter;
39-
import io.swagger.v3.oas.annotations.enums.ParameterIn;
40-
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
41-
import org.springframework.http.MediaType;
4246
import org.springframework.web.bind.annotation.RequestParam;
47+
import org.springframework.web.bind.annotation.RestController;
4348
import org.springframework.web.multipart.MultipartFile;
4449

45-
import jakarta.servlet.ServletException;
46-
import jakarta.servlet.http.HttpServletRequest;
47-
import jakarta.servlet.http.HttpServletResponse;
48-
import lombok.NonNull;
49-
import lombok.RequiredArgsConstructor;
50+
import java.io.IOException;
51+
52+
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
5053

5154
@BasePathAwareController
5255
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@@ -283,21 +286,58 @@ public void downloadUsers(HttpServletResponse response) throws SW360Exception {
283286
description = "Upload a users CSV file to import users into the system. Requires ADMIN authority.",
284287
tags = {"ImportExport"},
285288
parameters = {
286-
@Parameter(name = "Content-Type", in = ParameterIn.HEADER, required = true, description = "The content type of the request. Supported values: multipart/mixed or multipart/form-data.")
289+
@Parameter(
290+
name = "Content-Type", in = ParameterIn.HEADER, required = true,
291+
description = "The content type of the request. " +
292+
"Supported values: " + MediaType.MULTIPART_MIXED_VALUE + " or " +
293+
MediaType.MULTIPART_FORM_DATA_VALUE + "."
294+
)
295+
},
296+
responses = {
297+
@ApiResponse(
298+
responseCode = "200", description = "Users imported successfully.",
299+
content = {
300+
@Content(mediaType = "application/json",
301+
schema = @Schema(implementation = RequestSummary.class))
302+
}
303+
),
304+
@ApiResponse(
305+
responseCode = "207", description = "Partial import done, some users failed to import. More " +
306+
"information in the `message` field of response.",
307+
content = {
308+
@Content(mediaType = "application/json",
309+
schema = @Schema(implementation = RequestSummary.class))
310+
}
311+
),
312+
@ApiResponse(
313+
responseCode = "400", description = "The uploaded CSV is in bad format."
314+
),
315+
@ApiResponse(
316+
responseCode = "403", description = "User is not an admin."
317+
),
318+
@ApiResponse(
319+
responseCode = "500", description = "Failed to upload the file."
320+
)
287321
}
288322
)
289323
@PreAuthorize("hasAuthority('ADMIN')")
290-
@RequestMapping(value = IMPORTEXPORT_URL + "/uploadUsers", method = RequestMethod.POST, consumes = {
291-
MediaType.MULTIPART_MIXED_VALUE,
292-
MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
293-
public ResponseEntity<RequestSummary> uploadUsers(
294-
@Parameter(description = "The users CSV file to be uploaded. Expected columns: GivenName, Lastname, Email, Department, UserGroup (optional), GID (optional), PasswdHash (optional), wantsMailNotification (optional)")
295-
@RequestParam("usersFile") MultipartFile file,
296-
HttpServletRequest request, HttpServletResponse response
297-
) throws TException, IOException, ServletException {
298-
324+
@RequestMapping(value = IMPORTEXPORT_URL + "/usersCsv", method = RequestMethod.POST,
325+
consumes = {MediaType.MULTIPART_MIXED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE},
326+
produces = {MediaType.APPLICATION_JSON_VALUE})
327+
public ResponseEntity<RequestSummary> uploadUsersCsv(
328+
@Parameter(
329+
description = "The users CSV file to be uploaded. " +
330+
"Expected columns: " +
331+
"GivenName, Lastname, Email, Department, UserGroup, GID, PasswdHash, wantsMailNotification (optional)"
332+
)
333+
@RequestParam("usersCsv") MultipartFile file
334+
) throws IOException {
299335
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
300-
RequestSummary requestSummary = importExportService.uploadUsers(sw360User, file, request);
301-
return ResponseEntity.ok(requestSummary);
336+
RequestSummary requestSummary = importExportService.uploadUsers(sw360User, file);
337+
if (requestSummary.getRequestStatus() == RequestStatus.SUCCESS) {
338+
return ResponseEntity.ok(requestSummary);
339+
} else {
340+
return new ResponseEntity<>(requestSummary, HttpStatus.MULTI_STATUS);
341+
}
302342
}
303343
}

0 commit comments

Comments
 (0)