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

Duplicate of commcare_2.54 #1434

Merged
merged 3 commits into from
Sep 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public static CommCareNetworkService createNoAuthCommCareNetworkService() {
return createCommCareNetworkService(null, false, true, ImmutableMultimap.of());
}

public static CommCareNetworkService createNoAuthCommCareNetworkService(Multimap<String, String> params) {
return createCommCareNetworkService(null, false, true, params);
}

private static boolean isValidRedirect(HttpUrl url, HttpUrl newUrl) {
// unless it's https, don't worry about it
if (!url.scheme().equals("https")) {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/org/commcare/session/RemoteQuerySessionManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.commcare.session;

import static org.commcare.suite.model.QueryPrompt.INPUT_TYPE_DATERANGE;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
Expand All @@ -11,6 +13,7 @@
import org.commcare.suite.model.QueryPrompt;
import org.commcare.suite.model.RemoteQueryDatum;
import org.commcare.suite.model.SessionDatum;
import org.commcare.util.DateRangeUtils;
import org.javarosa.core.model.ItemsetBinding;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.ExternalDataInstance;
Expand All @@ -19,6 +22,7 @@
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.model.instance.utils.TreeUtilities;
import org.javarosa.core.model.utils.ItemSetUtils;
import org.javarosa.core.services.Logger;
import org.javarosa.core.util.OrderedHashtable;
import org.javarosa.model.xform.XPathReference;
import org.javarosa.xml.util.InvalidStructureException;
Expand All @@ -31,6 +35,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -74,8 +79,15 @@ private void initUserAnswers() throws XPathException {
QueryPrompt prompt = queryPrompts.get(promptId);

if (isPromptSupported(prompt) && prompt.getDefaultValueExpr() != null) {
userAnswers.put(prompt.getKey(),
FunctionUtils.toString(prompt.getDefaultValueExpr().eval(evaluationContext)));
String value = FunctionUtils.toString(prompt.getDefaultValueExpr().eval(evaluationContext));
if(INPUT_TYPE_DATERANGE.equals(prompt.getInput())){
try {
value = DateRangeUtils.formatDateRangeAnswer(value);
} catch (ParseException e) {
Logger.exception("Error parsing default date range " + value + " for " + promptId, e);
}
}
userAnswers.put(prompt.getKey(), value);
}
}
}
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/org/commcare/util/DateRangeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.commcare.util;

import org.commcare.modern.util.Pair;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.annotation.Nullable;

public class DateRangeUtils {

// Changing this will require changing this format on ES end as well
public static final String DATE_RANGE_ANSWER_PREFIX = "__range__";
public static final String DATE_RANGE_ANSWER_DELIMITER = "__";
public static final String DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER = " to ";
private static final String DATE_FORMAT = "yyyy-MM-dd";

/**
* @param humanReadableDateRange human readable fomat for date range as 'startDate to endDate'
* @return a Pair of start time and end time that can be supplied to MaterialDatePicker to set a date range,
* @throws ParseException if the given humanReadableDateRange is not in 'yyyy-mm-dd to yyyy-mm-dd' format
*/
@Nullable
public static Pair<Long, Long> parseHumanReadableDate(String humanReadableDateRange) throws ParseException {
if (humanReadableDateRange.contains(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER)) {
String[] humanReadableDateRangeSplit = humanReadableDateRange.split(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER);
if (humanReadableDateRangeSplit.length == 2) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US);
Date startDate = sdf.parse(humanReadableDateRangeSplit[0]);
Date endDate = sdf.parse(humanReadableDateRangeSplit[1]);
return new Pair<>(getTimeFromDateOffsettingTz(startDate), getTimeFromDateOffsettingTz(endDate));
}
}
throw new ParseException("Argument " + humanReadableDateRange + " should be formatted as 'yyyy-mm-dd to yyyy-mm-dd'", 0);
}

private static Long getTimeFromDateOffsettingTz(Date date) {
return date.getTime() - date.getTimezoneOffset() * 60 * 1000;
}

/**
* Formats __range__startDate__endDate as 'startDate to EndDate'
*
* @param dateRangeAnswer A date range value in form of '__range__startDate__endDate'
* @return human readable format 'startDate to EndDate' for given dateRangeAnswer
*/
public static String getHumanReadableDateRange(String dateRangeAnswer) {
if (dateRangeAnswer != null && dateRangeAnswer.startsWith(DATE_RANGE_ANSWER_PREFIX)) {
String[] dateRangeSplit = dateRangeAnswer.split(DATE_RANGE_ANSWER_DELIMITER);
if (dateRangeSplit.length == 4) {
return getHumanReadableDateRange(dateRangeSplit[2], dateRangeSplit[3]);
}
}
return dateRangeAnswer;
}


// Formats as 'startDate to endDate'
public static String getHumanReadableDateRange(String startDate, String endDate) {
return startDate + DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER + endDate;
}

// Formats as '__range__startDate__endDate'
public static String formatDateRangeAnswer(String startDate, String endDate) {
return DATE_RANGE_ANSWER_PREFIX + startDate + DATE_RANGE_ANSWER_DELIMITER + endDate;
}

public static String formatDateRangeAnswer(String humanReadableDateRange) throws ParseException {
Pair<Long, Long> selection = DateRangeUtils.parseHumanReadableDate(humanReadableDateRange);
String startDate = DateRangeUtils.getDateFromTime(selection.first);
String endDate = DateRangeUtils.getDateFromTime(selection.second);
return DATE_RANGE_ANSWER_PREFIX + startDate + DATE_RANGE_ANSWER_DELIMITER + endDate;
}

// Convers given time as yyyy-mm-dd
public static String getDateFromTime(Long time) {
return new SimpleDateFormat(DATE_FORMAT, Locale.US).format(new Date(time));
}
}
4 changes: 3 additions & 1 deletion src/main/java/org/commcare/util/LogTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ public class LogTypes {

public static final String TYPE_CC_UPDATE = "commcare-update";

public static final String TYPE_NETWORK = "commcare-network";

/**
* Logs related to Firebase Cloud Messaging
*/
public static final String TYPE_FCM = "fcm";

public static final String TYPE_NETWORK = "commcare-network";
public static final String TYPE_MEDIA_EVENT = "media-event";

}
18 changes: 18 additions & 0 deletions src/test/java/org/commcare/util/DateRangeUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.commcare.util;

import junit.framework.TestCase;

import org.junit.Test;

import java.text.ParseException;

public class DateRangeUtilsTest extends TestCase {

@Test
public void testDateConversion() throws ParseException {
String dateRange = "2020-02-15 to 2021-03-18";
String formattedDateRange = DateRangeUtils.formatDateRangeAnswer(dateRange);
assertEquals("__range__2020-02-15__2021-03-18", formattedDateRange);
assertEquals(dateRange, DateRangeUtils.getHumanReadableDateRange(formattedDateRange));
}
}
Loading