Skip to content

Commit

Permalink
AJ-1941: collection id is always auto-generated on creation (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidangb authored Aug 9, 2024
1 parent 4d2534b commit 9b8fc15
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.UUID;
import org.databiosphere.workspacedataservice.annotations.DeploymentMode.DataPlane;
import org.databiosphere.workspacedataservice.generated.CollectionApi;
import org.databiosphere.workspacedataservice.generated.CollectionRequestServerModel;
import org.databiosphere.workspacedataservice.generated.CollectionServerModel;
import org.databiosphere.workspacedataservice.service.CollectionService;
import org.databiosphere.workspacedataservice.service.PermissionService;
Expand Down Expand Up @@ -36,15 +37,15 @@ public CollectionController(
* id.
*
* @param workspaceId Workspace id (required)
* @param collectionServerModel The collection to create (required)
* @param collectionRequestServerModel The collection to create (required)
* @return The collection just created. (status code 201)
*/
@Override
public ResponseEntity<CollectionServerModel> createCollectionV1(
UUID workspaceId, CollectionServerModel collectionServerModel) {
UUID workspaceId, CollectionRequestServerModel collectionRequestServerModel) {
permissionService.requireWritePermission(WorkspaceId.of(workspaceId));
CollectionServerModel coll =
collectionService.save(WorkspaceId.of(workspaceId), collectionServerModel);
collectionService.save(WorkspaceId.of(workspaceId), collectionRequestServerModel);
return new ResponseEntity<>(coll, HttpStatus.CREATED);
}

Expand Down Expand Up @@ -93,21 +94,23 @@ public ResponseEntity<List<CollectionServerModel>> listCollectionsV1(UUID worksp

/**
* PUT /collections/v1/{workspaceId}/{collectionId} : Update the specified collection.
* WdsCollection id is optional in the request body. If specified, it must match the collection id
* specified in the url.
*
* @param workspaceId Workspace id (required)
* @param collectionId WdsCollection id (required)
* @param collectionServerModel The collection to update (required)
* @param collectionRequestServerModel The collection to update (required)
* @return The collection just updated. (status code 200)
*/
@Override
public ResponseEntity<CollectionServerModel> updateCollectionV1(
UUID workspaceId, UUID collectionId, CollectionServerModel collectionServerModel) {
UUID workspaceId,
UUID collectionId,
CollectionRequestServerModel collectionRequestServerModel) {
permissionService.requireWritePermission(WorkspaceId.of(workspaceId));
CollectionServerModel coll =
collectionService.update(
WorkspaceId.of(workspaceId), CollectionId.of(collectionId), collectionServerModel);
WorkspaceId.of(workspaceId),
CollectionId.of(collectionId),
collectionRequestServerModel);
return new ResponseEntity<>(coll, HttpStatus.OK);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.databiosphere.workspacedataservice.config.TenancyProperties;
import org.databiosphere.workspacedataservice.dao.CollectionDao;
import org.databiosphere.workspacedataservice.dao.CollectionRepository;
import org.databiosphere.workspacedataservice.generated.CollectionRequestServerModel;
import org.databiosphere.workspacedataservice.generated.CollectionServerModel;
import org.databiosphere.workspacedataservice.service.model.exception.CollectionException;
import org.databiosphere.workspacedataservice.service.model.exception.ConflictException;
Expand Down Expand Up @@ -76,33 +77,29 @@ void setWorkspaceId(@Nullable @SingleTenant WorkspaceId workspaceId) {
* Insert a new collection
*
* @param workspaceId the workspace to contain this collection
* @param collectionServerModel the collection definition
* @param collectionRequestServerModel the collection definition
* @return the created collection
*/
@WriteTransaction
public CollectionServerModel save(
WorkspaceId workspaceId, CollectionServerModel collectionServerModel) {
WorkspaceId workspaceId, CollectionRequestServerModel collectionRequestServerModel) {

// if WDS is running in single-tenant mode, ensure the specified workspace matches
if (tenancyProperties.getEnforceCollectionsMatchWorkspaceId()
&& !workspaceId.equals(this.workspaceId)) {
throw new ValidationException("Cannot create collection in this workspace.");
}

// if user did not specify an id, generate one
CollectionId collectionId;
if (collectionServerModel.getId() != null) {
collectionId = CollectionId.of(collectionServerModel.getId());
} else {
collectionId = CollectionId.of(UUID.randomUUID());
}
// generate a collection id
CollectionId collectionId = CollectionId.of(UUID.randomUUID());

// translate CollectionServerModel to WdsCollection
WdsCollection wdsCollectionRequest =
new WdsCollectionCreateRequest(
workspaceId,
collectionId,
collectionServerModel.getName(),
collectionServerModel.getDescription());
collectionRequestServerModel.getName(),
collectionRequestServerModel.getDescription());

// save, handle exceptions, and translate to the response model
CollectionServerModel response = saveAndHandleExceptions(wdsCollectionRequest);
Expand Down Expand Up @@ -195,21 +192,13 @@ public CollectionServerModel get(WorkspaceId workspaceId, CollectionId collectio
*
* @param workspaceId the workspace containing the collection to be updated
* @param collectionId id of the collection to be updated
* @param collectionServerModel object containing the updated name and description. Collection id
* is optional in this object. If specified, it must match the collectionId argument.
* @param collectionRequestServerModel object containing the updated name and description.
* @return the updated collection
*/
public CollectionServerModel update(
WorkspaceId workspaceId,
CollectionId collectionId,
CollectionServerModel collectionServerModel) {

// if collection id is specified in the CollectionServerModel, ensure it matches
if (collectionServerModel.getId() != null
&& !collectionServerModel.getId().equals(collectionId.id())) {
throw new ValidationException(
"Collection id in request body does not match collection id in URL. You can omit the collection id from the request body.");
}
CollectionRequestServerModel collectionRequestServerModel) {

// retrieve the collection; throw if collection not found
WdsCollection found =
Expand All @@ -218,8 +207,8 @@ public CollectionServerModel update(
.orElseThrow(() -> new MissingObjectException(COLLECTION));

// optimization: if the request doesn't change anything, don't bother writing to the db
if (found.name().equals(collectionServerModel.getName())
&& found.description().equals(collectionServerModel.getDescription())) {
if (found.name().equals(collectionRequestServerModel.getName())
&& found.description().equals(collectionRequestServerModel.getDescription())) {
// make sure this response has an id
CollectionServerModel response = new CollectionServerModel(found.name(), found.description());
response.id(found.collectionId().id());
Expand All @@ -231,8 +220,8 @@ public CollectionServerModel update(
new WdsCollection(
found.workspaceId(),
found.collectionId(),
collectionServerModel.getName(),
collectionServerModel.getDescription());
collectionRequestServerModel.getName(),
collectionRequestServerModel.getDescription());

// save, handle exceptions, and translate to the response model
CollectionServerModel response = saveAndHandleExceptions(updateRequest);
Expand Down Expand Up @@ -298,7 +287,7 @@ public List<UUID> listCollections(String version) {
}

/**
* @deprecated Use {@link #save(WorkspaceId, CollectionServerModel)} instead.
* @deprecated Use {@link #save(WorkspaceId, CollectionRequestServerModel)} instead.
*/
@Deprecated(forRemoval = true, since = "v0.14.0")
public void createCollection(UUID collectionId, String version) {
Expand All @@ -314,7 +303,7 @@ public void createCollection(UUID collectionId, String version) {
}

/**
* @deprecated Use {@link #save(WorkspaceId, CollectionServerModel)} instead.
* @deprecated Use {@link #save(WorkspaceId, CollectionRequestServerModel)} instead.
*/
@Deprecated(forRemoval = true, since = "v0.14.0")
public void createCollection(WorkspaceId workspaceId, CollectionId collectionId, String version) {
Expand Down
Loading

0 comments on commit 9b8fc15

Please sign in to comment.