Skip to content

Commit a615d84

Browse files
authored
Merge pull request #1430 from dimagi/commcare_2.54
Backmerge Commcare 2.54.0
2 parents 68291dd + 554eb21 commit a615d84

File tree

5 files changed

+120
-2
lines changed

5 files changed

+120
-2
lines changed

src/main/java/org/commcare/core/network/CommCareNetworkServiceGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public static CommCareNetworkService createNoAuthCommCareNetworkService() {
140140
return createCommCareNetworkService(null, false, true, ImmutableMultimap.of());
141141
}
142142

143+
public static CommCareNetworkService createNoAuthCommCareNetworkService(Multimap<String, String> params) {
144+
return createCommCareNetworkService(null, false, true, params);
145+
}
146+
143147
private static boolean isValidRedirect(HttpUrl url, HttpUrl newUrl) {
144148
// unless it's https, don't worry about it
145149
if (!url.scheme().equals("https")) {

src/main/java/org/commcare/session/RemoteQuerySessionManager.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.commcare.session;
22

3+
import static org.commcare.suite.model.QueryPrompt.INPUT_TYPE_DATERANGE;
4+
35
import com.google.common.collect.ArrayListMultimap;
46
import com.google.common.collect.ImmutableMap;
57
import com.google.common.collect.Multimap;
@@ -11,6 +13,7 @@
1113
import org.commcare.suite.model.QueryPrompt;
1214
import org.commcare.suite.model.RemoteQueryDatum;
1315
import org.commcare.suite.model.SessionDatum;
16+
import org.commcare.util.DateRangeUtils;
1417
import org.javarosa.core.model.ItemsetBinding;
1518
import org.javarosa.core.model.condition.EvaluationContext;
1619
import org.javarosa.core.model.instance.ExternalDataInstance;
@@ -19,6 +22,7 @@
1922
import org.javarosa.core.model.instance.TreeReference;
2023
import org.javarosa.core.model.instance.utils.TreeUtilities;
2124
import org.javarosa.core.model.utils.ItemSetUtils;
25+
import org.javarosa.core.services.Logger;
2226
import org.javarosa.core.util.OrderedHashtable;
2327
import org.javarosa.model.xform.XPathReference;
2428
import org.javarosa.xml.util.InvalidStructureException;
@@ -31,6 +35,7 @@
3135
import java.io.IOException;
3236
import java.io.InputStream;
3337
import java.net.URL;
38+
import java.text.ParseException;
3439
import java.util.ArrayList;
3540
import java.util.Enumeration;
3641
import java.util.HashMap;
@@ -74,8 +79,15 @@ private void initUserAnswers() throws XPathException {
7479
QueryPrompt prompt = queryPrompts.get(promptId);
7580

7681
if (isPromptSupported(prompt) && prompt.getDefaultValueExpr() != null) {
77-
userAnswers.put(prompt.getKey(),
78-
FunctionUtils.toString(prompt.getDefaultValueExpr().eval(evaluationContext)));
82+
String value = FunctionUtils.toString(prompt.getDefaultValueExpr().eval(evaluationContext));
83+
if(INPUT_TYPE_DATERANGE.equals(prompt.getInput())){
84+
try {
85+
value = DateRangeUtils.formatDateRangeAnswer(value);
86+
} catch (ParseException e) {
87+
Logger.exception("Error parsing default date range " + value + " for " + promptId, e);
88+
}
89+
}
90+
userAnswers.put(prompt.getKey(), value);
7991
}
8092
}
8193
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.commcare.util;
2+
3+
import org.commcare.modern.util.Pair;
4+
5+
import java.text.ParseException;
6+
import java.text.SimpleDateFormat;
7+
import java.util.Date;
8+
import java.util.Locale;
9+
10+
import javax.annotation.Nullable;
11+
12+
public class DateRangeUtils {
13+
14+
// Changing this will require changing this format on ES end as well
15+
public static final String DATE_RANGE_ANSWER_PREFIX = "__range__";
16+
public static final String DATE_RANGE_ANSWER_DELIMITER = "__";
17+
public static final String DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER = " to ";
18+
private static final String DATE_FORMAT = "yyyy-MM-dd";
19+
20+
/**
21+
* @param humanReadableDateRange human readable fomat for date range as 'startDate to endDate'
22+
* @return a Pair of start time and end time that can be supplied to MaterialDatePicker to set a date range,
23+
* @throws ParseException if the given humanReadableDateRange is not in 'yyyy-mm-dd to yyyy-mm-dd' format
24+
*/
25+
@Nullable
26+
public static Pair<Long, Long> parseHumanReadableDate(String humanReadableDateRange) throws ParseException {
27+
if (humanReadableDateRange.contains(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER)) {
28+
String[] humanReadableDateRangeSplit = humanReadableDateRange.split(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER);
29+
if (humanReadableDateRangeSplit.length == 2) {
30+
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US);
31+
Date startDate = sdf.parse(humanReadableDateRangeSplit[0]);
32+
Date endDate = sdf.parse(humanReadableDateRangeSplit[1]);
33+
return new Pair<>(getTimeFromDateOffsettingTz(startDate), getTimeFromDateOffsettingTz(endDate));
34+
}
35+
}
36+
throw new ParseException("Argument " + humanReadableDateRange + " should be formatted as 'yyyy-mm-dd to yyyy-mm-dd'", 0);
37+
}
38+
39+
private static Long getTimeFromDateOffsettingTz(Date date) {
40+
return date.getTime() - date.getTimezoneOffset() * 60 * 1000;
41+
}
42+
43+
/**
44+
* Formats __range__startDate__endDate as 'startDate to EndDate'
45+
*
46+
* @param dateRangeAnswer A date range value in form of '__range__startDate__endDate'
47+
* @return human readable format 'startDate to EndDate' for given dateRangeAnswer
48+
*/
49+
public static String getHumanReadableDateRange(String dateRangeAnswer) {
50+
if (dateRangeAnswer != null && dateRangeAnswer.startsWith(DATE_RANGE_ANSWER_PREFIX)) {
51+
String[] dateRangeSplit = dateRangeAnswer.split(DATE_RANGE_ANSWER_DELIMITER);
52+
if (dateRangeSplit.length == 4) {
53+
return getHumanReadableDateRange(dateRangeSplit[2], dateRangeSplit[3]);
54+
}
55+
}
56+
return dateRangeAnswer;
57+
}
58+
59+
60+
// Formats as 'startDate to endDate'
61+
public static String getHumanReadableDateRange(String startDate, String endDate) {
62+
return startDate + DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER + endDate;
63+
}
64+
65+
// Formats as '__range__startDate__endDate'
66+
public static String formatDateRangeAnswer(String startDate, String endDate) {
67+
return DATE_RANGE_ANSWER_PREFIX + startDate + DATE_RANGE_ANSWER_DELIMITER + endDate;
68+
}
69+
70+
public static String formatDateRangeAnswer(String humanReadableDateRange) throws ParseException {
71+
Pair<Long, Long> selection = DateRangeUtils.parseHumanReadableDate(humanReadableDateRange);
72+
String startDate = DateRangeUtils.getDateFromTime(selection.first);
73+
String endDate = DateRangeUtils.getDateFromTime(selection.second);
74+
return DATE_RANGE_ANSWER_PREFIX + startDate + DATE_RANGE_ANSWER_DELIMITER + endDate;
75+
}
76+
77+
// Convers given time as yyyy-mm-dd
78+
public static String getDateFromTime(Long time) {
79+
return new SimpleDateFormat(DATE_FORMAT, Locale.US).format(new Date(time));
80+
}
81+
}

src/main/java/org/commcare/util/LogTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,7 @@ public class LogTypes {
113113
* Logs related to Firebase Cloud Messaging
114114
*/
115115
public static final String TYPE_FCM = "fcm";
116+
117+
public static final String TYPE_MEDIA_EVENT = "media-event";
118+
116119
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.commcare.util;
2+
3+
import junit.framework.TestCase;
4+
5+
import org.junit.Test;
6+
7+
import java.text.ParseException;
8+
9+
public class DateRangeUtilsTest extends TestCase {
10+
11+
@Test
12+
public void testDateConversion() throws ParseException {
13+
String dateRange = "2020-02-15 to 2021-03-18";
14+
String formattedDateRange = DateRangeUtils.formatDateRangeAnswer(dateRange);
15+
assertEquals("__range__2020-02-15__2021-03-18", formattedDateRange);
16+
assertEquals(dateRange, DateRangeUtils.getHumanReadableDateRange(formattedDateRange));
17+
}
18+
}

0 commit comments

Comments
 (0)