Skip to content

Commit

Permalink
Merge pull request #1221 from dimagi/instanceLoadingFix
Browse files Browse the repository at this point in the history
Fixes loading Search Input Instance from storage
  • Loading branch information
shubham1g5 authored Feb 15, 2023
2 parents 4a27931 + 1de808c commit 01a9276
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
18 changes: 11 additions & 7 deletions src/cli/java/org/commcare/util/screen/MultiSelectEntityScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. " +
Expand All @@ -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];
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/java/org/commcare/util/screen/QueryScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private ExternalDataInstance getUserInputInstance() {
Map<String, String> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/main/java/org/commcare/suite/model/StackFrameStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testScreenCreatesVirtualInstance() throws Exception {
Map<String, String> 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(),
Expand Down

0 comments on commit 01a9276

Please sign in to comment.