-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
MTable.java
1159 lines (1059 loc) · 33.5 KB
/
MTable.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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. 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, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html *
* Contributor(s): Carlos Ruiz - globalqss *
*****************************************************************************/
package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.base.IModelFactory;
import org.adempiere.base.IServiceReferenceHolder;
import org.adempiere.base.Service;
import org.adempiere.model.GenericPO;
import org.compiere.db.AdempiereDatabase;
import org.compiere.db.Database;
import org.compiere.db.partition.ITablePartitionService;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;
import org.compiere.util.Util;
import org.idempiere.cache.ImmutableIntPOCache;
import org.idempiere.cache.ImmutablePOSupport;
/**
* Persistent Table Model
* <p>
* Change log:
* <ul>
* <li>2007-02-01 - teo_sarca - [ 1648850 ] MTable.getClass works incorrect for table "Fact_Acct"
* </ul>
* <ul>
* <li>2007-08-30 - vpj-cd - [ 1784588 ] Use ModelPackage of EntityType to Find Model Class
* </ul>
* @author Jorg Janke
* @author Teo Sarca, teo.sarca@gmail.com
* <li>BF [ 3017117 ] MTable.getClass returns bad class
* https://sourceforge.net/p/adempiere/bugs/2433/
* @version $Id: MTable.java,v 1.3 2006/07/30 00:58:04 jjanke Exp $
*/
public class MTable extends X_AD_Table implements ImmutablePOSupport
{
/**
*
*/
private static final long serialVersionUID = 6774131577483620665L;
public final static int MAX_OFFICIAL_ID = 999999;
/**
* Get MTable from Cache (immutable)
* @param AD_Table_ID id
* @return MTable
*/
public static MTable get (int AD_Table_ID)
{
return get(Env.getCtx(), AD_Table_ID);
}
/**
* Get MTable from Cache (immutable)
* @param ctx context
* @param AD_Table_ID id
* @return MTable
*/
public static MTable get (Properties ctx, int AD_Table_ID)
{
return get(ctx, AD_Table_ID, null);
} // get
/**
* Get MTable from Cache (immutable)
* @param ctx context
* @param AD_Table_ID id
* @param trxName transaction
* @return MTable
*/
public static synchronized MTable get (Properties ctx, int AD_Table_ID, String trxName)
{
Integer key = Integer.valueOf(AD_Table_ID);
MTable retValue = s_cache.get (ctx, key, e -> new MTable(ctx, e));
if (retValue != null)
return retValue;
retValue = new MTable (ctx, AD_Table_ID, trxName);
if (retValue.get_ID () == AD_Table_ID)
{
s_cache.put (key, retValue, e -> new MTable(Env.getCtx(), e));
return retValue;
}
return null;
} // get
/**
* Get updateable copy of MTable from cache
* @param ctx
* @param AD_Table_ID
* @param trxName
* @return MTable
*/
public static MTable getCopy(Properties ctx, int AD_Table_ID, String trxName)
{
MTable table = get(ctx, AD_Table_ID, trxName);
if (table != null)
table = new MTable(ctx, table, trxName);
return table;
}
/**
* Get MTable from Cache
* @param ctx context
* @param tableName case insensitive table name
* @return MTable
*/
public static synchronized MTable get (Properties ctx, String tableName)
{
return get(ctx, tableName, null);
} // get
/**
* Get MTable from Cache
* @param ctx context
* @param tableName case insensitive table name
* @param trxName
* @return MTable
*/
public static synchronized MTable get (Properties ctx, String tableName, String trxName)
{
if (tableName == null)
return null;
MTable[] tables = s_cache.values().toArray(new MTable[0]);
for (MTable retValue : tables)
{
if (tableName.equalsIgnoreCase(retValue.getTableName()))
{
return s_cache.get (ctx, retValue.get_ID(), e -> new MTable(ctx, e));
}
}
//
MTable retValue = null;
String sql = "SELECT * FROM AD_Table WHERE UPPER(TableName)=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, trxName);
pstmt.setString(1, tableName.toUpperCase());
rs = pstmt.executeQuery ();
if (rs.next())
retValue = new MTable (ctx, rs, trxName);
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
if (retValue != null)
{
Integer key = Integer.valueOf(retValue.getAD_Table_ID());
s_cache.put (key, retValue, e -> new MTable(Env.getCtx(), e));
}
return retValue;
} // get
/**
* Get Table Name
* @param ctx context
* @param AD_Table_ID table
* @return table name
*/
public static String getTableName (Properties ctx, int AD_Table_ID)
{
return MTable.get(ctx, AD_Table_ID).getTableName();
} // getTableName
/** Cache */
private static ImmutableIntPOCache<Integer,MTable> s_cache = new ImmutableIntPOCache<Integer,MTable>(Table_Name, Table_Name, 20, 0, false, 0);
/** Static Logger */
private static CLogger s_log = CLogger.getCLogger (MTable.class);
private static final CCache<String, IServiceReferenceHolder<IModelFactory>> s_modelFactoryCache = new CCache<>(null, "IModelFactory", 100, 120, false, 2000);
/**
* Get Java Model Class for Table
* @param tableName table name
* @return Java model class or null
*/
public static Class<?> getClass (String tableName)
{
IServiceReferenceHolder<IModelFactory> cache = s_modelFactoryCache.get(tableName);
if (cache != null)
{
IModelFactory service = cache.getService();
if (service != null)
{
Class<?> clazz = service.getClass(tableName);
if (clazz != null)
return clazz;
}
s_modelFactoryCache.remove(tableName);
}
List<IServiceReferenceHolder<IModelFactory>> factoryList = Service.locator().list(IModelFactory.class).getServiceReferences();
if (factoryList == null)
return null;
for(IServiceReferenceHolder<IModelFactory> factory : factoryList) {
IModelFactory service = factory.getService();
if (service != null) {
Class<?> clazz = service.getClass(tableName);
if (clazz != null)
{
s_modelFactoryCache.put(tableName, factory);
return clazz;
}
}
}
return null;
} // getClass
/**
* UUID based Constructor
* @param ctx Context
* @param AD_Table_UU UUID key
* @param trxName Transaction
*/
public MTable(Properties ctx, String AD_Table_UU, String trxName) {
super(ctx, AD_Table_UU, trxName);
if (Util.isEmpty(AD_Table_UU))
setInitialDefaults();
}
/**
* Standard Constructor
* @param ctx context
* @param AD_Table_ID id
* @param trxName transaction
*/
public MTable (Properties ctx, int AD_Table_ID, String trxName)
{
super (ctx, AD_Table_ID, trxName);
if (AD_Table_ID == 0)
setInitialDefaults();
} // MTable
/**
* Set the initial defaults for a new record
*/
private void setInitialDefaults() {
setAccessLevel (ACCESSLEVEL_SystemOnly); // 4
setEntityType (ENTITYTYPE_UserMaintained); // U
setIsChangeLog (false);
setIsDeleteable (false);
setIsHighVolume (false);
setIsSecurityEnabled (false);
setIsView (false); // N
setReplicationType (REPLICATIONTYPE_Local);
}
/**
* Load Constructor
* @param ctx context
* @param rs result set
* @param trxName transaction
*/
public MTable (Properties ctx, ResultSet rs, String trxName)
{
super(ctx, rs, trxName);
} // MTable
/**
* Copy constructor
* @param copy
*/
public MTable(MTable copy)
{
this(Env.getCtx(), copy);
}
/**
* Copy constructor
* @param ctx
* @param copy
*/
public MTable(Properties ctx, MTable copy)
{
this(ctx, copy, (String) null);
}
/**
* Copy constructor
* @param ctx
* @param copy
* @param trxName
*/
public MTable(Properties ctx, MTable copy, String trxName)
{
//-1 to avoid infinite loop
this(ctx, -1, trxName);
copyPO(copy);
this.m_columns = copy.m_columns != null ? Arrays.stream(copy.m_columns).map(e -> {return new MColumn(ctx, e, trxName);}).toArray(MColumn[]::new): null;
this.m_columnNameMap = copy.m_columnNameMap != null ? new HashMap<String, Integer>(copy.m_columnNameMap) : null;
this.m_columnIdMap = copy.m_columnIdMap != null ? new HashMap<Integer, Integer>(copy.m_columnIdMap) : null;
this.m_viewComponents = copy.m_viewComponents != null ? Arrays.stream(copy.m_viewComponents).map(e -> {return new MViewComponent(ctx, e, trxName);}).toArray(MViewComponent[]::new) : null;
}
/** Columns */
private MColumn[] m_columns = null;
/** Key Columns */
private String[] m_KeyColumns = null;
/** column name to column index map **/
private Map<String, Integer> m_columnNameMap;
/** ad_column_id to column index map **/
private Map<Integer, Integer> m_columnIdMap;
/** View Components */
private MViewComponent[] m_viewComponents = null;
/**
* Get Columns
* @param requery true to re-query from DB
* @return array of column
*/
public synchronized MColumn[] getColumns (boolean requery)
{
if (m_columns != null && !requery)
return m_columns;
m_columnNameMap = new HashMap<String, Integer>();
m_columnIdMap = new HashMap<Integer, Integer>();
String sql = "SELECT * FROM AD_Column WHERE AD_Table_ID=? AND IsActive='Y' ORDER BY ColumnName";
ArrayList<MColumn> list = new ArrayList<MColumn>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, getAD_Table_ID());
rs = pstmt.executeQuery ();
while (rs.next ()) {
MColumn column = new MColumn (getCtx(), rs, get_TrxName());
if (is_Immutable())
column.markImmutable();
list.add (column);
m_columnNameMap.put(column.getColumnName().toUpperCase(), list.size() - 1);
m_columnIdMap.put(column.getAD_Column_ID(), list.size() - 1);
}
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
//
if (list.size() > 0 && is_Immutable())
list.stream().forEach(e -> e.markImmutable());
m_columns = new MColumn[list.size ()];
list.toArray (m_columns);
return m_columns;
} // getColumns
/**
* Get Column via column name
* @param columnName (case insensitive)
* @return MColumn if found, null otherwise
*/
public MColumn getColumn (String columnName)
{
if (columnName == null || columnName.length() == 0)
return null;
int idx = getColumnIndex(columnName);
if (idx < 0)
return null;
return m_columns[idx];
} // getColumn
/**
* Get Column Index via column name
* @param ColumnName column name (case insensitive)
* @return index of column with ColumnName or -1 if not found
*/
public synchronized int getColumnIndex (String ColumnName)
{
if (m_columns == null)
getColumns(false);
Integer i = m_columnNameMap.get(ColumnName.toUpperCase());
if (i != null)
return i.intValue();
return -1;
} // getColumnIndex
/**
* Is column exists and is not virtual ?
* @param ColumnName column name (case insensitive)
* @return true if column exists and is not virtual
*/
public synchronized boolean columnExistsInDB (String ColumnName)
{
MColumn column = getColumn(ColumnName);
return column != null && ! column.isVirtualColumn();
} // columnExistsInDB
/**
* Column exists?
* @param ColumnName column name (case insensitive)
* @return true if column exists in dictionary
*/
public synchronized boolean columnExistsInDictionary (String ColumnName)
{
return getColumnIndex(ColumnName) >= 0;
} // columnExistsInDictionary
/**
* Get Column Index
* @param AD_Column_ID column id
* @return index of column with AD_Column_ID or -1 if not found
*/
public synchronized int getColumnIndex (int AD_Column_ID)
{
if (m_columns == null)
getColumns(false);
Integer i = m_columnIdMap.get(AD_Column_ID);
if (i != null)
return i.intValue();
return -1;
} // getColumnIndex
/**
* Table is with single primary key
* @return true if table has single primary key column
*/
public boolean isSingleKey()
{
String[] keys = getKeyColumns();
return keys.length == 1;
} // isSingleKey
/**
* Get Key Columns of Table
* @return array of key column name
*/
public String[] getKeyColumns()
{
if (m_KeyColumns != null)
return m_KeyColumns;
getColumns(false);
ArrayList<String> list = new ArrayList<String>();
//
for (int i = 0; i < m_columns.length; i++)
{
MColumn column = m_columns[i];
if (column.isKey()) {
m_KeyColumns = new String[]{column.getColumnName()};
return m_KeyColumns;
}
if (column.isParent())
list.add(column.getColumnName());
}
//check uuid key
if (list.isEmpty()) {
MColumn uuColumn = getColumn(PO.getUUIDColumnName(getTableName()));
if (uuColumn != null) {
m_KeyColumns = new String[]{uuColumn.getColumnName()};
return m_KeyColumns;
}
}
String[] retValue = new String[list.size()];
retValue = list.toArray(retValue);
m_KeyColumns = retValue;
return m_KeyColumns;
} // getKeyColumns
/**
* @return true if table has single key column and the key column name is the same as the table name plus _ID.
*/
public boolean isIDKeyTable()
{
String idColName = getTableName() + "_ID";
return (getKeyColumns() != null && getKeyColumns().length == 1 && getKeyColumns()[0].equals(idColName));
}
/**
* @return true if table has single key column and the key column name ends with _UU.
*/
public boolean isUUIDKeyTable()
{
String uuColName = PO.getUUIDColumnName(getTableName());
return (getKeyColumns() != null && getKeyColumns().length == 1 && getKeyColumns()[0].equals(uuColName));
}
/**
* @return true if table has a UUID column (column name ends with _UU)
*/
public boolean hasUUIDKey()
{
String uuColName = PO.getUUIDColumnName(getTableName());
if (m_columns == null)
getColumns(false);
return m_columnNameMap.get(uuColName.toUpperCase()) != null;
}
/**
* Get Identifier Columns of Table (IsIdentifier=Y)
* @return array of identifier column name
*/
public String[] getIdentifierColumns() {
ArrayList<KeyNamePair> listkn = new ArrayList<KeyNamePair>();
for (MColumn column : getColumns(false)) {
if (column.isIdentifier())
listkn.add(new KeyNamePair(column.getSeqNo(), column.getColumnName()));
}
// Order by SeqNo
Collections.sort(listkn, new Comparator<KeyNamePair>(){
public int compare(KeyNamePair s1,KeyNamePair s2){
if (s1.getKey() < s2.getKey())
return -1;
else if (s1.getKey() > s2.getKey())
return 1;
else
return 0;
}});
String[] retValue = new String[listkn.size()];
for (int i = 0; i < listkn.size(); i++) {
retValue[i] = listkn.get(i).getName();
}
return retValue;
} // getIdentifierColumns
/**
* Get PO Instance for this table
* @param Record_ID record id. 0 to create new record instance, > 0 to load existing record instance.
* @param trxName
* @return PO for Record_ID or null
*/
public PO getPO (int Record_ID, String trxName)
{
String tableName = getTableName();
if (Record_ID != 0 && !isSingleKey())
{
log.log(Level.WARNING, "(id) - Multi-Key " + tableName);
return null;
}
PO po = null;
IServiceReferenceHolder<IModelFactory> cache = s_modelFactoryCache.get(tableName);
if (cache != null)
{
IModelFactory service = cache.getService();
if (service != null)
{
po = service.getPO(tableName, Record_ID, trxName);
if (po != null)
{
if (po.get_ID() != Record_ID && Record_ID > 0)
po = null;
return po;
}
}
s_modelFactoryCache.remove(tableName);
}
List<IServiceReferenceHolder<IModelFactory>> factoryList = Service.locator().list(IModelFactory.class).getServiceReferences();
if (factoryList != null)
{
for(IServiceReferenceHolder<IModelFactory> factory : factoryList)
{
IModelFactory service = factory.getService();
if (service != null)
{
po = service.getPO(tableName, Record_ID, trxName);
if (po != null)
{
if (po.get_ID() != Record_ID && Record_ID > 0)
po = null;
s_modelFactoryCache.put(tableName, factory);
break;
}
}
}
}
if (po == null && s_modelFactoryCache.get(tableName) == null)
{
po = new GenericPO(tableName, getCtx(), Record_ID, trxName);
if (po.get_ID() != Record_ID && Record_ID > 0)
po = null;
// TODO: how to add GenericPO to the s_modelFactoryCache ??
}
return po;
} // getPO
private static final ThreadLocal<String[]> partialPOResultSetColumns = new ThreadLocal<>();
/**
* Get columns included in result set of {@link #getPO(int, String)} call.<br/>
* Use by {@link #getPartialPO(ResultSet, String[], String)}.
* @return columns included in result set of {@link #getPO(int, String)} call
*/
protected static final String[] getPartialPOResultSetColumns() {
return partialPOResultSetColumns.get();
}
/**
* Get PO Instance from result set that only include some of the columns of the PO model.
* @param rs result set
* @param selectColumns
* @param trxName transaction
* @return immutable PO instance
*/
public final PO getPartialPO (ResultSet rs, String[] selectColumns, String trxName)
{
try {
partialPOResultSetColumns.set(selectColumns);
PO po = getPO(rs, trxName);
po.makeImmutable();
return po;
} finally {
partialPOResultSetColumns.remove();
}
}
/**
* Get PO Instance from result set
* @param rs result set
* @param trxName transaction
* @return PO instance
*/
public PO getPO (ResultSet rs, String trxName)
{
String tableName = getTableName();
PO po = null;
IServiceReferenceHolder<IModelFactory> cache = s_modelFactoryCache.get(tableName);
if (cache != null)
{
IModelFactory service = cache.getService();
if (service != null)
{
po = service.getPO(tableName, rs, trxName);
if (po != null)
return po;
}
s_modelFactoryCache.remove(tableName);
}
List<IServiceReferenceHolder<IModelFactory>> factoryList = Service.locator().list(IModelFactory.class).getServiceReferences();
if (factoryList != null)
{
for(IServiceReferenceHolder<IModelFactory> factory : factoryList) {
IModelFactory service = factory.getService();
if (service != null)
{
po = service.getPO(tableName, rs, trxName);
if (po != null)
{
s_modelFactoryCache.put(tableName, factory);
break;
}
}
}
}
if (po == null)
{
po = new GenericPO(tableName, getCtx(), rs, trxName);
}
return po;
} // getPO
/**
* Get PO Instance
*
* @param uuID UUID. Throw IllegalArgumentException if this is null or empty string.
* @param trxName transaction
* @return PO for uuID
*/
public PO getPOByUU (String uuID, String trxName)
{
PO po = getPO(0, trxName);
po.loadByUU(uuID, trxName);
return po;
} // getPOByUU
/**
* Get PO Instance
* @param whereClause SQL where clause
* @param trxName transaction
* @return PO for whereClause or null
*/
public PO getPO (String whereClause, String trxName)
{
return getPO(whereClause, null, trxName);
} // getPO
/**
* Get PO instance
* @param whereClause SQL where clause
* @param params parameters for whereClause
* @param trxName
* @return PO instance or null
*/
public PO getPO(String whereClause, Object[] params, String trxName)
{
if (whereClause == null || whereClause.length() == 0)
return null;
//
PO po = null;
POInfo info = POInfo.getPOInfo(getCtx(), getAD_Table_ID(), trxName);
if (info == null) return null;
StringBuilder sqlBuffer = info.buildSelect();
sqlBuffer.append(" WHERE ").append(whereClause);
String sql = sqlBuffer.toString();
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, trxName);
if (params != null && params.length > 0)
{
for (int i = 0; i < params.length; i++)
{
pstmt.setObject(i+1, params[i]);
}
}
rs = pstmt.executeQuery ();
if (rs.next ())
{
po = getPO(rs, trxName);
}
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
log.saveError("Error", e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
return po;
}
@Override
protected boolean beforeSave (boolean newRecord)
{
if (isView() && isDeleteable())
setIsDeleteable(false);
// Validate table name is valid DB identifier
String error = Database.isValidIdentifier(getTableName());
if (!Util.isEmpty(error)) {
log.saveError("Error", Msg.getMsg(getCtx(), error) + " [TableName]");
return false;
}
// Validate table partition configuration
if (is_ValueChanged(COLUMNNAME_IsPartition)) {
ITablePartitionService service = DB.getDatabase().getTablePartitionService();
if (service == null) {
log.saveError("Error", Msg.getMsg(getCtx(), "DBAdapterNoTablePartitionSupport"));
return false;
}
error = service.isValidConfiguration(this);
if (!Util.isEmpty(error)) {
log.saveError("Error", Msg.getMsg(getCtx(), error));
return false;
}
}
return true;
} // beforeSave
@Override
protected boolean afterSave (boolean newRecord, boolean success)
{
if (!success)
return success;
if(!isView()) {
// Create or update table sequence
MSequence seq = MSequence.get(getCtx(), getTableName(), get_TrxName());
if (seq == null || seq.get_ID() == 0)
MSequence.createTableSequence(getCtx(), getTableName(), get_TrxName());
else if (!seq.getName().equals(getTableName()))
{
seq.setName(getTableName());
seq.saveEx();
}
}
// Reset logged table list
if (newRecord || is_ValueChanged(COLUMNNAME_IsChangeLog)) {
MChangeLog.resetLoggedList();
}
return success;
} // afterSave
/**
* Get Create table DDL
* @return create table DDL
*/
public String getSQLCreate()
{
AdempiereDatabase db = DB.getDatabase();
if (db.isNativeMode())
return db.getSQLCreate(this);
else
return Database.getDatabase(Database.DB_ORACLE).getSQLCreate(this);
} // getSQLCreate
/**
* Get AD_Table_ID via table name
* @param tableName
* @return AD_Table_ID
*/
public static int getTable_ID(String tableName) {
return getTable_ID(tableName, null);
}
/**
* Get AD_Table_ID via table name
* @param tableName String
* @param trxName
* @return AD_Table_ID
*/
public static int getTable_ID(String tableName, String trxName) {
MTable table = get(Env.getCtx(), tableName, trxName);
return table != null ? table.getAD_Table_ID() : 0;
}
/**
* Create new query for whereClause
* @param whereClause SQL whereClause
* @param trxName
* @return new Query instance
*/
public Query createQuery(String whereClause, String trxName)
{
return new Query(this.getCtx(), this, whereClause, trxName);
}
/**
* Get active view components
* @param reload true to reload from DB
* @return array of view component
*/
public MViewComponent[] getViewComponent(boolean reload)
{
if (!isView() || !isActive())
return null;
if (m_viewComponents != null && !reload)
return m_viewComponents;
Query query = new Query(getCtx(), MViewComponent.Table_Name, MViewComponent.COLUMNNAME_AD_Table_ID + "=?", get_TrxName());
query.setParameters(getAD_Table_ID());
query.setOrderBy(MViewComponent.COLUMNNAME_SeqNo);
query.setOnlyActiveRecords(true);
List<MViewComponent> list = query.list();
if (list.size() > 0 && is_Immutable())
list.stream().forEach(e -> e.markImmutable());
m_viewComponents = new MViewComponent[list.size()];
list.toArray(m_viewComponents);
return m_viewComponents;
}
/**
* String Representation
* @return info
*/
@Override
public String toString()
{
StringBuilder sb = new StringBuilder ("MTable[");
sb.append (get_ID()).append ("-").append (getTableName()).append ("]");
return sb.toString ();
} // toString
/**
* Verify if the table has record with ID=0
* @return true if table has record with ID=0
*/
public static boolean isZeroIDTable(String tablename) {
return (tablename.equals("AD_Org") ||
tablename.equals("AD_OrgInfo") ||
tablename.equals("AD_Client") || // IDEMPIERE-668
tablename.equals("AD_ClientInfo") ||
tablename.equals("AD_AllClients_V") ||
tablename.equals("AD_ReportView") ||
tablename.equals("AD_Role") ||
tablename.equals("AD_AllRoles_V") ||
tablename.equals("AD_System") ||
tablename.equals("AD_User") ||
tablename.equals("AD_AllUsers_V") ||
tablename.equals("C_DocType") ||
tablename.equals("GL_Category") ||
tablename.equals("M_AttributeSet") ||
tablename.equals("M_AttributeSetInstance"));
}
@Override
public MTable markImmutable() {
if (is_Immutable())
return this;
makeImmutable();
if (m_columns != null && m_columns.length > 0)
Arrays.stream(m_columns).forEach(e -> e.markImmutable());
if (m_viewComponents != null && m_viewComponents.length > 0)
Arrays.stream(m_viewComponents).forEach(e -> e.markImmutable());
return this;
}
/**
* Get first AD_Window that's using this table from AD_Menu.
* @return AD_Window_ID or -1 if not found
*/
public int getWindowIDFromMenu() {
return DB.getSQLValueEx(null, "SELECT a.AD_Window_ID FROM AD_Window a "
+ "INNER JOIN AD_Tab b ON (a.AD_Window_ID=b.AD_Window_ID) "
+ "INNER JOIN AD_Menu m ON (a.AD_Window_ID=m.AD_Window_ID AND m.IsActive='Y' AND m.Action='W') "
+ "WHERE a.IsActive='Y' AND b.IsActive='Y' AND b.AD_Table_ID=? ORDER BY b.TabLevel, a.AD_Window_ID", getAD_Table_ID());
}
/**
* Get the UUID of Zero ID record
* @return UUID or null
*/
public String getUUIDFromZeroID() {
if (! MTable.isZeroIDTable(getTableName()))
return null;
StringBuilder sqluu = new StringBuilder()
.append("SELECT ")
.append(PO.getUUIDColumnName(getTableName()))
.append(" FROM ")
.append(getTableName())
.append(" WHERE ")
.append(getKeyColumns()[0])
.append("=0");
String uuidFromZeroID = DB.getSQLValueStringEx(get_TrxName(), sqluu.toString());
return uuidFromZeroID;
}
private List<MColumn> partitionKeyColumns;
private List<String> partitionKeyColumnNames;
/**
* @param requery true to reload from DB
* @return partition key columns
*/
public List<MColumn> getPartitionKeyColumns(boolean requery)
{
if (partitionKeyColumns != null && !requery)
return partitionKeyColumns;
partitionKeyColumnNames = null;
String whereClause = MColumn.COLUMNNAME_AD_Table_ID + "=? AND " + MColumn.COLUMNNAME_IsPartitionKey + "='Y'";
partitionKeyColumns = new Query(getCtx(), MColumn.Table_Name, whereClause, null)
.setParameters(getAD_Table_ID())
.setOnlyActiveRecords(true)
.setOrderBy(MColumn.COLUMNNAME_SeqNoPartition)
.list();
return partitionKeyColumns;