Skip to content

Commit

Permalink
Make the change in Document.java
Browse files Browse the repository at this point in the history
Execution, IDocumentField and IDocumentChangesCollector: minor
improvements

verify if lookup value is still valid
#551
  • Loading branch information
metas-ts committed Aug 29, 2017
1 parent 426ded2 commit 6e28b22
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@ public List<JSONDocument> processParametersChangeEvents(
final IProcessInstancesRepository instancesRepository = getRepository(processId);

return Execution.callInNewExecution("", () -> {
final IDocumentChangesCollector changesCollector = Execution.getCurrentDocumentChangesCollectorOrNull();

final IDocumentChangesCollector changesCollector = Execution.getCurrentDocumentChangesCollectorOrNull(); // get our collector to fill with the changes that we will record

instancesRepository.forProcessInstanceWritable(pinstanceId, changesCollector, processInstance -> {

processInstance.processParameterValueChanges(events, REASON_Value_DirectSetFromCommitAPI);
return null; // void
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static IDocumentChangesCollector getCurrentDocumentChangesCollector()
}

/**
* @return actual {@link IDocumentChangesCollector} or {@link NullDocumentChangesCollector}
* @return actual {@link IDocumentChangesCollector} or {@link NullDocumentChangesCollector}. Never returns {@code null}.
*/
public static IDocumentChangesCollector getCurrentDocumentChangesCollectorOrNull()
{
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/de/metas/ui/web/window/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ public void processValueChange(final String fieldName, final Object value, final
}
}

public void processValueChanges(final List<JSONDocumentChangedEvent> events, final ReasonSupplier reason) throws DocumentFieldReadonlyException
public void processValueChanges(@NonNull final List<JSONDocumentChangedEvent> events, final ReasonSupplier reason) throws DocumentFieldReadonlyException
{
for (final JSONDocumentChangedEvent event : events)
{
Expand Down Expand Up @@ -1451,6 +1451,21 @@ else if (DependencyType.LookupValues == triggeringDependencyType)
final boolean lookupValuesStaled = documentField.setLookupValuesStaled(triggeringFieldName);
if (lookupValuesStaled && !lookupValuesStaledOld)
{
// https://github.com/metasfresh/metasfresh-webui-api/issues/551 check if we can leave the old value as it is
final Object valueOld = documentField.getValue();
if (valueOld != null)
{
final boolean currentValueStillValid = documentField.getLookupValues() // because we did setLookupValuesStaled(), this causes a reload
.stream()
.anyMatch(value -> Objects.equals(value, valueOld)); // check if the current value is still value after we reloaded the list
if(!currentValueStillValid)
{
documentField.setValue(null, changesCollector);
changesCollector.collectValueIfChanged(documentField, valueOld, reason);
}
}

// https://github.com/metasfresh/metasfresh-webui-frontend/issues/1165 - the value was not stale, but now it is => notify the frontend so it shall invalidate its cache
changesCollector.collectLookupValuesStaled(documentField, reason);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IDocumentChangesCollector
/**
* Mark the changes of given document path as primary changes.
*
* Primary changes are those changes which are on a document which was directly references by REST endpoint.
* Primary changes are those changes which are on a document which was directly referenced by REST endpoint.
*
* @param documentPath
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ default DocumentPath getDocumentPath()

void setDisplayed(LogicExpressionResult displayed);

/**
* Notify this instance that it's lookup values are staled. So next time they are needed, they need to be reloaded.
*
* @param triggeringFieldName
* @return
*/
boolean setLookupValuesStaled(String triggeringFieldName);

LookupValuesList getLookupValues();
Expand Down

0 comments on commit 6e28b22

Please sign in to comment.