Skip to content

Commit

Permalink
only delete rows on process next, set to null otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosttveit committed Sep 15, 2023
1 parent 8012ec8 commit 152d09c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
14 changes: 12 additions & 2 deletions src/Altinn.App.Core/Helpers/DataModel/DataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private static bool IsPropertyWithJsonName(PropertyInfo propertyInfo, string key
}

/// <inheritdoc />
public void RemoveField(string key)
public void RemoveField(string key, bool deleteRows = false)
{
var keys_split = key.Split('.');
var keys = keys_split[0..^1];
Expand Down Expand Up @@ -242,7 +242,17 @@ public void RemoveField(string key)
throw new ArgumentException($"Tried to remove row {key}, ended in a non-list ({propertyValue?.GetType()})");
}

listValue.RemoveAt(lastGroupIndex.Value);
if (deleteRows)
{
listValue.RemoveAt(lastGroupIndex.Value);
}
else
{

var genericType = listValue.GetType().GetGenericArguments().FirstOrDefault();
var nullValue = genericType?.IsValueType == true ? Activator.CreateInstance(genericType) : null;
listValue[lastGroupIndex.Value] = nullValue;
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Altinn.App.Core/Helpers/IDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface IDataModelAccessor
/// <summary>
/// Remove a value from the wrapped datamodel
/// </summary>
void RemoveField(string key);
void RemoveField(string key, bool deleteRows = false);

/// <summary>
/// Verify that a Key is a valid lookup for the datamodel
Expand Down
14 changes: 8 additions & 6 deletions src/Altinn.App.Core/Implementation/DefaultTaskEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private async Task RemoveHiddenData(Instance instance, Guid instanceGuid, List<D
// Remove hidden data before validation
var layoutSet = _appResources.GetLayoutSetForTask(dataType.TaskId);
var evaluationState = await _layoutEvaluatorStateInitializer.Init(instance, data, layoutSet?.Id);
LayoutEvaluator.RemoveHiddenData(evaluationState);
LayoutEvaluator.RemoveHiddenData(evaluationState, true);
}

// save the updated data if there are changes
Expand All @@ -282,22 +282,24 @@ private async Task RemoveShadowFields(Instance instance, Guid instanceGuid, List
instanceGuid, modelType, instance.Org, app, instanceOwnerPartyId, dataElementId);

var modifier = new IgnorePropertiesWithPrefix(dataType.AppLogic.ShadowFields.Prefix);
JsonSerializerOptions options = new ()
JsonSerializerOptions options = new()
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { modifier.ModifyPrefixInfo }
},
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};

string serializedData = JsonSerializer.Serialize(data, options);
if (dataType.AppLogic.ShadowFields.SaveToDataType != null) {
if (dataType.AppLogic.ShadowFields.SaveToDataType != null)
{
var saveToDataType = dataTypesToLock.Find(dt => dt.Id == dataType.AppLogic.ShadowFields.SaveToDataType);
if (saveToDataType == null) {
if (saveToDataType == null)
{
throw new Exception($"SaveToDataType {dataType.AppLogic.ShadowFields.SaveToDataType} not found");
}

Type saveToModelType = _appModel.GetModelType(saveToDataType.AppLogic.ClassRef);
var updatedData = JsonSerializer.Deserialize(serializedData, saveToModelType);
await _dataClient.InsertFormData(updatedData, instanceGuid, saveToModelType ?? modelType, instance.Org, app, instanceOwnerPartyId, saveToDataType.Id);
Expand Down
4 changes: 2 additions & 2 deletions src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ private static void HiddenFieldsForRemovalRecurs(LayoutEvaluatorState state, Has
/// <summary>
/// Remove fields that are only refrenced from hidden fields from the data object in the state.
/// </summary>
public static void RemoveHiddenData(LayoutEvaluatorState state)
public static void RemoveHiddenData(LayoutEvaluatorState state, bool deleteRows = false)
{
var fields = GetHiddenFieldsForRemoval(state);
foreach (var field in fields)
{
state.RemoveDataField(field);
state.RemoveDataField(field, deleteRows);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public ComponentContext GetComponentContext(string pageName, string componentId,
/// <summary>
/// Set the value of a field to null.
/// </summary>
public void RemoveDataField(string key)
public void RemoveDataField(string key, bool deleteRows = false)
{
_dataModel.RemoveField(key);
_dataModel.RemoveField(key, deleteRows);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions test/Altinn.App.Core.Tests/Helpers/JsonDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public string AddIndicies(string key, ReadOnlySpan<int> indicies = default)
}

/// <inheritdoc />
public void RemoveField(string key)
public void RemoveField(string key, bool deleteRows = false)
{
throw new NotImplementedException("Impossible to remove fields in a json model");
}
Expand All @@ -153,4 +153,4 @@ public bool VerifyKey(string key)
{
throw new NotImplementedException("Impossible to verify keys in a json model");
}
}
}

0 comments on commit 152d09c

Please sign in to comment.