KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > POInfo


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17 import java.sql.*;
18 import java.math.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Persistet Object Info.
24  * Provides structural information
25  *
26  * @author Jorg Janke
27  * @version $Id: POInfo.java,v 1.12 2003/11/01 02:33:35 jjanke Exp $
28  */

29 public class POInfo
30 {
31     /**
32      * POInfo Factory
33      * @param ctx context
34      * @param AD_Table_ID AD_Table_ID
35      * @return POInfo
36      */

37     public static POInfo getPOInfo (Properties ctx, int AD_Table_ID)
38     {
39         Integer JavaDoc key = new Integer JavaDoc(AD_Table_ID);
40         POInfo retValue = (POInfo)s_cache.get(key);
41         if (retValue == null)
42         {
43             retValue = new POInfo(ctx, AD_Table_ID);
44             if (retValue.getColumnCount() > 0)
45                 s_cache.put(key, retValue);
46         }
47         return retValue;
48     } // getPOInfo
49

50     /** Cache of POInfo */
51     private static CCache s_cache = new CCache("poInfo", 300);
52
53     /*************************************************************************/
54
55     /**
56      * Create Persistent Info
57      * @param ctx context
58      * @param AD_Table_ID AD_ Table_ID
59      */

60     private POInfo (Properties ctx, int AD_Table_ID)
61     {
62         m_ctx = ctx;
63         m_AD_Table_ID = AD_Table_ID;
64         loadInfo ();
65     } // PInfo
66

67     /** Context */
68     private Properties m_ctx = null;
69     /** Table_ID */
70     private int m_AD_Table_ID = 0;
71     /** Table Name */
72     private String JavaDoc m_TableName = null;
73     /** Columns */
74     private Column[] m_columns = null;
75
76     private Logger log = Logger.getCLogger(getClass());
77
78     /**
79      * Load Table/Column Info
80      */

81     private void loadInfo()
82     {
83         ArrayList list = new ArrayList(15);
84         boolean english = Env.isBaseLanguage(m_ctx, "AD_Table");
85         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
86         sql.append("SELECT t.TableName, c.ColumnName,c.AD_Reference_ID," // 1..3
87
+ "c.IsMandatory,c.IsUpdateable,c.DefaultValue," // 4..6
88
+ "e.Name,e.Description, c.AD_Column_ID, " // 7..9
89
+ "c.IsKey,c.IsParent, " // 10..11
90
+ "c.AD_Reference_Value_ID, vr.Code, " // 12..13
91
+ "c.FieldLength, c.ValueMin, c.ValueMax "); // 14..16
92
sql.append("FROM AD_Table t"
93             + " INNER JOIN AD_Column c ON (t.AD_Table_ID=c.AD_Table_ID)"
94             + " LEFT OUTER JOIN AD_Val_Rule vr ON (c.AD_Val_Rule_ID=vr.AD_Val_Rule_ID)"
95             + " INNER JOIN AD_Element");
96         if (!english)
97             sql.append("_Trl");
98         sql.append(" e "
99             + " ON (c.AD_Element_ID=e.AD_Element_ID) "
100             + "WHERE t.AD_Table_ID=?"
101             + " AND c.IsActive='Y'");
102         if (!english)
103             sql.append(" AND e.AD_Language='").append(Env.getAD_Language(m_ctx)).append("'");
104         //
105
try
106         {
107             PreparedStatement pstmt = DB.prepareStatement(sql.toString());
108             pstmt.setInt(1, m_AD_Table_ID);
109             ResultSet rs = pstmt.executeQuery();
110             while (rs.next())
111             {
112                 if (m_TableName == null)
113                     m_TableName = rs.getString(1);
114                 String JavaDoc ColumnName = rs.getString(2);
115                 int AD_Reference_ID = rs.getInt(3);
116                 boolean IsMandatory = "Y".equals(rs.getString(4));
117                 boolean IsUpdateable = "Y".equals(rs.getString(5));
118                 String JavaDoc DefaultLogic = rs.getString(6);
119                 String JavaDoc Name = rs.getString(7);
120                 String JavaDoc Description = rs.getString(8);
121                 int AD_Column_ID = rs.getInt(9);
122                 boolean IsKey = "Y".equals(rs.getString(10));
123                 boolean IsParent = "Y".equals(rs.getString(11));
124                 int AD_Reference_Value_ID = rs.getInt(12);
125                 String JavaDoc ValidationCode = rs.getString(13);
126                 int FieldLength = rs.getInt(14);
127                 String JavaDoc ValueMin = rs.getString(15);
128                 String JavaDoc ValueMax = rs.getString(16);
129
130                 Column col = new Column (
131                     AD_Column_ID, ColumnName, AD_Reference_ID,
132                     IsMandatory, IsUpdateable,
133                     DefaultLogic, Name, Description,
134                     IsKey, IsParent,
135                     AD_Reference_Value_ID, ValidationCode,
136                     FieldLength, ValueMin, ValueMax);
137                 list.add(col);
138             }
139             rs.close();
140             pstmt.close();
141         }
142         catch (SQLException e)
143         {
144             log.error("POInfo.loadInfo - " + sql.toString(), e);
145         }
146         // convert to array
147
m_columns = new Column[list.size()];
148         list.toArray(m_columns);
149     } // loadInfo
150

151     /**
152      * String representation
153      * @return String Representation
154      */

155     public String JavaDoc toString()
156     {
157         return "POInfo[" + getTableName() + ",AD_Table_ID=" + getAD_Table_ID() + "]";
158     } // toString
159

160     /**
161      * String representation for index
162      * @param index column index
163      * @return String Representation
164      */

165     public String JavaDoc toString (int index)
166     {
167         if (index < 0 || index >= m_columns.length)
168             return "POInfo[" + getTableName() + "-InvalidIndex]";
169         return "POInfo[" + getTableName() + "-" + m_columns[index].toString() + "]";
170     } // toString
171

172     /**
173      * Get Table Name
174      * @return Table Name
175      */

176     public String JavaDoc getTableName()
177     {
178         return m_TableName;
179     } // getTableName
180

181     /**
182      * Get AD_Table_ID
183      * @return AD_Table_ID
184      */

185     public int getAD_Table_ID()
186     {
187         return m_AD_Table_ID;
188     } // getAD_Table_ID
189

190     /*************************************************************************/
191
192     /**
193      * Get ColumnCount
194      * @return column count
195      */

196     public int getColumnCount()
197     {
198         return m_columns.length;
199     } // getColumnCount
200

201     /**
202      * Get ColumnIndex
203      * @param ColumnName column name
204      * @return index of column with ColumnName or -1 if not found
205      */

206     public int getColumnIndex (String JavaDoc ColumnName)
207     {
208         for (int i = 0; i < m_columns.length; i++)
209         {
210             if (ColumnName.equals(m_columns[i].ColumnName))
211                 return i;
212         }
213         return -1;
214     } // getColumnIndex
215

216     /**
217      * Get Column Name
218      * @param index index
219      * @return ColumnName column name
220      */

221     public String JavaDoc getColumnName (int index)
222     {
223         if (index < 0 || index >= m_columns.length)
224             return null;
225         return m_columns[index].ColumnName;
226     } // getColumnName
227

228     /**
229      * Get Column Label
230      * @param index index
231      * @return column label
232      */

233     public String JavaDoc getColumnLabel (int index)
234     {
235         if (index < 0 || index >= m_columns.length)
236             return null;
237         return m_columns[index].ColumnLabel;
238     } // getColumnLabel
239

240     /**
241      * Get Column Description
242      * @param index index
243      * @returncolumn description
244      */

245     public String JavaDoc getColumnDescription (int index)
246     {
247         if (index < 0 || index >= m_columns.length)
248             return null;
249         return m_columns[index].ColumnDescription;
250     } // getColumnDescription
251

252     /**
253      * Get Column Class
254      * @param index index
255      * @return Class
256      */

257     public Class JavaDoc getColumnClass (int index)
258     {
259         if (index < 0 || index >= m_columns.length)
260             return null;
261         return m_columns[index].ColumnClass;
262     } // getColumnClass
263

264     /**
265      * Get Column Display Type
266      * @param index index
267      * @return DisplayType
268      */

269     public int getColumnDisplayType (int index)
270     {
271         if (index < 0 || index >= m_columns.length)
272             return DisplayType.String;
273         return m_columns[index].DisplayType;
274     } // getColumnDisplayType
275

276     /**
277      * Get Column Default Logic
278      * @param index index
279      * @return Default Logic
280      */

281     public String JavaDoc getDefaultLogic (int index)
282     {
283         if (index < 0 || index >= m_columns.length)
284             return null;
285         return m_columns[index].DefaultLogic;
286     } // getDefaultLogic
287

288     /**
289      * Is Column Mandatory
290      * @param index index
291      * @return true if column mandatory
292      */

293     public boolean isColumnMandatory (int index)
294     {
295         if (index < 0 || index >= m_columns.length)
296             return false;
297         return m_columns[index].IsMandatory;
298     } // isMandatory
299

300     /**
301      * Is Column Updateable
302      * @param index index
303      * @return true if column updateable
304      */

305     public boolean isColumnUpdateable (int index)
306     {
307         if (index < 0 || index >= m_columns.length)
308             return false;
309         return m_columns[index].IsUpdateable;
310     } // isUpdateable
311

312     /**
313      * Set Column Updateable
314      * @param index index
315      * @param updateable column updateable
316      */

317     public void setColumnUpdateable (int index, boolean updateable)
318     {
319         if (index < 0 || index >= m_columns.length)
320             return;
321         m_columns[index].IsUpdateable = updateable;
322     } // setColumnUpdateable
323

324     /**
325      * Set all columns updateable
326      * @param updateable updateable
327      */

328     public void setUpdateable (boolean updateable)
329     {
330         for (int i = 0; i < m_columns.length; i++)
331             m_columns[i].IsUpdateable = updateable;
332     } // setUpdateable
333

334     /**
335      * Is Lookup Column
336      * @param index index
337      * @return true if it is a lookup column
338      */

339     public boolean isColumnLookup (int index)
340     {
341         if (index < 0 || index >= m_columns.length)
342             return false;
343         return DisplayType.isLookup(m_columns[index].DisplayType);
344     } // isColumnLookup
345

346     /**
347      * Get Lookup
348      * @param index index
349      * @return Lookup
350      */

351     public Lookup getColumnLookup (int index)
352     {
353         if (!isColumnLookup(index))
354             return null;
355         //
356
int WindowNo = 0;
357         // List, Table, TableDir
358
Lookup lookup = null;
359         try
360         {
361             lookup = MLookupFactory.get (m_ctx, WindowNo,
362                 m_columns[index].AD_Column_ID, m_columns[index].DisplayType,
363                 Env.getLanguage(m_ctx), m_columns[index].ColumnName,
364                 m_columns[index].AD_Reference_Value_ID,
365                 m_columns[index].IsParent, m_columns[index].ValidationCode);
366         }
367         catch (Exception JavaDoc e)
368         {
369             lookup = null; // cannot create Lookup
370
}
371         return lookup;
372         /** @todo other lookup types */
373     } // getColumnLookup
374

375     /**
376      * Is Column Key
377      * @param index index
378      * @return true if column is the key
379      */

380     public boolean isColumnKey (int index)
381     {
382         if (index < 0 || index >= m_columns.length)
383             return false;
384         return m_columns[index].IsKey;
385     } // isColumnKey
386

387     /**
388      * Is Column Parent
389      * @param index index
390      * @return true if column is a Parent
391      */

392     public boolean isColumnParent (int index)
393     {
394         if (index < 0 || index >= m_columns.length)
395             return false;
396         return m_columns[index].IsParent;
397     } // isColumnParent
398

399     /**
400      * Get Column FieldLength
401      * @param index index
402      * @return field length
403      */

404     public int getFieldLength (int index)
405     {
406         if (index < 0 || index >= m_columns.length)
407             return 0;
408         return m_columns[index].FieldLength;
409     } // getFieldLength
410

411     /**
412      * Validate Content
413      * @param index index
414      * @param value new Value
415      * @return null if all valid otherwise error message
416      */

417     public String JavaDoc validate (int index, Object JavaDoc value)
418     {
419         if (index < 0 || index >= m_columns.length)
420             return "RangeError";
421         // Mandatory (i.e. not null
422
if (m_columns[index].IsMandatory && value == null)
423         {
424             return "IsMandatory";
425         }
426         // Length ignored
427

428         //
429
if (m_columns[index].ValueMin != null)
430         {
431             BigDecimal value_BD = null;
432             try
433             {
434                 if (m_columns[index].ValueMin_BD != null)
435                     value_BD = new BigDecimal(value.toString());
436             }
437             catch (Exception JavaDoc ex){}
438             // Both are Numeric
439
if (m_columns[index].ValueMin_BD != null && value_BD != null)
440             { // error: 1 - 0 => 1 - OK: 1 - 1 => 0 & 1 - 10 => -1
441
int comp = m_columns[index].ValueMin_BD.compareTo(value_BD);
442                 if (comp > 0)
443                 {
444                     return "MinValue=" + m_columns[index].ValueMin_BD + "(" + m_columns[index].ValueMin + ")"
445                       + " - compared with Numeric Value=" + value_BD + "(" + value + ")"
446                       + " - results in " + comp;
447                 }
448             }
449             else // String
450
{
451                 int comp = m_columns[index].ValueMin.compareTo(value);
452                 if (comp > 0)
453                 {
454                     return "MinValue=" + m_columns[index].ValueMin
455                       + " - compared with String Value=" + value
456                       + " - results in " + comp;
457                 }
458             }
459         }
460         if (m_columns[index].ValueMax != null)
461         {
462             BigDecimal value_BD = null;
463             try
464             {
465                 if (m_columns[index].ValueMax_BD != null)
466                     value_BD = new BigDecimal(value.toString());
467             }
468             catch (Exception JavaDoc ex){}
469             // Both are Numeric
470
if (m_columns[index].ValueMax_BD != null && value_BD != null)
471             { // error 12 - 20 => -1 - OK: 12 - 12 => 0 & 12 - 10 => 1
472
int comp = m_columns[index].ValueMax_BD.compareTo(value_BD);
473                 if (comp < 0)
474                 {
475                     return "MaxValue=" + m_columns[index].ValueMax_BD + "(" + m_columns[index].ValueMax + ")"
476                       + " - compared with Numeric Value=" + value_BD + "(" + value + ")"
477                       + " - results in " + comp;
478                 }
479             }
480             else // String
481
{
482                 int comp = m_columns[index].ValueMax.compareTo(value);
483                 if (comp < 0)
484                 {
485                     return "MaxValue=" + m_columns[index].ValueMax
486                       + " - compared with String Value=" + value
487                       + " - results in " + comp;
488                 }
489             }
490         }
491         return null;
492     } // validate
493

494     /*************************************************************************/
495
496     /**
497      * Column Info Vlaue Object
498      */

499     class Column
500     {
501         /**
502          * Constructor
503          * @param ad_Column_ID Column ID
504          * @param columnName Dolumn name
505          * @param displayType Display Type
506          * @param isMandatory Mandatory
507          * @param isUpdateable Updateable
508          * @param defaultLogic Default Logic
509          * @param columnLabel Column Label
510          * @param columnDescription Column Description
511          * @param isKey true if key
512          * @param isParent true if parent
513          * @param ad_Reference_Value_ID reference value
514          * @param validationCode sql validation code
515          * @param fieldLength Field Length
516          * @param valueMin minimal value
517          * @param valueMax maximal value
518          */

519         Column (int ad_Column_ID, String JavaDoc columnName, int displayType,
520             boolean isMandatory, boolean isUpdateable, String JavaDoc defaultLogic,
521             String JavaDoc columnLabel, String JavaDoc columnDescription,
522             boolean isKey, boolean isParent,
523             int ad_Reference_Value_ID, String JavaDoc validationCode,
524             int fieldLength, String JavaDoc valueMin, String JavaDoc valueMax)
525         {
526             AD_Column_ID = ad_Column_ID;
527             ColumnName = columnName;
528             DisplayType = displayType;
529             if (columnName.equals("AD_Language"))
530             {
531                 DisplayType = org.compiere.util.DisplayType.String;
532                 ColumnClass = String JavaDoc.class;
533             }
534             else if (columnName.equals("Posted"))
535             {
536                 ColumnClass = Boolean JavaDoc.class;
537             }
538             else if (columnName.equals("Record_ID"))
539             {
540                 DisplayType = org.compiere.util.DisplayType.ID;
541                 ColumnClass = Integer JavaDoc.class;
542             }
543             else
544                 ColumnClass = org.compiere.util.DisplayType.getClass(displayType, true);
545             IsMandatory = isMandatory;
546             IsUpdateable = isUpdateable;
547             DefaultLogic = defaultLogic;
548             ColumnLabel = columnLabel;
549             ColumnDescription = columnDescription;
550             IsKey = isKey;
551             IsParent = isParent;
552             //
553
AD_Reference_Value_ID = ad_Reference_Value_ID;
554             ValidationCode = validationCode;
555             //
556
FieldLength = fieldLength;
557             ValueMin = valueMin;
558             try
559             {
560                 if (valueMin != null && valueMin.length() > 0)
561                     ValueMin_BD = new BigDecimal(valueMin);
562             }
563             catch (Exception JavaDoc ex)
564             {
565                 log.error("Column - ValueMin=" + valueMin, ex);
566             }
567             ValueMax = valueMax;
568             try
569             {
570                 if (valueMax != null && valueMax.length() > 0)
571                     ValueMax_BD = new BigDecimal(valueMax);
572             }
573             catch (Exception JavaDoc ex)
574             {
575                 log.error("Column - ValueMax=" + valueMax, ex);
576             }
577         } // Column
578

579         public int AD_Column_ID;
580         public String JavaDoc ColumnName;
581         public int DisplayType;
582         public Class JavaDoc ColumnClass;
583         public boolean IsMandatory;
584         public String JavaDoc DefaultLogic;
585         public boolean IsUpdateable;
586         public String JavaDoc ColumnLabel;
587         public String JavaDoc ColumnDescription;
588         public boolean IsKey;
589         public boolean IsParent;
590         //
591
public int AD_Reference_Value_ID;
592         public String JavaDoc ValidationCode;
593         //
594
public int FieldLength;
595         public String JavaDoc ValueMin;
596         public String JavaDoc ValueMax;
597         public BigDecimal ValueMin_BD = null;
598         public BigDecimal ValueMax_BD = null;
599
600         /**
601          * String representation
602          * @return info
603          */

604         public String JavaDoc toString()
605         {
606             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("POInfo.Column[");
607             sb.append(ColumnName).append(",ID=").append(AD_Column_ID)
608                 .append(",DisplayType=").append(DisplayType)
609                 .append(",ColumnClass=").append(ColumnClass);
610             sb.append("]");
611             return sb.toString();
612         } // toString
613
} // Column
614
} // POInfo
615
Popular Tags