/
SqlLookupDescriptor.java
795 lines (687 loc) · 25.6 KB
/
SqlLookupDescriptor.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
package de.metas.ui.web.window.descriptor.sql;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.concurrent.Immutable;
import org.adempiere.ad.element.api.AdWindowId;
import org.adempiere.ad.expression.api.ICachedStringExpression;
import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.expression.api.TranslatableParameterizedStringExpression;
import org.adempiere.ad.expression.api.impl.CompositeStringExpression;
import org.adempiere.ad.expression.api.impl.ConstantStringExpression;
import org.adempiere.ad.validationRule.INamePairPredicate;
import org.adempiere.ad.validationRule.IValidationRule;
import org.adempiere.ad.validationRule.impl.CompositeValidationRule;
import org.adempiere.ad.validationRule.impl.NullValidationRule;
import org.adempiere.db.DBConstants;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.model.InterfaceWrapperHelper;
import org.compiere.model.I_AD_Client;
import org.compiere.model.I_AD_Org;
import org.compiere.model.I_M_AttributeSetInstance;
import org.compiere.model.MLookupFactory;
import org.compiere.model.MLookupInfo;
import org.compiere.util.DisplayType;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import de.metas.i18n.TranslatableParameterizedString;
import de.metas.security.IUserRolePermissions;
import de.metas.security.impl.AccessSqlStringExpression;
import de.metas.security.permissions.Access;
import de.metas.ui.web.window.WindowConstants;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.descriptor.DocumentFieldWidgetType;
import de.metas.ui.web.window.descriptor.DocumentLayoutElementFieldDescriptor.LookupSource;
import de.metas.ui.web.window.descriptor.LookupDescriptor;
import de.metas.ui.web.window.descriptor.LookupDescriptorProvider;
import de.metas.ui.web.window.descriptor.LookupDescriptorProvider.LookupScope;
import de.metas.ui.web.window.descriptor.LookupDescriptorProviders;
import de.metas.ui.web.window.descriptor.factory.standard.DescriptorsFactoryHelper;
import de.metas.ui.web.window.model.lookup.GenericSqlLookupDataSourceFetcher;
import de.metas.ui.web.window.model.lookup.LookupDataSourceContext;
import de.metas.ui.web.window.model.lookup.LookupDataSourceFetcher;
import de.metas.ui.web.window.model.sql.DocActionValidationRule;
import de.metas.util.Check;
import lombok.ToString;
/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2016 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%
*/
@Immutable
public final class SqlLookupDescriptor implements ISqlLookupDescriptor
{
public static Builder builder()
{
return new Builder();
}
public static SqlLookupDescriptor cast(final LookupDescriptor descriptor)
{
return (SqlLookupDescriptor)descriptor;
}
public static LookupDescriptorProvider searchInTable(final String lookupTableName)
{
return builder()
.setCtxTableName(null) // tableName
.setCtxColumnName(InterfaceWrapperHelper.getKeyColumnName(lookupTableName))
.setDisplayType(DisplayType.Search)
.setReadOnlyAccess()
.buildProvider();
}
public static LookupDescriptorProvider productAttributes()
{
return builder()
.setCtxTableName(null) // tableName
.setCtxColumnName(I_M_AttributeSetInstance.COLUMNNAME_M_AttributeSetInstance_ID)
.setDisplayType(DisplayType.PAttribute)
.setReadOnlyAccess()
.buildProvider();
}
public static LookupDescriptorProvider listByAD_Reference_Value_ID(final int AD_Reference_Value_ID)
{
Check.assumeGreaterThanZero(AD_Reference_Value_ID, "AD_Reference_Value_ID");
return builder()
.setCtxTableName(null) // tableName
.setCtxColumnName(null)
.setDisplayType(DisplayType.List)
.setAD_Reference_Value_ID(AD_Reference_Value_ID)
.setReadOnlyAccess()
.buildProvider();
}
/**
* @param AD_Reference_Value_ID has to be > 0
* @param AD_Val_Rule_ID may be <= 0
*/
public static LookupDescriptorProvider searchByAD_Val_Rule_ID(
final int AD_Reference_Value_ID,
final int AD_Val_Rule_ID)
{
Check.assumeGreaterThanZero(AD_Reference_Value_ID, "AD_Reference_Value_ID");
return builder()
.setCtxTableName(null) // tableName
.setCtxColumnName(null)
.setAD_Reference_Value_ID(AD_Reference_Value_ID)
.setAD_Val_Rule_ID(AD_Val_Rule_ID)
.setDisplayType(DisplayType.Search)
.setReadOnlyAccess()
.buildProvider();
}
private static final int WINDOWNO_Dummy = 99999;
private final Optional<String> tableName;
private final Optional<WindowId> zoomIntoWindowId;
private final SqlForFetchingLookups sqlForFetchingExpression;
private final SqlForFetchingLookupById sqlForFetchingLookupByIdExpression;
private final int entityTypeIndex;
private final INamePairPredicate postQueryPredicate;
private final boolean highVolume;
private final boolean numericKey;
private final LookupSource lookupSourceType;
private final ImmutableSet<String> dependsOnFieldNames;
private final ImmutableSet<String> dependsOnTableNames;
private final GenericSqlLookupDataSourceFetcher lookupDataSourceFetcher;
private SqlLookupDescriptor(final Builder builder)
{
tableName = Optional.of(builder.sqlTableName);
zoomIntoWindowId = builder.getZoomIntoWindowId();
sqlForFetchingExpression = builder.sqlForFetchingExpression;
sqlForFetchingLookupByIdExpression = builder.sqlForFetchingLookupByIdExpression;
entityTypeIndex = builder.entityTypeIndex;
postQueryPredicate = builder.getPostQueryPredicate();
numericKey = builder.numericKey;
highVolume = builder.isHighVolume();
lookupSourceType = builder.getLookupSourceType();
dependsOnFieldNames = ImmutableSet.copyOf(builder.dependsOnFieldNames);
dependsOnTableNames = ImmutableSet.copyOf(builder.getDependsOnTableNames());
lookupDataSourceFetcher = GenericSqlLookupDataSourceFetcher.of(this); // keep it last!
}
@Override
public String toString()
{
return MoreObjects.toStringHelper(this)
.omitNullValues()
.add("tableName", tableName)
.add("zoomIntoWindowId", zoomIntoWindowId.orElse(null))
.add("highVolume", highVolume ? highVolume : null)
.add("sqlForFetching", sqlForFetchingExpression.toOneLineString())
.add("postQueryPredicate", postQueryPredicate == null || postQueryPredicate == INamePairPredicate.NULL ? null : postQueryPredicate)
.toString();
}
@Override
public int hashCode()
{
return Objects.hash(
tableName,
zoomIntoWindowId,
sqlForFetchingExpression,
sqlForFetchingLookupByIdExpression,
entityTypeIndex,
postQueryPredicate,
//
highVolume,
numericKey
// dependsOnFieldNames // not needed because it's computed
// lookupSourceType // not needed because it's computed
// lookupDataSourceFetcher // not needed because it's computed
);
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (!getClass().equals(obj.getClass()))
{
return false;
}
final SqlLookupDescriptor other = (SqlLookupDescriptor)obj;
return Objects.equals(tableName, other.tableName)
&& Objects.equals(zoomIntoWindowId, other.zoomIntoWindowId)
&& Objects.equals(sqlForFetchingExpression, other.sqlForFetchingExpression)
&& Objects.equals(sqlForFetchingLookupByIdExpression, other.sqlForFetchingLookupByIdExpression)
&& entityTypeIndex == other.entityTypeIndex
&& Objects.equals(postQueryPredicate, other.postQueryPredicate)
&& highVolume == other.highVolume
&& numericKey == other.numericKey
// dependsOnFieldNames // not needed because it's computed
// lookupSourceType // not needed because it's computed
// lookupDataSourceFetcher // not needed because it's computed
;
}
@Override
public LookupDataSourceFetcher getLookupDataSourceFetcher()
{
return lookupDataSourceFetcher;
}
@Override
public boolean isNumericKey()
{
return numericKey;
}
@Override
public Optional<String> getTableName()
{
return tableName;
}
@Override
public Optional<WindowId> getZoomIntoWindowId()
{
return zoomIntoWindowId;
}
public SqlForFetchingLookups getSqlForFetchingExpression()
{
return sqlForFetchingExpression;
}
@Override
public SqlForFetchingLookupById getSqlForFetchingLookupByIdExpression()
{
return sqlForFetchingLookupByIdExpression;
}
public int getEntityTypeIndex()
{
return entityTypeIndex;
}
@Override
public Set<String> getDependsOnFieldNames()
{
return dependsOnFieldNames;
}
@Override
public Set<String> getDependsOnTableNames()
{
return dependsOnTableNames;
}
public INamePairPredicate getPostQueryPredicate()
{
return postQueryPredicate;
}
@Override
public boolean hasParameters()
{
return !getSqlForFetchingExpression().getParameters().isEmpty()
|| !getPostQueryPredicate().getParameters().isEmpty();
}
@Override
public boolean isHighVolume()
{
return highVolume;
}
@Override
public LookupSource getLookupSourceType()
{
return lookupSourceType;
}
@ToString(of = { "sqlTableName", "ctxTableName", "ctxColumnName", "widgetType" })
public static final class Builder
{
// Parameters
private String ctxColumnName;
private String ctxTableName;
private DocumentFieldWidgetType widgetType;
private Integer displayType;
private int AD_Reference_Value_ID = -1;
private int AD_Val_Rule_ID = -1;
private LookupScope scope = LookupScope.DocumentField;
private Access requiredAccess = null;
//
// Built/prepared values
private boolean numericKey;
private Set<String> dependsOnFieldNames;
private final List<IValidationRule> validationRules = new ArrayList<>();
private IValidationRule validationRuleEffective = NullValidationRule.instance;
private String sqlTableName;
private SqlForFetchingLookups sqlForFetchingExpression;
private SqlForFetchingLookupById sqlForFetchingLookupByIdExpression;
private int entityTypeIndex = -1;
private AdWindowId zoomIntoAdWindowId = null;
private Builder()
{
}
public LookupDescriptorProvider buildProvider()
{
Check.assumeNotNull(displayType, "Parameter displayType is not null");
return buildProvider(ctxTableName, ctxColumnName, widgetType, displayType, AD_Reference_Value_ID, AD_Val_Rule_ID, validationRules);
}
public LookupDescriptor buildForDefaultScope()
{
return buildProvider()
.provide()
.orElseThrow(() -> new AdempiereException("No lookup for " + this));
}
private static LookupDescriptorProvider buildProvider(
final String sqlTableName,
final String sqlColumnName,
final DocumentFieldWidgetType widgetType, final int displayType,
final int AD_Reference_Value_ID,
final int AD_Val_Rule_ID,
final List<IValidationRule> additionalValidationRules)
{
if (widgetType == DocumentFieldWidgetType.ProcessButton)
{
return LookupDescriptorProviders.NULL;
}
else if (DisplayType.isAnyLookup(displayType)
|| DisplayType.Button == displayType && AD_Reference_Value_ID > 0)
{
return LookupDescriptorProviders.fromMemoizingFunction(scope -> SqlLookupDescriptor.builder()
.setCtxTableName(sqlTableName)
.setCtxColumnName(sqlColumnName)
.setDisplayType(displayType)
.setAD_Reference_Value_ID(AD_Reference_Value_ID)
.setAD_Val_Rule_ID(AD_Val_Rule_ID)
.setScope(scope)
.addValidationRules(additionalValidationRules)
.build());
}
else
{
return LookupDescriptorProviders.NULL;
}
}
private SqlLookupDescriptor build()
{
final boolean IsParent = false;
if (displayType == DisplayType.PAttribute && AD_Reference_Value_ID <= 0)
{
numericKey = true;
validationRuleEffective = NullValidationRule.instance;
dependsOnFieldNames = ImmutableSet.of();
setSqlExpressions_PAttribute();
}
else
{
final MLookupInfo lookupInfo = MLookupFactory.getLookupInfo(
WINDOWNO_Dummy,
displayType,
ctxTableName,
ctxColumnName,
AD_Reference_Value_ID,
IsParent,
AD_Val_Rule_ID);
numericKey = lookupInfo.isNumericKey();
validationRuleEffective = extractValidationRule(lookupInfo);
dependsOnFieldNames = ImmutableSet.<String>builder()
.addAll(validationRuleEffective.getPrefilterWhereClause().getParameterNames())
.addAll(validationRuleEffective.getPostQueryFilter().getParameters())
.build();
setSqlExpressions(lookupInfo);
}
return new SqlLookupDescriptor(this);
}
private IValidationRule extractValidationRule(final MLookupInfo lookupInfo)
{
final CompositeValidationRule.Builder validationRuleBuilder = CompositeValidationRule.builder();
validationRuleBuilder.add(lookupInfo.getValidationRule());
//
// Case: DocAction button => inject the DocActionValidationRule
// FIXME: hardcoded
if (displayType == DisplayType.Button && WindowConstants.FIELDNAME_DocAction.equals(ctxColumnName))
{
validationRuleBuilder.add(DocActionValidationRule.instance);
}
// Additional validation rules registered
validationRules.forEach(validationRuleBuilder::add);
return validationRuleBuilder.build();
}
private void setSqlExpressions(final MLookupInfo lookupInfo)
{
//
// WHERE
final IStringExpression sqlWhereFinal = buildSqlWhere(lookupInfo, scope, validationRuleEffective);
//
// ORDER BY
String lookup_SqlOrderBy = lookupInfo.getOrderBySqlPart();
if (Check.isEmpty(lookup_SqlOrderBy, true))
{
lookup_SqlOrderBy = String.valueOf(MLookupFactory.COLUMNINDEX_DisplayName);
}
//
// Set the SQLs
{
sqlTableName = lookupInfo.getTableName();
zoomIntoAdWindowId = lookupInfo.getZoomAD_Window_ID_Override();
sqlForFetchingExpression = buildSqlForFetching(lookupInfo, sqlWhereFinal, lookup_SqlOrderBy);
sqlForFetchingLookupByIdExpression = buildSqlForFetchingById(lookupInfo);
if (lookupInfo.isQueryHasEntityType())
{
entityTypeIndex = MLookupFactory.COLUMNINDEX_EntityType;
}
}
}
private void setSqlExpressions_PAttribute()
{
final String tableName = I_M_AttributeSetInstance.Table_Name;
final String keyColumnNameFQ = tableName + "." + I_M_AttributeSetInstance.COLUMNNAME_M_AttributeSetInstance_ID;
final String displayColumnSql = tableName + "." + I_M_AttributeSetInstance.COLUMNNAME_Description;
final CompositeStringExpression.Builder sqlSelectFrom = IStringExpression.composer()
.append("SELECT ")
.append(" ").append(keyColumnNameFQ) // Key
.append(", NULL") // Value
.append(",").append(displayColumnSql) // DisplayName
.append(", M_AttributeSetInstance.IsActive") // IsActive
.append(", NULL") // EntityType
.append(" FROM ").append(tableName);
//
// WHERE
final CompositeStringExpression.Builder sqlWhereFinal = IStringExpression.composer();
{
// Validation Rule's WHERE
final IStringExpression validationRuleWhereClause = buildSqlWhereClauseFromValidationRule(validationRuleEffective, scope);
if (!validationRuleWhereClause.isNullExpression())
{
sqlWhereFinal.appendIfNotEmpty("\n AND ");
sqlWhereFinal.append(" /* validation rule */ ").append("(").append(validationRuleWhereClause).append(")");
}
// Filter's WHERE
sqlWhereFinal.appendIfNotEmpty("\n AND ");
sqlWhereFinal.append(" /* filter */ ").append("(").append(displayColumnSql).append(") ILIKE ").append(LookupDataSourceContext.PARAM_FilterSql);
}
//
// ORDER BY
String lookup_SqlOrderBy = I_M_AttributeSetInstance.COLUMNNAME_Description;
if (Check.isEmpty(lookup_SqlOrderBy, true))
{
lookup_SqlOrderBy = String.valueOf(MLookupFactory.COLUMNINDEX_DisplayName);
}
//
// Assemble the SQLs
final SqlForFetchingLookups sqlForFetching = SqlForFetchingLookups.builder()
.sql(IStringExpression.composer()
.append(sqlSelectFrom) // SELECT ... FROM ...
.append("\n WHERE \n").append(sqlWhereFinal) // WHERE
.append("\n ORDER BY ").append(lookup_SqlOrderBy) // ORDER BY
.append("\n OFFSET ").append(SqlForFetchingLookups.PARAM_Offset.toStringWithMarkers())
.append("\n LIMIT ").append(SqlForFetchingLookups.PARAM_Limit.toStringWithMarkers()) // LIMIT
.wrap(AccessSqlStringExpression.wrapper(tableName, IUserRolePermissions.SQL_FULLYQUALIFIED, getRequiredAccess(tableName))) // security
.build()
.caching())
.build();
final SqlForFetchingLookupById sqlForFetchingLookupById = SqlForFetchingLookupById.builder()
.sql(IStringExpression.composer()
.append("SELECT ").append("ARRAY[").append(displayColumnSql).append(", NULL]")
.append("\n FROM ").append(tableName) // FROM
.append("\n WHERE ").append(keyColumnNameFQ).append("=").append(SqlForFetchingLookupById.SQL_PARAM_KeyId)
.build()
.caching())
.build();
//
// Set the SQLs
{
sqlTableName = tableName;
sqlForFetchingExpression = sqlForFetching;
sqlForFetchingLookupByIdExpression = sqlForFetchingLookupById;
}
}
private static IStringExpression buildSqlWhere(final MLookupInfo lookupInfo, final LookupScope scope, final IValidationRule validationRuleEffective)
{
final String tableName = lookupInfo.getTableName();
final String lookup_SqlWhere = lookupInfo.getWhereClauseSqlPart();
final TranslatableParameterizedString displayColumnSql = lookupInfo.getDisplayColumnSql();
final CompositeStringExpression.Builder sqlWhereFinal = IStringExpression.composer();
// Static lookup's WHERE
if (!Check.isEmpty(lookup_SqlWhere, true))
{
sqlWhereFinal.append(" /* lookup where clause */ ").append("(").append(lookup_SqlWhere).append(")");
}
// Validation Rule's WHERE
final IStringExpression validationRuleWhereClause = buildSqlWhereClauseFromValidationRule(validationRuleEffective, scope);
if (!validationRuleWhereClause.isNullExpression())
{
sqlWhereFinal.appendIfNotEmpty("\n AND ");
sqlWhereFinal.append(" /* validation rule */ ").append("(\n").append(validationRuleWhereClause).append("\n)\n");
}
// Filter's WHERE
sqlWhereFinal.appendIfNotEmpty("\n AND ");
sqlWhereFinal.append(" /* filter */ ")
.append(DBConstants.FUNCNAME_unaccent_string).append("(").append(displayColumnSql).append(", 1)")
.append(" ILIKE ")
.append(DBConstants.FUNCNAME_unaccent_string).append("(").append(LookupDataSourceContext.PARAM_FilterSql).append(", 1)");
// IsActive WHERE
if (!lookupInfo.isShowInactiveValues())
{
sqlWhereFinal.appendIfNotEmpty("\n AND ");
sqlWhereFinal.append(" /* active */ ('").append(SqlForFetchingLookupById.SQL_PARAM_ShowInactive)
.append("'='Y' OR ").append(tableName).append(".IsActive='Y')");
}
return sqlWhereFinal.build();
}
private static IStringExpression buildSqlWhereClauseFromValidationRule(final IValidationRule validationRule, final LookupScope scope)
{
final IStringExpression validationRuleWhereClause = validationRule.getPrefilterWhereClause();
if (validationRuleWhereClause.isNullExpression())
{
return IStringExpression.NULL;
}
if (scope == LookupScope.DocumentFilter && !validationRuleWhereClause.getParameters().isEmpty())
{
// don't add the validation rule if it has parameters
return IStringExpression.NULL;
}
return validationRuleWhereClause;
}
private SqlForFetchingLookups buildSqlForFetching(final MLookupInfo lookupInfo, final IStringExpression sqlWhere, final String sqlOrderBy)
{
final String tableName = lookupInfo.getTableName();
return SqlForFetchingLookups.builder()
.sql(IStringExpression.composer()
.append(lookupInfo.getSelectSqlPart()) // SELECT .. FROM ...
.append("\n WHERE \n").append(sqlWhere) // WHERE
.append("\n ORDER BY ").append(sqlOrderBy) // ORDER BY
.append("\n OFFSET ").append(SqlForFetchingLookups.PARAM_Offset) // OFFSET
.append("\n LIMIT ").append(SqlForFetchingLookups.PARAM_Limit) // LIMIT
.wrapIfTrue(!lookupInfo.isSecurityDisabled(), AccessSqlStringExpression.wrapper(tableName, IUserRolePermissions.SQL_FULLYQUALIFIED, getRequiredAccess(tableName))) // security
.build()
.caching())
.build();
}
private SqlForFetchingLookupById buildSqlForFetchingById(final MLookupInfo lookupInfo)
{
final IStringExpression displayColumnSQL = TranslatableParameterizedStringExpression.of(lookupInfo.getDisplayColumnSql());
final IStringExpression descriptionColumnSqlOrNull = TranslatableParameterizedStringExpression.of(lookupInfo.getDescriptionColumnSQL());
final IStringExpression descriptionColumnSQL;
if (descriptionColumnSqlOrNull == null || descriptionColumnSqlOrNull.isNullExpression())
{
descriptionColumnSQL = ConstantStringExpression.of("NULL");
}
else
{
descriptionColumnSQL = descriptionColumnSqlOrNull;
}
final IStringExpression validationMsgColumnSqlOrNull = TranslatableParameterizedStringExpression.of(lookupInfo.getValidationMsgColumnSQL());
final IStringExpression validationMsgColumnSQL;
if (validationMsgColumnSqlOrNull == null || validationMsgColumnSqlOrNull.isNullExpression())
{
validationMsgColumnSQL = ConstantStringExpression.of("NULL");
}
else
{
validationMsgColumnSQL = validationMsgColumnSqlOrNull;
}
final IStringExpression fromSqlPart = TranslatableParameterizedStringExpression.of(lookupInfo.getFromSqlPart());
final String keyColumnFQ = lookupInfo.getKeyColumnFQ();
final int displayType = lookupInfo.getDisplayType();
final String whereClauseSqlPart = lookupInfo.getWhereClauseSqlPart(); // assuming this is constant!
final CompositeStringExpression.Builder sqlBuilder = IStringExpression
.composer()
.append("SELECT ")
.append("\n ARRAY[").append(displayColumnSQL).append(", ").append(descriptionColumnSQL).append(",").append(lookupInfo.getActiveColumnSQL()).append(", ").append(validationMsgColumnSQL).append("]")
.append("\n FROM ")
.append(fromSqlPart)
.append("\n WHERE ")
.append(keyColumnFQ).append("=").append(SqlForFetchingLookupById.SQL_PARAM_KeyId)
.append(" ");
final boolean listOrButton = DisplayType.List == displayType || DisplayType.Button == displayType;
if (listOrButton)
{
// FIXME: make it better: this is actually adding the AD_Ref_List.AD_Reference_ID=....
sqlBuilder.append(" AND " + whereClauseSqlPart);
}
final ICachedStringExpression sql = sqlBuilder.build().caching();
return SqlForFetchingLookupById.builder()
.sql(sql)
.build();
}
private INamePairPredicate getPostQueryPredicate()
{
final INamePairPredicate postQueryPredicate = validationRuleEffective.getPostQueryFilter();
if (postQueryPredicate == null)
{
return INamePairPredicate.NULL;
}
if (scope == LookupScope.DocumentFilter && !postQueryPredicate.getParameters().isEmpty())
{
return INamePairPredicate.NULL;
}
return postQueryPredicate;
}
public Builder setCtxColumnName(final String columnName)
{
this.ctxColumnName = columnName;
return this;
}
public Builder setCtxTableName(final String tableName)
{
this.ctxTableName = tableName;
return this;
}
public Builder setWidgetType(final DocumentFieldWidgetType widgetType)
{
this.widgetType = widgetType;
return this;
}
public Builder setDisplayType(final int displayType)
{
this.displayType = displayType;
return this;
}
public Builder setAD_Reference_Value_ID(final int AD_Reference_Value_ID)
{
this.AD_Reference_Value_ID = AD_Reference_Value_ID;
return this;
}
public Builder setAD_Val_Rule_ID(final int AD_Val_Rule_ID)
{
this.AD_Val_Rule_ID = AD_Val_Rule_ID;
return this;
}
private boolean isHighVolume()
{
return DisplayType.TableDir != displayType && DisplayType.Table != displayType && DisplayType.List != displayType && DisplayType.Button != displayType;
}
private Builder setScope(final LookupScope scope)
{
Check.assumeNotNull(scope, "Parameter scope is not null");
this.scope = scope;
return this;
}
public Optional<WindowId> getZoomIntoWindowId()
{
return Optional.ofNullable(WindowId.ofNullable(zoomIntoAdWindowId));
}
private LookupSource getLookupSourceType()
{
return DescriptorsFactoryHelper.extractLookupSource(displayType, AD_Reference_Value_ID);
}
/**
* Advice the lookup to show all records on which current user has at least read only access
*/
public Builder setReadOnlyAccess()
{
this.requiredAccess = Access.READ;
return this;
}
private Access getRequiredAccess(final String tableName)
{
if (requiredAccess != null)
{
return requiredAccess;
}
// AD_Client_ID/AD_Org_ID (security fields): shall display only those entries on which current user has read-write access
if (I_AD_Client.Table_Name.equals(tableName)
|| I_AD_Org.Table_Name.equals(tableName))
{
return Access.WRITE;
}
// Default: all entries on which current user has at least readonly access
return Access.READ;
}
public Builder addValidationRule(final IValidationRule validationRule)
{
validationRules.add(validationRule);
return this;
}
public Builder addValidationRules(final Collection<IValidationRule> validationRules)
{
this.validationRules.addAll(validationRules);
return this;
}
public Set<String> getDependsOnTableNames()
{
return validationRuleEffective.getDependsOnTableNames();
}
}
}