|
| 1 | +/* |
| 2 | + * Copyright Siemens AG,2025. |
| 3 | + * Part of the SW360 Portal Project. |
| 4 | + * |
| 5 | + * This program and the accompanying materials are made |
| 6 | + * available under the terms of the Eclipse Public License 2.0 |
| 7 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +package org.eclipse.sw360.rest.resourceserver.department; |
| 13 | + |
| 14 | +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import org.apache.logging.log4j.LogManager; |
| 21 | +import org.apache.logging.log4j.Logger; |
| 22 | +import org.apache.thrift.TException; |
| 23 | +import org.eclipse.sw360.datahandler.common.SW360Constants; |
| 24 | +import org.eclipse.sw360.datahandler.thrift.RequestStatus; |
| 25 | +import org.eclipse.sw360.datahandler.thrift.RequestSummary; |
| 26 | +import org.eclipse.sw360.datahandler.thrift.SW360Exception; |
| 27 | +import org.eclipse.sw360.datahandler.thrift.users.User; |
| 28 | +import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.data.rest.webmvc.BasePathAwareController; |
| 31 | +import org.springframework.data.rest.webmvc.RepositoryLinksResource; |
| 32 | +import org.springframework.hateoas.server.RepresentationModelProcessor; |
| 33 | +import org.springframework.http.HttpStatus; |
| 34 | +import org.springframework.http.ResponseEntity; |
| 35 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 36 | +import org.springframework.web.bind.annotation.RequestBody; |
| 37 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 38 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 39 | +import org.springframework.web.bind.annotation.RequestParam; |
| 40 | +import org.springframework.web.bind.annotation.RestController; |
| 41 | + |
| 42 | +import com.google.common.base.Strings; |
| 43 | + |
| 44 | +import io.swagger.v3.oas.annotations.Operation; |
| 45 | +import io.swagger.v3.oas.annotations.Parameter; |
| 46 | +import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
| 47 | +import jakarta.servlet.http.HttpServletRequest; |
| 48 | +import lombok.NonNull; |
| 49 | +import lombok.RequiredArgsConstructor; |
| 50 | + |
| 51 | +@BasePathAwareController |
| 52 | +@RequiredArgsConstructor(onConstructor = @__(@Autowired)) |
| 53 | +@RestController |
| 54 | +@SecurityRequirement(name = "tokenAuth") |
| 55 | +@PreAuthorize("hasAuthority('ADMIN')") |
| 56 | +public class DepartmentController implements RepresentationModelProcessor<RepositoryLinksResource>{ |
| 57 | + public static final String DEPARTMENT_URL = "/departments"; |
| 58 | + private static final Logger log = LogManager.getLogger(DepartmentController.class); |
| 59 | + |
| 60 | + @NonNull |
| 61 | + private final Sw360DepartmentService departmentService; |
| 62 | + |
| 63 | + @NonNull |
| 64 | + private final RestControllerHelper restControllerHelper; |
| 65 | + |
| 66 | + @Override |
| 67 | + public RepositoryLinksResource process(RepositoryLinksResource resource) { |
| 68 | + resource.add(linkTo(DepartmentController.class).slash("api" + DEPARTMENT_URL).withRel("department")); |
| 69 | + return resource; |
| 70 | + } |
| 71 | + |
| 72 | + @Operation( |
| 73 | + description = "Manually active the service.", |
| 74 | + tags = {"Department"} |
| 75 | + ) |
| 76 | + @RequestMapping(value = DEPARTMENT_URL + "/manuallyActive", method = RequestMethod.POST) |
| 77 | + public ResponseEntity<RequestSummary> importDepartmentManually() |
| 78 | + throws TException { |
| 79 | + try { |
| 80 | + User user = restControllerHelper.getSw360UserFromAuthentication(); |
| 81 | + RequestSummary requestSummary = departmentService.importDepartmentManually(user); |
| 82 | + return ResponseEntity.ok(requestSummary); |
| 83 | + } catch (TException e) { |
| 84 | + log.error("Error importing department", e); |
| 85 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Operation( |
| 90 | + description = "Schedule import.", |
| 91 | + tags = {"Department"} |
| 92 | + ) |
| 93 | + @RequestMapping(value = DEPARTMENT_URL + "/scheduleImport", method = RequestMethod.POST) |
| 94 | + public ResponseEntity<Map<String, String>> scheduleImportDepartment() { |
| 95 | + try { |
| 96 | + User user = restControllerHelper.getSw360UserFromAuthentication(); |
| 97 | + |
| 98 | + if (departmentService.isDepartmentScheduled(user)) { |
| 99 | + return ResponseEntity.status(HttpStatus.CONFLICT) |
| 100 | + .body(Collections.singletonMap("message", "Department import is already scheduled.")); |
| 101 | + } |
| 102 | + |
| 103 | + RequestSummary requestSummary = departmentService.scheduleImportDepartment(user); |
| 104 | + |
| 105 | + Map<String, String> response = new HashMap<>(); |
| 106 | + response.put("status", requestSummary.getRequestStatus().name()); |
| 107 | + response.put("message", "Department import scheduled successfully"); |
| 108 | + |
| 109 | + return ResponseEntity.ok(response); |
| 110 | + } catch (SW360Exception e) { |
| 111 | + log.error("Schedule check failed: {}", e.getMessage()); |
| 112 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
| 113 | + .body(Collections.singletonMap("error", e.getMessage())); |
| 114 | + } catch (TException e) { |
| 115 | + log.error("Schedule import department: {}", e.getMessage()); |
| 116 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 117 | + .body(Collections.singletonMap("error", "Failed to schedule department import")); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Operation(summary = "Unschedule Department Import", |
| 122 | + description = "Cancels the scheduled import task for the department.", |
| 123 | + tags = {"Department"}) |
| 124 | + @RequestMapping(value = DEPARTMENT_URL + "/unscheduleImport", method = RequestMethod.POST) |
| 125 | + public ResponseEntity<Map<String, String>> unScheduleImportDepartment() throws TException { |
| 126 | + |
| 127 | + try { |
| 128 | + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); |
| 129 | + RequestStatus requestStatus = departmentService.unScheduleImportDepartment(sw360User); |
| 130 | + |
| 131 | + Map<String, String> response = new HashMap<>(); |
| 132 | + response.put("status", requestStatus.name()); |
| 133 | + response.put("message", "Department import unscheduled successfully"); |
| 134 | + |
| 135 | + return ResponseEntity.ok(response); |
| 136 | + } catch (TException e) { |
| 137 | + log.error("Failed to cancel scheduled department import: {}", e.getMessage()); |
| 138 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 139 | + .body(Collections.singletonMap("error", "Failed to unschedule department import")); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Operation(summary = "Update Folder Path Configuration", |
| 144 | + description = "Updates the department folder path configuration.") |
| 145 | + @RequestMapping(value = DEPARTMENT_URL + "/writePathFolder", method = RequestMethod.POST) |
| 146 | + public ResponseEntity<String> updatePath( |
| 147 | + @Parameter(description = "The path of the folder") |
| 148 | + @RequestParam String pathFolder) { |
| 149 | + try { |
| 150 | + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); |
| 151 | + departmentService.writePathFolderConfig(pathFolder, sw360User); |
| 152 | + return ResponseEntity.ok("Path updated successfully."); |
| 153 | + } catch (Exception e) { |
| 154 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 155 | + .body("Error updating path: " + e.getMessage()); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments