KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_forms > TranslationHandler


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): Openbravo SL
13   * Contributions are Copyright (C) 2001-2006 Openbravo S.L.
14   ******************************************************************************/

15 package org.openbravo.erpCommon.ad_forms;
16
17 import java.sql.*;
18
19 import org.xml.sax.helpers.*;
20 import org.xml.sax.*;
21
22
23 import org.openbravo.database.ConnectionProvider;
24
25 import org.apache.log4j.Logger;
26 //import org.compiere.util.*;
27

28 /**
29  * SAX Handler for parsing Translation
30  *
31  * @author Jorg Janke
32  * @version $Id: TranslationHandler.java,v 1.5 2003/10/04 03:52:36 jjanke Exp $
33  */

34 public class TranslationHandler extends DefaultHandler
35 {
36     /**
37      * Translation Handler
38      * @param AD_Client_ID only certain client if id >= 0
39      */

40
41   public TranslationHandler (ConnectionProvider cDB) {
42     m_AD_Client_ID = 0;
43     DB = cDB;
44   }
45     public TranslationHandler (int AD_Client_ID, ConnectionProvider cDB)
46     {
47     
48         m_AD_Client_ID = AD_Client_ID;
49     DB = cDB;
50    
51     } // TranslationHandler
52

53   private ConnectionProvider DB;
54
55     /** Client */
56     private int m_AD_Client_ID = -1;
57     /** Language */
58     private String JavaDoc m_AD_Language = null;
59     /** Is Base Language */
60     private boolean m_isBaseLanguage = false;
61     /** Table */
62     private String JavaDoc m_TableName = null;
63     /** Update SQL */
64     private String JavaDoc m_updateSQL = null;
65     /** Current ID */
66     private String JavaDoc m_curID = null;
67     /** Current ColumnName */
68     private String JavaDoc m_curColumnName = null;
69     /** Current Value */
70     private StringBuffer JavaDoc m_curValue = null;
71     /** SQL */
72     private StringBuffer JavaDoc m_sql = null;
73
74     private Timestamp m_time = new Timestamp(System.currentTimeMillis());
75     private int m_updateCount = 0;
76
77  
78  
79     
80   static Logger log4j= Logger.getLogger(TranslationHandler.class);
81
82     /*************************************************************************/
83
84     /**
85      * Receive notification of the start of an element.
86      *
87      * @param uri namespace
88      * @param localName simple name
89      * @param qName qualified name
90      * @param attributes attributes
91      * @throws org.xml.sax.SAXException
92      */

93     public void startElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes)
94         throws org.xml.sax.SAXException JavaDoc
95     {
96         if (qName.equals(Translation.XML_TAG))
97         {
98             m_AD_Language = attributes.getValue(Translation.XML_ATTRIBUTE_LANGUAGE);
99             try
100             {
101                 m_isBaseLanguage = TranslationData.baseLanguage(DB, m_AD_Language).equals("Y");
102        
103             }
104             catch (Exception JavaDoc e)
105             {
106         log4j.error(e.toString());
107             }
108       
109       
110             m_TableName = attributes.getValue(Translation.XML_ATTRIBUTE_TABLE);
111             m_updateSQL = "UPDATE " + m_TableName;
112             if (!m_isBaseLanguage)
113                 m_updateSQL += "_Trl";
114             m_updateSQL += " SET ";
115             if (log4j.isDebugEnabled()) log4j.debug("AD_Language=" + m_AD_Language + ", Base=" + m_isBaseLanguage + ", TableName=" + m_TableName);
116         }
117         else if (qName.equals(Translation.XML_ROW_TAG))
118         {
119             m_curID = attributes.getValue(Translation.XML_ROW_ATTRIBUTE_ID);
120             m_sql = new StringBuffer JavaDoc();
121         }
122         else if (qName.equals(Translation.XML_VALUE_TAG))
123         {
124             m_curColumnName = attributes.getValue(Translation.XML_VALUE_ATTRIBUTE_COLUMN);
125         
126         } else if (qName.equals(Translation.XML_CONTRIB)) {
127       m_AD_Language = attributes.getValue(Translation.XML_ATTRIBUTE_LANGUAGE);
128     }
129         else
130             log4j.error ("startElement - UNKNOWN TAG: " + qName);
131         m_curValue = new StringBuffer JavaDoc();
132     } // startElement
133

134     /**
135      * Receive notification of character data inside an element.
136      *
137      * @param ch buffer
138      * @param start start
139      * @param length length
140      * @throws SAXException
141      */

142     public void characters (char ch[], int start, int length)
143         throws SAXException
144     {
145         m_curValue.append(ch, start, length);
146     } // characters
147

148     /**
149      * Receive notification of the end of an element.
150      * @param uri namespace
151      * @param localName simple name
152      * @param qName qualified name
153      * @throws SAXException
154      */

155     public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
156         throws SAXException
157     {
158     // Log.trace(Log.l6_Database+1, "TranslationHandler.endElement", qName);
159
if (log4j.isDebugEnabled()) log4j.debug("endelement "+qName);
160         if (qName.equals(Translation.XML_TAG))
161         {
162         }
163         else if (qName.equals(Translation.XML_ROW_TAG))
164         {
165             // Set section
166
if (m_sql.length() > 0)
167                 m_sql.append(",");
168             m_sql.append("Updated=now()"); //.append(DB.TO_DATE(m_time, false));
169
if (!m_isBaseLanguage)
170                 m_sql.append(",IsTranslated='Y'");
171             // Where section
172
m_sql.append(" WHERE ")
173                 .append(m_TableName).append("_ID=").append(m_curID);
174             if (!m_isBaseLanguage)
175                 m_sql.append(" AND AD_Language='").append(m_AD_Language).append("'");
176             if (m_AD_Client_ID >= 0)
177                 m_sql.append(" AND AD_Client_ID=").append(m_AD_Client_ID);
178             // Update section
179
m_sql.insert(0, m_updateSQL);
180      if (log4j.isDebugEnabled()) log4j.debug(m_sql.toString());
181             // Execute
182
int no =0;
183     //
184
Statement st = null;
185       try{
186         st = DB.getStatement();
187         no = st.executeUpdate(m_sql.toString());
188       } catch (Exception JavaDoc e){log4j.error("183:"+m_sql.toString()+e.toString());}
189       finally {
190         try {
191           if (st != null) DB.releaseStatement(st);
192         } catch (Exception JavaDoc ignored) {}
193       }
194
195             if (no == 1)
196             {
197                     if (log4j.isDebugEnabled()) log4j.debug(m_sql.toString());
198                 m_updateCount++;
199             }
200             else if (no == 0)
201                 log4j.warn ("Not Found - " + m_sql.toString());
202             else
203                 log4j.error ("Update Rows=" + no + " (Should be 1) - " + m_sql.toString());
204         }
205         else if (qName.equals(Translation.XML_VALUE_TAG))
206         {
207             if (m_sql.length() > 0)
208                 m_sql.append(",");
209         m_sql.append(m_curColumnName).append("=").append(TO_STRING(m_curValue.toString()));
210         } else if (qName.equals(Translation.XML_CONTRIB)) {
211       if (log4j.isDebugEnabled()) log4j.debug("Contibutors:"+TO_STRING(m_curValue.toString()));
212       try {
213         TranslationData.insertContrib(DB, m_curValue.toString(), m_AD_Language);
214       } catch (Exception JavaDoc e) {
215         log4j.error(e.toString());
216             }
217     }
218     } // endElement
219

220
221     /**
222      * Get Number of updates
223      * @return update count
224      */

225     public int getUpdateCount()
226     {
227         return m_updateCount;
228     } // getUpdateCount
229

230     public String JavaDoc TO_STRING (String JavaDoc txt)
231     {
232         return TO_STRING (txt, 0);
233     } // TO_STRING
234

235     /**
236      * Package Strings for SQL command.
237      * <pre>
238      * - include in ' (single quotes)
239      * - replace ' with ''
240      * </pre>
241      * @param txt String with text
242      * @param maxLength Maximum Length of content or 0 to ignore
243      * @return escaped string for insert statement (NULL if null)
244      */

245     public String JavaDoc TO_STRING (String JavaDoc txt, int maxLength)
246     {
247         if (txt == null)
248             return "NULL";
249
250         // Length
251
String JavaDoc text = txt;
252         if (maxLength != 0 && text.length() > maxLength)
253             text = txt.substring(0, maxLength);
254
255         char quote = '\'';
256         // copy characters (wee need to look through anyway)
257
StringBuffer JavaDoc out = new StringBuffer JavaDoc();
258         out.append(quote); // '
259
for (int i = 0; i < text.length(); i++)
260         {
261             char c = text.charAt(i);
262             if (c == quote)
263                 out.append("''");
264             else
265                 out.append(c);
266         }
267         out.append(quote); // '
268
//
269
return out.toString();
270     } // TO_STRING
271

272
273
274 } // TranslationHandler
275
Popular Tags