1 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 28 34 public class TranslationHandler extends DefaultHandler 35 { 36 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 } 53 private ConnectionProvider DB; 54 55 56 private int m_AD_Client_ID = -1; 57 58 private String m_AD_Language = null; 59 60 private boolean m_isBaseLanguage = false; 61 62 private String m_TableName = null; 63 64 private String m_updateSQL = null; 65 66 private String m_curID = null; 67 68 private String m_curColumnName = null; 69 70 private StringBuffer m_curValue = null; 71 72 private StringBuffer 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 93 public void startElement (String uri, String localName, String qName, Attributes attributes) 94 throws org.xml.sax.SAXException 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 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 (); 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 (); 132 } 134 142 public void characters (char ch[], int start, int length) 143 throws SAXException 144 { 145 m_curValue.append(ch, start, length); 146 } 148 155 public void endElement (String uri, String localName, String qName) 156 throws SAXException 157 { 158 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 if (m_sql.length() > 0) 167 m_sql.append(","); 168 m_sql.append("Updated=now()"); if (!m_isBaseLanguage) 170 m_sql.append(",IsTranslated='Y'"); 171 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 m_sql.insert(0, m_updateSQL); 180 if (log4j.isDebugEnabled()) log4j.debug(m_sql.toString()); 181 int no =0; 183 Statement st = null; 185 try{ 186 st = DB.getStatement(); 187 no = st.executeUpdate(m_sql.toString()); 188 } catch (Exception e){log4j.error("183:"+m_sql.toString()+e.toString());} 189 finally { 190 try { 191 if (st != null) DB.releaseStatement(st); 192 } catch (Exception 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 e) { 215 log4j.error(e.toString()); 216 } 217 } 218 } 220 221 225 public int getUpdateCount() 226 { 227 return m_updateCount; 228 } 230 public String TO_STRING (String txt) 231 { 232 return TO_STRING (txt, 0); 233 } 235 245 public String TO_STRING (String txt, int maxLength) 246 { 247 if (txt == null) 248 return "NULL"; 249 250 String text = txt; 252 if (maxLength != 0 && text.length() > maxLength) 253 text = txt.substring(0, maxLength); 254 255 char quote = '\''; 256 StringBuffer out = new StringBuffer (); 258 out.append(quote); 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); return out.toString(); 270 } 272 273 274 } | Popular Tags |