36
36
import java .net .URLConnection ;
37
37
import java .nio .ByteBuffer ;
38
38
import java .sql .Timestamp ;
39
+ import java .time .LocalDateTime ;
39
40
import java .util .ArrayList ;
40
41
import java .util .Arrays ;
41
42
import java .util .Collection ;
54
55
import java .util .TimeZone ;
55
56
import java .util .function .Function ;
56
57
import java .util .function .Predicate ;
58
+ import java .util .stream .Collectors ;
57
59
58
60
import javax .net .ssl .SSLContext ;
59
61
import javax .servlet .http .HttpServletRequest ;
77
79
import org .apache .ofbiz .webapp .event .FileUploadProgressListener ;
78
80
import org .apache .ofbiz .widget .renderer .VisualTheme ;
79
81
80
- import com .ibm .icu .util .Calendar ;
81
-
82
82
/**
83
83
* HttpUtil - Misc HTTP Utility Functions
84
84
*/
@@ -91,7 +91,6 @@ public final class UtilHttp {
91
91
private static final String COMPOSITE_DELIMITER = "_c_" ;
92
92
private static final int MULTI_ROW_DELIMITER_LENGTH = MULTI_ROW_DELIMITER .length ();
93
93
private static final int ROW_SUBMIT_PREFIX_LENGTH = ROW_SUBMIT_PREFIX .length ();
94
- private static final int COMPOSITE_DELIMITER_LENGTH = COMPOSITE_DELIMITER .length ();
95
94
96
95
private static final String SESSION_KEY_TIMEZONE = "timeZone" ;
97
96
private static final String SESSION_KEY_THEME = "visualTheme" ;
@@ -1371,86 +1370,65 @@ public static String makeCompositeParam(String prefix, String suffix) {
1371
1370
}
1372
1371
1373
1372
/**
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:
1378
1376
* <pre>
1379
1377
* {@code
1380
1378
* <field name="meetingDate">
1381
1379
* <date-time type="timestamp" input-method="time-dropdown">
1382
1380
* </field>
1383
1381
* }
1384
1382
* </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.
1385
1388
*
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.
1395
1392
*/
1396
- public static Object makeParamValueFromComposite (HttpServletRequest request , String prefix , Locale locale ) {
1393
+ public static Object makeParamValueFromComposite (HttpServletRequest request , String prefix ) {
1397
1394
String compositeType = request .getParameter (makeCompositeParam (prefix , "compositeType" ));
1398
1395
if (UtilValidate .isEmpty (compositeType )) {
1399
1396
return null ;
1400
1397
}
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 ]));
1401
1405
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
+ }
1418
1409
1419
- // handle recomposition of data into the compositeType
1410
+ // Assemble the composite data from the components
1420
1411
if ("Timestamp" .equals (compositeType )) {
1421
1412
String date = data .get ("date" );
1422
1413
String hour = data .get ("hour" );
1423
1414
String minutes = data .get ("minutes" );
1424
1415
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 )) {
1429
1417
return null ;
1430
1418
}
1431
- if (UtilValidate .isEmpty (minutes )) {
1432
- return null ;
1433
- }
1434
- boolean isTwelveHour = UtilValidate .isNotEmpty (ampm );
1435
-
1436
- // create the timestamp from the data
1437
1419
try {
1438
1420
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 );
1444
1424
if (isAM && h == 12 ) {
1445
1425
h = 0 ;
1446
- }
1447
- if (!isAM && h < 12 ) {
1426
+ } else if (!isAM && h < 12 ) {
1448
1427
h += 12 ;
1449
1428
}
1450
1429
}
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 );
1454
1432
} catch (IllegalArgumentException e ) {
1455
1433
Debug .logWarning ("User input for composite timestamp was invalid: " + e .getMessage (), module );
1456
1434
return null ;
0 commit comments