Repository navigation
[NAE-2502] - Dashboard items: updateDashboardItem fails on an existin… #486
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
Open
martinkranec
wants to merge
2
commits into
release/7.1.0
Choose a base branch
from
NAE-2502
base: release/7.1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,6 @@ | |
| import com.netgrif.application.engine.objects.auth.domain.AbstractUser; | ||
| import com.netgrif.application.engine.objects.auth.domain.ActorTransformer; | ||
| import com.netgrif.application.engine.objects.auth.domain.LoggedUser; | ||
| import com.netgrif.application.engine.objects.common.ResourceNotFoundException; | ||
| import com.netgrif.application.engine.objects.common.ResourceNotFoundExceptionCode; | ||
| import com.netgrif.application.engine.objects.petrinet.domain.PetriNet; | ||
| import com.netgrif.application.engine.objects.petrinet.domain.throwable.TransitionNotExecutableException; | ||
| import com.netgrif.application.engine.objects.utils.MenuItemUtils; | ||
| import com.netgrif.application.engine.objects.workflow.domain.Case; | ||
|
|
@@ -31,9 +28,7 @@ | |
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.*; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
|
|
@@ -74,7 +69,7 @@ public Case getOrCreate(DashboardItemBody body) throws TransitionNotExecutableEx | |
| .author(loggedUser) | ||
| .build()).getCase(); | ||
| ToDataSetOutcome outcome = body.toDataSet(); | ||
| itemCase = setDataWithExecute(itemCase, DashboardItemConstants.TASK_CONFIGURE, outcome.getDataSet()); | ||
| itemCase = setDataWithExecute(itemCase, DashboardItemConstants.TASK_CONFIGURE, splitToPhases(outcome.getDataSet())); | ||
| return itemCase; | ||
| } | ||
|
|
||
|
|
@@ -91,7 +86,7 @@ public Case getOrCreate(DashboardItemBody body) throws TransitionNotExecutableEx | |
| public Case update(Case itemCase, DashboardItemBody body) throws TransitionNotExecutableException { | ||
| MenuItemUtils.sanitize(body.getId()); | ||
| ToDataSetOutcome outcome = body.toDataSet(); | ||
| itemCase = setDataWithExecute(itemCase, DashboardItemConstants.TASK_CONFIGURE, outcome.getDataSet()); | ||
| itemCase = setDataWithExecute(itemCase, DashboardItemConstants.TASK_CONFIGURE, splitToPhases(outcome.getDataSet())); | ||
| return itemCase; | ||
| } | ||
|
|
||
|
|
@@ -113,20 +108,111 @@ protected Case setData(Case useCase, String transId, Map<String, Map<String, Obj | |
| return dataService.setData(taskId, ImportHelper.populateDataset((Map) dataSet)).getCase(); | ||
| } | ||
|
|
||
| /** | ||
| * Assigns the task, sets the data one batch after another (in the given order) and finishes the task. | ||
| * If a step fails, the values the batches touched are restored in the still assigned task, in the same | ||
| * batch order, and the task is cancelled. An existing item thus keeps its data and its active state | ||
| * (assigning the task deactivates it, cancelling does not bring it back). | ||
| */ | ||
| @SuppressWarnings({"unchecked", "rawtypes"}) | ||
| protected Case setDataWithExecute(Case useCase, String transId, Map<String, Map<String, Object>> dataSet) throws TransitionNotExecutableException { | ||
| protected Case setDataWithExecute(Case useCase, String transId, List<Map<String, Map<String, Object>>> dataSetBatches) throws TransitionNotExecutableException { | ||
| AbstractUser loggedUser = userService.getLoggedOrSystem(); | ||
| String taskId = MenuItemUtils.findTaskIdInCase(useCase, transId); | ||
| List<Map<String, Map<String, Object>>> rollbackBatches = buildRollbackBatches(useCase, dataSetBatches); | ||
| Task task = taskService.findOne(taskId); | ||
| task = taskService.assignTask(TaskParams.with() | ||
| .task(task) | ||
| .user(loggedUser) | ||
| .build()).getTask(); | ||
| task = dataService.setData(task, ImportHelper.populateDataset((Map) dataSet)).getTask(); | ||
| return taskService.finishTask(TaskParams.with() | ||
| .task(task) | ||
| .user(loggedUser) | ||
| .build()).getCase(); | ||
| try { | ||
| for (Map<String, Map<String, Object>> batch : dataSetBatches) { | ||
| task = dataService.setData(task, ImportHelper.populateDataset((Map) batch)).getTask(); | ||
| } | ||
| return taskService.finishTask(TaskParams.with() | ||
| .task(task) | ||
| .user(loggedUser) | ||
| .build()).getCase(); | ||
| } catch (RuntimeException | TransitionNotExecutableException e) { | ||
| restoreQuietly(task, rollbackBatches); | ||
| cancelQuietly(task, loggedUser); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reads the current values of the fields the batches are going to write, in the same type and batch order. | ||
| * Empty for an item that has no id yet: a case that is still being created has nothing to go back to. | ||
| */ | ||
| private List<Map<String, Map<String, Object>>> buildRollbackBatches(Case useCase, List<Map<String, Map<String, Object>>> dataSetBatches) { | ||
| Case current = workflowService.findOne(useCase.getStringId()); | ||
| Object currentId = current.getDataSet().get(DashboardItemConstants.FIELD_ID) == null ? null | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it needed to be |
||
| : current.getDataSet().get(DashboardItemConstants.FIELD_ID).getValue(); | ||
| if (currentId == null || currentId.toString().isEmpty()) { | ||
| return List.of(); | ||
| } | ||
| List<Map<String, Map<String, Object>>> rollback = new ArrayList<>(); | ||
| for (Map<String, Map<String, Object>> batch : dataSetBatches) { | ||
| Map<String, Map<String, Object>> restore = new LinkedHashMap<>(); | ||
| batch.forEach((fieldId, entry) -> { | ||
| Map<String, Object> restoreEntry = new LinkedHashMap<>(); | ||
| restoreEntry.put("type", entry.get("type")); | ||
| restoreEntry.put("value", current.getDataSet().get(fieldId) == null ? null : current.getDataSet().get(fieldId).getValue()); | ||
| restore.put(fieldId, restoreEntry); | ||
| }); | ||
| rollback.add(restore); | ||
| } | ||
| return rollback; | ||
| } | ||
|
|
||
| @SuppressWarnings({"unchecked", "rawtypes"}) | ||
| private void restoreQuietly(Task task, List<Map<String, Map<String, Object>>> rollbackBatches) { | ||
| try { | ||
| Task assigned = taskService.findOne(task.getStringId()); | ||
| for (Map<String, Map<String, Object>> batch : rollbackBatches) { | ||
| assigned = dataService.setData(assigned, ImportHelper.populateDataset((Map) batch)).getTask(); | ||
| } | ||
| } catch (RuntimeException e) { | ||
| log.warn("Could not restore the data of task {} after a failed dashboard item update", task.getStringId(), e); | ||
| } | ||
| } | ||
|
|
||
| private void cancelQuietly(Task task, AbstractUser user) { | ||
| try { | ||
| taskService.cancelTask(TaskParams.with() | ||
| .task(taskService.findOne(task.getStringId())) | ||
| .user(user) | ||
| .build()); | ||
| } catch (RuntimeException e) { | ||
| log.warn("Could not cancel task {} after a failed dashboard item update", task.getStringId(), e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Splits the data set into the steps a user takes in the configuration form. Set events of the | ||
| * dashboard_item process reset the icon and name whenever the target (is_internal, menu item), the | ||
| * inherit flags or external_icon change, so the values of the last step must be written after them. | ||
| * The order inside a phase matters too (is_internal clears external_url), hence ordered lists. | ||
| */ | ||
| private List<Map<String, Map<String, Object>>> splitToPhases(Map<String, Map<String, Object>> dataSet) { | ||
| List<List<String>> phases = List.of( | ||
| List.of(DashboardItemConstants.FIELD_ID, DashboardItemConstants.FIELD_IS_INTERNAL, | ||
| DashboardItemConstants.FIELD_EXTERNAL_URL), | ||
| List.of(DashboardItemConstants.FIELD_MENU_ITEM_LIST), | ||
| List.of(DashboardItemConstants.FIELD_INHERIT_ICON, DashboardItemConstants.FIELD_INHERIT_NAME), | ||
| List.of(DashboardItemConstants.FIELD_EXTERNAL_ICON)); | ||
| List<Map<String, Map<String, Object>>> batches = new ArrayList<>(); | ||
| Map<String, Map<String, Object>> rest = new LinkedHashMap<>(dataSet); | ||
| for (List<String> phase : phases) { | ||
| Map<String, Map<String, Object>> batch = new LinkedHashMap<>(); | ||
| phase.forEach(fieldId -> { | ||
| if (rest.containsKey(fieldId)) { | ||
| batch.put(fieldId, rest.remove(fieldId)); | ||
| } | ||
| }); | ||
| batches.add(batch); | ||
| } | ||
| batches.add(rest); | ||
| return batches; | ||
| } | ||
|
|
||
| protected Case findCase(String processIdentifier, String query) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: netgrif/application-engine
Length of output: 6850
Script executed:
Repository: netgrif/application-engine
Length of output: 33110
Script executed:
Repository: netgrif/application-engine
Length of output: 19598
Script executed:
Repository: netgrif/application-engine
Length of output: 6155
Script executed:
Repository: netgrif/application-engine
Length of output: 8740
Script executed:
Repository: netgrif/application-engine
Length of output: 5062
Restore the case state when a phased update fails.
A later
DataService.setDatacall orfinishTaskcan fail after earlier batches have been saved.cancelQuietlyonly returns the task tokens and saves the current case; it does not restore the previous dataset. The assign event has already setis_activetofalse, so an existing dashboard item can remain inactive with partial updates.The
finishTaskfailure path also existed before this PR. The phased writes add new failure points between batches and therefore worsen the partial-state window. Use one atomic write sequence, or restore the original case data and active state when any phase fails.