From 0ebf42cb65868007235fc35183e15d70f84b1577 Mon Sep 17 00:00:00 2001 From: Shubham Goyal Date: Wed, 15 Feb 2023 11:36:50 +0100 Subject: [PATCH 1/2] add instance refId to instance read methods --- .../util/screen/MultiSelectEntityScreen.java | 18 +++++++++++------- .../org/commcare/util/screen/QueryScreen.java | 2 +- .../MemoryVirtualDataInstanceStorage.java | 2 +- .../core/interfaces/RemoteInstanceFetcher.java | 2 +- .../interfaces/VirtualDataInstanceStorage.java | 3 ++- .../commcare/suite/model/StackFrameStep.java | 2 +- .../instance/ExternalDataInstanceSource.java | 4 ++-- .../suite/model/test/QueryModelTests.java | 2 +- 8 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/cli/java/org/commcare/util/screen/MultiSelectEntityScreen.java b/src/cli/java/org/commcare/util/screen/MultiSelectEntityScreen.java index 1a6804837e..c28a50e34d 100644 --- a/src/cli/java/org/commcare/util/screen/MultiSelectEntityScreen.java +++ b/src/cli/java/org/commcare/util/screen/MultiSelectEntityScreen.java @@ -127,8 +127,8 @@ private void setSelectedEntities(String input, @Nullable String[] selectedValues } private void prcessSelectionAsGuid(String guid) throws CommCareSessionException { - ExternalDataInstance cachedInstance = virtualDataInstanceStorage.read( - guid, getSession().getNeededDatum().getDataId()); + String datumId = getNeededDatumId(); + ExternalDataInstance cachedInstance = virtualDataInstanceStorage.read(guid, datumId, datumId); if (cachedInstance == null) { throw new CommCareSessionException( "Could not make selection with reference id " + guid + " on this screen. " + @@ -137,6 +137,10 @@ private void prcessSelectionAsGuid(String guid) throws CommCareSessionException storageReferenceId = guid; } + private String getNeededDatumId() { + return getSession().getNeededDatum().getDataId(); + } + private void processSelectedReferences(TreeReference[] selectedRefs) { if (validateSelectionSize(selectedRefs.length)) { String[] evaluatedValues = new String[selectedRefs.length]; @@ -190,8 +194,8 @@ protected void updateSession(CommCareSession session) { } if (storageReferenceId != null) { if (selectedValuesInstance == null) { - selectedValuesInstance = virtualDataInstanceStorage.read( - storageReferenceId, getSession().getNeededDatum().getDataId()); + String datumId = getNeededDatumId(); + selectedValuesInstance = virtualDataInstanceStorage.read(storageReferenceId, datumId, datumId); } ExternalDataInstanceSource externalDataInstanceSource = ExternalDataInstanceSource.buildVirtual( selectedValuesInstance, storageReferenceId); @@ -203,8 +207,8 @@ protected void updateSession(CommCareSession session) { @Override public void updateDatum(CommCareSession session, String input) { storageReferenceId = input; - String dataId = session.getNeededDatum().getDataId(); - selectedValuesInstance = virtualDataInstanceStorage.read(storageReferenceId, dataId); + String dataId = getNeededDatumId(); + selectedValuesInstance = virtualDataInstanceStorage.read(storageReferenceId, dataId, dataId); ExternalDataInstanceSource externalDataInstanceSource = ExternalDataInstanceSource.buildVirtual( selectedValuesInstance, storageReferenceId); session.setDatum(STATE_MULTIPLE_DATUM_VAL, dataId, input, externalDataInstanceSource); @@ -235,7 +239,7 @@ public String getBreadcrumb(String input, UserSandbox sandbox, SessionWrapper se protected EvaluationContext getAutoLaunchEvaluationContext(String nextInput) { ExternalDataInstance instance; if (referencesContainStep(nextInput)) { - instance = virtualDataInstanceStorage.read(nextInput, "next_input"); + instance = virtualDataInstanceStorage.read(nextInput, "next_input", getNeededDatumId()); } else { // empty instance instance = VirtualInstances.buildSelectedValuesInstance("next_input", new String[0]); diff --git a/src/cli/java/org/commcare/util/screen/QueryScreen.java b/src/cli/java/org/commcare/util/screen/QueryScreen.java index 917d0facda..4578d10626 100644 --- a/src/cli/java/org/commcare/util/screen/QueryScreen.java +++ b/src/cli/java/org/commcare/util/screen/QueryScreen.java @@ -184,7 +184,7 @@ private ExternalDataInstance getUserInputInstance() { Map userQueryValues = remoteQuerySessionManager.getUserQueryValues(false); String key = getInstanceKey(instanceId, userQueryValues); if (instanceStorage.contains(key)) { - return instanceStorage.read(key, instanceId); + return instanceStorage.read(key, instanceId, refId); } ExternalDataInstance userInputInstance = VirtualInstances.buildSearchInputInstance( diff --git a/src/main/java/org/commcare/core/interfaces/MemoryVirtualDataInstanceStorage.java b/src/main/java/org/commcare/core/interfaces/MemoryVirtualDataInstanceStorage.java index c0e10ee356..9b432e57b3 100644 --- a/src/main/java/org/commcare/core/interfaces/MemoryVirtualDataInstanceStorage.java +++ b/src/main/java/org/commcare/core/interfaces/MemoryVirtualDataInstanceStorage.java @@ -31,7 +31,7 @@ public String write(String key, ExternalDataInstance dataInstance) { } @Override - public ExternalDataInstance read(String key, String instanceId) { + public ExternalDataInstance read(String key, String instanceId, String refId) { return storage.get(key); } diff --git a/src/main/java/org/commcare/core/interfaces/RemoteInstanceFetcher.java b/src/main/java/org/commcare/core/interfaces/RemoteInstanceFetcher.java index 62b49683cb..ef62900a60 100644 --- a/src/main/java/org/commcare/core/interfaces/RemoteInstanceFetcher.java +++ b/src/main/java/org/commcare/core/interfaces/RemoteInstanceFetcher.java @@ -9,7 +9,7 @@ */ public interface RemoteInstanceFetcher { - AbstractTreeElement getExternalRoot(String instanceId, ExternalDataInstanceSource source) + AbstractTreeElement getExternalRoot(String instanceId, ExternalDataInstanceSource source, String refId) throws RemoteInstanceException; class RemoteInstanceException extends Exception { diff --git a/src/main/java/org/commcare/core/interfaces/VirtualDataInstanceStorage.java b/src/main/java/org/commcare/core/interfaces/VirtualDataInstanceStorage.java index 6ddee5d0bd..de4e3c9302 100644 --- a/src/main/java/org/commcare/core/interfaces/VirtualDataInstanceStorage.java +++ b/src/main/java/org/commcare/core/interfaces/VirtualDataInstanceStorage.java @@ -15,8 +15,9 @@ public interface VirtualDataInstanceStorage { * * @param key The instance storage key * @param instanceId The instanceId to apply to the loaded instance + * @param refId Unique reference id to apply to the loaded instance */ - ExternalDataInstance read(String key, String instanceId); + ExternalDataInstance read(String key, String instanceId, String refId); boolean contains(String key); } diff --git a/src/main/java/org/commcare/suite/model/StackFrameStep.java b/src/main/java/org/commcare/suite/model/StackFrameStep.java index 8209a011dc..96573031bb 100644 --- a/src/main/java/org/commcare/suite/model/StackFrameStep.java +++ b/src/main/java/org/commcare/suite/model/StackFrameStep.java @@ -139,7 +139,7 @@ public void initDataInstanceSources(RemoteInstanceFetcher remoteInstanceFetcher) throws RemoteInstanceFetcher.RemoteInstanceException { for (ExternalDataInstanceSource source : dataInstanceSources.values()) { if (source.needsInit()) { - source.remoteInit(remoteInstanceFetcher); + source.remoteInit(remoteInstanceFetcher, getId()); } } } diff --git a/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java b/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java index b57cf5ecde..d11e07a611 100644 --- a/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java +++ b/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java @@ -124,10 +124,10 @@ public void init(AbstractTreeElement root) { this.root = root; } - public void remoteInit(RemoteInstanceFetcher remoteInstanceFetcher) + public void remoteInit(RemoteInstanceFetcher remoteInstanceFetcher, String refID) throws RemoteInstanceFetcher.RemoteInstanceException { String instanceId = getInstanceId(); - init(remoteInstanceFetcher.getExternalRoot(instanceId, this)); + init(remoteInstanceFetcher.getExternalRoot(instanceId, this, refID)); setUpInstanceRoot(root, instanceId, new InstanceBase(instanceId)); } diff --git a/src/test/java/org/commcare/backend/suite/model/test/QueryModelTests.java b/src/test/java/org/commcare/backend/suite/model/test/QueryModelTests.java index 425c04c70d..e93e29c40e 100644 --- a/src/test/java/org/commcare/backend/suite/model/test/QueryModelTests.java +++ b/src/test/java/org/commcare/backend/suite/model/test/QueryModelTests.java @@ -69,7 +69,7 @@ public void testScreenCreatesVirtualInstance() throws Exception { Map input = ImmutableMap.of("name", "bob", "age", "23"); Assert.assertEquals( VirtualInstances.buildSearchInputInstance(refId, input).getRoot(), - virtualDataInstanceStorage.read(expectedInstanceStorageKey, instanceID).getRoot()); + virtualDataInstanceStorage.read(expectedInstanceStorageKey, instanceID, refId).getRoot()); CaseTestUtils.xpathEvalAndAssert( session.getEvaluationContext(), From 1de808c9aef77932c6bc9ac439be4a1fcbae5ef7 Mon Sep 17 00:00:00 2001 From: Shubham Goyal Date: Wed, 15 Feb 2023 15:51:23 +0100 Subject: [PATCH 2/2] lint:refID -> refId --- .../core/model/instance/ExternalDataInstanceSource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java b/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java index d11e07a611..78ef63f70c 100644 --- a/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java +++ b/src/main/java/org/javarosa/core/model/instance/ExternalDataInstanceSource.java @@ -124,10 +124,10 @@ public void init(AbstractTreeElement root) { this.root = root; } - public void remoteInit(RemoteInstanceFetcher remoteInstanceFetcher, String refID) + public void remoteInit(RemoteInstanceFetcher remoteInstanceFetcher, String refId) throws RemoteInstanceFetcher.RemoteInstanceException { String instanceId = getInstanceId(); - init(remoteInstanceFetcher.getExternalRoot(instanceId, this, refID)); + init(remoteInstanceFetcher.getExternalRoot(instanceId, this, refId)); setUpInstanceRoot(root, instanceId, new InstanceBase(instanceId)); }