Skip to content

Commit 0db7641

Browse files
committed
feat(rest): new rest endpoint for edit obligation
Signed-off-by: Bibhuti Bhusan Dash bibhuti230185 <[email protected]> Signed-off-by: Bibhuti Bhusan dash bibhuti230185 <[email protected]>
1 parent 31bef0c commit 0db7641

File tree

3 files changed

+101
-21
lines changed

3 files changed

+101
-21
lines changed

backend/licenses-core/src/main/java/org/eclipse/sw360/licenses/db/LicenseDatabaseHandler.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,58 @@
1010
*/
1111
package org.eclipse.sw360.licenses.db;
1212

13+
import com.google.common.collect.Lists;
14+
import com.google.common.collect.Sets;
15+
import com.ibm.cloud.cloudant.v1.Cloudant;
1316
import com.ibm.cloud.cloudant.v1.model.DocumentResult;
1417
import org.apache.commons.io.IOUtils;
18+
import org.apache.logging.log4j.LogManager;
19+
import org.apache.logging.log4j.Logger;
1520
import org.apache.thrift.TException;
1621
import org.eclipse.sw360.components.summary.SummaryType;
1722
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
1823
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
1924
import org.eclipse.sw360.datahandler.common.CommonUtils;
25+
import org.eclipse.sw360.datahandler.common.DatabaseSettings;
2026
import org.eclipse.sw360.datahandler.common.SW360Utils;
2127
import org.eclipse.sw360.datahandler.db.CustomPropertiesRepository;
28+
import org.eclipse.sw360.datahandler.db.DatabaseHandlerUtil;
2229
import org.eclipse.sw360.datahandler.db.ReleaseRepository;
2330
import org.eclipse.sw360.datahandler.db.VendorRepository;
2431
import org.eclipse.sw360.datahandler.entitlement.LicenseModerator;
2532
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
2633
import org.eclipse.sw360.datahandler.thrift.*;
34+
import org.eclipse.sw360.datahandler.thrift.changelogs.Operation;
2735
import org.eclipse.sw360.datahandler.thrift.components.Release;
2836
import org.eclipse.sw360.datahandler.thrift.licenses.*;
2937
import org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest;
3038
import org.eclipse.sw360.datahandler.thrift.users.RequestedAction;
3139
import org.eclipse.sw360.datahandler.thrift.users.User;
3240
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
33-
import org.eclipse.sw360.datahandler.thrift.changelogs.Operation;
34-
import org.eclipse.sw360.licenses.tools.SpdxConnector;
3541
import org.eclipse.sw360.exporter.LicenseExporter;
3642
import org.eclipse.sw360.licenses.tools.OSADLObligationConnector;
37-
import org.apache.http.HttpStatus;
38-
import org.apache.logging.log4j.LogManager;
39-
import org.apache.logging.log4j.Logger;
43+
import org.eclipse.sw360.licenses.tools.SpdxConnector;
4044
import org.jetbrains.annotations.NotNull;
4145
import org.json.JSONArray;
4246
import org.json.JSONObject;
43-
44-
import com.ibm.cloud.cloudant.v1.Cloudant;
45-
import com.google.common.collect.Sets;
46-
import static com.google.common.base.Strings.isNullOrEmpty;
47+
import org.spdx.library.InvalidSPDXAnalysisException;
4748

4849
import java.io.IOException;
4950
import java.io.InputStream;
50-
import java.nio.ByteBuffer;
51-
import java.util.function.Function;
5251
import java.net.MalformedURLException;
52+
import java.nio.ByteBuffer;
5353
import java.sql.Timestamp;
5454
import java.time.Instant;
5555
import java.util.*;
56+
import java.util.function.Function;
5657
import java.util.stream.Collectors;
5758

59+
import static com.google.common.base.Strings.isNullOrEmpty;
5860
import static org.eclipse.sw360.datahandler.common.CommonUtils.*;
5961
import static org.eclipse.sw360.datahandler.common.SW360Assert.assertNotNull;
6062
import static org.eclipse.sw360.datahandler.permissions.PermissionUtils.makePermission;
6163
import static org.eclipse.sw360.datahandler.thrift.ThriftValidate.*;
6264

63-
import org.eclipse.sw360.datahandler.db.DatabaseHandlerUtil;
64-
import com.google.common.collect.Lists;
65-
import org.eclipse.sw360.datahandler.common.DatabaseSettings;
66-
import org.spdx.library.InvalidSPDXAnalysisException;
6765

6866
/**
6967
* Class for accessing the CouchDB database
@@ -307,6 +305,8 @@ public String updateObligation(@NotNull Obligation oblig, User user) throws SW36
307305
return null;
308306
}
309307
Obligation oldObligation = getObligationsById(oblig.getId());
308+
// Setting the revision to avoid the document update conflict exception
309+
oblig.setRevision(oldObligation.getRevision());
310310
prepareTodo(oblig);
311311
obligRepository.update(oblig);
312312
oblig.setNode(null);

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/obligation/ObligationController.java

+64-3
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111

1212
import io.swagger.v3.oas.annotations.Operation;
1313
import io.swagger.v3.oas.annotations.Parameter;
14+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
1415
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
1516
import jakarta.servlet.http.HttpServletRequest;
1617
import lombok.NonNull;
1718
import lombok.RequiredArgsConstructor;
1819
import lombok.extern.slf4j.Slf4j;
20+
import org.apache.commons.lang3.exception.ExceptionUtils;
1921
import org.eclipse.sw360.datahandler.common.CommonUtils;
2022
import org.eclipse.sw360.datahandler.common.SW360Constants;
2123
import org.eclipse.sw360.datahandler.resourcelists.PaginationParameterException;
2224
import org.eclipse.sw360.datahandler.resourcelists.PaginationResult;
2325
import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
26+
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
2427
import org.eclipse.sw360.datahandler.thrift.licenses.License;
2528
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
26-
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
2729
import org.eclipse.sw360.datahandler.thrift.users.User;
2830
import org.eclipse.sw360.rest.resourceserver.core.HalResource;
2931
import org.eclipse.sw360.rest.resourceserver.core.MultiStatus;
@@ -32,15 +34,15 @@
3234
import org.springframework.data.domain.Pageable;
3335
import org.springframework.data.rest.webmvc.BasePathAwareController;
3436
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
37+
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
38+
import org.springframework.hateoas.CollectionModel;
3539
import org.springframework.hateoas.EntityModel;
3640
import org.springframework.hateoas.server.RepresentationModelProcessor;
37-
import org.springframework.hateoas.CollectionModel;
3841
import org.springframework.http.HttpStatus;
3942
import org.springframework.http.ResponseEntity;
4043
import org.springframework.security.access.prepost.PreAuthorize;
4144
import org.springframework.web.bind.annotation.*;
4245
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
43-
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
4446

4547
import java.net.URI;
4648
import java.net.URISyntaxException;
@@ -170,6 +172,65 @@ public ResponseEntity<List<MultiStatus>> deleteObligations(
170172
return new ResponseEntity<>(results, HttpStatus.MULTI_STATUS);
171173
}
172174

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+
}
173234
@Override
174235
public RepositoryLinksResource process(RepositoryLinksResource resource) {
175236
resource.add(linkTo(ObligationController.class).slash("api" + OBLIGATION_URL).withRel("obligations"));

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/obligation/Sw360ObligationService.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
import org.apache.thrift.protocol.TProtocol;
1818
import org.apache.thrift.transport.THttpClient;
1919
import org.apache.thrift.transport.TTransportException;
20+
import org.eclipse.sw360.datahandler.common.CommonUtils;
2021
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
21-
import org.eclipse.sw360.datahandler.thrift.users.User;
22-
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
2322
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService;
23+
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
24+
import org.eclipse.sw360.datahandler.thrift.users.User;
2425
import org.springframework.beans.factory.annotation.Autowired;
2526
import org.springframework.beans.factory.annotation.Value;
26-
import org.springframework.stereotype.Service;
2727
import org.springframework.http.converter.HttpMessageNotReadableException;
28+
import org.springframework.stereotype.Service;
2829

2930
import java.util.List;
3031

@@ -80,4 +81,22 @@ private LicenseService.Iface getThriftLicenseClient() throws TTransportException
8081
TProtocol protocol = new TCompactProtocol(thriftClient);
8182
return new LicenseService.Client(protocol);
8283
}
84+
85+
public Obligation updateObligation(Obligation obligation, User sw360User) {
86+
try {
87+
if (CommonUtils.isNullEmptyOrWhitespace(obligation.getTitle())
88+
|| CommonUtils.isNullEmptyOrWhitespace(obligation.getText())) {
89+
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
90+
sw360LicenseClient.updateObligation(obligation, sw360User);
91+
92+
return obligation;
93+
94+
} else {
95+
throw new HttpMessageNotReadableException("Obligation Title, Text are required. Obligation Title, Text cannot contain only space character.");
96+
}
97+
} catch (TException e) {
98+
throw new RuntimeException("Error updating obligation", e);
99+
}
100+
}
101+
83102
}

0 commit comments

Comments
 (0)