forked from idempiere/idempiere
/
PO.java
5403 lines (5051 loc) · 149 KB
/
PO.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 *
*****************************************************************************/
package org.compiere.model;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Timestamp;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.adempiere.base.event.EventManager;
import org.adempiere.base.event.IEventTopics;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.adempiere.process.UUIDGenerator;
import org.compiere.Adempiere;
import org.compiere.acct.Doc;
import org.compiere.util.AdempiereUserError;
import org.compiere.util.CCache;
import org.compiere.util.CLogMgt;
import org.compiere.util.CLogger;
import org.compiere.util.CacheMgt;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.Evaluatee;
import org.compiere.util.Ini;
import org.compiere.util.Language;
import org.compiere.util.Msg;
import org.compiere.util.SecureEngine;
import org.compiere.util.Trace;
import org.compiere.util.Trx;
import org.compiere.util.Util;
import org.compiere.util.ValueNamePair;
import org.osgi.service.event.Event;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* Persistent Object.
* Superclass for actual implementations
*
* @author Jorg Janke
* @version $Id: PO.java,v 1.12 2006/08/09 16:38:47 jjanke Exp $
*
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>FR [ 1675490 ] ModelValidator on modelChange after events
* <li>BF [ 1704828 ] PO.is_Changed() and PO.is_ValueChanged are not consistent
* <li>FR [ 1720995 ] Add PO.saveEx() and PO.deleteEx() methods
* <li>BF [ 1990856 ] PO.set_Value* : truncate string more than needed
* <li>FR [ 2042844 ] PO.get_Translation improvements
* <li>FR [ 2818369 ] Implement PO.get_ValueAs*(columnName)
* https://sourceforge.net/p/adempiere/feature-requests/754/
* <li>BF [ 2849122 ] PO.AfterSave is not rollback on error
* https://sourceforge.net/p/adempiere/bugs/2073/
* <li>BF [ 2859125 ] Can't set AD_OrgBP_ID
* https://sourceforge.net/p/adempiere/bugs/2095/
* <li>BF [ 2866493 ] VTreePanel is not saving who did the node move
* https://sourceforge.net/p/adempiere/bugs/2135/
* @author Teo Sarca, teo.sarca@gmail.com
* <li>BF [ 2876259 ] PO.insertTranslation query is not correct
* https://sourceforge.net/p/adempiere/bugs/2168/
* @author Victor Perez, e-Evolution SC
* <li>[ 2195894 ] Improve performance in PO engine
* <li>https://sourceforge.net/p/adempiere/feature-requests/555/
* <li>BF [2947622] The replication ID (Primary Key) is not working
* <li>https://sourceforge.net/p/adempiere/bugs/2308/
*/
public abstract class PO
implements Serializable, Comparator<Object>, Evaluatee, Cloneable
{
/**
*
*/
private static final long serialVersionUID = 3153695115162945843L;
public static final String LOCAL_TRX_PREFIX = "POSave";
private static final String USE_TIMEOUT_FOR_UPDATE = "org.adempiere.po.useTimeoutForUpdate";
private static final String USE_OPTIMISTIC_LOCKING = "org.idempiere.po.useOptimisticLocking";
/** default timeout, 300 seconds **/
private static final int QUERY_TIME_OUT = 300;
/**
* Set Document Value Workflow Manager
* @param docWFMgr mgr
*/
public static void setDocWorkflowMgr (DocWorkflowMgr docWFMgr)
{
s_docWFMgr = docWFMgr;
s_log.config (s_docWFMgr.toString());
} // setDocWorkflowMgr
/** Document Value Workflow Manager */
private static DocWorkflowMgr s_docWFMgr = null;
/** User Maintained Entity Type */
static public final String ENTITYTYPE_UserMaintained = "U";
/** Dictionary Maintained Entity Type */
static public final String ENTITYTYPE_Dictionary = "D";
/**************************************************************************
* Create New Persistent Object
* @param ctx context
*/
public PO (Properties ctx)
{
this (ctx, 0, null, null, (String[]) null);
} // PO
/**
* Create and Load existing Persistent Object
* @param ID The unique ID of the object
* @param ctx context
* @param trxName transaction name
*/
public PO (Properties ctx, int ID, String trxName)
{
this (ctx, ID, trxName, null, (String[]) null);
} // PO
/**
* Create and load existing Persistent Object
* @param ctx Context
* @param ID Unique ID of the object
* @param trxName Transaction name
* @param virtualColumns names of virtual columns to load along with the regular table columns
*/
public PO (Properties ctx, int ID, String trxName, String ... virtualColumns)
{
this (ctx, ID, trxName, null, virtualColumns);
}
/**
* Create and Load existing Persistent Object.
* @param ctx context
* @param rs optional - load from current result set position (no navigation, not closed)
* if null, a new record is created.
* @param trxName transaction name
*/
public PO (Properties ctx, ResultSet rs, String trxName)
{
this (ctx, 0, trxName, rs);
} // PO
/**
* Create and Load existing Persistent Object.
* <pre>
* You load
* - an existing single key record with new PO (ctx, Record_ID)
* or new PO (ctx, Record_ID, trxName)
* or new PO (ctx, rs, get_TrxName())
* - a new single key record with new PO (ctx, 0)
* - an existing multi key record with new PO (ctx, rs, get_TrxName())
* - a new multi key record with new PO (ctx, null)
* The ID for new single key records is created automatically,
* you need to set the IDs for multi-key records explicitly.
* </pre>
* @param ctx context
* @param ID the ID if 0, the record defaults are applied - ignored if re exists
* @param trxName transaction name
* @param rs optional - load from current result set position (no navigation, not closed)
* @param virtualColumns optional - names of virtual columns to load along with the regular table columns
*/
public PO (Properties ctx, int ID, String trxName, ResultSet rs, String ... virtualColumns)
{
p_ctx = ctx != null ? ctx : Env.getCtx();
m_trxName = trxName;
p_info = initPO(ctx);
if (p_info == null || p_info.getTableName() == null)
throw new IllegalArgumentException ("Invalid PO Info - " + p_info);
//
int size = p_info.getColumnCount();
m_oldValues = new Object[size];
m_newValues = new Object[size];
m_setErrors = new ValueNamePair[size];
m_setErrorsFilled = false;
if (rs != null)
load(rs);
else
load(ID, trxName, virtualColumns);
checkCrossTenant(false);
} // PO
/**
* Create New PO by Copying existing (key not copied).
* @param ctx context
* @param source source object
* @param AD_Client_ID client
* @param AD_Org_ID org
*/
public PO (Properties ctx, PO source, int AD_Client_ID, int AD_Org_ID)
{
this (ctx, 0, null, (String[]) null); // create new
//
if (source != null)
copyValues (source, this);
setAD_Client_ID(AD_Client_ID);
setAD_Org_ID(AD_Org_ID);
} // PO
/**
* Copy all properties from copy. Method to help the implementation of copy constructor.
* @param copy
*/
protected void copyPO(PO copy)
{
this.m_attachment = copy.m_attachment != null ? new MAttachment(copy.m_attachment) : null;
this.m_attributes = copy.m_attributes != null ? new HashMap<String, Object>(copy.m_attributes) : null;
this.m_createNew = copy.m_createNew;
this.m_custom = copy.m_custom != null ? new HashMap<String, String>(copy.m_custom) : null;
this.m_IDs = copy.m_IDs != null ? Arrays.copyOf(copy.m_IDs, copy.m_IDs.length) : null;
this.m_KeyColumns = copy.m_KeyColumns != null ? Arrays.copyOf(copy.m_KeyColumns, copy.m_KeyColumns.length) : null;
this.m_lobInfo = copy.m_lobInfo != null ? copy.m_lobInfo.stream().map(PO_LOB::new).collect(Collectors.toCollection(ArrayList::new)) : null;
this.m_newValues = copy.m_newValues != null ? Arrays.copyOf(copy.m_newValues, copy.m_newValues.length) : null;
this.m_oldValues = copy.m_oldValues != null ? Arrays.copyOf(copy.m_oldValues, copy.m_oldValues.length) : null;
this.s_acctColumns = copy.s_acctColumns != null ? copy.s_acctColumns.stream().collect(Collectors.toCollection(ArrayList::new)) : null;
}
/** Logger */
protected transient CLogger log = CLogger.getCLogger (getClass());
/** Static Logger */
private static CLogger s_log = CLogger.getCLogger (PO.class);
/** Context */
protected transient Properties p_ctx;
/** Model Info */
protected transient volatile POInfo p_info = null;
/** Original Values */
private Object[] m_oldValues = null;
/** New Values */
private Object[] m_newValues = null;
/** Errors when setting */
private ValueNamePair[] m_setErrors = null;
private boolean m_setErrorsFilled = false; // to optimize not traveling the array if no errors
/** Record_IDs */
private Object[] m_IDs = new Object[] {I_ZERO};
/** Key Columns */
private String[] m_KeyColumns = null;
/** Create New for Multi Key */
private boolean m_createNew = false;
/** Attachment with entries */
private MAttachment m_attachment = null;
/** Deleted ID */
private int m_idOld = 0;
/** Custom Columns */
private HashMap<String,String> m_custom = null;
/** Attributes */
private HashMap<String,Object> m_attributes = null;
/** Zero Integer */
protected static final Integer I_ZERO = Integer.valueOf(0);
/** Accounting Columns */
private ArrayList <String> s_acctColumns = null;
/** Trifon - Indicates that this record is created by replication functionality.*/
private boolean m_isReplication = false;
/** Immutable flag **/
private boolean m_isImmutable = false;
private String[] m_optimisticLockingColumns = new String[] {"Updated"};
private Boolean m_useOptimisticLocking = null;
/** Indices of virtual columns that were already resolved */
private Set<Integer> loadedVirtualColumns = new HashSet<>();
/** Access Level S__ 100 4 System info */
public static final int ACCESSLEVEL_SYSTEM = 4;
/** Access Level _C_ 010 2 Client info */
public static final int ACCESSLEVEL_CLIENT = 2;
/** Access Level __O 001 1 Organization info */
public static final int ACCESSLEVEL_ORG = 1;
/** Access Level SCO 111 7 System shared info */
public static final int ACCESSLEVEL_ALL = 7;
/** Access Level SC_ 110 6 System/Client info */
public static final int ACCESSLEVEL_SYSTEMCLIENT = 6;
/** Access Level _CO 011 3 Client shared info */
public static final int ACCESSLEVEL_CLIENTORG = 3;
/**
* Initialize and return PO_Info
* @param ctx context
* @return POInfo
*/
abstract protected POInfo initPO (Properties ctx);
/**
* Get Table Access Level
* @return Access Level
*/
abstract protected int get_AccessLevel();
/**
* String representation
* @return String representation
*/
public String toString()
{
StringBuilder sb = new StringBuilder("PO[")
.append(get_WhereClause(true)).append("]");
return sb.toString();
} // toString
/**
* Equals based on ID
* @param cmp comparator
* @return true if ID the same
*/
public boolean equals (Object cmp)
{
if (cmp == null)
return false;
if (!(cmp instanceof PO))
return false;
if (cmp.getClass().equals(this.getClass()))
// if both ID's are zero they can't be compared by ID
if (((PO)cmp).get_ID() == 0 && get_ID() == 0)
return super.equals(cmp);
else
return ((PO)cmp).get_ID() == get_ID();
return super.equals(cmp);
} // equals
public int hashCode()
{
assert false : "hashCode not designed";
return 42; // any arbitrary constant will do
}
/**
* Compare based on DocumentNo, Value, Name, Description
* @param o1 Object 1
* @param o2 Object 2
* @return -1 if o1 < o2
*/
public int compare (Object o1, Object o2)
{
if (o1 == null)
return -1;
else if (o2 == null)
return 1;
if (!(o1 instanceof PO))
throw new ClassCastException ("Not PO -1- " + o1);
if (!(o2 instanceof PO))
throw new ClassCastException ("Not PO -2- " + o2);
// same class
Collator collator = Collator.getInstance();
if (o1.getClass().equals(o2.getClass()))
{
int index = get_ColumnIndex("DocumentNo");
if (index == -1)
index = get_ColumnIndex("Value");
if (index == -1)
index = get_ColumnIndex("Name");
if (index == -1)
index = get_ColumnIndex("Description");
if (index != -1)
{
PO po1 = (PO)o1;
Object comp1 = po1.get_Value(index);
PO po2 = (PO)o2;
Object comp2 = po2.get_Value(index);
if (comp1 == null)
return -1;
else if (comp2 == null)
return 1;
return collator.compare(comp1.toString(), comp2.toString());
}
}
return collator.compare(o1.toString(), o2.toString());
} // compare
/**
* Get TableName.
* @return table name
*/
public String get_TableName()
{
return p_info.getTableName();
} // get_TableName
/**
* Get Key Columns.
* @return table name
*/
public String[] get_KeyColumns()
{
return m_KeyColumns;
} // get_KeyColumns
/**
* Get Table ID.
* @return table id
*/
public int get_Table_ID()
{
return p_info.getAD_Table_ID();
} // get_TableID
/**
* Return Single Key Record ID
* @return ID or 0
*/
public int get_ID()
{
Object oo = m_IDs[0];
if (oo != null && oo instanceof Integer)
return ((Integer)oo).intValue();
return 0;
} // getID
/**
* Return Deleted Single Key Record ID
* @return ID or 0
*/
public int get_IDOld()
{
return m_idOld;
} // getID
/**
* Get Context
* @return context
*/
public Properties getCtx()
{
return p_ctx;
} // getCtx
/**
* Get Logger
* @return logger
*/
public CLogger get_Logger()
{
return log;
} // getLogger
/**************************************************************************
* Get Value
* @param index index
* @return value
*/
public final Object get_Value (int index)
{
if (index < 0 || index >= get_ColumnCount())
{
log.log(Level.WARNING, "Index invalid - " + index);
return null;
}
if (m_newValues[index] != null)
{
if (m_newValues[index].equals(Null.NULL))
return null;
return m_newValues[index];
}
if(p_info.isVirtualColumn(index) && p_info.isVirtualDBColumn(index))
loadVirtualColumn(index);
return m_oldValues[index];
} // get_Value
/**
* Get Value as int
* @param index index
* @return int value or 0
*/
public int get_ValueAsInt (int index)
{
Object value = get_Value(index);
if (value == null)
return 0;
if (value instanceof Integer)
return ((Integer)value).intValue();
try
{
return Integer.parseInt(value.toString());
}
catch (NumberFormatException ex)
{
log.warning(p_info.getColumnName(index) + " - " + ex.getMessage());
return 0;
}
} // get_ValueAsInt
/**
* Get Value
* @param columnName column name
* @return value or null
*/
public final Object get_Value (String columnName)
{
int index = get_ColumnIndex(columnName);
if (index < 0)
{
log.log(Level.WARNING, "Column not found - " + get_TableName() + "." + columnName);
Trace.printStack();
return null;
}
return get_Value (index);
} // get_Value
/**
* Get Encrypted Value
* @param columnName column name
* @return value or null
*/
protected final Object get_ValueE (String columnName)
{
return get_Value (columnName);
} // get_ValueE
/**
* Get Column Value
* @param variableName name
* @return value or ""
*/
public String get_ValueAsString (String variableName)
{
Object value = get_Value (variableName);
if (value == null)
return "";
return value.toString();
} // get_ValueAsString
/**
* Get Value of Column
* @param AD_Column_ID column
* @return value or null
*/
public final Object get_ValueOfColumn (int AD_Column_ID)
{
int index = p_info.getColumnIndex(AD_Column_ID);
if (index < 0)
{
log.log(Level.WARNING, "Not found - AD_Column_ID=" + AD_Column_ID);
return null;
}
return get_Value (index);
} // get_ValueOfColumn
/**
* Get Old Value
* @param index index
* @return value
*/
public final Object get_ValueOld (int index)
{
if (index < 0 || index >= get_ColumnCount())
{
log.log(Level.WARNING, "Index invalid - " + index);
return null;
}
return m_oldValues[index];
} // get_ValueOld
/**
* Get Old Value
* @param columnName column name
* @return value or null
*/
public final Object get_ValueOld (String columnName)
{
int index = get_ColumnIndex(columnName);
if (index < 0)
{
log.log(Level.WARNING, "Column not found - " + get_TableName() + "." + columnName);
return null;
}
return get_ValueOld (index);
} // get_ValueOld
/**
* Get Old Value as int
* @param columnName column name
* @return int value or 0
*/
public int get_ValueOldAsInt (String columnName)
{
Object value = get_ValueOld(columnName);
if (value == null)
return 0;
if (value instanceof Integer)
return ((Integer)value).intValue();
try
{
return Integer.parseInt(value.toString());
}
catch (NumberFormatException ex)
{
log.warning(columnName + " - " + ex.getMessage());
return 0;
}
} // get_ValueOldAsInt
/**
* Is Value Changed
* @param index index
* @return true if changed
*/
public final boolean is_ValueChanged (int index)
{
if (index < 0 || index >= get_ColumnCount())
{
log.log(Level.WARNING, "Index invalid - " + index);
return false;
}
if (m_newValues[index] == null)
return false;
if (m_newValues[index] == Null.NULL && m_oldValues[index] == null)
return false;
return !m_newValues[index].equals(m_oldValues[index]);
} // is_ValueChanged
/**
* Is Value Changed
* @param columnName column name
* @return true if changed
*/
public final boolean is_ValueChanged (String columnName)
{
int index = get_ColumnIndex(columnName);
if (index < 0)
{
log.log(Level.WARNING, "Column not found - " + get_TableName() + "." + columnName);
return false;
}
return is_ValueChanged (index);
} // is_ValueChanged
/**
* Return new - old.
* - New Value if Old Value is null
* - New Value - Old Value if Number
* - otherwise null
* @param index index
* @return new - old or null if not appropriate or not changed
*/
public final Object get_ValueDifference (int index)
{
if (index < 0 || index >= get_ColumnCount())
{
log.log(Level.WARNING, "Index invalid - " + index);
return null;
}
Object nValue = m_newValues[index];
// No new Value or NULL
if (nValue == null || nValue == Null.NULL)
return null;
//
Object oValue = m_oldValues[index];
if (oValue == null || oValue == Null.NULL)
return nValue;
if (nValue instanceof BigDecimal)
{
BigDecimal obd = (BigDecimal)oValue;
return ((BigDecimal)nValue).subtract(obd);
}
else if (nValue instanceof Integer)
{
int result = ((Integer)nValue).intValue();
result -= ((Integer)oValue).intValue();
return Integer.valueOf(result);
}
//
log.warning("Invalid type - New=" + nValue);
return null;
} // get_ValueDifference
/**
* Return new - old.
* - New Value if Old Value is null
* - New Value - Old Value if Number
* - otherwise null
* @param columnName column name
* @return new - old or null if not appropriate or not changed
*/
public final Object get_ValueDifference (String columnName)
{
int index = get_ColumnIndex(columnName);
if (index < 0)
{
log.log(Level.WARNING, "Column not found - " + get_TableName() + "." + columnName);
return null;
}
return get_ValueDifference (index);
} // get_ValueDifference
/**************************************************************************
* Set Value
* @param ColumnName column name
* @param value value
* @return true if value set
*/
protected final boolean set_Value (String ColumnName, Object value)
{
return set_Value(ColumnName, value, true);
}
/**************************************************************************
* Set Value
* @param ColumnName column name
* @param value value
* @param checkWritable
* @return true if value set
*/
protected final boolean set_Value (String ColumnName, Object value, boolean checkWritable)
{
checkImmutable();
if (value instanceof String && ColumnName.equals("WhereClause")
&& value.toString().toUpperCase().indexOf("=NULL") != -1)
log.warning("Invalid Null Value - " + ColumnName + "=" + value);
int index = get_ColumnIndex(ColumnName);
if (index < 0)
{
log.log(Level.SEVERE, "Column not found - " + get_TableName() + "." + ColumnName);
log.saveError("ColumnNotFound", get_TableName() + "." + ColumnName);
return false;
}
if (ColumnName.endsWith("_ID") && value instanceof String )
{
// Convert to Integer only if info class is Integer - teo_sarca [ 2859125 ]
Class<?> clazz = p_info.getColumnClass(p_info.getColumnIndex(ColumnName));
if (Integer.class == clazz)
{
log.severe("Invalid Data Type for " + ColumnName + "=" + value);
value = Integer.parseInt((String)value);
}
}
return set_Value (index, value, checkWritable);
} // setValue
/**
* Set Encrypted Value
* @param ColumnName column name
* @param value value
* @return true if value set
*/
protected final boolean set_ValueE (String ColumnName, Object value)
{
return set_Value (ColumnName, value);
} // setValueE
/**
* Set Value if updateable and correct class.
* (and to NULL if not mandatory)
* @param index index
* @param value value
* @return true if value set
*/
protected final boolean set_Value (int index, Object value)
{
return set_Value(index, value, true);
}
/**
* Set Value if updateable and correct class.
* (and to NULL if not mandatory)
* @param index index
* @param value value
* @param checkWritable
* @return true if value set
*/
protected final boolean set_Value (int index, Object value, boolean checkWritable)
{
checkImmutable();
if (index < 0 || index >= get_ColumnCount())
{
log.log(Level.WARNING, "Index invalid - " + index);
return false;
}
String ColumnName = p_info.getColumnName(index);
String colInfo = " - " + ColumnName;
//
m_setErrors[index] = null;
if (checkWritable)
{
if (p_info.isVirtualColumn(index))
{
log.log(Level.WARNING, "Virtual Column" + colInfo);
log.saveError("VirtualColumn", "Virtual Column" + colInfo);
m_setErrors[index] = new ValueNamePair("VirtualColumn", "Virtual Column" + colInfo);
m_setErrorsFilled = true;
return false;
}
//
// globalqss -- Bug 1618469 - is throwing not updateable even on new records
if ( ( ! p_info.isColumnUpdateable(index) ) && ( ! is_new() ) )
{
colInfo += " - NewValue=" + value + " - OldValue=" + get_Value(index);
log.log(Level.WARNING, "Column not updateable" + colInfo);
log.saveError("ColumnReadonly", "Column not updateable" + colInfo);
m_setErrors[index] = new ValueNamePair("ColumnReadonly", "Column not updateable" + colInfo);
m_setErrorsFilled = true;
return false;
}
}
//
if (value == null)
{
if (checkWritable && p_info.isColumnMandatory(index))
{
log.saveError("FillMandatory", ColumnName);
m_setErrors[index] = new ValueNamePair("FillMandatory", ColumnName);
m_setErrorsFilled = true;
return false;
}
m_newValues[index] = Null.NULL; // correct
if (log.isLoggable(Level.FINER)) log.finer(ColumnName + " = null");
}
else
{
// matching class or generic object
if (value.getClass().equals(p_info.getColumnClass(index))
|| p_info.getColumnClass(index) == Object.class)
m_newValues[index] = value; // correct
// Integer can be set as BigDecimal
else if (value.getClass() == BigDecimal.class
&& p_info.getColumnClass(index) == Integer.class)
m_newValues[index] = Integer.valueOf(((BigDecimal)value).intValue());
// Set Boolean
else if (p_info.getColumnClass(index) == Boolean.class
&& ("Y".equals(value) || "N".equals(value)) )
m_newValues[index] = Boolean.valueOf("Y".equals(value));
// added by vpj-cd
// To solve BUG [ 1618423 ] Set Project Type button in Project window throws warning
// generated because C_Project.C_Project_Type_ID is defined as button in dictionary
// although is ID (integer) in database
else if (value.getClass() == Integer.class
&& p_info.getColumnClass(index) == String.class)
m_newValues[index] = value;
else if (value.getClass() == String.class
&& p_info.getColumnClass(index) == Integer.class)
try
{
m_newValues[index] = Integer.valueOf((String)value);
}
catch (NumberFormatException e)
{
String errmsg = ColumnName
+ " - Class invalid: " + value.getClass().toString()
+ ", Should be " + p_info.getColumnClass(index).toString() + ": " + value;
log.log(Level.SEVERE, errmsg);
log.saveError("WrongDataType", errmsg);
m_setErrors[index] = new ValueNamePair("WrongDataType", errmsg);
m_setErrorsFilled = true;
return false;
}
else
{
String errmsg = ColumnName
+ " - Class invalid: " + value.getClass().toString()
+ ", Should be " + p_info.getColumnClass(index).toString() + ": " + value;
log.log(Level.SEVERE, errmsg);
log.saveError("WrongDataType", errmsg);
m_setErrors[index] = new ValueNamePair("WrongDataType", errmsg);
m_setErrorsFilled = true;
return false;
}
// Validate (Min/Max)
String error = p_info.validate(index, value);
if (error != null)
{
log.log(Level.WARNING, ColumnName + "=" + value + " - " + error);
int separatorIndex = error.indexOf(";");
if (separatorIndex > 0) {
log.saveError(error.substring(0,separatorIndex), error.substring(separatorIndex+1));
m_setErrors[index] = new ValueNamePair(error.substring(0,separatorIndex), error.substring(separatorIndex+1));
} else {
log.saveError(error, ColumnName);
m_setErrors[index] = new ValueNamePair(error, ColumnName);
}
m_setErrorsFilled = true;
return false;
}
// Length for String
if (p_info.getColumnClass(index) == String.class)
{
String stringValue = value.toString();
int length = p_info.getFieldLength(index);
if (stringValue.length() > length && length > 0)
{
log.warning(ColumnName + " - Value too long - truncated to length=" + length);
m_newValues[index] = stringValue.substring(0,length);
}
}
// Validate reference list [1762461]
if (p_info.getColumn(index).DisplayType == DisplayType.List &&
p_info.getColumn(index).AD_Reference_Value_ID > 0 &&
value instanceof String) {
if (MRefList.get(getCtx(), p_info.getColumn(index).AD_Reference_Value_ID,
(String) value, get_TrxName()) != null)
;
else {
StringBuilder validValues = new StringBuilder();
for (ValueNamePair vp : MRefList.getList(getCtx(), p_info.getColumn(index).AD_Reference_Value_ID, false))
validValues.append(" - ").append(vp.getValue());
String errmsg = ColumnName + " Invalid value - "
+ value + " - Reference_ID=" + p_info.getColumn(index).AD_Reference_Value_ID + validValues.toString();
log.saveError("Validate", errmsg);
m_setErrors[index] = new ValueNamePair("Validate", errmsg);
m_setErrorsFilled = true;
return false;
}
}
if (log.isLoggable(Level.FINEST)) log.finest(ColumnName + " = " + m_newValues[index] + " (OldValue="+m_oldValues[index]+")");
}
set_Keys (ColumnName, m_newValues[index]);
// FR 2962094 Fill ProcessedOn when the Processed column is changing from N to Y
setProcessedOn(ColumnName, value, m_oldValues[index]);
return true;
} // setValue
/* FR 2962094 - Finish implementation of weighted average costing
Fill the column ProcessedOn (if it exists) with a bigdecimal representation of current timestamp (with nanoseconds)
*/
public void setProcessedOn(String ColumnName, Object value, Object oldValue) {
checkImmutable();
if ("Processed".equals(ColumnName)
&& value instanceof Boolean
&& ((Boolean)value).booleanValue() == true
&& (oldValue == null
|| (oldValue instanceof Boolean
&& ((Boolean)oldValue).booleanValue() == false))) {
if (get_ColumnIndex("ProcessedOn") > 0) {
// fill processed on column
//get current time from db
Timestamp ts = DB.getSQLValueTS(null, "SELECT CURRENT_TIMESTAMP FROM DUAL");
long mili = ts.getTime();
int nano = ts.getNanos();
double doublets = Double.parseDouble(Long.toString(mili) + "." + Integer.toString(nano));
BigDecimal bdtimestamp = BigDecimal.valueOf(doublets);
set_Value("ProcessedOn", bdtimestamp);
}
}
}
/**
* Set Value w/o check (update, r/o, ..).
* Used when Column is R/O
* Required for key and parent values