Skip to content

Commit

Permalink
Merge pull request #1451 from dimagi/copy_of_commcare_2.54
Browse files Browse the repository at this point in the history
Duplicate of commcare_2.54
  • Loading branch information
avazirna authored Oct 29, 2024
2 parents 67bcb5a + 6b59b9a commit e410ccf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/commcare/cases/entity/AsyncEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ public String getSortField(int i) {
sortData[i] = "<invalid xpath: " + xpe.getMessage() + ">";
}
}
return sortData[i];
}
return sortData[i];
}
return sortData[i];
}
} catch (IOException e) {
Logger.exception("Error while getting sort field", e);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/commcare/session/CommCareSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,6 @@ public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif,
Vector<Entry> entries = getEntriesForCommand(command);
Vector<Menu> menus = getMenusForCommand(command);

Menu menu = null;
Entry entry = null;
Hashtable<String, DataInstance> instancesInScope = new Hashtable<>();
Hashtable<String, DataInstance> menuInstances = null;
Expand All @@ -649,8 +648,7 @@ public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif,
}
}

if (!menus.isEmpty()) {
menu = menus.elementAt(0);
for (Menu menu : menus) {
if (menu != null) {
menuInstances = menu.getInstances(instancesToInclude);
}
Expand Down
38 changes: 35 additions & 3 deletions src/main/java/org/javarosa/core/model/FormIndex.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package org.javarosa.core.model;

import org.javarosa.core.model.instance.TreeReference;

import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.ExtWrapNullable;
import org.javarosa.core.util.externalizable.Externalizable;
import org.javarosa.core.util.externalizable.PrototypeFactory;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

/**
Expand All @@ -22,15 +30,15 @@
*
* @author Clayton Sims
*/
public class FormIndex {
public class FormIndex implements Externalizable {

private boolean beginningOfForm = false;
private boolean endOfForm = false;

/**
* The index of the questiondef in the current context
*/
private final int localIndex;
private int localIndex;

/**
* The multiplicity of the current instance of a repeated question or group
Expand All @@ -44,6 +52,10 @@ public class FormIndex {

private TreeReference reference;

// needed for serialization
public FormIndex(){
}

public static FormIndex createBeginningOfFormIndex() {
FormIndex begin = new FormIndex(-1, null);
begin.beginningOfForm = true;
Expand Down Expand Up @@ -453,4 +465,24 @@ public void assignRefs(FormDef f) {
i++;
}
}

@Override
public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
beginningOfForm = ExtUtil.readBool(in);
endOfForm = ExtUtil.readBool(in);
localIndex = ExtUtil.readInt(in);
instanceIndex = ExtUtil.readInt(in);
reference = (TreeReference)ExtUtil.read(in, new ExtWrapNullable(TreeReference.class), pf);
nextLevel = (FormIndex)ExtUtil.read(in, new ExtWrapNullable(FormIndex.class), pf);
}

@Override
public void writeExternal(DataOutputStream out) throws IOException {
ExtUtil.writeBool(out, beginningOfForm);
ExtUtil.writeBool(out, endOfForm);
ExtUtil.writeNumeric(out, localIndex);
ExtUtil.writeNumeric(out, instanceIndex);
ExtUtil.write(out, new ExtWrapNullable(reference));
ExtUtil.write(out, new ExtWrapNullable(nextLevel));
}
}
19 changes: 19 additions & 0 deletions src/test/java/org/commcare/backend/session/test/MenuTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.commcare.modern.session.SessionWrapper;
import org.commcare.session.CommCareSession;
import org.commcare.suite.model.AssertionSet;
import org.commcare.suite.model.Menu;
import org.commcare.suite.model.Suite;
Expand All @@ -12,6 +15,8 @@
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
* Tests for assertions in menus
*/
Expand All @@ -36,4 +41,18 @@ public void testAssertionsEvaluated() throws Exception {
assertNotNull(assertions);
assertEquals("custom_assertion.m0.0", assertionFailures.getArgument());
}

/**
* When there are multiple menu blocks with same ids, we should accumulate the required instances from all
* of the menus and their contained entries
*/
@Test
public void testMenuInstances_WhenMenuHaveSameIds() {
SessionWrapper currentSession = appWithMenuAssertions.getSession();
EvaluationContext ec = currentSession.getEvaluationContext(currentSession.getIIF(), "m3", null);
List<String> instanceIds = ec.getInstanceIds();
assertEquals(2, instanceIds.size());
assertTrue(instanceIds.contains("my_instance"));
assertTrue(instanceIds.contains("casedb"));
}
}
3 changes: 2 additions & 1 deletion src/test/resources/app_structure/suite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@
<text>Menu</text>
<command id="m0-f0"/>
</menu>
<menu id="m3">
<menu id="m3" relevant="count(instance('my_instance'))>0">
<text>Menu</text>
<command id="m0-f1"/>
<instance id="my_instance" src="my_instance_source"/>
</menu>
<endpoint id="case_list" respect-relevancy="false">
<argument id="selected_cases" instance-id="selected_cases" instance-src="jr://instance/selected-entities"/>
Expand Down

0 comments on commit e410ccf

Please sign in to comment.