Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/sshoc 475 new endpoint for item patch #485

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,20 @@ public ResponseEntity<DatasetDto> updateDataset(@PathVariable("persistentId") St
@RequestParam(value = "draft", defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(datasetService.updateDataset(persistentId, updatedDataset, draft, approved));
return ResponseEntity.ok(datasetService.updateDataset(persistentId, updatedDataset, draft, approved, false));
}

@Operation(summary = "Patch dataset for given persistentId")
@PatchMapping(path = "/{persistentId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DatasetDto> patchDataset(@PathVariable("persistentId") String persistentId,
@Parameter(
description = "Dataset patch",
required = true,
schema = @Schema(implementation = DatasetCore.class)) @RequestBody DatasetCore updatedDataset,
@RequestParam(value = "draft", defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(datasetService.updateDataset(persistentId, updatedDataset, draft, approved, true));
}

@Operation(summary = "Revert dataset to target version by its persistentId and versionId that is reverted to")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,20 @@ public ResponseEntity<PublicationDto> updatePublication(@PathVariable("persisten
@RequestParam(value = "draft", required = false, defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(publicationService.updatePublication(persistentId, updatedPublication, draft, approved));
return ResponseEntity.ok(publicationService.updatePublication(persistentId, updatedPublication, draft, approved, false));
}

@Operation(summary = "Patch publication for given persistentId")
@PatchMapping(path = "/{persistentId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<PublicationDto> patchPublication(@PathVariable("persistentId") String persistentId,
@Parameter(
description = "Publication patch",
required = true,
schema = @Schema(implementation = PublicationCore.class)) @RequestBody PublicationCore updatedPublication,
@RequestParam(value = "draft", required = false, defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(publicationService.updatePublication(persistentId, updatedPublication, draft, approved, true));
}

@Operation(summary = "Revert publication to target version by its persistentId and versionId that is reverted to")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public ResponseEntity<ToolDto> updateTool(@PathVariable("persistentId") String p
return ResponseEntity.ok(toolService.updateTool(persistentId, updatedTool, draft, approved));
}

@Operation(summary = "Patching a tool with new data for a given persistentId")
@PatchMapping(path = "/{persistentId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ToolDto> patchTool(@PathVariable("persistentId") String persistentId,
@Parameter(
description = "Tool patch",
required = true,
schema = @Schema(implementation = ToolCore.class)) @RequestBody ToolCore updatedTool,
@RequestParam(value = "draft", defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(toolService.updateTool(persistentId, updatedTool, draft, approved, true));
}

@Operation(summary = "Revert tool to target version by its persistentId and versionId that is reverted to")
@PutMapping(path = "/{persistentId}/versions/{versionId}/revert", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ToolDto> revertTool(@PathVariable("persistentId") String persistentId, @PathVariable("versionId") long versionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public ResponseEntity<TrainingMaterialDto> updateTrainingMaterial(@PathVariable(
@RequestParam(value = "draft", required = false, defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(trainingMaterialService.updateTrainingMaterial(persistentId, updatedTrainingMaterial, draft, approved));
return ResponseEntity.ok(trainingMaterialService.updateTrainingMaterial(persistentId, updatedTrainingMaterial, draft, approved, false));
}

@Operation(summary = "Patch training material for given persistentId")
@PatchMapping(path = "/{persistentId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<TrainingMaterialDto> patchTrainingMaterial(@PathVariable("persistentId") String persistentId,
@Parameter(
description = "Training material patch",
required = true,
schema = @Schema(implementation = TrainingMaterialCore.class)) @RequestBody TrainingMaterialCore updatedTrainingMaterial,
@RequestParam(value = "draft", required = false, defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(trainingMaterialService.updateTrainingMaterial(persistentId, updatedTrainingMaterial, draft, approved, true));
}

@Operation(summary = "Revert training material to target version by its persistentId and versionId that is reverted to")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,20 @@ public ResponseEntity<WorkflowDto> updateWorkflow(@PathVariable("persistentId")
@RequestParam(value = "draft", defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(workflowService.updateWorkflow(workflowPersistentId, updatedWorkflow, draft, approved));
return ResponseEntity.ok(workflowService.updateWorkflow(workflowPersistentId, updatedWorkflow, draft, approved, false));
}

@Operation(summary = "Patch workflow for given persistentId")
@PatchMapping(path = "/{persistentId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<WorkflowDto> patchWorkflow(@PathVariable("persistentId") String workflowPersistentId,
@Parameter(
description = "Workflow patch",
required = true,
schema = @Schema(implementation = WorkflowCore.class)) @RequestBody WorkflowCore updatedWorkflow,
@RequestParam(value = "draft", defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(workflowService.updateWorkflow(workflowPersistentId, updatedWorkflow, draft, approved, true));
}

@Operation(summary = "Revert workflow to target version by its persistentId and versionId that is reverted to")
Expand Down Expand Up @@ -167,7 +180,21 @@ public ResponseEntity<StepDto> updateStep(@PathVariable("persistentId") String w
@RequestParam(value = "draft", defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(stepService.updateStep(workflowPersistentId, stepPersistentId, updatedStep, draft, approved));
return ResponseEntity.ok(stepService.updateStep(workflowPersistentId, stepPersistentId, updatedStep, draft, approved, false));
}

@Operation(summary = "Patch step for given persistentId and workflow persistentId")
@PatchMapping(path = "/{persistentId}/steps/{stepPersistentId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<StepDto> patchStep(@PathVariable("persistentId") String workflowPersistentId,
@PathVariable("stepPersistentId") String stepPersistentId,
@Parameter(
description = "Updated step",
required = true,
schema = @Schema(implementation = StepCore.class)) @RequestBody StepCore updatedStep,
@RequestParam(value = "draft", defaultValue = "false") boolean draft,
@RequestParam(value = "approved", defaultValue = "true") boolean approved) throws VersionNotChangedException {

return ResponseEntity.ok(stepService.updateStep(workflowPersistentId, stepPersistentId, updatedStep, draft, approved, true));
}

@Operation(summary = "Revert step to target version by its persistentId and versionId that is reverted to")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public DatasetDto createDataset(DatasetCore datasetCore, boolean draft) {
}


public DatasetDto updateDataset(String persistentId, DatasetCore datasetCore, boolean draft, boolean approved)
public DatasetDto updateDataset(String persistentId, DatasetCore datasetCore, boolean draft, boolean approved, boolean patchMode)
throws VersionNotChangedException {
Dataset dataset = updateItem(persistentId, datasetCore, draft, approved);
Dataset dataset = updateItem(persistentId, datasetCore, draft, approved, patchMode);
return prepareItemDto(dataset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import eu.sshopencloud.marketplace.dto.auth.UserDto;
import eu.sshopencloud.marketplace.dto.items.*;
import eu.sshopencloud.marketplace.dto.sources.SourceDto;
import eu.sshopencloud.marketplace.dto.vocabularies.PropertyDto;
import eu.sshopencloud.marketplace.dto.vocabularies.PropertyTypeDto;
import eu.sshopencloud.marketplace.dto.vocabularies.*;
import eu.sshopencloud.marketplace.dto.workflows.WorkflowDto;
import eu.sshopencloud.marketplace.mappers.items.ItemExtBasicConverter;
import eu.sshopencloud.marketplace.mappers.vocabularies.VocabularyBasicMapper;
Expand All @@ -28,15 +27,14 @@
import eu.sshopencloud.marketplace.services.sources.SourceService;
import eu.sshopencloud.marketplace.services.vocabularies.PropertyTypeService;
import eu.sshopencloud.marketplace.services.vocabularies.VocabularyService;
import jakarta.persistence.EntityNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.lang.NonNull;
import org.springframework.security.access.AccessDeniedException;

import jakarta.persistence.EntityNotFoundException;

import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -200,73 +198,95 @@ protected boolean checkIfWorkflow(String itemPersistentId) {

protected I updateItem(String persistentId, C itemCore, boolean draft, boolean approved)
throws VersionNotChangedException {
return updateItem(persistentId, itemCore, draft, approved, false);
}

protected I updateItem(String persistentId, C itemCore, boolean draft, boolean approved, boolean patchMode)
throws VersionNotChangedException {
I currentItem = loadItemForCurrentUser(persistentId);
ComparisonResult comparisonResult = ComparisonResult.UPDATED;
if (!draft && currentItem.getStatus() != ItemStatus.DRAFT &&
!(approved && LoggedInUserHolder.getLoggedInUser() != null &&
!LoggedInUserHolder.getLoggedInUser().isContributor() &&
currentItem.getStatus() != ItemStatus.APPROVED)) {
comparisonResult = recognizePotentialChanges(currentItem, (ItemCore) itemCore);
comparisonResult = compareAndPatch(currentItem, itemCore, patchMode);
if (comparisonResult == ComparisonResult.UNMODIFIED
&& !(currentItem.getStatus() != ItemStatus.APPROVED && approved
&& LoggedInUserHolder.getLoggedInUser() != null
&& LoggedInUserHolder.getLoggedInUser().isModerator())) {
&& LoggedInUserHolder.getLoggedInUser() != null
&& LoggedInUserHolder.getLoggedInUser().isModerator())) {
throw new VersionNotChangedException();
}
}
return createOrUpdateItemVersion(itemCore, currentItem, draft, approved,
comparisonResult == ComparisonResult.CONFLICT);
}

protected ComparisonResult compareAndPatch(I currentItem, C itemRelCore) {
return compareAndPatch(currentItem, itemRelCore, false);
}


protected ComparisonResult recognizePotentialChanges(I currentItem, ItemCore itemCore) {
protected ComparisonResult compareAndPatch(I currentItem, C itemRelCore, boolean patchMode) {
ItemCore itemCore = itemRelCore instanceof WorkflowStepCore ?
((WorkflowStepCore) itemRelCore).getStepCore() : ((ItemCore) itemRelCore);
ItemDto currentItemDto = ItemsComparator.toDto(currentItem);
currentItemDto.setRelatedItems(itemRelatedItemService.getItemRelatedItems(currentItem));
complete(currentItemDto, currentItem);
ItemDto itemDtoFromSource = null;
if (itemCore.getSource() != null && itemCore.getSource().getId() != null
if (patchMode && itemCore.getSource() != null && itemCore.getSource().getId() != null
&& itemCore.getSourceItemId() != null) {
Item itemFromSource = getLastItemBySource(currentItem, itemCore.getSource().getId(),
itemCore.getSourceItemId());
Item itemFromSource = getItemBySource(currentItem, itemCore.getSource().getId(),
itemCore.getSourceItemId(), patchMode);
if (itemFromSource != null) {
itemDtoFromSource = ItemsComparator.toDtoSource(itemFromSource);
itemDtoFromSource.setRelatedItems(itemRelatedItemService.getItemRelatedItems(itemFromSource));
complete(itemDtoFromSource, itemFromSource);
}
}
ComparisonResult comparisonResult;
ItemDifferencesCore currentItemDifferences = ItemsComparator.differentiateItems(itemCore, currentItemDto);
if (itemDtoFromSource != null) {
if (itemDtoFromSource != null && patchMode) {
ItemDifferencesCore itemFromSourceDifferences = ItemsComparator.differentiateItems(itemCore,
itemDtoFromSource);
if (itemFromSourceDifferences.isEqual()) {
return ComparisonResult.UNMODIFIED;
comparisonResult = ComparisonResult.UNMODIFIED;
} else {
if (currentItemDifferences.isEqual()) {
return ComparisonResult.UNMODIFIED;
comparisonResult = ComparisonResult.UNMODIFIED;
} else {
if (ItemsConflictComparator.isConflict(currentItemDifferences, itemFromSourceDifferences)) {
return ComparisonResult.CONFLICT;
comparisonResult = ComparisonResult.CONFLICT;
} else {
return ComparisonResult.UPDATED;
comparisonResult = ComparisonResult.UPDATED;
}
}
}
} else {
if (currentItemDifferences.isEqual()) {
return ComparisonResult.UNMODIFIED;
comparisonResult = ComparisonResult.UNMODIFIED;
} else {
return ComparisonResult.UPDATED;
comparisonResult = ComparisonResult.UPDATED;
}
}

if (patchMode) {
if (comparisonResult == ComparisonResult.CONFLICT || comparisonResult == ComparisonResult.UPDATED) {
ItemsPatcher.patchItemCore(currentItemDto, itemDtoFromSource, itemCore);
}
}
return comparisonResult;
}


private Item getLastItemBySource(@NonNull I currentItem, @NonNull Long sourceId, @NonNull String sourceItemId) {
private Item getItemBySource(@NonNull I currentItem, @NonNull Long sourceId, @NonNull String sourceItemId, boolean patchMode) {
List<Item> history = loadItemHistory(currentItem);
if (patchMode) {
history = history.reversed();
}
for (Item historicalItem : history)
if (historicalItem.getSource() != null) {
if (sourceId.equals(historicalItem.getSource().getId()) && sourceItemId.equals(
historicalItem.getSourceItemId()) && historicalItem.getInformationContributor().isSystemContributor()) {
historicalItem.getSourceItemId()) && historicalItem.getInformationContributor().getRole().hasContributorPrivileges()) {
return historicalItem;
}
}
Expand Down
Loading