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

Make language slug available via session metadata (formplayer branch) #1441

Merged
merged 4 commits into from
Oct 16, 2024
Merged
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
8 changes: 6 additions & 2 deletions src/cli/java/org/commcare/util/screen/EntityScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ protected void setSession(SessionWrapper session) throws CommCareSessionExceptio
if (mShortDetail == null) {
throw new CommCareSessionException("Missing detail definition for: " + detailId);
}

evalContext = mSession.getEvaluationContext();
EntityScreenContext context = getEntityScreenContext();
String locale = null;
if (context != null) {
locale = context.getLocale();
}
evalContext = mSession.getEvaluationContextWithLocale(locale);
}

@Trace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class EntityScreenContext extends ScreenContext{
private final int mSortIndex;
private final int mCasesPerPage;
private final String[] mSelectedValues;
private final String mLocale;

/**
* If requesting a case detail will be a case id, else null. When the case id is given it is used to short
Expand All @@ -20,13 +21,14 @@ public class EntityScreenContext extends ScreenContext{
private final boolean fuzzySearch;

public EntityScreenContext(int offset, String searchText, int sortIndex, int casesPerPage,
String[] selectedValues, String detailSelection, boolean isFuzzySearch) {
String[] selectedValues, String locale, String detailSelection, boolean isFuzzySearch) {
super(true);
mOffSet = offset;
mSearchText = searchText;
mSortIndex = sortIndex;
mCasesPerPage = casesPerPage == 0 ? DEFAULT_CASES_PER_PAGE : casesPerPage;
mSelectedValues = selectedValues;
mLocale = locale;
mDetailSelection = detailSelection;
fuzzySearch = isFuzzySearch;
}
Expand All @@ -42,6 +44,7 @@ public EntityScreenContext(boolean respectRelevancy) {
mSortIndex = 0;
mCasesPerPage = DEFAULT_CASES_PER_PAGE;
mSelectedValues = null;
mLocale = null;
mDetailSelection = null;
fuzzySearch = false;
}
Expand All @@ -66,6 +69,10 @@ public String[] getSelectedValues() {
return mSelectedValues;
}

public String getLocale() {
return mLocale;
}

public String getDetailSelection() {
return mDetailSelection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public ExternalDataInstance getSpecializedExternalDataInstance(ExternalDataInsta

@Override
@Nonnull
public InstanceRoot generateRoot(ExternalDataInstance instance) {
public InstanceRoot generateRoot(ExternalDataInstance instance, String locale) {
String ref = instance.getReference();
if (ref.contains(LedgerInstanceTreeElement.MODEL_NAME)) {
return setupLedgerData(instance);
Expand All @@ -83,7 +83,7 @@ public InstanceRoot generateRoot(ExternalDataInstance instance) {
} else if (ref.contains("fixture")) {
return setupFixtureData(instance);
} else if (instance.getReference().contains("session")) {
return setupSessionData(instance);
return setupSessionData(instance, locale);
} else if (ref.startsWith(ExternalDataInstance.JR_REMOTE_REFERENCE)) {
return setupExternalDataInstance(instance, ref, SessionFrame.STATE_QUERY_REQUEST);
} else if (ref.startsWith(JR_SELECTED_ENTITIES_REFERENCE)) {
Expand Down Expand Up @@ -236,15 +236,15 @@ protected TreeElement loadFixtureRoot(ExternalDataInstance instance,
}
}

protected InstanceRoot setupSessionData(ExternalDataInstance instance) {
protected InstanceRoot setupSessionData(ExternalDataInstance instance, String locale) {
if (this.mPlatform == null) {
throw new RuntimeException("Cannot generate session instance with undeclared platform!");
}
User u = mSandbox.getLoggedInUserUnsafe();
TreeElement root =
SessionInstanceBuilder.getSessionInstance(sessionWrapper.getFrame(), getDeviceId(),
getVersionString(), getCurrentDrift(), u.getUsername(), u.getUniqueId(),
u.getProperties(), getWindowWidth());
u.getProperties(), getWindowWidth(), locale);
root.setParent(instance.getBase());
return new ConcreteInstanceRoot(root);
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/commcare/modern/session/SessionWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ public EvaluationContext getEvaluationContext() {
return getEvaluationContext(getIIF());
}

public EvaluationContext getEvaluationContextWithLocale(String locale) {
return getEvaluationContext(getIIF(), locale);
}

@Override
public EvaluationContext getRestrictedEvaluationContext(String commandId,
Set<String> instancesToInclude) {
return getEvaluationContext(getIIF(), commandId, instancesToInclude);
return getEvaluationContext(getIIF(), commandId, instancesToInclude, null);
}

@Override
Expand All @@ -81,7 +85,7 @@ public EvaluationContext getEvaluationContextWithAccumulatedInstances(String com
* @return The evaluation context relevant for the provided command id
*/
public EvaluationContext getEvaluationContext(String commandId) {
return getEvaluationContext(getIIF(), commandId, null);
return getEvaluationContext(getIIF(), commandId, null, null);
}

public CommCareInstanceInitializer getIIF() {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/commcare/session/CommCareSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,11 @@ public void clearAllState() {
* @return Evaluation context for current session state
*/
public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif) {
return this.getEvaluationContext(iif, getCommand(), null);
return this.getEvaluationContext(iif, getCommand(), null, null);
}

public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif, String locale) {
return this.getEvaluationContext(iif, getCommand(), null, locale);
}

/**
Expand All @@ -626,7 +630,8 @@ public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif)
*/
public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif,
String command,
Set<String> instancesToInclude) {
Set<String> instancesToInclude,
String locale) {
if (command == null) {
return new EvaluationContext(null);
}
Expand Down Expand Up @@ -661,7 +666,7 @@ public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif,

for (Enumeration en = instancesInScope.keys(); en.hasMoreElements(); ) {
String key = (String)en.nextElement();
instancesInScope.put(key, instancesInScope.get(key).initialize(iif, key));
instancesInScope.put(key, instancesInScope.get(key).initialize(iif, key, locale));
}
addInstancesFromFrame(instancesInScope, iif);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ public class SessionInstanceBuilder {
public static TreeElement getSessionInstance(SessionFrame frame, String deviceId,
String appversion, long drift,
String username, String userId,
Hashtable<String, String> userFields, String windowWidth) {
Hashtable<String, String> userFields, String windowWidth,
String applanguage) {
TreeElement sessionRoot = new TreeElement("session", 0);

addSessionNavData(sessionRoot, frame);
addMetadata(sessionRoot, deviceId, appversion, username, userId, drift, windowWidth);
addMetadata(sessionRoot, deviceId, appversion, username, userId, drift, windowWidth, applanguage);
addUserProperties(sessionRoot, userFields);

return sessionRoot;
Expand Down Expand Up @@ -88,7 +89,8 @@ private static String getCalloutSearchResultCount(StackFrameStep step) {

private static void addMetadata(TreeElement sessionRoot, String deviceId,
String appversion, String username,
String userId, long drift, String windowWidth) {
String userId, long drift, String windowWidth,
String applanguage) {
TreeElement sessionMeta = new TreeElement("context", 0);

addData(sessionMeta, "deviceid", deviceId);
Expand All @@ -97,6 +99,7 @@ private static void addMetadata(TreeElement sessionRoot, String deviceId,
addData(sessionMeta, "userid", userId);
addData(sessionMeta, "drift", String.valueOf(drift));
addData(sessionMeta, "window_width", windowWidth);
addData(sessionMeta, "applanguage", applanguage);

sessionRoot.addChild(sessionMeta);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/javarosa/core/model/FormDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ public void initialize(boolean newInstance, boolean isCompletedInstance, Instanc
for (Enumeration en = formInstances.keys(); en.hasMoreElements(); ) {
String instanceId = (String)en.nextElement();
DataInstance instance = formInstances.get(instanceId);
formInstances.put(instanceId, instance.initialize(factory, instanceId));
formInstances.put(instanceId, instance.initialize(factory, instanceId, null));
}
setEvaluationContext(this.exprEvalContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void setID(int recordid) {
this.recordid = recordid;
}

public abstract DataInstance initialize(InstanceInitializationFactory initializer, String instanceId);
public abstract DataInstance initialize(InstanceInitializationFactory initializer, String instanceId, String locale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you not create a concrete method with the original signature which calls the abstract one with the locale being null? Then you would not have to pass the null everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very true! This did indeed get rid of some unnecessary null usage 🎉


public CacheHost getCacheHost() {
return mCacheHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,19 @@ public void writeExternal(DataOutputStream out) throws IOException {
}

@Override
public DataInstance initialize(InstanceInitializationFactory initializer, String instanceId) {
public DataInstance initialize(InstanceInitializationFactory initializer, String instanceId, String locale) {
base = new InstanceBase(instanceId);
InstanceRoot instanceRoot = initializer.generateRoot(this);
InstanceRoot instanceRoot = initializer.generateRoot(this, locale);
// this indirectly calls `this.copyFromSource` via the InstanceRoot so that we call the
// correct method based on the type
instanceRoot.setupNewCopy(this);
return initializer.getSpecializedExternalDataInstance(this);
}

public DataInstance initialize(InstanceInitializationFactory initializer, String instanceId) {
return initialize(initializer, instanceId, null);
}

public void copyFromSource(InstanceRoot instanceRoot) {
root = instanceRoot.getRoot();
base.setChild(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,17 @@ public static boolean isHomogeneous(TreeElement a, TreeElement b) {
}

@Override
public DataInstance initialize(InstanceInitializationFactory initializer, String instanceId) {
public DataInstance initialize(InstanceInitializationFactory initializer, String instanceId, String locale) {
this.instanceid = instanceId;
root.setInstanceName(instanceId);

return this;
}

public DataInstance initialize(InstanceInitializationFactory initializer, String instanceId) {
return initialize(initializer, instanceId, null);
}

@Override
public String[] getMetaDataFields() {
return new String[]{META_XMLNS, META_ID};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ExternalDataInstance getSpecializedExternalDataInstance(ExternalDataInsta
}

@Nonnull
public InstanceRoot generateRoot(ExternalDataInstance instance) {
public InstanceRoot generateRoot(ExternalDataInstance instance, String locale) {
return ConcreteInstanceRoot.NULL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public void testScreenCreatesVirtualInstance() throws Exception {
// test loading instance with new ref
ExternalDataInstance instance = new ExternalDataInstance("jr://instance/search-input/registry1",
"custom-id");
Assert.assertNotNull(session.getIIF().generateRoot(instance).getRoot());
Assert.assertNotNull(session.getIIF().generateRoot(instance, null).getRoot());

// test that we can still load instances using the legacy ref
ExternalDataInstance legacyInstance = new ExternalDataInstance("jr://instance/search-input",
"search-input:registry1");
Assert.assertNotNull(session.getIIF().generateRoot(legacyInstance).getRoot());
Assert.assertNotNull(session.getIIF().generateRoot(legacyInstance, null).getRoot());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ExternalDataInstance getSpecializedExternalDataInstance(ExternalDataInsta
}

@Override
public InstanceRoot generateRoot(ExternalDataInstance instance) {
public InstanceRoot generateRoot(ExternalDataInstance instance, String locale) {
String ref = instance.getReference();
if (ref.contains(CaseInstanceTreeElement.MODEL_NAME)) {
CaseInstanceTreeElement root = new CaseInstanceTreeElement(instance.getBase(), sandbox.getCaseStorage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ExternalDataInstance getSpecializedExternalDataInstance(ExternalDataInsta
return instance;
}
@Override
public InstanceRoot generateRoot(ExternalDataInstance instance) {
public InstanceRoot generateRoot(ExternalDataInstance instance, String locale) {
throw new RuntimeException("Loading external instances isn't supported " +
"using this instance initialization factory.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ExternalDataInstance getSpecializedExternalDataInstance(ExternalDataInsta
}

@Override
public InstanceRoot generateRoot(ExternalDataInstance instance) {
public InstanceRoot generateRoot(ExternalDataInstance instance, String locale) {
String ref = instance.getReference();

if(instances.containsKey(ref)) {
Expand Down
Loading