|
11 | 11 |
|
12 | 12 | import io.swagger.v3.oas.annotations.Operation;
|
13 | 13 | import io.swagger.v3.oas.annotations.Parameter;
|
| 14 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
14 | 15 | import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
15 | 16 | import jakarta.servlet.http.HttpServletRequest;
|
16 | 17 | import lombok.NonNull;
|
17 | 18 | import lombok.RequiredArgsConstructor;
|
18 | 19 | import lombok.extern.slf4j.Slf4j;
|
| 20 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
19 | 21 | import org.eclipse.sw360.datahandler.common.CommonUtils;
|
20 | 22 | import org.eclipse.sw360.datahandler.common.SW360Constants;
|
21 | 23 | import org.eclipse.sw360.datahandler.resourcelists.PaginationParameterException;
|
22 | 24 | import org.eclipse.sw360.datahandler.resourcelists.PaginationResult;
|
23 | 25 | import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
|
| 26 | +import org.eclipse.sw360.datahandler.thrift.RequestStatus; |
24 | 27 | import org.eclipse.sw360.datahandler.thrift.licenses.License;
|
25 | 28 | import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
|
26 |
| -import org.eclipse.sw360.datahandler.thrift.RequestStatus; |
27 | 29 | import org.eclipse.sw360.datahandler.thrift.users.User;
|
28 | 30 | import org.eclipse.sw360.rest.resourceserver.core.HalResource;
|
29 | 31 | import org.eclipse.sw360.rest.resourceserver.core.MultiStatus;
|
|
32 | 34 | import org.springframework.data.domain.Pageable;
|
33 | 35 | import org.springframework.data.rest.webmvc.BasePathAwareController;
|
34 | 36 | import org.springframework.data.rest.webmvc.RepositoryLinksResource;
|
| 37 | +import org.springframework.data.rest.webmvc.ResourceNotFoundException; |
| 38 | +import org.springframework.hateoas.CollectionModel; |
35 | 39 | import org.springframework.hateoas.EntityModel;
|
36 | 40 | import org.springframework.hateoas.server.RepresentationModelProcessor;
|
37 |
| -import org.springframework.hateoas.CollectionModel; |
38 | 41 | import org.springframework.http.HttpStatus;
|
39 | 42 | import org.springframework.http.ResponseEntity;
|
40 | 43 | import org.springframework.security.access.prepost.PreAuthorize;
|
41 | 44 | import org.springframework.web.bind.annotation.*;
|
42 | 45 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
43 |
| -import org.springframework.data.rest.webmvc.ResourceNotFoundException; |
44 | 46 |
|
45 | 47 | import java.net.URI;
|
46 | 48 | import java.net.URISyntaxException;
|
@@ -170,6 +172,65 @@ public ResponseEntity<List<MultiStatus>> deleteObligations(
|
170 | 172 | return new ResponseEntity<>(results, HttpStatus.MULTI_STATUS);
|
171 | 173 | }
|
172 | 174 |
|
| 175 | + /** |
| 176 | + * Edit an existing obligation by id. |
| 177 | + * |
| 178 | + * @param id The id of the obligation to be edited. |
| 179 | + * @param obligation The obligation details to be updated. |
| 180 | + * @return ResponseEntity with a message indicating the result of the operation. |
| 181 | + */ |
| 182 | + @Operation( |
| 183 | + summary = "Edit an existing obligation.", |
| 184 | + description = "Edit an existing obligation by id.", |
| 185 | + tags = {"Obligations"}, |
| 186 | + responses = { |
| 187 | + @ApiResponse(responseCode = "200", description = "Successfully edited the obligation."), |
| 188 | + @ApiResponse(responseCode = "400", description = "Bad request when obligation title or text is empty."), |
| 189 | + @ApiResponse(responseCode = "401", description = "Unauthorized access."), |
| 190 | + @ApiResponse(responseCode = "404", description = "Obligation not found."), |
| 191 | + @ApiResponse(responseCode = "500", description = "Internal server error.") |
| 192 | + } |
| 193 | + ) |
| 194 | + @PreAuthorize("hasAuthority('WRITE')") |
| 195 | + @PatchMapping(value = OBLIGATION_URL + "/{id}") |
| 196 | + public ResponseEntity<String> editObligation( |
| 197 | + @Parameter(description = "The id of the obligation to be edited.") |
| 198 | + @PathVariable("id") String id, |
| 199 | + @Parameter(description = "The obligation details to be updated.") |
| 200 | + @RequestBody Obligation obligation |
| 201 | + ) { |
| 202 | + try { |
| 203 | + if (CommonUtils.isNullEmptyOrWhitespace(obligation.getTitle()) |
| 204 | + || CommonUtils.isNullEmptyOrWhitespace(obligation.getText()) |
| 205 | + ) { |
| 206 | + log.error("Obligation title or text is empty"); |
| 207 | + return new ResponseEntity<>("Obligation title or text is empty", HttpStatus.BAD_REQUEST); |
| 208 | + } |
| 209 | + |
| 210 | + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); |
| 211 | + checkIfObligationExists(id); |
| 212 | + obligation.setId(id); // Ensure the id is set to the existing obligation's id |
| 213 | + Obligation updatedObligation = obligationService.updateObligation(obligation, sw360User); |
| 214 | + log.debug("Obligation {} updated successfully", updatedObligation); |
| 215 | + return new ResponseEntity<>("Obligation with id " + updatedObligation.getId() + " has been updated successfully", HttpStatus.OK); |
| 216 | + } catch (ResourceNotFoundException e) { |
| 217 | + return new ResponseEntity<>("Obligation not found", HttpStatus.NOT_FOUND); |
| 218 | + |
| 219 | + } catch (Exception e) { |
| 220 | + log.error("Error updating obligation with id {}", id, e); |
| 221 | + String rootCauseMessage = ExceptionUtils.getRootCauseMessage(e); |
| 222 | + return new ResponseEntity<>("Internal server error: " + rootCauseMessage, HttpStatus.INTERNAL_SERVER_ERROR); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + private void checkIfObligationExists(String id) throws ResourceNotFoundException { |
| 227 | + try { |
| 228 | + obligationService.getObligationById(id); |
| 229 | + } catch (Exception e) { |
| 230 | + log.error("Error getting obligation with id {}", id, e); |
| 231 | + throw new ResourceNotFoundException("Obligation not found"); |
| 232 | + } |
| 233 | + } |
173 | 234 | @Override
|
174 | 235 | public RepositoryLinksResource process(RepositoryLinksResource resource) {
|
175 | 236 | resource.add(linkTo(ObligationController.class).slash("api" + OBLIGATION_URL).withRel("obligations"));
|
|
0 commit comments