Skip to content

Commit 1720a2f

Browse files
committed
Improved: Refactor ‘UtilHttp#makeParamValueFromComposite’
(OFBIZ-11138) Use ‘LocalDateTime’ instead of ‘Calendar’ and ‘HttpServletRequest#getParameterMap’ instead of ‘HttpServletRequest#getParameterNames’. The tests and callers has been adapted to remove the ‘locale’ arguments which made sense only for ‘Calendar’. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863439 13f79535-47bb-0310-9956-ffa450edef68
1 parent 479c16d commit 1720a2f

File tree

4 files changed

+39
-62
lines changed

4 files changed

+39
-62
lines changed

framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.net.URLConnection;
3737
import java.nio.ByteBuffer;
3838
import java.sql.Timestamp;
39+
import java.time.LocalDateTime;
3940
import java.util.ArrayList;
4041
import java.util.Arrays;
4142
import java.util.Collection;
@@ -54,6 +55,7 @@
5455
import java.util.TimeZone;
5556
import java.util.function.Function;
5657
import java.util.function.Predicate;
58+
import java.util.stream.Collectors;
5759

5860
import javax.net.ssl.SSLContext;
5961
import javax.servlet.http.HttpServletRequest;
@@ -77,8 +79,6 @@
7779
import org.apache.ofbiz.webapp.event.FileUploadProgressListener;
7880
import org.apache.ofbiz.widget.renderer.VisualTheme;
7981

80-
import com.ibm.icu.util.Calendar;
81-
8282
/**
8383
* HttpUtil - Misc HTTP Utility Functions
8484
*/
@@ -91,7 +91,6 @@ public final class UtilHttp {
9191
private static final String COMPOSITE_DELIMITER = "_c_";
9292
private static final int MULTI_ROW_DELIMITER_LENGTH = MULTI_ROW_DELIMITER.length();
9393
private static final int ROW_SUBMIT_PREFIX_LENGTH = ROW_SUBMIT_PREFIX.length();
94-
private static final int COMPOSITE_DELIMITER_LENGTH = COMPOSITE_DELIMITER.length();
9594

9695
private static final String SESSION_KEY_TIMEZONE = "timeZone";
9796
private static final String SESSION_KEY_THEME = "visualTheme";
@@ -1371,86 +1370,65 @@ public static String makeCompositeParam(String prefix, String suffix) {
13711370
}
13721371

13731372
/**
1374-
* Given the prefix of a composite parameter, recomposes a single Object from
1375-
* the composite according to compositeType. For example, consider the following
1376-
* form widget field,
1377-
*
1373+
* Assembles a composite object from a set of parameters identified by a common prefix.
1374+
* <p>
1375+
* For example, consider the following form widget field:
13781376
* <pre>
13791377
* {@code
13801378
* <field name="meetingDate">
13811379
* <date-time type="timestamp" input-method="time-dropdown">
13821380
* </field>
13831381
* }
13841382
* </pre>
1383+
* The HTML result is three input boxes to input the date, hour and minutes separately.
1384+
* The parameter names are named {@code meetingDate_c_date}, {@code meetingDate_c_hour},
1385+
* {@code meetingDate_c_minutes}. Additionally, there will be a field named {@code meetingDate_c_compositeType}
1386+
* with a value of "Timestamp". where "_c_" is the {@link #COMPOSITE_DELIMITER}. These parameters will then be
1387+
* re-composed into a Timestamp object from the composite fields.
13851388
*
1386-
* The result in HTML is three input boxes to input the date, hour and minutes separately.
1387-
* The parameter names are named meetingDate_c_date, meetingDate_c_hour, meetingDate_c_minutes.
1388-
* Additionally, there will be a field named meetingDate_c_compositeType with a value of "Timestamp".
1389-
* where _c_ is the COMPOSITE_DELIMITER. These parameters will then be recomposed into a Timestamp
1390-
* object from the composite fields.
1391-
*
1392-
* @param request
1393-
* @param prefix
1394-
* @return Composite object from data or null if not supported or a parsing error occurred.
1389+
* @param request the HTTP request containing the parameters
1390+
* @param prefix the string identifying the set of parameters that must be composed
1391+
* @return a composite object from data or {@code null} if not supported or a parsing error occurred.
13951392
*/
1396-
public static Object makeParamValueFromComposite(HttpServletRequest request, String prefix, Locale locale) {
1393+
public static Object makeParamValueFromComposite(HttpServletRequest request, String prefix) {
13971394
String compositeType = request.getParameter(makeCompositeParam(prefix, "compositeType"));
13981395
if (UtilValidate.isEmpty(compositeType)) {
13991396
return null;
14001397
}
1398+
// Collect the components.
1399+
String prefixDelim = prefix + COMPOSITE_DELIMITER;
1400+
Map<String, String> data = request.getParameterMap().entrySet().stream()
1401+
.filter(e -> e.getKey().startsWith(prefixDelim))
1402+
.collect(Collectors.toMap(
1403+
e -> e.getKey().substring(prefixDelim.length()),
1404+
e -> e.getValue()[0]));
14011405

1402-
// collect the composite fields into a map
1403-
Map<String, String> data = new HashMap<>();
1404-
request.getParameterMap().forEach((name, values) -> {
1405-
if (!name.startsWith(prefix + COMPOSITE_DELIMITER)) {
1406-
return;
1407-
}
1408-
// extract the suffix of the composite name
1409-
String suffix = name.substring(name.indexOf(COMPOSITE_DELIMITER) + COMPOSITE_DELIMITER_LENGTH);
1410-
1411-
// and the value of this parameter
1412-
String value = values[0];
1413-
1414-
// key = suffix, value = parameter data
1415-
data.put(suffix, value);
1416-
});
1417-
if (Debug.verboseOn()) { Debug.logVerbose("Creating composite type with parameter data: " + data.toString(), module); }
1406+
if (Debug.verboseOn()) {
1407+
Debug.logVerbose("Creating composite type with parameter data: " + data.toString(), module);
1408+
}
14181409

1419-
// handle recomposition of data into the compositeType
1410+
// Assemble the composite data from the components
14201411
if ("Timestamp".equals(compositeType)) {
14211412
String date = data.get("date");
14221413
String hour = data.get("hour");
14231414
String minutes = data.get("minutes");
14241415
String ampm = data.get("ampm");
1425-
if (date == null || date.length() < 10) {
1426-
return null;
1427-
}
1428-
if (UtilValidate.isEmpty(hour)) {
1416+
if (date == null || date.length() < 10 || UtilValidate.isEmpty(hour) || UtilValidate.isEmpty(minutes)) {
14291417
return null;
14301418
}
1431-
if (UtilValidate.isEmpty(minutes)) {
1432-
return null;
1433-
}
1434-
boolean isTwelveHour = UtilValidate.isNotEmpty(ampm);
1435-
1436-
// create the timestamp from the data
14371419
try {
14381420
int h = Integer.parseInt(hour);
1439-
Timestamp timestamp = Timestamp.valueOf(date.substring(0, 10) + " 00:00:00.000");
1440-
Calendar cal = Calendar.getInstance(locale);
1441-
cal.setTime(timestamp);
1442-
if (isTwelveHour) {
1443-
boolean isAM = ("AM".equals(ampm) ? true : false);
1421+
Timestamp ts = Timestamp.valueOf(date.substring(0, 10) + " 00:00:00.000");
1422+
if (UtilValidate.isNotEmpty(ampm)) {
1423+
boolean isAM = "AM".equals(ampm);
14441424
if (isAM && h == 12) {
14451425
h = 0;
1446-
}
1447-
if (!isAM && h < 12) {
1426+
} else if (!isAM && h < 12) {
14481427
h += 12;
14491428
}
14501429
}
1451-
cal.set(Calendar.HOUR_OF_DAY, h);
1452-
cal.set(Calendar.MINUTE, Integer.parseInt(minutes));
1453-
return new Timestamp(cal.getTimeInMillis());
1430+
LocalDateTime ldt = ts.toLocalDateTime().withHour(h).withMinute(Integer.parseInt(minutes));
1431+
return Timestamp.valueOf(ldt);
14541432
} catch (IllegalArgumentException e) {
14551433
Debug.logWarning("User input for composite timestamp was invalid: " + e.getMessage(), module);
14561434
return null;

framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.time.Month;
3131
import java.util.Arrays;
3232
import java.util.Collections;
33-
import java.util.Locale;
3433
import java.util.Map;
3534
import java.util.function.Predicate;
3635

@@ -135,7 +134,7 @@ public void basicMakeParamValueFromComposite() {
135134
"meetingDate_c_date", new String[] {"2019-07-14"},
136135
"meetingDate_c_hour", new String[] {"13"},
137136
"meetingDate_c_minutes", new String[] {"8"}));
138-
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
137+
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate"),
139138
equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 13, 8))));
140139
}
141140

@@ -146,7 +145,7 @@ public void emptyTypeMakeParamValueFromComposite() {
146145
"meetingDate_c_date", new String[] {"2019-07-14"},
147146
"meetingDate_c_hour", new String[] {"13"},
148147
"meetingDate_c_minutes", new String[] {"8"}));
149-
assertNull(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT));
148+
assertNull(UtilHttp.makeParamValueFromComposite(req, "meetingDate"));
150149
}
151150

152151
@Test
@@ -158,23 +157,23 @@ public void ampmMakeParamValueFromComposite() {
158157
"meetingDate_c_hour", new String[] {"12"},
159158
"meetingDate_c_minutes", new String[] {"8"},
160159
"meetingDate_c_ampm", new String[] {"AM"}));
161-
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
160+
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate"),
162161
equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 0, 8))));
163162

164163
when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
165164
"meetingDate_c_date", new String[] {"2019-07-14"},
166165
"meetingDate_c_hour", new String[] {"8"},
167166
"meetingDate_c_minutes", new String[] {"8"},
168167
"meetingDate_c_ampm", new String[] {"PM"}));
169-
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
168+
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate"),
170169
equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 20, 8))));
171170

172171
when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
173172
"meetingDate_c_date", new String[] {"2019-07-14"},
174173
"meetingDate_c_hour", new String[] {"18"},
175174
"meetingDate_c_minutes", new String[] {"8"},
176175
"meetingDate_c_ampm", new String[] {"PM"}));
177-
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
176+
assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate"),
178177
equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 18, 8))));
179178
}
180179
}

framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public String invoke(Event event, RequestMap requestMap, HttpServletRequest requ
171171

172172
// make any composite parameter data (e.g., from a set of parameters {name_c_date, name_c_hour, name_c_minutes})
173173
if (value == null) {
174-
value = UtilHttp.makeParamValueFromComposite(request, name, locale);
174+
value = UtilHttp.makeParamValueFromComposite(request, name);
175175
}
176176
}
177177

framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceMultiEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public String invoke(Event event, RequestMap requestMap, HttpServletRequest requ
269269

270270
// make any composite parameter data (e.g., from a set of parameters {name_c_date, name_c_hour, name_c_minutes})
271271
if (value == null) {
272-
value = UtilHttp.makeParamValueFromComposite(request, paramName + curSuffix, locale);
272+
value = UtilHttp.makeParamValueFromComposite(request, paramName + curSuffix);
273273
}
274274

275275
if (value == null) {

0 commit comments

Comments
 (0)