-
Notifications
You must be signed in to change notification settings - Fork 32
/
DocumentFieldValueLoaders.java
617 lines (541 loc) · 18.6 KB
/
DocumentFieldValueLoaders.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package de.metas.ui.web.window.descriptor.sql;
import java.awt.Color;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import org.compiere.util.DisplayType;
import org.compiere.util.SecureEngine;
import org.compiere.util.TimeUtil;
import org.slf4j.Logger;
import de.metas.i18n.ITranslatableString;
import de.metas.i18n.TranslatableStrings;
import de.metas.logging.LogManager;
import de.metas.ui.web.window.datatypes.ColorValue;
import de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue;
import de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue;
import de.metas.ui.web.window.datatypes.LookupValuesList;
import de.metas.ui.web.window.datatypes.Password;
import de.metas.ui.web.window.descriptor.LookupDescriptor;
import de.metas.ui.web.window.model.lookup.LabelsLookup;
import de.metas.util.Check;
import de.metas.util.IColorRepository;
import de.metas.util.MFColor;
import de.metas.util.NumberUtils;
import de.metas.util.Services;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
/**
* Factory methods to create specific {@link DocumentFieldValueLoader} instances.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
public final class DocumentFieldValueLoaders
{
public static DocumentFieldValueLoader toString(final String sqlColumnName, final boolean encrypted)
{
if (encrypted)
{
return new EncryptedStringDocumentFieldValueLoader(sqlColumnName);
}
else
{
return new StringDocumentFieldValueLoader(sqlColumnName);
}
}
public static DocumentFieldValueLoader toPassword(final String sqlColumnName, final boolean encrypted)
{
if (encrypted)
{
return new EncryptedPasswordDocumentFieldValueLoader(sqlColumnName);
}
else
{
return new PasswordDocumentFieldValueLoader(sqlColumnName);
}
}
public static DocumentFieldValueLoader toByteArray(final String sqlColumnName, final boolean encrypted)
{
return new ByteArrayDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toBoolean(final String sqlColumnName, final boolean encrypted)
{
return new BooleanDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toJULDate(final String sqlColumnName, final boolean encrypted)
{
return new JULDateDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toZonedDateTime(final String sqlColumnName, final boolean encrypted)
{
return new ZonedDateTimeDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toInstant(final String sqlColumnName, final boolean encrypted)
{
return new InstantDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toLocalDate(final String sqlColumnName, final boolean encrypted)
{
return new LocalDateDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toLocalTime(final String sqlColumnName, final boolean encrypted)
{
return new LocalTimeDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toBigDecimal(final String sqlColumnName, final boolean encrypted, final Integer precision)
{
if (precision != null)
{
return new BigDecimalWithPrecisionDocumentFieldValueLoader(sqlColumnName, encrypted, precision);
}
else
{
return new BigDecimalDocumentFieldValueLoader(sqlColumnName, encrypted);
}
}
public static DocumentFieldValueLoader toInteger(final String sqlColumnName, final boolean encrypted)
{
return new IntegerDocumentFieldValueLoader(sqlColumnName, encrypted);
}
public static DocumentFieldValueLoader toLookupValue(
@NonNull final String sqlColumnName,
@NonNull final String sqlDisplayColumnName,
final boolean numericKey)
{
if (Check.isEmpty(sqlDisplayColumnName, true))
{
throw new IllegalArgumentException("sqlDisplayColumnName shall not be null or empty");
}
if (numericKey)
{
return new IntegerLookupValueDocumentFieldValueLoader(sqlColumnName, sqlDisplayColumnName/* , sqlDescriptionColumnName */);
}
else
{
return new StringLookupValueDocumentFieldValueLoader(sqlColumnName, sqlDisplayColumnName/* , sqlDescriptionColumnName */);
}
}
public static DocumentFieldValueLoader toLabelValues(final String sqlColumnName)
{
return new LabelsLookupValueDocumentFieldValueLoader(sqlColumnName);
}
public static DocumentFieldValueLoader toColor(final String sqlColumnName)
{
return new ColorDocumentFieldValueLoader(sqlColumnName);
}
//
//
//
//
//
private static final transient Logger logger = LogManager.getLogger(DocumentFieldValueLoaders.class);
private DocumentFieldValueLoaders()
{
}
private static Object decrypt(final Object value)
{
if (value == null)
{
return null;
}
return SecureEngine.decrypt(value);
}
@Value
private static final class StringDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final String value = rs.getString(sqlColumnName);
return value;
}
}
@Value
private static final class EncryptedStringDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final String value = rs.getString(sqlColumnName);
return decrypt(value);
}
}
@Value
private static final class PasswordDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
@Override
public Password retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final String value = rs.getString(sqlColumnName);
return Password.ofNullableString(value);
}
}
@Value
private static final class EncryptedPasswordDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
@Override
public Password retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final String value = rs.getString(sqlColumnName);
final Object valueDecrypted = decrypt(value);
if (valueDecrypted == null)
{
return null;
}
return Password.ofNullableString(valueDecrypted.toString());
}
}
@Value
private static final class ByteArrayDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public byte[] retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final Object valueObj = rs.getObject(sqlColumnName);
final byte[] valueBytes;
if (rs.wasNull())
{
valueBytes = null;
}
else if (valueObj instanceof Clob)
{
final Clob lob = (Clob)valueObj;
final long length = lob.length();
valueBytes = lob.getSubString(1, (int)length).getBytes(StandardCharsets.UTF_8);
}
else if (valueObj instanceof Blob)
{
final Blob lob = (Blob)valueObj;
final long length = lob.length();
valueBytes = lob.getBytes(1, (int)length);
}
else if (valueObj instanceof String)
{
valueBytes = ((String)valueObj).getBytes(StandardCharsets.UTF_8);
}
else if (valueObj instanceof byte[])
{
valueBytes = (byte[])valueObj;
}
else
{
logger.warn("Unknown LOB value '{}' for {}. Considering it null.", valueObj, sqlColumnName);
valueBytes = null;
}
//
return valueBytes;
}
}
@Value
private static final class BooleanDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Boolean retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
String valueStr = rs.getString(sqlColumnName);
if (encrypted)
{
valueStr = valueStr == null ? null : decrypt(valueStr).toString();
}
return DisplayType.toBoolean(valueStr);
}
}
@Value
private static final class JULDateDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final Timestamp valueTS = rs.getTimestamp(sqlColumnName);
final java.util.Date value = valueTS == null ? null : new java.util.Date(valueTS.getTime());
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class ZonedDateTimeDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final ZonedDateTime value = TimeUtil.asZonedDateTime(rs.getTimestamp(sqlColumnName));
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class InstantDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final Instant value = TimeUtil.asInstant(rs.getTimestamp(sqlColumnName));
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class LocalDateDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final LocalDate value = TimeUtil.asLocalDate(rs.getTimestamp(sqlColumnName));
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class LocalTimeDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final LocalTime value = TimeUtil.asLocalTime(rs.getTimestamp(sqlColumnName));
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class BigDecimalDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final BigDecimal value = rs.getBigDecimal(sqlColumnName);
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class BigDecimalWithPrecisionDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
private final int precision;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
BigDecimal value = rs.getBigDecimal(sqlColumnName);
value = value == null ? null : NumberUtils.setMinimumScale(value, precision);
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class IntegerDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final boolean encrypted;
@Override
public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final int valueInt = rs.getInt(sqlColumnName);
final Integer value = rs.wasNull() ? null : valueInt;
return encrypted ? decrypt(value) : value;
}
}
@Value
private static final class IntegerLookupValueDocumentFieldValueLoader implements DocumentFieldValueLoader
{
@NonNull
private final String sqlColumnName;
@NonNull
private final String sqlDisplayColumnName;
@Override
public IntegerLookupValue retrieveFieldValue(
final ResultSet rs,
final boolean isDisplayColumnAvailable,
final String adLanguage,
final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final int id = rs.getInt(sqlColumnName);
if (rs.wasNull())
{
return null;
}
if (isDisplayColumnAvailable)
{
final DisplayNameAndDescription result = DocumentFieldValueLoaders.extractDisplayNameAndDescription(
rs,
sqlDisplayColumnName,
adLanguage);
return IntegerLookupValue.builder()
.id(id)
.displayName(result.getDisplayName())
.description(result.getDescription())
.build();
}
else
{
return IntegerLookupValue.unknown(id);
}
}
}
@Value
private static final class StringLookupValueDocumentFieldValueLoader implements DocumentFieldValueLoader
{
@NonNull
private final String sqlColumnName;
@NonNull
private final String sqlDisplayColumnName;
@Override
public StringLookupValue retrieveFieldValue(
@NonNull final ResultSet rs,
final boolean isDisplayColumnAvailable,
final String adLanguage,
final LookupDescriptor lookupDescriptor_NOTUSED) throws SQLException
{
final String key = rs.getString(sqlColumnName);
if (rs.wasNull())
{
return null;
}
if (isDisplayColumnAvailable)
{
final DisplayNameAndDescription result = DocumentFieldValueLoaders.extractDisplayNameAndDescription(
rs,
sqlDisplayColumnName,
adLanguage);
return StringLookupValue.builder()
.id(key)
.displayName(result.getDisplayName())
.description(result.getDescription())
.build();
}
else
{
return StringLookupValue.unknown(key);
}
}
}
private static DisplayNameAndDescription extractDisplayNameAndDescription(
@NonNull final ResultSet rs,
final String sqlDisplayColumnName,
final String adLanguage) throws SQLException
{
final ITranslatableString displayName;
final ITranslatableString description;
final Array array = rs.getArray(sqlDisplayColumnName);
if (array == null)
{
displayName = TranslatableStrings.empty();
description = TranslatableStrings.empty();
}
else
{
final String[] nameAndDescription = (String[])array.getArray();
displayName = TranslatableStrings.singleLanguage(adLanguage, nameAndDescription[0]);
final boolean hasDescription = nameAndDescription.length > 1;
if (hasDescription)
{
description = TranslatableStrings.singleLanguage(adLanguage, nameAndDescription[1]);
}
else
{
description = TranslatableStrings.empty();
}
}
return new DisplayNameAndDescription(displayName, description);
}
@Value
private static class DisplayNameAndDescription
{
@NonNull
ITranslatableString displayName;
@NonNull
ITranslatableString description;
}
@Value
private static class LabelsLookupValueDocumentFieldValueLoader implements DocumentFieldValueLoader
{
private final String sqlColumnName;
private final String sqlDisplayColumnName = null;
@Override
public LookupValuesList retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage, final LookupDescriptor lookupDescriptor) throws SQLException
{
// FIXME: atm we are avoiding NPE by returning EMPTY.
// We need to make sure we always get a lookup...
// Also, please note this method is called with null lookup when loading view row, even though the labels field is not part of the grid.
// This might be a performance penalty too, so:
// * make sure never load labels in grid mode
// * or, load labels for the whole page, all together (avoiding SQL N+1 issue)
if (lookupDescriptor == null)
{
return LookupValuesList.EMPTY;
}
final LabelsLookup lookup = LabelsLookup.cast(lookupDescriptor);
final String linkColumnName = lookup.getLinkColumnName();
final int linkId = rs.getInt(linkColumnName);
return lookup.retrieveExistingValues(linkId);
}
}
@Builder
@Value
private static class ColorDocumentFieldValueLoader implements DocumentFieldValueLoader
{
@NonNull
private final String sqlColumnName;
@Override
public Object retrieveFieldValue(ResultSet rs, boolean isDisplayColumnAvailable, String adLanguage, LookupDescriptor lookupDescriptor) throws SQLException
{
final int adColorId = rs.getInt(sqlColumnName);
if (adColorId <= 0)
{
return null;
}
final IColorRepository colorRepository = Services.get(IColorRepository.class);
final MFColor color = colorRepository.getColorById(adColorId);
if (color == null)
{
return null;
}
final Color awtColor = color.toFlatColor().getFlatColor();
return ColorValue.ofRGB(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue());
}
}
}