KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > impexp > ImpFormatRow


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.impexp;
15
16 import java.math.*;
17 import java.sql.*;
18 import java.text.*;
19
20 import org.compiere.util.*;
21 import org.compiere.model.*;
22
23 /**
24  * Import Format Row with pasing capability
25  *
26  * @author Jorg Janke
27  * @version $Id: ImpFormatRow.java,v 1.6 2003/01/18 04:42:22 jjanke Exp $
28  */

29 public final class ImpFormatRow
30 {
31     /**
32      * Constructor for fixed format
33      * @param seqNo sequence
34      * @param columnName db dolumn name
35      * @param startNo start no
36      * @param endNo and no
37      * @param dataType data type - see constants DATATYPE_
38      * @param maxLength if String it is the maximum length (truncated)
39      */

40     public ImpFormatRow(int seqNo, String JavaDoc columnName, int startNo, int endNo, String JavaDoc dataType, int maxLength)
41     {
42         m_seqNo = seqNo;
43         setColumnName(columnName);
44         m_startNo = startNo;
45         m_endNo = endNo;
46         setDataType (dataType);
47         setMaxLength (maxLength);
48     } // ImpFormatRow
49

50     /**
51      * Constructor for non-fixed format
52      * @param seqNo sequence
53      * @param columnName db column name
54      * @param dataType data type - see constants DATATYPE_
55      * @param maxLength if String it is the maximum length (truncated)
56      */

57     public ImpFormatRow(int seqNo, String JavaDoc columnName, String JavaDoc dataType, int maxLength)
58     {
59         m_seqNo = seqNo;
60         setColumnName(columnName);
61         setDataType (dataType);
62         setMaxLength (maxLength);
63     } // ImpFormatRow
64

65     private int m_seqNo;
66     private String JavaDoc m_columnName;
67     private int m_startNo = 0;
68     private int m_endNo = 0;
69     private String JavaDoc m_dataType;
70     private String JavaDoc m_dataFormat = "";
71     private String JavaDoc m_decimalPoint = ".";
72     private boolean m_divideBy100 = false;
73     private String JavaDoc m_constantValue = "";
74     private boolean m_constantIsString = true;
75     private String JavaDoc m_callout = "";
76     private SimpleDateFormat m_dformat = null;
77     private int m_maxLength = 0;
78
79     /**
80      * Sequence No
81      * @return seq no
82      */

83     public int getSeqNo ()
84     {
85         return m_seqNo;
86     } // getSeqNo
87

88     /**
89      * Set Sequence No
90      * @param newSeqNo sequence
91      */

92     public void setSeqNo (int newSeqNo)
93     {
94         m_seqNo = newSeqNo;
95     } // setSeqNo
96

97     /**
98      * Start Position
99      * @param newStartNo start position
100      */

101     public void setStartNo (int newStartNo)
102     {
103         m_startNo = newStartNo;
104     } // setStartNo
105

106     /**
107      * Get Start Position
108      * @return start position
109      */

110     public int getStartNo()
111     {
112         return m_startNo;
113     } // getStartNo
114

115     /**
116      * End Position
117      * @param newEndNo end position
118      */

119     public void setEndNo (int newEndNo)
120     {
121         m_endNo = newEndNo;
122     } // setEndNo
123

124     /**
125      * Get End Position
126      * @return End Position
127      */

128     public int getEndNo ()
129     {
130         return m_endNo;
131     } // getEndNo
132

133     /**
134      * Column
135      * @param columnName column name
136      */

137     public void setColumnName (String JavaDoc columnName)
138     {
139         if (columnName == null || columnName.length() == 0)
140             throw new IllegalArgumentException JavaDoc("ColumnName must be at least 1 char");
141         else
142             m_columnName = columnName;
143     } // setColumnName
144

145     /**
146      * Get Column Name
147      * @return Column Name
148      */

149     public String JavaDoc getColumnName()
150     {
151         return m_columnName;
152     } // getColumnName
153

154     /**
155      * Data Type
156      * @param dataType data type - see constants DATATYPE_
157      */

158     public void setDataType (String JavaDoc dataType)
159     {
160         if (dataType.equals(DATATYPE_String) || dataType.equals(DATATYPE_Date)
161             || dataType.equals(DATATYPE_Number) || dataType.equals(DATATYPE_Constant))
162             m_dataType = dataType;
163         else
164             throw new IllegalArgumentException JavaDoc("DataType must be S/D/N/C");
165     } // setDataType
166

167     /** String Data type */
168     public static final String JavaDoc DATATYPE_String = "S";
169     /** Data Data type */
170     public static final String JavaDoc DATATYPE_Date = "D";
171     /** Numeric Data type */
172     public static final String JavaDoc DATATYPE_Number = "N";
173     /** Constant Data type */
174     public static final String JavaDoc DATATYPE_Constant = "C";
175
176     /**
177      * Data Type
178      * @return data type
179      */

180     public String JavaDoc getDataType()
181     {
182         return m_dataType;
183     } // getDataType
184

185     /**
186      * Is String
187      * @return true if data type is String
188      */

189     public boolean isString()
190     {
191         if (m_dataType.equals(DATATYPE_Constant))
192             return m_constantIsString;
193         return m_dataType.equals(DATATYPE_String);
194     } // isString
195

196     /**
197      * Is Number
198      * @return true if data type is Number
199      */

200     public boolean isNumber()
201     {
202         return m_dataType.equals(DATATYPE_Number);
203     }
204
205     /**
206      * Is Date
207      * @return true if data type is Date
208      */

209     public boolean isDate()
210     {
211         return m_dataType.equals(DATATYPE_Date);
212     }
213
214     /**
215      * Is Constant
216      * @return true if data type is Constant
217      */

218     public boolean isConstant()
219     {
220         return m_dataType.equals(DATATYPE_Constant);
221     }
222
223     /**
224      * Set Format Info
225      * @param dataFormat data format - see constants DATATYPE_
226      * @param decimalPoint decimal point representation
227      * @param divideBy100 divide number by 100
228      * @param constantValue constant value
229      * @param callout Java callout
230      */

231     public void setFormatInfo (String JavaDoc dataFormat, String JavaDoc decimalPoint, boolean divideBy100,
232         String JavaDoc constantValue, String JavaDoc callout)
233     {
234         if (dataFormat == null)
235             m_dataFormat = "";
236         else
237             m_dataFormat = dataFormat;
238         // number
239
if (decimalPoint == null || !decimalPoint.equals(","))
240             m_decimalPoint = ".";
241         else
242             m_decimalPoint = ",";
243         m_divideBy100 = divideBy100;
244         // constant
245
if (constantValue == null || constantValue.length() == 0 || !m_dataType.equals(DATATYPE_Constant))
246         {
247             m_constantValue = "";
248             m_constantIsString = true;
249         }
250         else
251         {
252             m_constantValue = constantValue;
253             m_constantIsString = false;
254             for (int i = 0; i < m_constantValue.length(); i++)
255             {
256                 char c = m_constantValue.charAt(i);
257                 if (!(Character.isDigit(c) || c == '.')) // if a constant number, it must be with . (not ,)
258
{
259                     m_constantIsString = true;
260                     break;
261                 }
262             }
263         }
264         // callout
265
if (callout == null)
266             m_callout = "";
267         else
268             m_callout = callout;
269     } // setFormatInfo
270

271     /**
272      * Get Format
273      * @return Data Format
274      */

275     public String JavaDoc getDataFormat()
276     {
277         return m_dataFormat;
278     }
279
280     /**
281      * Get Decimal Point
282      * @return Decimal Point
283      */

284     public String JavaDoc getDecimalPoint()
285     {
286         return m_decimalPoint;
287     }
288
289     /**
290      * Divide result by 100
291      * @return true if result will be divided by 100
292      */

293     public boolean isDivideBy100()
294     {
295         return m_divideBy100;
296     }
297
298     /**
299      * Get the constant value
300      * @return constant value
301      */

302     public String JavaDoc getConstantValue()
303     {
304         return m_constantValue;
305     }
306
307     /**
308      * Get Callout
309      * @return callout
310      */

311     public String JavaDoc getCallout()
312     {
313         return m_callout;
314     } // getCallout
315

316     /**
317      * Set maximum length for Strings (truncated).
318      * Ignored, if 0
319      * @param maxLength max length
320      */

321     public void setMaxLength (int maxLength)
322     {
323         m_maxLength = maxLength;
324     } // setMaxLength
325

326     /*************************************************************************/
327
328     /**
329      * Parse value.
330      * Field content in [] are treated as comments
331      * @param info data item
332      * @return pased info
333      */

334     public String JavaDoc parse (String JavaDoc info)
335     {
336         if (info == null || info.length() == 0)
337             return "";
338
339         // Comment ?
340
if (info.startsWith("[") && info.endsWith("]"))
341             return "";
342         //
343
String JavaDoc retValue = null;
344         if (isNumber())
345             retValue = parseNumber (info);
346         else if (isDate())
347             retValue = parseDate (info);
348         else if (isConstant())
349             retValue = m_constantIsString ? parseString (m_constantValue) : m_constantValue;
350         else
351             retValue = parseString (info);
352         //
353
if (m_callout.length() > 2)
354         {
355             Callout call = null;
356             String JavaDoc method = m_callout.substring(2);
357
358             if (m_callout.charAt(0) == 'S' && m_callout.charAt(1) == '_')
359             {
360                 call = new CalloutSystem();
361             }
362             else
363             {
364                 try
365                 {
366                     Class JavaDoc cClass = Class.forName("com.compiere.custom.CalloutUser");
367                     call = (Callout)cClass.newInstance();
368                 }
369                 catch (Exception JavaDoc e)
370                 {
371                     Log.error("ImpFormatRow.parse", e);
372                 }
373             }
374             if (call == null)
375                 Log.error("ImpFormatRow.parse - No Class");
376             else
377                 retValue = call.convert (method, retValue);
378         }
379         //
380
if (retValue == null)
381             retValue = "";
382         return retValue.trim();
383     } // parse
384

385     /**
386      * Return date as YYYY-MM-DD HH24:MI:SS (JDBC Timestamp format w/o miliseconds)
387      * @param info data
388      * @return date as JDBC format String
389      */

390     private String JavaDoc parseDate (String JavaDoc info)
391     {
392         if (m_dformat == null)
393         {
394             try
395             {
396                 m_dformat = new SimpleDateFormat(m_dataFormat);
397             }
398             catch (Exception JavaDoc e)
399             {
400                 Log.error ("ImpFormatRow.parseDate Format=" + m_dataFormat, e);
401             }
402             if (m_dformat == null)
403                 m_dformat = (SimpleDateFormat)DateFormat.getDateInstance();
404             m_dformat.setLenient(true);
405         }
406
407         Timestamp ts = null;
408         try
409         {
410             ts = new Timestamp (m_dformat.parse(info).getTime());
411         }
412         catch (ParseException pe)
413         {
414             Log.error ("ImpFormatRow.parseDate - " + info, pe);
415         }
416         if (ts == null)
417             ts = new Timestamp (System.currentTimeMillis());
418         //
419
String JavaDoc dateString = ts.toString();
420         return dateString.substring(0, dateString.indexOf(".")); // cut off miliseconds
421
} // parseNumber
422

423     /**
424      * Return String.
425      * - clean ' and backslash
426      * - check max length
427      * @param info data
428      * @return info with in SQL format
429      */

430     private String JavaDoc parseString (String JavaDoc info)
431     {
432         String JavaDoc retValue = info;
433         // Length restriction
434
if (m_maxLength > 0 && retValue.length() > m_maxLength)
435             retValue = retValue.substring(0, m_maxLength);
436
437         // copy characters (wee need to look through anyway)
438
StringBuffer JavaDoc out = new StringBuffer JavaDoc(retValue.length());
439         for (int i = 0; i < retValue.length(); i++)
440         {
441             char c = retValue.charAt(i);
442             if (c == '\'')
443                 out.append("''");
444             else if (c == '\\')
445                 out.append("\\\\");
446             else
447                 out.append(c);
448         }
449         return out.toString();
450     } // parseString
451

452     /**
453      * Return number with "." decimal
454      * @param info data
455      * @return converted number
456      */

457     private String JavaDoc parseNumber (String JavaDoc info)
458     {
459         boolean hasPoint = info.indexOf(".") != -1;
460         boolean hasComma = info.indexOf(",") != -1;
461         // delete thousands
462
if (hasComma && m_decimalPoint.equals("."))
463             info = info.replace(',', ' ');
464         if (hasPoint && m_decimalPoint.equals(","))
465             info = info.replace('.', ' ');
466         hasComma = info.indexOf(",") != -1;
467
468         // replace decimal
469
if (hasComma && m_decimalPoint.equals(","))
470             info = info.replace(',', '.');
471
472         // remove everything but digits & '.'
473
char[] charArray = info.toCharArray();
474         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
475         for (int i = 0; i < charArray.length; i++)
476             if (Character.isDigit(charArray[i]) || charArray[i] == '.')
477                 sb.append(charArray[i]);
478
479         if (sb.length() == 0)
480             return "0";
481         BigDecimal bd = new BigDecimal(sb.toString());
482         if (m_divideBy100) // assumed two decimal scale
483
bd = bd.divide(new BigDecimal(100.0), 2, BigDecimal.ROUND_HALF_UP);
484         return bd.toString();
485     } // parseNumber
486

487 } // ImpFormatFow
488
Popular Tags