Skip to content
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

[experiment] replay actions #1330

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/cli/java/org/commcare/util/screen/QueryScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import com.google.common.collect.Multimap;

import org.commcare.cases.util.StringUtils;
import org.commcare.core.encryption.CryptUtil;
import org.commcare.core.interfaces.VirtualDataInstanceStorage;
import org.commcare.data.xml.VirtualInstances;
Expand All @@ -19,7 +18,6 @@
import org.commcare.session.RemoteQuerySessionManager;
import org.commcare.suite.model.RemoteQueryDatum;
import org.commcare.suite.model.QueryPrompt;
import org.javarosa.core.model.SelectChoice;
import org.javarosa.core.model.instance.ExternalDataInstance;
import org.javarosa.core.model.instance.ExternalDataInstanceSource;
import org.javarosa.core.services.locale.Localization;
Expand All @@ -33,7 +31,6 @@
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import datadog.trace.api.Trace;

Expand Down Expand Up @@ -149,10 +146,10 @@ public Pair<ExternalDataInstance, String> processResponse(InputStream responseDa
return instanceOrError;
}

public void updateSession(ExternalDataInstance dataInstance) {
public void updateSession(ExternalDataInstance dataInstance, Multimap<String, String> queryData) {
if (dataInstance != null) {
ExternalDataInstance userInputInstance = getUserInputInstance();
sessionWrapper.setQueryDatum(dataInstance, userInputInstance);
sessionWrapper.setQueryDatum(dataInstance, userInputInstance, queryData);
}
}

Expand Down Expand Up @@ -247,7 +244,7 @@ public boolean handleInputAndUpdateSession(CommCareSession session, String input
Multimap<String, String> requestData = getQueryParams(false);
InputStream response = sessionUtils.makeQueryRequest(url, requestData, domainedUsername, password);
Pair<ExternalDataInstance, String> instanceOrError = processResponse(response, url, requestData);
updateSession(instanceOrError.first);
updateSession(instanceOrError.first, requestData);
if (currentMessage != null) {
out.println(currentMessage);
}
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/org/commcare/session/CommCareSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.Vector;
import java.util.*;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -505,15 +500,26 @@ public void setDatum(String type, String keyId, String value, ExternalDataInstan
* Set a (xml) data instance as the result to a session query datum.
* The instance is available in session's evaluation context until the corresponding query frame is removed
*/
public void setQueryDatum(ExternalDataInstance queryResultInstance, ExternalDataInstance... extras) {
public void setQueryDatum(ExternalDataInstance queryResultInstance) {
setQueryDatum(queryResultInstance, null);
}

public void setQueryDatum(ExternalDataInstance queryResultInstance, ExternalDataInstance instance) {
setQueryDatum(queryResultInstance, instance, null);
}

public void setQueryDatum(ExternalDataInstance queryResultInstance, ExternalDataInstance instance, Multimap<String, String> extras) {
SessionDatum datum = getNeededDatum();
if (datum instanceof RemoteQueryDatum) {
StackFrameStep step = new StackFrameStep(
SessionFrame.STATE_QUERY_REQUEST, datum.getDataId(), datum.getValue());
step.addDataInstanceSource(queryResultInstance.getSource());
for (ExternalDataInstance instance : extras) {
if (instance != null) {
step.addDataInstanceSource(instance.getSource());
}
if (extras != null) {
extras.entries().forEach(entry -> step.addExtra(entry.getKey(), entry.getValue()));
}
frame.pushStep(step);
syncState();
} else {
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/commcare/xml/StackFrameStepParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,16 @@ private StackFrameStep parseJump() throws InvalidStructureException, IOException
}

private StackFrameStep parseValue(String type, String datumId) throws XmlPullParserException, IOException, InvalidStructureException {
//TODO: ... require this to have a value!!!! It's not processing this properly
String value = parser.getAttributeValue(null, "value");
boolean valueIsXpath;
if (value == null) {
//must have a child
value = parser.nextText();
//Can we get here, or would this have caused an exception?
if (value == null) {
throw new InvalidStructureException("Stack frame element must define a value expression or have a direct value", parser);
} else {
if (value != null) {
value = value.trim();
if (value.isEmpty()) {
value = null;
}
}
valueIsXpath = false;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/commcare/xml/ParserTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public class ParserTestUtils {

public static <T extends CommCareElementParser> T buildParser(String xml, Class<T> parserClass) {
public static <T extends ElementParser> T buildParser(String xml, Class<T> parserClass) {
return buildParser(xml, (xmlParser) -> {
try {
Constructor<T> constructor = parserClass.getConstructor(KXmlParser.class);
Expand All @@ -23,7 +23,7 @@ public static <T extends CommCareElementParser> T buildParser(String xml, Class<
});
}

public static <T extends CommCareElementParser> T buildParser(String xml, Function<KXmlParser, T> builder) {
public static <T extends ElementParser> T buildParser(String xml, Function<KXmlParser, T> builder) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
KXmlParser parser = ElementParser.instantiateParser(inputStream);
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/commcare/xml/StackParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.commcare.xml;

import org.commcare.session.SessionFrame;
import org.commcare.suite.model.StackFrameStep;
import org.commcare.suite.model.StackOperation;
import org.javarosa.xml.util.InvalidStructureException;
import org.junit.Assert;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class StackParserTest {

@Test
public void testParseRewind() throws IOException, InvalidStructureException, XmlPullParserException {
String xml = "<push><rewind /></push>";
StackOpParser parser = ParserTestUtils.buildParser(xml, StackOpParser::new);
StackOperation op = parser.parse();
Assert.assertEquals(StackOperation.OPERATION_PUSH, op.getOp());
StackFrameStep steps = op.getStackFrameSteps().get(0);
Assert.assertEquals(steps.getElementType(), SessionFrame.STATE_REWIND);
System.out.println(steps.getValue());
Assert.assertNull(steps.getValue());
}
}